os2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. Copyright (c) 1990-2005 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2005-Feb-10 or later
  4. (the contents of which are also included in zip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. #include "zip.h"
  9. #ifndef UTIL /* the companion #endif is a bit of ways down ... */
  10. #include <time.h>
  11. #if defined(__IBMC__) || defined(MSC)
  12. #include <direct.h>
  13. #endif
  14. /* Extra malloc() space in names for cutpath() */
  15. #define PAD 0
  16. #define PATH_END '/'
  17. #include "os2zip.h"
  18. /* Library functions not in (most) header files */
  19. extern char *label;
  20. local ulg label_time = 0;
  21. local ulg label_mode = 0;
  22. local time_t label_utim = 0;
  23. /* Local functions */
  24. local char *readd OF((DIR *));
  25. local char *readd(d)
  26. DIR *d; /* directory stream to read from */
  27. /* Return a pointer to the next name in the directory stream d, or NULL if
  28. no more entries or an error occurs. */
  29. {
  30. struct dirent *e;
  31. e = readdir(d);
  32. return e == NULL ? (char *) NULL : e->d_name;
  33. }
  34. int wild(w)
  35. char *w; /* path/pattern to match */
  36. /* If not in exclude mode, expand the pattern based on the contents of the
  37. file system. Return an error code in the ZE_ class. */
  38. {
  39. DIR *d; /* stream for reading directory */
  40. char *e; /* name found in directory */
  41. int r; /* temporary variable */
  42. char *n; /* constructed name from directory */
  43. int f; /* true if there was a match */
  44. char *a; /* alloc'ed space for name */
  45. char *p; /* path */
  46. char *q; /* name */
  47. char v[5]; /* space for device current directory */
  48. if (volume_label == 1) {
  49. volume_label = 2;
  50. label = getVolumeLabel((w != NULL && w[1] == ':') ? to_up(w[0]) : '\0',
  51. &label_time, &label_mode, &label_utim);
  52. if (label != NULL) {
  53. newname(label, 0, 0);
  54. }
  55. if (w == NULL || (w[1] == ':' && w[2] == '\0')) return ZE_OK;
  56. /* "zip -$ foo a:" can be used to force drive name */
  57. }
  58. if (w == NULL)
  59. return ZE_OK;
  60. /* special handling of stdin request */
  61. if (strcmp(w, "-") == 0) /* if compressing stdin */
  62. return newname(w, 0, 0);
  63. /* Allocate and copy pattern */
  64. if ((p = a = malloc(strlen(w) + 1)) == NULL)
  65. return ZE_MEM;
  66. strcpy(p, w);
  67. /* catch special case: treat "*.*" as "*" for DOS-impaired people */
  68. r = strlen(p);
  69. if (strcmp(p + r - 3, "*.*") == 0)
  70. p[r - 2] = '\0';
  71. /* Normalize path delimiter as '/'. */
  72. for (q = p; *q; q++) /* use / consistently */
  73. if (*q == '\\')
  74. *q = '/';
  75. /* Only name can have special matching characters */
  76. if ((q = isshexp(p)) != NULL &&
  77. (strrchr(q, '/') != NULL || strrchr(q, ':') != NULL))
  78. {
  79. free((zvoid *)a);
  80. return ZE_PARMS;
  81. }
  82. /* Separate path and name into p and q */
  83. if ((q = strrchr(p, '/')) != NULL && (q == p || q[-1] != ':'))
  84. {
  85. *q++ = '\0'; /* path/name -> path, name */
  86. if (*p == '\0') /* path is just / */
  87. p = strcpy(v, "/.");
  88. }
  89. else if ((q = strrchr(p, ':')) != NULL)
  90. { /* has device and no or root path */
  91. *q++ = '\0';
  92. p = strcat(strcpy(v, p), ":"); /* copy device as path */
  93. if (*q == '/') /* -> device:/., name */
  94. {
  95. strcat(p, "/");
  96. q++;
  97. }
  98. strcat(p, ".");
  99. }
  100. else if (recurse && (strcmp(p, ".") == 0 || strcmp(p, "..") == 0))
  101. { /* current or parent directory */
  102. /* I can't understand Mark's code so I am adding a hack here to get
  103. * "zip -r foo ." to work. Allow the dubious "zip -r foo .." but
  104. * reject "zip -rm foo ..".
  105. */
  106. if (dispose && strcmp(p, "..") == 0)
  107. ziperr(ZE_PARMS, "cannot remove parent directory");
  108. q = "*";
  109. }
  110. else /* no path or device */
  111. {
  112. q = p;
  113. p = strcpy(v, ".");
  114. }
  115. if (recurse && *q == '\0') {
  116. q = "*";
  117. }
  118. /* Search that level for matching names */
  119. if ((d = opendir(p)) == NULL)
  120. {
  121. free((zvoid *)a);
  122. return ZE_MISS;
  123. }
  124. if ((r = strlen(p)) > 1 &&
  125. (strcmp(p + r - 2, ":.") == 0 || strcmp(p + r - 2, "/.") == 0))
  126. *(p + r - 1) = '\0';
  127. f = 0;
  128. while ((e = readd(d)) != NULL) {
  129. if (strcmp(e, ".") && strcmp(e, "..") && MATCH(q, e, 0))
  130. {
  131. f = 1;
  132. if (strcmp(p, ".") == 0) { /* path is . */
  133. r = procname(e, 0); /* name is name */
  134. if (r) {
  135. f = 0;
  136. break;
  137. }
  138. } else
  139. {
  140. if ((n = malloc(strlen(p) + strlen(e) + 2)) == NULL)
  141. {
  142. free((zvoid *)a);
  143. closedir(d);
  144. return ZE_MEM;
  145. }
  146. n = strcpy(n, p);
  147. if (n[r = strlen(n) - 1] != '/' && n[r] != ':')
  148. strcat(n, "/");
  149. r = procname(strcat(n, e), 0); /* name is path/name */
  150. free((zvoid *)n);
  151. if (r) {
  152. f = 0;
  153. break;
  154. }
  155. }
  156. }
  157. }
  158. closedir(d);
  159. /* Done */
  160. free((zvoid *)a);
  161. return f ? ZE_OK : ZE_MISS;
  162. }
  163. int procname(n, caseflag)
  164. char *n; /* name to process */
  165. int caseflag; /* true to force case-sensitive match */
  166. /* Process a name or sh expression to operate on (or exclude). Return
  167. an error code in the ZE_ class. */
  168. {
  169. char *a; /* path and name for recursion */
  170. DIR *d; /* directory stream from opendir() */
  171. char *e; /* pointer to name from readd() */
  172. int m; /* matched flag */
  173. char *p; /* path for recursion */
  174. struct stat s; /* result of stat() */
  175. struct zlist far *z; /* steps through zfiles list */
  176. if (n == NULL) /* volume_label request in freshen|delete mode ?? */
  177. return ZE_OK;
  178. if (strcmp(n, "-") == 0) /* if compressing stdin */
  179. return newname(n, 0, caseflag);
  180. else if (LSSTAT(n, &s)
  181. #if defined(__TURBOC__) || defined(__WATCOMC__)
  182. /* For these 2 compilers, stat() succeeds on wild card names! */
  183. || isshexp(n)
  184. #endif
  185. )
  186. {
  187. /* Not a file or directory--search for shell expression in zip file */
  188. p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
  189. m = 1;
  190. for (z = zfiles; z != NULL; z = z->nxt) {
  191. if (MATCH(p, z->iname, caseflag))
  192. {
  193. z->mark = pcount ? filter(z->zname, caseflag) : 1;
  194. if (verbose)
  195. fprintf(mesg, "zip diagnostic: %scluding %s\n",
  196. z->mark ? "in" : "ex", z->name);
  197. m = 0;
  198. }
  199. }
  200. free((zvoid *)p);
  201. return m ? ZE_MISS : ZE_OK;
  202. }
  203. /* Live name--use if file, recurse if directory */
  204. for (p = n; *p; p++) /* use / consistently */
  205. if (*p == '\\')
  206. *p = '/';
  207. if ((s.st_mode & S_IFDIR) == 0)
  208. {
  209. /* add or remove name of file */
  210. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  211. return m;
  212. } else {
  213. /* Add trailing / to the directory name */
  214. if ((p = malloc(strlen(n)+2)) == NULL)
  215. return ZE_MEM;
  216. if (strcmp(n, ".") == 0 || strcmp(n, "/.") == 0) {
  217. *p = '\0'; /* avoid "./" prefix and do not create zip entry */
  218. } else {
  219. strcpy(p, n);
  220. a = p + strlen(p);
  221. if (a[-1] != '/')
  222. strcpy(a, "/");
  223. if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
  224. free((zvoid *)p);
  225. return m;
  226. }
  227. }
  228. /* recurse into directory */
  229. if (recurse && (d = opendir(n)) != NULL)
  230. {
  231. while ((e = readd(d)) != NULL) {
  232. if (strcmp(e, ".") && strcmp(e, ".."))
  233. {
  234. if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  235. {
  236. closedir(d);
  237. free((zvoid *)p);
  238. return ZE_MEM;
  239. }
  240. strcat(strcpy(a, p), e);
  241. if ((m = procname(a, caseflag)) != ZE_OK) /* recurse on name */
  242. {
  243. if (m == ZE_MISS)
  244. zipwarn("name not matched: ", a);
  245. else
  246. ziperr(m, a);
  247. }
  248. free((zvoid *)a);
  249. }
  250. }
  251. closedir(d);
  252. }
  253. free((zvoid *)p);
  254. } /* (s.st_mode & S_IFDIR) == 0) */
  255. return ZE_OK;
  256. }
  257. char *ex2in(x, isdir, pdosflag)
  258. char *x; /* external file name */
  259. int isdir; /* input: x is a directory */
  260. int *pdosflag; /* output: force MSDOS file attributes? */
  261. /* Convert the external file name to a zip file name, returning the malloc'ed
  262. string or NULL if not enough memory. */
  263. {
  264. char *n; /* internal file name (malloc'ed) */
  265. char *t; /* shortened name */
  266. int dosflag;
  267. dosflag = dosify || IsFileSystemFAT(x) || (x == label);
  268. if (!dosify && use_longname_ea && (t = GetLongPathEA(x)) != NULL)
  269. {
  270. x = t;
  271. dosflag = 0;
  272. }
  273. /* Find starting point in name before doing malloc */
  274. /* Strip drive specification */
  275. t = *x && *(x + 1) == ':' ? x + 2 : x;
  276. /* Strip "//host/share/" part of a UNC name */
  277. if ((!strncmp(x,"//",2) || !strncmp(x,"\\\\",2)) &&
  278. (x[2] != '\0' && x[2] != '/' && x[2] != '\\')) {
  279. n = x + 2;
  280. while (*n != '\0' && *n != '/' && *n != '\\')
  281. n++; /* strip host name */
  282. if (*n != '\0') {
  283. n++;
  284. while (*n != '\0' && *n != '/' && *n != '\\')
  285. n++; /* strip `share' name */
  286. }
  287. if (*n != '\0')
  288. t = n + 1;
  289. }
  290. /* Strip leading "/" to convert an absolute path into a relative path */
  291. while (*t == '/' || *t == '\\')
  292. t++;
  293. /* Strip leading "./" as well as drive letter */
  294. while (*t == '.' && (t[1] == '/' || t[1] == '\\'))
  295. t += 2;
  296. /* Make changes, if any, to the copied name (leave original intact) */
  297. for (n = t; *n; n++)
  298. if (*n == '\\')
  299. *n = '/';
  300. if (!pathput)
  301. t = last(t, PATH_END);
  302. /* Malloc space for internal name and copy it */
  303. if ((n = malloc(strlen(t) + 1)) == NULL)
  304. return NULL;
  305. strcpy(n, t);
  306. if (dosify)
  307. msname(n);
  308. /* Returned malloc'ed name */
  309. if (pdosflag)
  310. *pdosflag = dosflag;
  311. return n;
  312. }
  313. char *in2ex(n)
  314. char *n; /* internal file name */
  315. /* Convert the zip file name to an external file name, returning the malloc'ed
  316. string or NULL if not enough memory. */
  317. {
  318. char *x; /* external file name */
  319. if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  320. return NULL;
  321. strcpy(x, n);
  322. return x;
  323. }
  324. void stamp(f, d)
  325. char *f; /* name of file to change */
  326. ulg d; /* dos-style time to change it to */
  327. /* Set last updated and accessed time of file f to the DOS time d. */
  328. {
  329. SetFileTime(f, d);
  330. }
  331. ulg filetime(f, a, n, t)
  332. char *f; /* name of file to get info on */
  333. ulg *a; /* return value: file attributes */
  334. long *n; /* return value: file size */
  335. iztimes *t; /* return value: access, modific. and creation times */
  336. /* If file *f does not exist, return 0. Else, return the file's last
  337. modified date and time as an MSDOS date and time. The date and
  338. time is returned in a long with the date most significant to allow
  339. unsigned integer comparison of absolute times. Also, if a is not
  340. a NULL pointer, store the file attributes there, with the high two
  341. bytes being the Unix attributes, and the low byte being a mapping
  342. of that to DOS attributes. If n is not NULL, store the file size
  343. there. If t is not NULL, the file's access, modification and creation
  344. times are stored there as UNIX time_t values.
  345. If f is "-", use standard input as the file. If f is a device, return
  346. a file size of -1 */
  347. {
  348. struct stat s; /* results of stat() */
  349. char *name;
  350. ulg r;
  351. unsigned int len = strlen(f);
  352. int isstdin = !strcmp(f, "-");
  353. if (f == label) {
  354. if (a != NULL)
  355. *a = label_mode;
  356. if (n != NULL)
  357. *n = -2L; /* convention for a label name */
  358. if (t != NULL)
  359. t->atime = t->mtime = t->ctime = label_utim;
  360. return label_time;
  361. }
  362. if ((name = malloc(len + 1)) == NULL) {
  363. ZIPERR(ZE_MEM, "filetime");
  364. }
  365. strcpy(name, f);
  366. if (name[len - 1] == '/')
  367. name[len - 1] = '\0';
  368. /* not all systems allow stat'ing a file with / appended */
  369. if (isstdin) {
  370. /* it is common for some PC based compilers to
  371. fail with fstat() on devices or pipes */
  372. if (fstat(fileno(stdin), &s) != 0) {
  373. s.st_mode = S_IFREG; s.st_size = -1L;
  374. }
  375. time(&s.st_ctime);
  376. s.st_atime = s.st_mtime = s.st_ctime;
  377. } else if (LSSTAT(name, &s) != 0) {
  378. /* Accept about any file kind including directories
  379. * (stored with trailing / with -r option)
  380. */
  381. free(name);
  382. return 0;
  383. }
  384. if (a != NULL) {
  385. *a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name));
  386. }
  387. if (n != NULL)
  388. *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  389. #ifdef __WATCOMC__
  390. /* of course, Watcom always has to make an exception */
  391. if (s.st_atime == 312764400)
  392. s.st_atime = s.st_mtime;
  393. if (s.st_ctime == 312764400)
  394. s.st_ctime = s.st_mtime;
  395. #endif
  396. if (t != NULL) {
  397. t->atime = s.st_atime;
  398. t->mtime = s.st_mtime;
  399. t->ctime = s.st_ctime;
  400. }
  401. r = GetFileTime(name);
  402. free(name);
  403. return r;
  404. }
  405. int deletedir(d)
  406. char *d; /* directory to delete */
  407. /* Delete the directory *d if it is empty, do nothing otherwise.
  408. Return the result of rmdir(), delete(), or system().
  409. */
  410. {
  411. return rmdir(d);
  412. }
  413. #if defined MY_ZCALLOC /* Special zcalloc function for MEMORY16 (MSDOS/OS2) */
  414. #ifdef MSC /* Microsoft C */
  415. zvoid far *zcalloc (unsigned items, unsigned size)
  416. {
  417. return (zvoid far *)halloc((long)items, size);
  418. }
  419. zvoid zcfree (zvoid far *ptr)
  420. {
  421. hfree((void huge *)ptr);
  422. }
  423. #endif /* MSC */
  424. #endif /* MY_ZCALLOC */
  425. #endif /* !UTIL */