unix.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. unix/unix.c - Zip 3
  3. Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
  4. See the accompanying file LICENSE, version 2007-Mar-4 or later
  5. (the contents of which are also included in zip.h) for terms of use.
  6. If, for some reason, all these files are missing, the Info-ZIP license
  7. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  8. */
  9. #include "zip.h"
  10. #ifndef UTIL /* the companion #endif is a bit of ways down ... */
  11. #include <time.h>
  12. #if defined(MINIX) || defined(__mpexl)
  13. # ifdef S_IWRITE
  14. # undef S_IWRITE
  15. # endif /* S_IWRITE */
  16. # define S_IWRITE S_IWUSR
  17. #endif /* MINIX */
  18. #if (!defined(S_IWRITE) && defined(S_IWUSR))
  19. # define S_IWRITE S_IWUSR
  20. #endif
  21. #if defined(HAVE_DIRENT_H) || defined(_POSIX_VERSION)
  22. # include <dirent.h>
  23. #else /* !HAVE_DIRENT_H */
  24. # ifdef HAVE_NDIR_H
  25. # include <ndir.h>
  26. # endif /* HAVE_NDIR_H */
  27. # ifdef HAVE_SYS_NDIR_H
  28. # include <sys/ndir.h>
  29. # endif /* HAVE_SYS_NDIR_H */
  30. # ifdef HAVE_SYS_DIR_H
  31. # include <sys/dir.h>
  32. # endif /* HAVE_SYS_DIR_H */
  33. # ifndef dirent
  34. # define dirent direct
  35. # endif
  36. #endif /* HAVE_DIRENT_H || _POSIX_VERSION */
  37. #define PAD 0
  38. #define PATH_END '/'
  39. /* Library functions not in (most) header files */
  40. #ifdef _POSIX_VERSION
  41. # include <utime.h>
  42. #else
  43. int utime OF((char *, time_t *));
  44. #endif
  45. extern char *label;
  46. local ulg label_time = 0;
  47. local ulg label_mode = 0;
  48. local time_t label_utim = 0;
  49. /* Local functions */
  50. local char *readd OF((DIR *));
  51. #ifdef NO_DIR /* for AT&T 3B1 */
  52. #include <sys/dir.h>
  53. #ifndef dirent
  54. # define dirent direct
  55. #endif
  56. typedef FILE DIR;
  57. /*
  58. ** Apparently originally by Rich Salz.
  59. ** Cleaned up and modified by James W. Birdsall.
  60. */
  61. #define opendir(path) fopen(path, "r")
  62. struct dirent *readdir(dirp)
  63. DIR *dirp;
  64. {
  65. static struct dirent entry;
  66. if (dirp == NULL)
  67. return NULL;
  68. for (;;)
  69. if (fread (&entry, sizeof (struct dirent), 1, dirp) == 0)
  70. return NULL;
  71. else if (entry.d_ino)
  72. return (&entry);
  73. } /* end of readdir() */
  74. #define closedir(dirp) fclose(dirp)
  75. #endif /* NO_DIR */
  76. local char *readd(d)
  77. DIR *d; /* directory stream to read from */
  78. /* Return a pointer to the next name in the directory stream d, or NULL if
  79. no more entries or an error occurs. */
  80. {
  81. struct dirent *e;
  82. e = readdir(d);
  83. return e == NULL ? (char *) NULL : e->d_name;
  84. }
  85. int procname(n, caseflag)
  86. char *n; /* name to process */
  87. int caseflag; /* true to force case-sensitive match */
  88. /* Process a name or sh expression to operate on (or exclude). Return
  89. an error code in the ZE_ class. */
  90. {
  91. char *a; /* path and name for recursion */
  92. DIR *d; /* directory stream from opendir() */
  93. char *e; /* pointer to name from readd() */
  94. int m; /* matched flag */
  95. char *p; /* path for recursion */
  96. z_stat s; /* result of stat() */
  97. struct zlist far *z; /* steps through zfiles list */
  98. if (strcmp(n, "-") == 0) /* if compressing stdin */
  99. return newname(n, 0, caseflag);
  100. else if (LSSTAT(n, &s))
  101. {
  102. /* Not a file or directory--search for shell expression in zip file */
  103. p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
  104. m = 1;
  105. for (z = zfiles; z != NULL; z = z->nxt) {
  106. if (MATCH(p, z->iname, caseflag))
  107. {
  108. z->mark = pcount ? filter(z->zname, caseflag) : 1;
  109. if (verbose)
  110. fprintf(mesg, "zip diagnostic: %scluding %s\n",
  111. z->mark ? "in" : "ex", z->name);
  112. m = 0;
  113. }
  114. }
  115. free((zvoid *)p);
  116. return m ? ZE_MISS : ZE_OK;
  117. }
  118. /* Live name--use if file, recurse if directory */
  119. #ifdef OS390
  120. if (S_ISREG(s.st_mode) || S_ISLNK(s.st_mode))
  121. #else
  122. # ifdef S_IFLNK
  123. if ((s.st_mode & S_IFREG) == S_IFREG || (s.st_mode & S_IFLNK) == S_IFLNK)
  124. # else
  125. if ((s.st_mode & S_IFREG) == S_IFREG)
  126. # endif
  127. #endif
  128. {
  129. /* add or remove name of file */
  130. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  131. return m;
  132. }
  133. #ifdef OS390
  134. else if (S_ISDIR(s.st_mode))
  135. #else
  136. else if ((s.st_mode & S_IFDIR) == S_IFDIR)
  137. #endif
  138. {
  139. /* Add trailing / to the directory name */
  140. if ((p = malloc(strlen(n)+2)) == NULL)
  141. return ZE_MEM;
  142. if (strcmp(n, ".") == 0) {
  143. *p = '\0'; /* avoid "./" prefix and do not create zip entry */
  144. } else {
  145. strcpy(p, n);
  146. a = p + strlen(p);
  147. if (a[-1] != '/')
  148. strcpy(a, "/");
  149. if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
  150. free((zvoid *)p);
  151. return m;
  152. }
  153. }
  154. /* recurse into directory */
  155. if (recurse && (d = opendir(n)) != NULL)
  156. {
  157. while ((e = readd(d)) != NULL) {
  158. if (strcmp(e, ".") && strcmp(e, ".."))
  159. {
  160. if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  161. {
  162. closedir(d);
  163. free((zvoid *)p);
  164. return ZE_MEM;
  165. }
  166. strcat(strcpy(a, p), e);
  167. if ((m = procname(a, caseflag)) != ZE_OK) /* recurse on name */
  168. {
  169. if (m == ZE_MISS)
  170. zipwarn("name not matched: ", a);
  171. else
  172. ziperr(m, a);
  173. }
  174. free((zvoid *)a);
  175. }
  176. }
  177. closedir(d);
  178. }
  179. free((zvoid *)p);
  180. } /* (s.st_mode & S_IFDIR) */
  181. #ifdef OS390
  182. else if (S_ISFIFO(s.st_mode))
  183. #else
  184. else if ((s.st_mode & S_IFIFO) == S_IFIFO)
  185. #endif
  186. {
  187. if (allow_fifo) {
  188. /* FIFO (Named Pipe) - handle as normal file */
  189. /* add or remove name of FIFO */
  190. /* zip will stop if FIFO is open and wait for pipe to be fed and closed */
  191. if (noisy) zipwarn("Reading FIFO (Named Pipe): ", n);
  192. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  193. return m;
  194. } else {
  195. zipwarn("ignoring FIFO (Named Pipe) - use -FI to read: ", n);
  196. return ZE_OK;
  197. }
  198. } /* S_IFIFO */
  199. else
  200. zipwarn("ignoring special file: ", n);
  201. return ZE_OK;
  202. }
  203. char *ex2in(x, isdir, pdosflag)
  204. char *x; /* external file name */
  205. int isdir; /* input: x is a directory */
  206. int *pdosflag; /* output: force MSDOS file attributes? */
  207. /* Convert the external file name to a zip file name, returning the malloc'ed
  208. string or NULL if not enough memory. */
  209. {
  210. char *n; /* internal file name (malloc'ed) */
  211. char *t = NULL; /* shortened name */
  212. int dosflag;
  213. dosflag = dosify; /* default for non-DOS and non-OS/2 */
  214. /* Find starting point in name before doing malloc */
  215. /* Strip "//host/share/" part of a UNC name */
  216. if (!strncmp(x,"//",2) && (x[2] != '\0' && x[2] != '/')) {
  217. n = x + 2;
  218. while (*n != '\0' && *n != '/')
  219. n++; /* strip host name */
  220. if (*n != '\0') {
  221. n++;
  222. while (*n != '\0' && *n != '/')
  223. n++; /* strip `share' name */
  224. }
  225. if (*n != '\0')
  226. t = n + 1;
  227. } else
  228. t = x;
  229. while (*t == '/')
  230. t++; /* strip leading '/' chars to get a relative path */
  231. while (*t == '.' && t[1] == '/')
  232. t += 2; /* strip redundant leading "./" sections */
  233. /* Make changes, if any, to the copied name (leave original intact) */
  234. if (!pathput)
  235. t = last(t, PATH_END);
  236. /* Malloc space for internal name and copy it */
  237. if ((n = malloc(strlen(t) + 1)) == NULL)
  238. return NULL;
  239. strcpy(n, t);
  240. if (dosify)
  241. msname(n);
  242. #ifdef EBCDIC
  243. strtoasc(n, n); /* here because msname() needs native coding */
  244. #endif
  245. /* Returned malloc'ed name */
  246. if (pdosflag)
  247. *pdosflag = dosflag;
  248. if (isdir) return n; /* avoid warning on unused variable */
  249. return n;
  250. }
  251. char *in2ex(n)
  252. char *n; /* internal file name */
  253. /* Convert the zip file name to an external file name, returning the malloc'ed
  254. string or NULL if not enough memory. */
  255. {
  256. char *x; /* external file name */
  257. if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  258. return NULL;
  259. #ifdef EBCDIC
  260. strtoebc(x, n);
  261. #else
  262. strcpy(x, n);
  263. #endif
  264. return x;
  265. }
  266. /*
  267. * XXX use ztimbuf in both POSIX and non POSIX cases ?
  268. */
  269. void stamp(f, d)
  270. char *f; /* name of file to change */
  271. ulg d; /* dos-style time to change it to */
  272. /* Set last updated and accessed time of file f to the DOS time d. */
  273. {
  274. #ifdef _POSIX_VERSION
  275. struct utimbuf u; /* argument for utime() const ?? */
  276. #else
  277. time_t u[2]; /* argument for utime() */
  278. #endif
  279. /* Convert DOS time to time_t format in u */
  280. #ifdef _POSIX_VERSION
  281. u.actime = u.modtime = dos2unixtime(d);
  282. utime(f, &u);
  283. #else
  284. u[0] = u[1] = dos2unixtime(d);
  285. utime(f, u);
  286. #endif
  287. }
  288. ulg filetime(f, a, n, t)
  289. char *f; /* name of file to get info on */
  290. ulg *a; /* return value: file attributes */
  291. zoff_t *n; /* return value: file size */
  292. iztimes *t; /* return value: access, modific. and creation times */
  293. /* If file *f does not exist, return 0. Else, return the file's last
  294. modified date and time as an MSDOS date and time. The date and
  295. time is returned in a long with the date most significant to allow
  296. unsigned integer comparison of absolute times. Also, if a is not
  297. a NULL pointer, store the file attributes there, with the high two
  298. bytes being the Unix attributes, and the low byte being a mapping
  299. of that to DOS attributes. If n is not NULL, store the file size
  300. there. If t is not NULL, the file's access, modification and creation
  301. times are stored there as UNIX time_t values.
  302. If f is "-", use standard input as the file. If f is a device, return
  303. a file size of -1 */
  304. {
  305. z_stat s; /* results of stat() */
  306. /* converted to pointer from using FNMAX - 11/8/04 EG */
  307. char *name;
  308. int len = strlen(f);
  309. if (f == label) {
  310. if (a != NULL)
  311. *a = label_mode;
  312. if (n != NULL)
  313. *n = -2L; /* convention for a label name */
  314. if (t != NULL)
  315. t->atime = t->mtime = t->ctime = label_utim;
  316. return label_time;
  317. }
  318. if ((name = malloc(len + 1)) == NULL) {
  319. ZIPERR(ZE_MEM, "filetime");
  320. }
  321. strcpy(name, f);
  322. if (name[len - 1] == '/')
  323. name[len - 1] = '\0';
  324. /* not all systems allow stat'ing a file with / appended */
  325. if (strcmp(f, "-") == 0) {
  326. if (zfstat(fileno(stdin), &s) != 0) {
  327. free(name);
  328. error("fstat(stdin)");
  329. }
  330. }
  331. else if (LSSTAT(name, &s) != 0) {
  332. /* Accept about any file kind including directories
  333. * (stored with trailing / with -r option)
  334. */
  335. free(name);
  336. return 0;
  337. }
  338. free(name);
  339. if (a != NULL) {
  340. #ifndef OS390
  341. *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  342. #else
  343. /*
  344. ** The following defines are copied from the unizip source and represent the
  345. ** legacy Unix mode flags. These fixed bit masks are no longer required
  346. ** by XOPEN standards - the S_IS### macros being the new recommended method.
  347. ** The approach here of setting the legacy flags by testing the macros should
  348. ** work under any _XOPEN_SOURCE environment (and will just rebuild the same bit
  349. ** mask), but is required if the legacy bit flags differ from legacy Unix.
  350. */
  351. #define UNX_IFDIR 0040000 /* Unix directory */
  352. #define UNX_IFREG 0100000 /* Unix regular file */
  353. #define UNX_IFSOCK 0140000 /* Unix socket (BSD, not SysV or Amiga) */
  354. #define UNX_IFLNK 0120000 /* Unix symbolic link (not SysV, Amiga) */
  355. #define UNX_IFBLK 0060000 /* Unix block special (not Amiga) */
  356. #define UNX_IFCHR 0020000 /* Unix character special (not Amiga) */
  357. #define UNX_IFIFO 0010000 /* Unix fifo (BCC, not MSC or Amiga) */
  358. {
  359. mode_t legacy_modes;
  360. /* Initialize with permission bits--which are not implementation-optional */
  361. legacy_modes = s.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX);
  362. if (S_ISDIR(s.st_mode))
  363. legacy_modes |= UNX_IFDIR;
  364. if (S_ISREG(s.st_mode))
  365. legacy_modes |= UNX_IFREG;
  366. if (S_ISLNK(s.st_mode))
  367. legacy_modes |= UNX_IFLNK;
  368. if (S_ISBLK(s.st_mode))
  369. legacy_modes |= UNX_IFBLK;
  370. if (S_ISCHR(s.st_mode))
  371. legacy_modes |= UNX_IFCHR;
  372. if (S_ISFIFO(s.st_mode))
  373. legacy_modes |= UNX_IFIFO;
  374. if (S_ISSOCK(s.st_mode))
  375. legacy_modes |= UNX_IFSOCK;
  376. *a = ((ulg)legacy_modes << 16) | !(s.st_mode & S_IWRITE);
  377. }
  378. #endif
  379. if ((s.st_mode & S_IFMT) == S_IFDIR) {
  380. *a |= MSDOS_DIR_ATTR;
  381. }
  382. }
  383. if (n != NULL)
  384. *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  385. if (t != NULL) {
  386. t->atime = s.st_atime;
  387. t->mtime = s.st_mtime;
  388. t->ctime = t->mtime; /* best guess, (s.st_ctime: last status change!!) */
  389. }
  390. return unix2dostime(&s.st_mtime);
  391. }
  392. #ifndef QLZIP /* QLZIP Unix2QDOS cross-Zip supplies an extended variant */
  393. int set_new_unix_extra_field(z, s)
  394. struct zlist far *z;
  395. z_stat *s;
  396. /* New unix extra field.
  397. Currently only UIDs and GIDs are stored. */
  398. {
  399. int uid_size;
  400. int gid_size;
  401. int ef_data_size;
  402. char *extra;
  403. char *cextra;
  404. ulg id;
  405. int b;
  406. uid_size = sizeof(s->st_uid);
  407. gid_size = sizeof(s->st_gid);
  408. /* New extra field
  409. tag (2 bytes)
  410. size (2 bytes)
  411. version (1 byte)
  412. uid_size (1 byte - size in bytes)
  413. uid (variable)
  414. gid_size (1 byte - size in bytes)
  415. gid (variable)
  416. */
  417. ef_data_size = 1 + 1 + uid_size + 1 + gid_size;
  418. if ((extra = (char *)malloc(z->ext + 4 + ef_data_size)) == NULL)
  419. return ZE_MEM;
  420. if ((cextra = (char *)malloc(z->ext + 4 + ef_data_size)) == NULL)
  421. return ZE_MEM;
  422. if (z->ext)
  423. memcpy(extra, z->extra, z->ext);
  424. if (z->cext)
  425. memcpy(cextra, z->cextra, z->cext);
  426. free(z->extra);
  427. z->extra = extra;
  428. free(z->cextra);
  429. z->cextra = cextra;
  430. z->extra[z->ext + 0] = 'u';
  431. z->extra[z->ext + 1] = 'x';
  432. z->extra[z->ext + 2] = (char)ef_data_size; /* length of data part */
  433. z->extra[z->ext + 3] = 0;
  434. z->extra[z->ext + 4] = 1; /* version */
  435. /* UID */
  436. z->extra[z->ext + 5] = (char)(uid_size); /* uid size in bytes */
  437. b = 6;
  438. id = (ulg)(s->st_uid);
  439. z->extra[z->ext + b] = (char)(id & 0xFF);
  440. if (uid_size > 1) {
  441. b++;
  442. id = id >> 8;
  443. z->extra[z->ext + b] = (char)(id & 0xFF);
  444. if (uid_size > 2) {
  445. b++;
  446. id = id >> 8;
  447. z->extra[z->ext + b] = (char)(id & 0xFF);
  448. b++;
  449. id = id >> 8;
  450. z->extra[z->ext + b] = (char)(id & 0xFF);
  451. if (uid_size == 8) {
  452. b++;
  453. id = id >> 8;
  454. z->extra[z->ext + b] = (char)(id & 0xFF);
  455. b++;
  456. id = id >> 8;
  457. z->extra[z->ext + b] = (char)(id & 0xFF);
  458. b++;
  459. id = id >> 8;
  460. z->extra[z->ext + b] = (char)(id & 0xFF);
  461. b++;
  462. id = id >> 8;
  463. z->extra[z->ext + b] = (char)(id & 0xFF);
  464. }
  465. }
  466. }
  467. /* GID */
  468. b++;
  469. z->extra[z->ext + b] = (char)(gid_size); /* gid size in bytes */
  470. b++;
  471. id = (ulg)(s->st_gid);
  472. z->extra[z->ext + b] = (char)(id & 0xFF);
  473. if (gid_size > 1) {
  474. b++;
  475. id = id >> 8;
  476. z->extra[z->ext + b] = (char)(id & 0xFF);
  477. if (gid_size > 2) {
  478. b++;
  479. id = id >> 8;
  480. z->extra[z->ext + b] = (char)(id & 0xFF);
  481. b++;
  482. id = id >> 8;
  483. z->extra[z->ext + b] = (char)(id & 0xFF);
  484. if (gid_size == 8) {
  485. b++;
  486. id = id >> 8;
  487. z->extra[z->ext + b] = (char)(id & 0xFF);
  488. b++;
  489. id = id >> 8;
  490. z->extra[z->ext + b] = (char)(id & 0xFF);
  491. b++;
  492. id = id >> 8;
  493. z->extra[z->ext + b] = (char)(id & 0xFF);
  494. b++;
  495. id = id >> 8;
  496. z->extra[z->ext + b] = (char)(id & 0xFF);
  497. }
  498. }
  499. }
  500. /* copy local extra field to central directory extra field */
  501. memcpy((z->cextra) + z->cext, (z->extra) + z->ext, 4 + ef_data_size);
  502. z->ext = z->ext + 4 + ef_data_size;
  503. z->cext = z->cext + 4 + ef_data_size;
  504. return ZE_OK;
  505. }
  506. int set_extra_field(z, z_utim)
  507. struct zlist far *z;
  508. iztimes *z_utim;
  509. /* store full data in local header but just modification time stamp info
  510. in central header */
  511. {
  512. z_stat s;
  513. char *name;
  514. int len = strlen(z->name);
  515. /* For the full sized UT local field including the UID/GID fields, we
  516. * have to stat the file again. */
  517. if ((name = malloc(len + 1)) == NULL) {
  518. ZIPERR(ZE_MEM, "set_extra_field");
  519. }
  520. strcpy(name, z->name);
  521. if (name[len - 1] == '/')
  522. name[len - 1] = '\0';
  523. /* not all systems allow stat'ing a file with / appended */
  524. if (LSSTAT(name, &s)) {
  525. free(name);
  526. return ZE_OPEN;
  527. }
  528. free(name);
  529. #define EB_L_UT_SIZE (EB_HEADSIZE + EB_UT_LEN(2))
  530. #define EB_C_UT_SIZE (EB_HEADSIZE + EB_UT_LEN(1))
  531. /* The flag UIDGID_NOT_16BIT should be set by the pre-compile configuration
  532. script when it detects st_uid or st_gid sizes differing from 16-bit.
  533. */
  534. #ifndef UIDGID_NOT_16BIT
  535. /* The following "second-level" check for st_uid and st_gid members being
  536. 16-bit wide is only added as a safety precaution in case the "first-level"
  537. check failed to define the UIDGID_NOT_16BIT symbol.
  538. The first-level check should have been implemented in the automatic
  539. compile configuration process.
  540. */
  541. # ifdef UIDGID_ARE_16B
  542. # undef UIDGID_ARE_16B
  543. # endif
  544. /* The following expression is a compile-time constant and should (hopefully)
  545. get optimized away by any sufficiently intelligent compiler!
  546. */
  547. # define UIDGID_ARE_16B (sizeof(s.st_uid) == 2 && sizeof(s.st_gid) == 2)
  548. # define EB_L_UX2_SIZE (EB_HEADSIZE + EB_UX2_MINLEN)
  549. # define EB_C_UX2_SIZE EB_HEADSIZE
  550. # define EF_L_UNIX_SIZE (EB_L_UT_SIZE + (UIDGID_ARE_16B ? EB_L_UX2_SIZE : 0))
  551. # define EF_C_UNIX_SIZE (EB_C_UT_SIZE + (UIDGID_ARE_16B ? EB_C_UX2_SIZE : 0))
  552. #else
  553. # define EF_L_UNIX_SIZE EB_L_UT_SIZE
  554. # define EF_C_UNIX_SIZE EB_C_UT_SIZE
  555. #endif /* !UIDGID_NOT_16BIT */
  556. if ((z->extra = (char *)malloc(EF_L_UNIX_SIZE)) == NULL)
  557. return ZE_MEM;
  558. if ((z->cextra = (char *)malloc(EF_C_UNIX_SIZE)) == NULL)
  559. return ZE_MEM;
  560. z->extra[0] = 'U';
  561. z->extra[1] = 'T';
  562. z->extra[2] = (char)EB_UT_LEN(2); /* length of data part of local e.f. */
  563. z->extra[3] = 0;
  564. z->extra[4] = EB_UT_FL_MTIME | EB_UT_FL_ATIME; /* st_ctime != creation */
  565. z->extra[5] = (char)(s.st_mtime);
  566. z->extra[6] = (char)(s.st_mtime >> 8);
  567. z->extra[7] = (char)(s.st_mtime >> 16);
  568. z->extra[8] = (char)(s.st_mtime >> 24);
  569. z->extra[9] = (char)(s.st_atime);
  570. z->extra[10] = (char)(s.st_atime >> 8);
  571. z->extra[11] = (char)(s.st_atime >> 16);
  572. z->extra[12] = (char)(s.st_atime >> 24);
  573. #ifndef UIDGID_NOT_16BIT
  574. /* Only store the UID and GID in the old Ux extra field if the runtime
  575. system provides them in 16-bit wide variables. */
  576. if (UIDGID_ARE_16B) {
  577. z->extra[13] = 'U';
  578. z->extra[14] = 'x';
  579. z->extra[15] = (char)EB_UX2_MINLEN; /* length of data part of local e.f. */
  580. z->extra[16] = 0;
  581. z->extra[17] = (char)(s.st_uid);
  582. z->extra[18] = (char)(s.st_uid >> 8);
  583. z->extra[19] = (char)(s.st_gid);
  584. z->extra[20] = (char)(s.st_gid >> 8);
  585. }
  586. #endif /* !UIDGID_NOT_16BIT */
  587. z->ext = EF_L_UNIX_SIZE;
  588. memcpy(z->cextra, z->extra, EB_C_UT_SIZE);
  589. z->cextra[EB_LEN] = (char)EB_UT_LEN(1);
  590. #ifndef UIDGID_NOT_16BIT
  591. if (UIDGID_ARE_16B) {
  592. /* Copy header of Ux extra field from local to central */
  593. memcpy(z->cextra+EB_C_UT_SIZE, z->extra+EB_L_UT_SIZE, EB_C_UX2_SIZE);
  594. z->cextra[EB_LEN+EB_C_UT_SIZE] = 0;
  595. }
  596. #endif
  597. z->cext = EF_C_UNIX_SIZE;
  598. #if 0 /* UID/GID presence is now signaled by central EF_IZUNIX2 field ! */
  599. /* lower-middle external-attribute byte (unused until now):
  600. * high bit => (have GMT mod/acc times) >>> NO LONGER USED! <<<
  601. * second-high bit => have Unix UID/GID info
  602. * NOTE: The high bit was NEVER used in any official Info-ZIP release,
  603. * but its future use should be avoided (if possible), since it
  604. * was used as "GMT mod/acc times local extra field" flags in Zip beta
  605. * versions 2.0j up to 2.0v, for about 1.5 years.
  606. */
  607. z->atx |= 0x4000;
  608. #endif /* never */
  609. /* new unix extra field */
  610. set_new_unix_extra_field(z, &s);
  611. return ZE_OK;
  612. }
  613. #endif /* !QLZIP */
  614. int deletedir(d)
  615. char *d; /* directory to delete */
  616. /* Delete the directory *d if it is empty, do nothing otherwise.
  617. Return the result of rmdir(), delete(), or system().
  618. For VMS, d must be in format [x.y]z.dir;1 (not [x.y.z]).
  619. */
  620. {
  621. # ifdef NO_RMDIR
  622. /* code from Greg Roelofs, who horked it from Mark Edwards (unzip) */
  623. int r, len;
  624. char *s; /* malloc'd string for system command */
  625. len = strlen(d);
  626. if ((s = malloc(len + 34)) == NULL)
  627. return 127;
  628. sprintf(s, "IFS=\" \t\n\" /bin/rmdir %s 2>/dev/null", d);
  629. r = system(s);
  630. free(s);
  631. return r;
  632. # else /* !NO_RMDIR */
  633. return rmdir(d);
  634. # endif /* ?NO_RMDIR */
  635. }
  636. #endif /* !UTIL */
  637. /******************************/
  638. /* Function version_local() */
  639. /******************************/
  640. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__386BSD__) || \
  641. defined(__OpenBSD__) || defined(__bsdi__)
  642. #include <sys/param.h> /* for the BSD define */
  643. /* if we have something newer than NET/2 we'll use uname(3) */
  644. #if (BSD > 199103)
  645. #include <sys/utsname.h>
  646. #endif /* BSD > 199103 */
  647. #endif /* __{Net,Free,Open,386}BSD__ || __bsdi__ */
  648. void version_local()
  649. {
  650. #ifdef __GNUC__
  651. # ifdef NX_CURRENT_COMPILER_RELEASE
  652. char compiler_name[80];
  653. # endif
  654. #else
  655. # if (defined( __SUNPRO_C))
  656. char compiler_name[33];
  657. # else
  658. # if (defined( __HP_cc) || defined( __IBMC__))
  659. char compiler_name[33];
  660. # else
  661. # if (defined( __DECC_VER))
  662. char compiler_name[33];
  663. int compiler_typ;
  664. # else
  665. # if ((defined(CRAY) || defined(cray)) && defined(_RELEASE))
  666. char compiler_name[40];
  667. # endif
  668. # endif
  669. # endif
  670. # endif
  671. #endif
  672. #ifdef BSD
  673. # if (BSD > 199103)
  674. struct utsname u;
  675. char os_name[40];
  676. # else
  677. # if defined(__NETBSD__))
  678. static ZCONST char *netbsd[] = { "_ALPHA", "", "A", "B" };
  679. char os_name[40];
  680. # endif /* __NETBSD__ */
  681. # endif /* BSD > 199103 */
  682. #else /* !BSD */
  683. #if ((defined(CRAY) || defined(cray)) && defined(_UNICOS))
  684. char os_name[40];
  685. #endif /* (CRAY && defined(_UNICOS)) */
  686. #endif /* ?BSD */
  687. /* Define the compiler name and version string */
  688. #ifdef __GNUC__
  689. # ifdef NX_CURRENT_COMPILER_RELEASE
  690. sprintf(compiler_name, "NeXT DevKit %d.%02d (gcc " __VERSION__ ")",
  691. NX_CURRENT_COMPILER_RELEASE/100, NX_CURRENT_COMPILER_RELEASE%100);
  692. # define COMPILER_NAME compiler_name
  693. # else
  694. # define COMPILER_NAME "gcc " __VERSION__
  695. # endif
  696. #else /* !__GNUC__ */
  697. # if defined(__SUNPRO_C)
  698. sprintf( compiler_name, "Sun C version %x", __SUNPRO_C);
  699. # define COMPILER_NAME compiler_name
  700. # else
  701. # if (defined( __HP_cc))
  702. if ((__HP_cc% 100) == 0)
  703. {
  704. sprintf( compiler_name, "HP C version A.%02d.%02d",
  705. (__HP_cc/ 10000), ((__HP_cc% 10000)/ 100));
  706. }
  707. else
  708. {
  709. sprintf( compiler_name, "HP C version A.%02d.%02d.%02d",
  710. (__HP_cc/ 10000), ((__HP_cc% 10000)/ 100), (__HP_cc% 100));
  711. }
  712. # define COMPILER_NAME compiler_name
  713. # else
  714. # if (defined( __DECC_VER))
  715. sprintf( compiler_name, "DEC C version %c%d.%d-%03d",
  716. ((compiler_typ = (__DECC_VER / 10000) % 10) == 6 ? 'T' :
  717. (compiler_typ == 8 ? 'S' : 'V')),
  718. __DECC_VER / 10000000,
  719. (__DECC_VER % 10000000) / 100000, __DECC_VER % 1000);
  720. # define COMPILER_NAME compiler_name
  721. # else
  722. # if ((defined(CRAY) || defined(cray)) && defined(_RELEASE))
  723. sprintf(compiler_name, "cc version %d", _RELEASE);
  724. # define COMPILER_NAME compiler_name
  725. # else
  726. # ifdef __IBMC__
  727. sprintf( compiler_name, "IBM C version %d.%d.%d",
  728. (__IBMC__/ 100), ((__IBMC__/ 10)% 10), (__IBMC__% 10));
  729. # define COMPILER_NAME compiler_name
  730. # else
  731. # ifdef __VERSION__
  732. # define COMPILER_NAME "cc " __VERSION__
  733. # else
  734. # define COMPILER_NAME "cc "
  735. # endif
  736. # endif
  737. # endif
  738. # endif
  739. # endif
  740. # endif
  741. #endif /* ?__GNUC__ */
  742. /* Define the name to use for the OS we're compiling on */
  743. #if defined(sgi) || defined(__sgi)
  744. # define OS_NAME "Silicon Graphics IRIX"
  745. #else
  746. #ifdef sun
  747. # ifdef sparc
  748. # ifdef __SVR4
  749. # define OS_NAME "Sun SPARC/Solaris"
  750. # else /* may or may not be SunOS */
  751. # define OS_NAME "Sun SPARC"
  752. # endif
  753. # else
  754. # if defined(sun386) || defined(i386)
  755. # define OS_NAME "Sun 386i"
  756. # else
  757. # if defined(mc68020) || defined(__mc68020__)
  758. # define OS_NAME "Sun 3"
  759. # else /* mc68010 or mc68000: Sun 2 or earlier */
  760. # define OS_NAME "Sun 2"
  761. # endif
  762. # endif
  763. # endif
  764. #else
  765. #ifdef __hpux
  766. # define OS_NAME "HP-UX"
  767. #else
  768. #ifdef __osf__
  769. # define OS_NAME "DEC OSF/1"
  770. #else
  771. #ifdef _AIX
  772. # define OS_NAME "IBM AIX"
  773. #else
  774. #ifdef aiws
  775. # define OS_NAME "IBM RT/AIX"
  776. #else
  777. #if defined(CRAY) || defined(cray)
  778. # ifdef _UNICOS
  779. sprintf(os_name, "Cray UNICOS release %d", _UNICOS);
  780. # define OS_NAME os_name
  781. # else
  782. # define OS_NAME "Cray UNICOS"
  783. # endif
  784. #else
  785. #if defined(uts) || defined(UTS)
  786. # define OS_NAME "Amdahl UTS"
  787. #else
  788. #ifdef NeXT
  789. # ifdef mc68000
  790. # define OS_NAME "NeXTStep/black"
  791. # else
  792. # define OS_NAME "NeXTStep for Intel"
  793. # endif
  794. #else
  795. #if defined(linux) || defined(__linux__)
  796. # ifdef __ELF__
  797. # define OS_NAME "Linux ELF"
  798. # else
  799. # define OS_NAME "Linux a.out"
  800. # endif
  801. #else
  802. #ifdef MINIX
  803. # define OS_NAME "Minix"
  804. #else
  805. #ifdef M_UNIX
  806. # define OS_NAME "SCO Unix"
  807. #else
  808. #ifdef M_XENIX
  809. # define OS_NAME "SCO Xenix"
  810. #else
  811. #ifdef BSD
  812. # if (BSD > 199103)
  813. # define OS_NAME os_name
  814. uname(&u);
  815. sprintf(os_name, "%s %s", u.sysname, u.release);
  816. # else
  817. # ifdef __NetBSD__
  818. # define OS_NAME os_name
  819. # ifdef NetBSD0_8
  820. sprintf(os_name, "NetBSD 0.8%s", netbsd[NetBSD0_8]);
  821. # else
  822. # ifdef NetBSD0_9
  823. sprintf(os_name, "NetBSD 0.9%s", netbsd[NetBSD0_9]);
  824. # else
  825. # ifdef NetBSD1_0
  826. sprintf(os_name, "NetBSD 1.0%s", netbsd[NetBSD1_0]);
  827. # endif /* NetBSD1_0 */
  828. # endif /* NetBSD0_9 */
  829. # endif /* NetBSD0_8 */
  830. # else
  831. # ifdef __FreeBSD__
  832. # define OS_NAME "FreeBSD 1.x"
  833. # else
  834. # ifdef __bsdi__
  835. # define OS_NAME "BSD/386 1.0"
  836. # else
  837. # ifdef __386BSD__
  838. # define OS_NAME "386BSD"
  839. # else
  840. # define OS_NAME "Unknown BSD"
  841. # endif /* __386BSD__ */
  842. # endif /* __bsdi__ */
  843. # endif /* FreeBSD */
  844. # endif /* NetBSD */
  845. # endif /* BSD > 199103 */
  846. #else
  847. #ifdef __CYGWIN__
  848. # define OS_NAME "Cygwin"
  849. #else
  850. #if defined(i686) || defined(__i686) || defined(__i686__)
  851. # define OS_NAME "Intel 686"
  852. #else
  853. #if defined(i586) || defined(__i586) || defined(__i586__)
  854. # define OS_NAME "Intel 586"
  855. #else
  856. #if defined(i486) || defined(__i486) || defined(__i486__)
  857. # define OS_NAME "Intel 486"
  858. #else
  859. #if defined(i386) || defined(__i386) || defined(__i386__)
  860. # define OS_NAME "Intel 386"
  861. #else
  862. #ifdef pyr
  863. # define OS_NAME "Pyramid"
  864. #else
  865. #if defined(ultrix) || defined(__ultrix)
  866. # if defined(mips) || defined(__mips)
  867. # define OS_NAME "DEC/MIPS"
  868. # else
  869. # if defined(vax) || defined(__vax)
  870. # define OS_NAME "DEC/VAX"
  871. # else /* __alpha? */
  872. # define OS_NAME "DEC/Alpha"
  873. # endif
  874. # endif
  875. #else
  876. #ifdef gould
  877. # define OS_NAME "Gould"
  878. #else
  879. #ifdef MTS
  880. # define OS_NAME "MTS"
  881. #else
  882. #ifdef __convexc__
  883. # define OS_NAME "Convex"
  884. #else
  885. #ifdef __QNX__
  886. # define OS_NAME "QNX 4"
  887. #else
  888. #ifdef __QNXNTO__
  889. # define OS_NAME "QNX Neutrino"
  890. #else
  891. #ifdef __APPLE__
  892. # ifdef __i386__
  893. # define OS_NAME "Mac OS X Intel"
  894. # else /* __i386__ */
  895. # ifdef __ppc__
  896. # define OS_NAME "Mac OS X PowerPC"
  897. # else /* __ppc__ */
  898. # ifdef __ppc64__
  899. # define OS_NAME "Mac OS X PowerPC64"
  900. # else /* __ppc64__ */
  901. # define OS_NAME "Mac OS X"
  902. # endif /* __ppc64__ */
  903. # endif /* __ppc__ */
  904. # endif /* __i386__ */
  905. #else
  906. # define OS_NAME "Unknown"
  907. #endif /* Apple */
  908. #endif /* QNX Neutrino */
  909. #endif /* QNX 4 */
  910. #endif /* Convex */
  911. #endif /* MTS */
  912. #endif /* Gould */
  913. #endif /* DEC */
  914. #endif /* Pyramid */
  915. #endif /* 386 */
  916. #endif /* 486 */
  917. #endif /* 586 */
  918. #endif /* 686 */
  919. #endif /* Cygwin */
  920. #endif /* BSD */
  921. #endif /* SCO Xenix */
  922. #endif /* SCO Unix */
  923. #endif /* Minix */
  924. #endif /* Linux */
  925. #endif /* NeXT */
  926. #endif /* Amdahl */
  927. #endif /* Cray */
  928. #endif /* RT/AIX */
  929. #endif /* AIX */
  930. #endif /* OSF/1 */
  931. #endif /* HP-UX */
  932. #endif /* Sun */
  933. #endif /* SGI */
  934. /* Define the compile date string */
  935. #ifdef __DATE__
  936. # define COMPILE_DATE " on " __DATE__
  937. #else
  938. # define COMPILE_DATE ""
  939. #endif
  940. printf("Compiled with %s for Unix (%s)%s.\n\n",
  941. COMPILER_NAME, OS_NAME, COMPILE_DATE);
  942. } /* end function version_local() */
  943. /* 2006-03-23 SMS.
  944. * Emergency replacement for strerror(). (Useful on SunOS 4.*.)
  945. * Enable by specifying "LOCAL_UNZIP=-DNEED_STRERROR=1" on the "make"
  946. * command line.
  947. */
  948. #ifdef NEED_STRERROR
  949. char *strerror( err)
  950. int err;
  951. {
  952. extern char *sys_errlist[];
  953. extern int sys_nerr;
  954. static char no_msg[ 64];
  955. if ((err >= 0) && (err < sys_nerr))
  956. {
  957. return sys_errlist[ err];
  958. }
  959. else
  960. {
  961. sprintf( no_msg, "(no message, code = %d.)", err);
  962. return no_msg;
  963. }
  964. }
  965. #endif /* def NEED_STRERROR */
  966. /* 2006-03-23 SMS.
  967. * Emergency replacement for memmove(). (Useful on SunOS 4.*.)
  968. * Enable by specifying "LOCAL_UNZIP=-DNEED_MEMMOVE=1" on the "make"
  969. * command line.
  970. */
  971. #ifdef NEED_MEMMOVE
  972. /* memmove.c -- copy memory.
  973. Copy LENGTH bytes from SOURCE to DEST. Does not null-terminate.
  974. In the public domain.
  975. By David MacKenzie <djm@gnu.ai.mit.edu>.
  976. Adjusted by SMS.
  977. */
  978. void *memmove(dest0, source0, length)
  979. void *dest0;
  980. void const *source0;
  981. size_t length;
  982. {
  983. char *dest = dest0;
  984. char const *source = source0;
  985. if (source < dest)
  986. /* Moving from low mem to hi mem; start at end. */
  987. for (source += length, dest += length; length; --length)
  988. *--dest = *--source;
  989. else if (source != dest)
  990. {
  991. /* Moving from hi mem to low mem; start at beginning. */
  992. for (; length; --length)
  993. *dest++ = *source++;
  994. }
  995. return dest0;
  996. }
  997. #endif /* def NEED_MEMMOVE */