aosvs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. Copyright (c) 1990-1999 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 1999-Oct-05 or later
  4. (the contents of which are also included in zip.h) for terms of use.
  5. If, for some reason, both of these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html
  7. */
  8. #include <dirent.h>
  9. #include <time.h>
  10. #include "zip.h"
  11. #include <paru.h> /* parameter definitions */
  12. #include <sys_calls.h> /* AOS/VS system call interface */
  13. #include <packets/filestatus.h> /* AOS/VS ?FSTAT packet defs */
  14. #ifndef UTIL /* AOS/VS specific fileio code not needed for UTILs */
  15. #define PAD 0
  16. #define PATH_END ':'
  17. /*
  18. * could probably avoid the union -
  19. * all are same size & we're going to assume this
  20. */
  21. typedef union zvsfstat_stru
  22. {
  23. P_FSTAT norm_fstat_packet; /* normal fstat packet */
  24. P_FSTAT_DIR dir_fstat_packet; /* DIR/CPD fstat packet */
  25. P_FSTAT_UNIT unit_fstat_packet; /* unit (device) fstat packet */
  26. P_FSTAT_IPC ipc_fstat_packet; /* IPC file fstat packet */
  27. } ZVSFSTAT_STRU;
  28. typedef struct zextrafld
  29. {
  30. char extra_header_id[2]; /* set to VS - in theory, an int */
  31. char extra_data_size[2]; /* size of rest, in Intel little-endian order */
  32. char extra_sentinel[4]; /* set to FCI w/ trailing null */
  33. unsigned char extra_rev; /* set to 10 for rev 1.0 */
  34. ZVSFSTAT_STRU fstat_packet; /* the fstat packet */
  35. char aclbuf[$MXACL]; /* raw ACL, or link-resolution name */
  36. } ZEXTRAFLD;
  37. #define ZEXTRA_HEADID "VS"
  38. #define ZEXTRA_SENTINEL "FCI"
  39. #define ZEXTRA_REV (unsigned char) 10
  40. local ZEXTRAFLD zzextrafld; /* buffer for extra field containing
  41. ?FSTAT packet & ACL buffer */
  42. local char zlinkres[$MXPL]; /* buf for link resolution contents */
  43. local char znamebuf[$MXPL]; /* buf for AOS/VS filename */
  44. static char vsnamebuf[$MXPL];
  45. static char uxnamebuf[FNMAX];
  46. static P_FSTAT vsfstatbuf;
  47. local ulg label_time = 0;
  48. local ulg label_mode = 0;
  49. local time_t label_utim = 0;
  50. /* Local functions */
  51. local char *readd OF((DIR *));
  52. char *readd(d)
  53. DIR *d; /* directory stream to read from */
  54. /* Return a pointer to the next name in the directory stream d, or NULL if
  55. no more entries or an error occurs. */
  56. {
  57. struct dirent *e;
  58. e = readdir(d);
  59. return e == NULL ? (char *) NULL : e->d_name;
  60. }
  61. int procname(n, caseflag)
  62. char *n; /* name to process */
  63. int caseflag; /* true to force case-sensitive match */
  64. /* Process a name or sh expression to operate on (or exclude). Return
  65. an error code in the ZE_ class. */
  66. {
  67. char *a; /* path and name for recursion */
  68. DIR *d; /* directory stream from opendir() */
  69. char *e; /* pointer to name from readd() */
  70. int m; /* matched flag */
  71. char *p; /* path for recursion */
  72. struct stat s; /* result of stat() */
  73. struct zlist far *z; /* steps through zfiles list */
  74. if (strcmp(n, "-") == 0) /* if compressing stdin */
  75. return newname(n, 0, caseflag);
  76. else if (LSSTAT(n, &s))
  77. {
  78. /* Not a file or directory--search for shell expression in zip file */
  79. p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
  80. m = 1;
  81. for (z = zfiles; z != NULL; z = z->nxt) {
  82. if (MATCH(p, z->iname, caseflag))
  83. {
  84. z->mark = pcount ? filter(z->zname, caseflag) : 1;
  85. if (verbose)
  86. fprintf(mesg, "zip diagnostic: %scluding %s\n",
  87. z->mark ? "in" : "ex", z->name);
  88. m = 0;
  89. }
  90. }
  91. free((zvoid *)p);
  92. return m ? ZE_MISS : ZE_OK;
  93. }
  94. /* Live name--use if file, recurse if directory */
  95. if ((s.st_mode & S_IFDIR) == 0)
  96. {
  97. /* add or remove name of file */
  98. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  99. return m;
  100. } else {
  101. /* Add trailing / to the directory name */
  102. if ((p = malloc(strlen(n)+2)) == NULL)
  103. return ZE_MEM;
  104. if (strcmp(n, ".") == 0) {
  105. *p = '\0'; /* avoid "./" prefix and do not create zip entry */
  106. } else {
  107. strcpy(p, n);
  108. a = p + strlen(p);
  109. if (a[-1] != '/')
  110. strcpy(a, "/");
  111. if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
  112. free((zvoid *)p);
  113. return m;
  114. }
  115. }
  116. /* recurse into directory */
  117. if (recurse && (d = opendir(n)) != NULL)
  118. {
  119. while ((e = readd(d)) != NULL) {
  120. if (strcmp(e, ".") && strcmp(e, ".."))
  121. {
  122. if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  123. {
  124. closedir(d);
  125. free((zvoid *)p);
  126. return ZE_MEM;
  127. }
  128. strcat(strcpy(a, p), e);
  129. if ((m = procname(a, caseflag)) != ZE_OK) /* recurse on name */
  130. {
  131. if (m == ZE_MISS)
  132. zipwarn("name not matched: ", a);
  133. else
  134. ziperr(m, a);
  135. }
  136. free((zvoid *)a);
  137. }
  138. }
  139. closedir(d);
  140. }
  141. free((zvoid *)p);
  142. } /* (s.st_mode & S_IFDIR) == 0) */
  143. return ZE_OK;
  144. }
  145. char *strlower(s)
  146. char *s; /* string to convert */
  147. /* Convert all uppercase letters to lowercase in string s */
  148. {
  149. char *p; /* scans string */
  150. for (p = s; *p; p++)
  151. if (*p >= 'A' && *p <= 'Z')
  152. *p += 'a' - 'A';
  153. return s;
  154. }
  155. char *strupper(s)
  156. char *s; /* string to convert */
  157. /* Convert all lowercase letters to uppercase in string s */
  158. {
  159. char *p; /* scans string */
  160. for (p = s; *p; p++)
  161. if (*p >= 'a' && *p <= 'z')
  162. *p -= 'a' - 'A';
  163. return s;
  164. }
  165. char *ex2in(x, isdir, pdosflag)
  166. char *x; /* external file name */
  167. int isdir; /* input: x is a directory */
  168. int *pdosflag; /* output: force MSDOS file attributes? */
  169. /* Convert the external file name to a zip file name, returning the malloc'ed
  170. string or NULL if not enough memory. */
  171. {
  172. char *n; /* internal file name (malloc'ed) */
  173. char *t; /* shortened name */
  174. int dosflag;
  175. dosflag = dosify; /* default for non-DOS and non-OS/2 */
  176. /* Find starting point in name before doing malloc */
  177. for (t = x; *t == '/'; t++)
  178. ;
  179. if (*t == '=') /* AOS/VS for ./ */
  180. t++;
  181. else if (*t == ':') /* AOS/VS for / */
  182. t++;
  183. if (!pathput)
  184. t = last(t, PATH_END);
  185. if (*t == '^') /* AOS/VS for ../ */
  186. {
  187. if ((n = malloc(strlen(t) + 3)) == NULL)
  188. return NULL;
  189. strcpy(n, "../");
  190. strcpy(n + 3, t + 1);
  191. }
  192. else if (*t == '@') /* AOS/VS for :PER:, kind of like /dev/ */
  193. {
  194. if ((n = malloc(strlen(t) + 5)) == NULL)
  195. return NULL;
  196. strcpy(n, "/PER/");
  197. strcpy(n + 5, t + 1);
  198. }
  199. else
  200. {
  201. if ((n = malloc(strlen(t) + 1)) == NULL)
  202. return NULL;
  203. strcpy(n, t);
  204. }
  205. /* now turn AOS/VS dir separators (colons) into slashes */
  206. for (t = n; *t != '\0'; t++)
  207. if (*t == ':')
  208. *t = '/';
  209. /*
  210. * Convert filename to uppercase (for correct matching).
  211. * (It may make more sense to patch the matching code, since
  212. * we may want those filenames in uppercase on the target system,
  213. * but this seems better at present. If we're converting, uppercase
  214. * also seems to make sense.)
  215. */
  216. strupper(n);
  217. if (dosify)
  218. msname(n);
  219. /* Returned malloc'ed name */
  220. if (pdosflag)
  221. *pdosflag = dosflag;
  222. return n;
  223. }
  224. char *in2ex(n)
  225. char *n; /* internal file name */
  226. /* Convert the zip file name to an external file name, returning the malloc'ed
  227. string or NULL if not enough memory. */
  228. {
  229. char *x; /* external file name */
  230. if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  231. return NULL;
  232. strcpy(x, n);
  233. return x;
  234. }
  235. void stamp(f, d)
  236. char *f; /* name of file to change */
  237. ulg d; /* dos-style time to change it to */
  238. /* Set last updated and accessed time of file f to the DOS time d. */
  239. {
  240. time_t u[2]; /* argument for utime() */
  241. /* Convert DOS time to time_t format in u */
  242. u[0] = u[1] = dos2unixtime(d);
  243. utime(f, u);
  244. }
  245. ulg filetime(f, a, n, t)
  246. char *f; /* name of file to get info on */
  247. ulg *a; /* return value: file attributes */
  248. long *n; /* return value: file size */
  249. iztimes *t; /* return value: access, modific. and creation times */
  250. /* If file *f does not exist, return 0. Else, return the file's last
  251. modified date and time as an MSDOS date and time. The date and
  252. time is returned in a long with the date most significant to allow
  253. unsigned integer comparison of absolute times. Also, if a is not
  254. a NULL pointer, store the file attributes there, with the high two
  255. bytes being the Unix attributes, and the low byte being a mapping
  256. of that to DOS attributes. If n is not NULL, store the file size
  257. there. If t is not NULL, the file's access, modification and creation
  258. times are stored there as UNIX time_t values.
  259. If f is "-", use standard input as the file. If f is a device, return
  260. a file size of -1 */
  261. {
  262. struct stat s; /* results of stat() */
  263. /* convert FNMAX to malloc - 11/8/04 EG */
  264. char *name;
  265. int len = strlen(f);
  266. if (f == label) {
  267. if (a != NULL)
  268. *a = label_mode;
  269. if (n != NULL)
  270. *n = -2L; /* convention for a label name */
  271. if (t != NULL)
  272. t->atime = t->mtime = t->ctime = label_utim;
  273. return label_time;
  274. }
  275. if ((name = malloc(len + 1)) == NULL) {
  276. ZIPERR(ZE_MEM, "filetime");
  277. }
  278. strcpy(name, f);
  279. if (name[len - 1] == '/')
  280. name[len - 1] = '\0';
  281. /* not all systems allow stat'ing a file with / appended */
  282. if (strcmp(f, "-") == 0) {
  283. if (fstat(fileno(stdin), &s) != 0)
  284. error("fstat(stdin)");
  285. } else if (LSSTAT(name, &s) != 0) {
  286. /* Accept about any file kind including directories
  287. * (stored with trailing / with -r option)
  288. */
  289. free(name);
  290. return 0;
  291. }
  292. free(name);
  293. if (a != NULL) {
  294. *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  295. if ((s.st_mode & S_IFDIR) != 0) {
  296. *a |= MSDOS_DIR_ATTR;
  297. }
  298. }
  299. if (n != NULL)
  300. *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  301. if (t != NULL) {
  302. t->atime = s.st_atime;
  303. t->mtime = s.st_mtime;
  304. t->ctime = s.st_ctime;
  305. }
  306. return unix2dostime(&s.st_ctime);
  307. }
  308. int deletedir(d)
  309. char *d;
  310. {
  311. return rmdir(d);
  312. }
  313. int set_extra_field(z, z_utim)
  314. struct zlist far *z;
  315. iztimes *z_utim;
  316. /* create extra field and change z->att if desired */
  317. /* NOTE: this AOS/VS version assumes the pathname in z->name is an
  318. * AOS/VS pathname, not a unix-style one. Since you can zip up using
  319. * unix-style pathnames, this may create problems occasionally.
  320. * We COULD add code to parse back to AOS/VS format ...
  321. * (This might also fail for other reasons such as access denied, but
  322. * that should already have occurred.)
  323. * We set the central-dir extra fld pointer & length here to the same data.
  324. */
  325. {
  326. int aclend = 0;
  327. /*
  328. * use this to simplify because different calls depending on
  329. * whether links are resolved
  330. */
  331. unsigned short errc;
  332. z->ext = 0; /* init to no extra field */
  333. /* get the ?FSTAT info & the acl - if no errors, get memory & store.
  334. * (first, we have to cut off the trailing slash that was added if
  335. * it's a dir, since AOS/VS doesn't accept that kind of thing)
  336. */
  337. strncpy(znamebuf, z->name, $MXPL);
  338. znamebuf[$MXPL-1] = '\0';
  339. if (znamebuf[strlen(znamebuf)-1] == '/')
  340. znamebuf[strlen(znamebuf)-1] = '\0';
  341. if (linkput)
  342. errc = sys_fstat(znamebuf, BIT1, &(zzextrafld.fstat_packet));
  343. else
  344. errc = sys_fstat(znamebuf, 0, &(zzextrafld.fstat_packet));
  345. if (errc)
  346. {
  347. fprintf(stderr,
  348. "\n Warning: can't get ?FSTAT info & acl of %s - error %d\n ",
  349. znamebuf, errc);
  350. perror("sys_fstat()");
  351. }
  352. else
  353. {
  354. /* store the ACL - or, if a link (no ACL!), store the resolution name */
  355. if (zzextrafld.fstat_packet.norm_fstat_packet.styp_type != $FLNK)
  356. {
  357. if ((errc = sys_gacl(znamebuf, zzextrafld.aclbuf)) != 0)
  358. {
  359. fprintf(stderr, "\n Warning: can't get acl of %s - error %d\n ",
  360. z->name, errc);
  361. perror("sys_gacl()");
  362. }
  363. else
  364. {
  365. /* find length of ACL - ends with double-null */
  366. while (aclend++ < $MXACL &&
  367. (zzextrafld.aclbuf[aclend - 1] != '\0' ||
  368. zzextrafld.aclbuf[aclend] != '\0'))
  369. /* EMPTY LOOP */ ;
  370. if ((z->cextra = z->extra =
  371. malloc(sizeof(ZEXTRAFLD) - $MXACL + aclend + 4)) != NULL)
  372. {
  373. strncpy(zzextrafld.extra_header_id, ZEXTRA_HEADID,
  374. sizeof(zzextrafld.extra_header_id));
  375. strncpy(zzextrafld.extra_sentinel, ZEXTRA_SENTINEL,
  376. sizeof(zzextrafld.extra_sentinel));
  377. zzextrafld.extra_rev = ZEXTRA_REV; /* this is a char, no need
  378. to worry about byte order */
  379. /* set size (Intel (little-endian)) 2-byte int, which we've set
  380. as array to make it easier */
  381. errc = (unsigned short) (sizeof(ZEXTRAFLD) - $MXACL + aclend + 4 -
  382. sizeof(zzextrafld.extra_header_id) -
  383. sizeof(zzextrafld.extra_data_size));
  384. zzextrafld.extra_data_size[0] = errc & 0xFF; /* low-order byte */
  385. zzextrafld.extra_data_size[1] = errc >> 8; /* high-order byte */
  386. memcpy((char *) z->extra, (char *) &zzextrafld,
  387. sizeof(ZEXTRAFLD) - $MXACL + aclend + 4);
  388. z->cext = z->ext = sizeof(ZEXTRAFLD) - $MXACL + aclend + 4;
  389. }
  390. }
  391. }
  392. else /* a link */
  393. {
  394. if ((errc = sys_glink(z->name, zzextrafld.aclbuf)) != 0)
  395. {
  396. fprintf(stderr,
  397. "\n Warning: can't get link-resolution of %s - error %d\n ",
  398. z->name, errc);
  399. perror("sys_glink()");
  400. }
  401. else
  402. {
  403. aclend = strlen(zzextrafld.aclbuf) + 1;
  404. if ((z->extra = malloc(sizeof(ZEXTRAFLD) - $MXACL + aclend + 4))
  405. != NULL)
  406. {
  407. strncpy(zzextrafld.extra_header_id, ZEXTRA_HEADID,
  408. sizeof(zzextrafld.extra_header_id));
  409. strncpy(zzextrafld.extra_sentinel, ZEXTRA_SENTINEL,
  410. sizeof(zzextrafld.extra_sentinel));
  411. zzextrafld.extra_rev = ZEXTRA_REV; /* this is a char, no need
  412. to worry about byte order */
  413. /* set size (Intel (little-endian)) 2-byte int, which we've set
  414. as array to make it easier */
  415. errc = (unsigned short) (sizeof(ZEXTRAFLD) - $MXACL + aclend + 4 -
  416. sizeof(zzextrafld.extra_header_id) -
  417. sizeof(zzextrafld.extra_data_size));
  418. zzextrafld.extra_data_size[0] = errc & 0xFF; /* low-order byte */
  419. zzextrafld.extra_data_size[1] = errc >> 8; /* high-order byte */
  420. memcpy((char *) z->extra, (char *) &zzextrafld,
  421. sizeof(ZEXTRAFLD) - $MXACL + aclend + 4);
  422. z->ext = sizeof(ZEXTRAFLD) - $MXACL + aclend + 4;
  423. }
  424. }
  425. }
  426. }
  427. return ZE_OK;
  428. }
  429. #endif /* !UTIL */
  430. void version_local()
  431. {
  432. printf("Compiled with %s under %s.\n",
  433. "a C compiler",
  434. "AOS/VS"
  435. );
  436. }
  437. /*
  438. * This file defines for AOS/VS two Unix functions relating to links;
  439. * the calling code should have the following defines:
  440. *
  441. * #define lstat(path,buf) zvs_lstat(path,buf)
  442. * #define readlink(path,buf,nbytes) zvs_readlink(path,buf,nbytes)
  443. *
  444. * For these functions, I'm going to define yet 2 MORE filename buffers
  445. * and also insert code to change pathnames to Unix & back. This is
  446. * easier than changing all the other places this kind of thing happens to
  447. * be efficient. This is a kludge. I'm also going to put the functions
  448. * here for my immediate convenience rather than somewhere else for
  449. * someone else's.
  450. *
  451. * WARNING: the use of static buffers means that you'd better get your
  452. * data out of these buffers before the next call to any of these functions!
  453. *
  454. */
  455. /* =========================================================================
  456. * ZVS_LSTAT() - get (or simulate) stat information WITHOUT following symlinks
  457. * This is intended to look to the outside like the unix lstat()
  458. * function. We do a quick-&-dirty filename conversion.
  459. *
  460. * If the file is NOT a symbolic link, we can just do a stat() on it and
  461. * that should be fine. But if it IS a link, we have to set the elements
  462. * of the stat struct ourselves, since AOS/VS doesn't have a built-in
  463. * lstat() function.
  464. *
  465. * RETURNS: 0 on success, or -1 otherwise
  466. *
  467. */
  468. int zvs_lstat(char *path, struct stat *buf)
  469. {
  470. char *cp_vs = vsnamebuf;
  471. char *cp_ux = path;
  472. int mm, dd, yy;
  473. /*
  474. * Convert the Unix pathname to an AOS/VS pathname.
  475. * This is quick & dirty; it won't handle (for instance) pathnames with
  476. * ../ in the middle of them, and may choke on other Unixisms. We hope
  477. * they're unlikely.
  478. */
  479. if (!strncmp(cp_ux, "../", 3))
  480. {
  481. *cp_vs++ = '^'; /* AOS/VS for ../ */
  482. cp_ux += 3;
  483. }
  484. else if (!strncmp(cp_ux, "./", 2))
  485. {
  486. *cp_vs++ = '='; /* AOS/VS for ./ */
  487. cp_ux += 2;
  488. }
  489. do
  490. {
  491. if (*cp_ux == '/')
  492. {
  493. *cp_vs++ = ':';
  494. }
  495. else
  496. {
  497. *cp_vs++ = (char) toupper(*cp_ux);
  498. }
  499. } while (*cp_ux++ != '\0' && cp_vs - vsnamebuf < sizeof(vsnamebuf));
  500. /* If Unix name was too long for our buffer, return an error return */
  501. if (cp_vs - vsnamebuf >= sizeof(vsnamebuf) && *(cp_vs - 1) != '\0')
  502. return (-1); /* error */
  503. /* Make AOS/VS ?FSTAT call that won't follow links & see if we find
  504. * anything. If not, we return error.
  505. */
  506. if (sys_fstat(vsnamebuf,
  507. BIT1, /* BIT1 says to not resolve links */
  508. &vsfstatbuf))
  509. return (-1); /* error */
  510. /* If we DID find the file but it's not a link,
  511. * call stat() and return its value.
  512. */
  513. if (vsfstatbuf.styp_type != $FLNK)
  514. return (stat(path, buf)); /* call with Unix pathname ... */
  515. /* Otherwise, we have to kludge up values for the stat structure */
  516. memset((char *) buf, 0, sizeof(*buf)); /* init to nulls (0 values) */
  517. buf->st_mode = S_IFLNK | 0777; /* link and rwxrwxrwx */
  518. buf->st_uid = -1; /* this is what we get on AOS/VS
  519. anyway (maybe unless we set up
  520. a dummy password file?) */
  521. buf->st_nlink = 1;
  522. /* The DG date we've got is days since 12/31/67 and seconds/2. So we
  523. * need to subtract 732 days (if that's not negative), convert to seconds,
  524. * and add adjusted seconds.
  525. */
  526. if (vsfstatbuf.stch.short_time[0] < 732)
  527. buf->st_ctime = buf->st_mtime = buf->st_atime = 0L;
  528. else
  529. {
  530. buf->st_ctime = buf->st_mtime = buf->st_atime =
  531. ((long) vsfstatbuf.stch.short_time[0] - 732L) * 24L * 3600L +
  532. 2L * (long) vsfstatbuf.stch.short_time[1];
  533. }
  534. /* And we need to get the filename linked to and use its length as
  535. * the file size. We'll use the Unix pathname buffer for this - hope
  536. * it's big enough. (We won't overwrite anything, but we could get a
  537. * truncated path.) If there's an error, here's our last chance to
  538. * say anything.
  539. */
  540. if ((buf->st_size = zvs_readlink(vsnamebuf, uxnamebuf, FNMAX)) < 0)
  541. return (-1);
  542. else
  543. return (0);
  544. } /* end zvs_lstat() */
  545. /* =========================================================================
  546. * ZVS_READLINK() - get pathname pointed to by an AOS/VS link file
  547. * This is intended to look to the outside like the unix readlink()
  548. * function. We do a quick-&-dirty filename conversion.
  549. *
  550. * RETURNS: the length of the output path (in bytes), or -1 if an error
  551. *
  552. */
  553. int zvs_readlink(char *path, char *buf, int nbytes)
  554. {
  555. char *cp_vs = vsnamebuf;
  556. char *cp_ux = buf;
  557. /* This is called with z->name, the filename the user gave, so we'll get
  558. * the link-resolution name on the assumption that it's a valid AOS/VS
  559. * name. We're also assuming a reasonable value (> 5) for nbytes.
  560. */
  561. if (sys_glink(path, vsnamebuf))
  562. return (-1); /* readlink() is supposed to return -1 on error */
  563. /* Now, convert the AOS/VS pathname to a Unix pathname.
  564. * Note that sys_glink(), unlike readlink(), does add a null.
  565. */
  566. if (*cp_vs == '^') /* AOS/VS for ../ */
  567. {
  568. strncpy(cp_ux, "../", 3);
  569. cp_ux += 3;
  570. cp_vs++;
  571. }
  572. else if (*cp_vs == '@') /* AOS/VS for :PER:, kind of like /dev/ */
  573. {
  574. strncpy(cp_ux, "/PER/", 5);
  575. cp_ux += 5;
  576. cp_vs++;
  577. }
  578. else if (*cp_vs == '=') /* AOS/VS for ./ */
  579. {
  580. strncpy(cp_ux, "./", 2);
  581. cp_ux += 2;
  582. cp_vs++;
  583. }
  584. while (*cp_vs != '\0' && cp_ux - buf < nbytes)
  585. {
  586. if (*cp_vs == ':')
  587. {
  588. *cp_ux++ = '/';
  589. }
  590. else
  591. {
  592. *cp_ux++ = (char) toupper(*cp_vs);
  593. }
  594. cp_vs++;
  595. }
  596. return (cp_ux - buf); /* # characters in Unix path (no trailing null) */
  597. } /* end zvs_readlink() */