msdos.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 /* little or no material in this file is used by UTIL */
  10. #include <dos.h>
  11. #include <time.h>
  12. #if defined(__GO32__) || defined(__TURBOC__)
  13. # include <dir.h> /* prototypes of find*() */
  14. typedef struct ffblk ff_dir;
  15. # define FATTR (hidden_files ? FA_HIDDEN+FA_SYSTEM+FA_DIREC : FA_DIREC)
  16. # define FFIRST(n,d,a) findfirst(n,(struct ffblk *)d,a)
  17. # define FNEXT(d) findnext((struct ffblk *)d)
  18. # if (defined(__TURBOC__) || (defined(__DJGPP__) && (__DJGPP__ >=2)))
  19. # if (defined(__DJGPP__) && (__DJGPP__ == 2) && (__DJGPP_MINOR__ == 0))
  20. # include <libc/dosio.h>
  21. # endif
  22. # define GetFileMode(name) _chmod(name, 0)
  23. # define SetFileMode(name, attr) _chmod(name, 1, attr)
  24. # else /* DJGPP v1.x */
  25. # define GetFileMode(name) bdosptr(0x43, (name), 0)
  26. # endif
  27. #endif /* __GO32__ || __TURBOC__ */
  28. #if defined(MSC) || defined(__WATCOMC__)
  29. typedef struct find_t ff_dir;
  30. # define FATTR (hidden_files ? _A_HIDDEN+_A_SYSTEM+_A_SUBDIR : _A_SUBDIR)
  31. # ifndef FA_LABEL
  32. # define FA_LABEL _A_VOLID
  33. # endif
  34. # define FFIRST(n,d,a) _dos_findfirst(n,a,(struct find_t *)d)
  35. # define FNEXT(d) _dos_findnext((struct find_t *)d)
  36. # define ff_name name
  37. # define ff_fdate wr_date
  38. # define ff_ftime wr_time
  39. # define ff_attrib attrib
  40. #endif /* MSC || __WATCOMC__ */
  41. #ifdef __EMX__
  42. # ifdef EMX_OBSOLETE /* emx 0.9b or earlier */
  43. # define size_t xxx_size_t
  44. # define wchar_t xxx_wchar_t
  45. # define tm xxx_tm
  46. # include <sys/emx.h>
  47. # undef size_t
  48. # undef wchar_t
  49. # undef tm
  50. # else /* !EMX_OBSOLETE */ /* emx 0.9c or newer */
  51. # include <emx/syscalls.h>
  52. # endif /* ?EMX_OBSOLETE */
  53. typedef struct _find ff_dir;
  54. # define FATTR (hidden_files ? _A_HIDDEN+_A_SYSTEM+_A_SUBDIR : _A_SUBDIR)
  55. # define FA_LABEL _A_VOLID
  56. # define FFIRST(n,d,a) __findfirst(n,a,d)
  57. # define FNEXT(d) __findnext(d)
  58. # define ff_name name
  59. # define ff_fdate date
  60. # define ff_ftime time
  61. # define ff_attrib attr
  62. # define GetFileMode(name) __chmod(name, 0, 0)
  63. # define SetFileMode(name, attr) __chmod(name, 1, attr)
  64. #endif /* __EMX__ */
  65. #ifndef SetFileMode
  66. # define SetFileMode(name, attr) _dos_setfileattr(name, attr)
  67. #endif
  68. #define PAD 0
  69. #define PATH_END '/'
  70. /* Library functions not in (most) header files */
  71. int rmdir OF((const char *));
  72. int utime OF((char *, ztimbuf *));
  73. /* Local functions */
  74. #ifndef GetFileMode
  75. int GetFileMode OF((char *name));
  76. #endif /* !GetFileMode */
  77. local int initDirSearch OF((char *name, ff_dir *ff_context_p));
  78. local char *getVolumeLabel OF((int, ulg *, ulg *, time_t *));
  79. local int wild_recurse OF((char *, char *));
  80. local int procname_dos OF((char *n, int caseflag, unsigned attribs));
  81. local int is_running_on_windows OF((void));
  82. #define MSDOS_INVALID_ATTR 0xFF
  83. #define getDirEntryAttr(d) ((d)->ff_attrib)
  84. /* Module level variables */
  85. extern char *label;
  86. local ulg label_time = 0;
  87. local ulg label_mode = 0;
  88. local time_t label_utim = 0;
  89. /* Module level constants */
  90. local ZCONST char wild_match_all[] = "*.*";
  91. #ifndef GetFileMode
  92. int GetFileMode(char *name)
  93. {
  94. unsigned int attr = 0;
  95. return (_dos_getfileattr(name, &attr) ? -1 : attr);
  96. }
  97. #endif /* !GetFileMode */
  98. local int initDirSearch(name, ff_context_p)
  99. char *name; /* name of directory to scan */
  100. ff_dir *ff_context_p; /* pointer to FFIRST/FNEXT context structure */
  101. {
  102. int r; /* FFIRST return value */
  103. char *p, *q; /* temporary copy of name, and aux pointer */
  104. if ((p = malloc(strlen(name) + (2 + sizeof(wild_match_all)))) == NULL)
  105. return ZE_MEM;
  106. strcpy(p, name);
  107. q = p + strlen(p);
  108. if (q[-1] == ':')
  109. *q++ = '.';
  110. if ((q - p) > 0 && *(q - 1) != '/')
  111. *q++ = '/';
  112. strcpy(q, wild_match_all);
  113. r = FFIRST(p, ff_context_p, FATTR);
  114. free((zvoid *)p);
  115. return (r ? ZE_MISS : ZE_OK);
  116. }
  117. local char *getVolumeLabel(drive, vtime, vmode, vutim)
  118. int drive; /* drive name: 'A' .. 'Z' or '\0' for current drive */
  119. ulg *vtime; /* volume label creation time (DOS format) */
  120. ulg *vmode; /* volume label file mode */
  121. time_t *vutim;/* volume label creation time (UNIX format) */
  122. /* If a volume label exists for the given drive, return its name and
  123. set its time and mode. The returned name must be static data. */
  124. {
  125. static char vol[14];
  126. ff_dir d;
  127. char *p;
  128. if (drive) {
  129. vol[0] = (char)drive;
  130. strcpy(vol+1, ":/");
  131. } else {
  132. strcpy(vol, "/");
  133. }
  134. strcat(vol, wild_match_all);
  135. if (FFIRST(vol, &d, FA_LABEL) == 0) {
  136. strncpy(vol, d.ff_name, sizeof(vol)-1);
  137. vol[sizeof(vol)-1] = '\0'; /* just in case */
  138. if ((p = strchr(vol, '.')) != NULL) /* remove dot, though PKZIP doesn't */
  139. strcpy(p, p + 1);
  140. *vtime = ((ulg)d.ff_fdate << 16) | ((ulg)d.ff_ftime & 0xffff);
  141. *vmode = (ulg)d.ff_attrib;
  142. *vutim = dos2unixtime(*vtime);
  143. return vol;
  144. }
  145. return NULL;
  146. }
  147. #ifdef MSDOS16
  148. #define ONENAMELEN 12 /* no 16-bit compilers supports LFN */
  149. #else
  150. #define ONENAMELEN 255
  151. #endif
  152. /* whole is a pathname with wildcards, wildtail points somewhere in the */
  153. /* middle of it. All wildcards to be expanded must come AFTER wildtail. */
  154. local int wild_recurse(whole, wildtail)
  155. char *whole;
  156. char *wildtail;
  157. {
  158. ff_dir dir;
  159. char *subwild, *name, *newwhole = NULL, *glue = NULL, plug = 0, plug2;
  160. ush newlen, amatch = 0;
  161. int e = ZE_MISS;
  162. if (!isshexp(wildtail)) {
  163. struct stat s; /* dummy buffer for stat() */
  164. if (!LSSTAT(whole, &s)) /* file exists ? */
  165. return procname(whole, 0);
  166. else
  167. return ZE_MISS; /* woops, no wildcards! */
  168. }
  169. /* back up thru path components till existing dir found */
  170. do {
  171. name = wildtail + strlen(wildtail) - 1;
  172. for (;;)
  173. if (name-- <= wildtail || *name == PATH_END) {
  174. subwild = name + 1;
  175. plug2 = *subwild;
  176. *subwild = 0;
  177. break;
  178. }
  179. if (glue)
  180. *glue = plug;
  181. glue = subwild;
  182. plug = plug2;
  183. e = initDirSearch(whole, &dir);
  184. } while (e == ZE_MISS && subwild > wildtail);
  185. wildtail = subwild; /* skip past non-wild components */
  186. if (e != ZE_OK) {
  187. if (glue)
  188. *glue = plug;
  189. goto ohforgetit;
  190. }
  191. subwild = strchr(wildtail + 1, PATH_END);
  192. /* this "+ 1" dodges the ^^^ hole left by *glue == 0 */
  193. if (subwild != NULL) {
  194. *(subwild++) = 0; /* wildtail = one component pattern */
  195. newlen = strlen(whole) + strlen(subwild) + (ONENAMELEN + 2);
  196. } else
  197. newlen = strlen(whole) + (ONENAMELEN + 1);
  198. if ((newwhole = malloc(newlen)) == NULL) {
  199. if (glue)
  200. *glue = plug;
  201. e = ZE_MEM;
  202. goto ohforgetit;
  203. }
  204. strcpy(newwhole, whole);
  205. newlen = strlen(newwhole);
  206. if (glue)
  207. *glue = plug; /* repair damage to whole */
  208. if (!isshexp(wildtail)) {
  209. e = ZE_MISS; /* non-wild name not found */
  210. goto ohforgetit;
  211. }
  212. do {
  213. if (strcmp(dir.ff_name, ".") && strcmp(dir.ff_name, "..")
  214. && MATCH(wildtail, dir.ff_name, 0)) {
  215. strcpy(newwhole + newlen, dir.ff_name);
  216. if (subwild) {
  217. name = newwhole + strlen(newwhole);
  218. *(name++) = PATH_END;
  219. strcpy(name, subwild);
  220. e = wild_recurse(newwhole, name);
  221. } else
  222. e = procname_dos(newwhole, 0, getDirEntryAttr(&dir));
  223. newwhole[newlen] = 0;
  224. if (e == ZE_OK)
  225. amatch = 1;
  226. else if (e != ZE_MISS)
  227. break;
  228. }
  229. } while (FNEXT(&dir) == 0);
  230. ohforgetit:
  231. if (subwild)
  232. *--subwild = PATH_END;
  233. if (newwhole)
  234. free(newwhole);
  235. if (e == ZE_MISS && amatch)
  236. e = ZE_OK;
  237. return e;
  238. }
  239. int wild(w)
  240. char *w; /* path/pattern to match */
  241. /* If not in exclude mode, expand the pattern based on the contents of the
  242. file system. Return an error code in the ZE_ class. */
  243. {
  244. char *p; /* path */
  245. char *q; /* diskless path */
  246. int e; /* result */
  247. if (volume_label == 1) {
  248. volume_label = 2;
  249. label = getVolumeLabel((w != NULL && w[1] == ':') ? to_up(w[0]) : '\0',
  250. &label_time, &label_mode, &label_utim);
  251. if (label != NULL)
  252. (void)newname(label, 0, 0);
  253. if (w == NULL || (w[1] == ':' && w[2] == '\0')) return ZE_OK;
  254. /* "zip -$ foo a:" can be used to force drive name */
  255. }
  256. /* special handling of stdin request */
  257. if (strcmp(w, "-") == 0) /* if compressing stdin */
  258. return newname(w, 0, 0);
  259. /* Allocate and copy pattern, leaving room to add "." if needed */
  260. if ((p = malloc(strlen(w) + 2)) == NULL)
  261. return ZE_MEM;
  262. strcpy(p, w);
  263. /* Normalize path delimiter as '/' */
  264. for (q = p; *q; q++) /* use / consistently */
  265. if (*q == '\\')
  266. *q = '/';
  267. /* Separate the disk part of the path */
  268. q = strchr(p, ':');
  269. if (q != NULL) {
  270. if (strchr(++q, ':')) /* sanity check for safety of wild_recurse */
  271. return ZE_MISS;
  272. } else
  273. q = p;
  274. /* Normalize bare disk names */
  275. if (q > p && !*q)
  276. strcpy(q, ".");
  277. /* Here we go */
  278. e = wild_recurse(p, q);
  279. free((zvoid *)p);
  280. return e;
  281. }
  282. local int procname_dos(n, caseflag, attribs)
  283. char *n; /* name to process */
  284. int caseflag; /* true to force case-sensitive match */
  285. unsigned attribs; /* file attributes, if available */
  286. /* Process a name or sh expression to operate on (or exclude). Return
  287. an error code in the ZE_ class. */
  288. {
  289. char *a; /* path and name for recursion */
  290. ff_dir *d; /* control structure for FFIRST/FNEXT */
  291. char *e; /* pointer to name from readd() */
  292. int m; /* matched flag */
  293. int ff_status; /* return value of FFIRST/FNEXT */
  294. char *p; /* path for recursion */
  295. struct stat s; /* result of stat() */
  296. struct zlist far *z; /* steps through zfiles list */
  297. if (n == NULL) /* volume_label request in freshen|delete mode ?? */
  298. return ZE_OK;
  299. if (strcmp(n, "-") == 0) /* if compressing stdin */
  300. return newname(n, 0, caseflag);
  301. else if (*n == '\0') return ZE_MISS;
  302. else if (attribs != MSDOS_INVALID_ATTR)
  303. {
  304. /* Avoid calling stat() for performance reasons when it is already known
  305. (from a previous directory scan) that the passed name corresponds to
  306. a "real existing" file. The only information needed further down in
  307. this function is the distinction between directory entries and other
  308. (typically normal file) entries. This distinction can be derived from
  309. the file's attributes that the directory lookup has already provided
  310. "for free".
  311. */
  312. s.st_mode = ((attribs & MSDOS_DIR_ATTR) ? S_IFDIR : S_IFREG);
  313. }
  314. else if (LSSTAT(n, &s)
  315. #ifdef __TURBOC__
  316. /* For this compiler, stat() succeeds on wild card names! */
  317. || isshexp(n)
  318. #endif
  319. )
  320. {
  321. /* Not a file or directory--search for shell expression in zip file */
  322. if (caseflag) {
  323. p = malloc(strlen(n) + 1);
  324. if (p != NULL)
  325. strcpy(p, n);
  326. } else
  327. p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
  328. m = 1;
  329. for (z = zfiles; z != NULL; z = z->nxt) {
  330. if (MATCH(p, z->iname, caseflag))
  331. {
  332. z->mark = pcount ? filter(z->zname, caseflag) : 1;
  333. if (z->mark) z->dosflag = 1; /* force DOS attribs for incl. names */
  334. if (verbose)
  335. fprintf(mesg, "zip diagnostic: %scluding %s\n",
  336. z->mark ? "in" : "ex", z->name);
  337. m = 0;
  338. }
  339. }
  340. free((zvoid *)p);
  341. return m ? ZE_MISS : ZE_OK;
  342. }
  343. /* Live name--use if file, recurse if directory */
  344. for (p = n; *p; p++) /* use / consistently */
  345. if (*p == '\\')
  346. *p = '/';
  347. if ((s.st_mode & S_IFDIR) == 0)
  348. {
  349. /* add or remove name of file */
  350. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  351. return m;
  352. } else {
  353. /* Add trailing / to the directory name */
  354. if ((p = malloc(strlen(n)+2)) == NULL)
  355. return ZE_MEM;
  356. if (strcmp(n, ".") == 0 || strcmp(n, "/.") == 0) {
  357. *p = '\0'; /* avoid "./" prefix and do not create zip entry */
  358. } else {
  359. strcpy(p, n);
  360. a = p + strlen(p);
  361. if (a[-1] != '/')
  362. strcpy(a, "/");
  363. if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
  364. free((zvoid *)p);
  365. return m;
  366. }
  367. }
  368. /* recurse into directory */
  369. if (recurse)
  370. {
  371. if ((d = malloc(sizeof(ff_dir))) == NULL ||
  372. (m = initDirSearch(n, d)) == ZE_MEM)
  373. {
  374. if (d != NULL)
  375. free((zvoid *)d);
  376. free((zvoid *)p);
  377. return ZE_MEM;
  378. }
  379. for (e = d->ff_name, ff_status = m;
  380. ff_status == 0;
  381. ff_status = FNEXT(d))
  382. {
  383. if (strcmp(e, ".") && strcmp(e, ".."))
  384. {
  385. if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  386. {
  387. free((zvoid *)d);
  388. free((zvoid *)p);
  389. return ZE_MEM;
  390. }
  391. strcat(strcpy(a, p), e);
  392. if ((m = procname_dos(a, caseflag, getDirEntryAttr(d)))
  393. != ZE_OK) /* recurse on name */
  394. {
  395. if (m == ZE_MISS)
  396. zipwarn("name not matched: ", a);
  397. else
  398. ziperr(m, a);
  399. }
  400. free((zvoid *)a);
  401. }
  402. }
  403. free((zvoid *)d);
  404. }
  405. free((zvoid *)p);
  406. } /* (s.st_mode & S_IFDIR) == 0) */
  407. return ZE_OK;
  408. }
  409. int procname(n, caseflag)
  410. char *n; /* name to process */
  411. int caseflag; /* true to force case-sensitive match */
  412. {
  413. return procname_dos(n, caseflag, MSDOS_INVALID_ATTR);
  414. }
  415. char *ex2in(x, isdir, pdosflag)
  416. char *x; /* external file name */
  417. int isdir; /* input: x is a directory */
  418. int *pdosflag; /* output: force MSDOS file attributes? */
  419. /* Convert the external file name to a zip file name, returning the malloc'ed
  420. string or NULL if not enough memory. */
  421. {
  422. char *n; /* internal file name (malloc'ed) */
  423. char *t; /* shortened name */
  424. int dosflag;
  425. dosflag = 1;
  426. /* Find starting point in name before doing malloc */
  427. /* Strip drive specification */
  428. t = *x && *(x + 1) == ':' ? x + 2 : x;
  429. /* Strip "//host/share/" part of a UNC name */
  430. if ((!strncmp(x,"//",2) || !strncmp(x,"\\\\",2)) &&
  431. (x[2] != '\0' && x[2] != '/' && x[2] != '\\')) {
  432. n = x + 2;
  433. while (*n != '\0' && *n != '/' && *n != '\\')
  434. n++; /* strip host name */
  435. if (*n != '\0') {
  436. n++;
  437. while (*n != '\0' && *n != '/' && *n != '\\')
  438. n++; /* strip `share' name */
  439. }
  440. if (*n != '\0')
  441. t = n + 1;
  442. }
  443. /* Strip leading "/" to convert an absolute path into a relative path */
  444. while (*t == '/' || *t == '\\')
  445. t++;
  446. /* Skip leading "./" as well */
  447. while (*t == '.' && (t[1] == '/' || t[1] == '\\'))
  448. t += 2;
  449. /* Make changes, if any, to the copied name (leave original intact) */
  450. for (n = t; *n; n++)
  451. if (*n == '\\')
  452. *n = '/';
  453. if (!pathput)
  454. t = last(t, PATH_END);
  455. /* Malloc space for internal name and copy it */
  456. if ((n = malloc(strlen(t) + 1)) == NULL)
  457. return NULL;
  458. strcpy(n, t);
  459. if (isdir == 42) return n; /* avoid warning on unused variable */
  460. if (dosify)
  461. msname(n);
  462. else
  463. #if defined(__DJGPP__) && __DJGPP__ >= 2
  464. if (_USE_LFN == 0)
  465. #endif
  466. strlwr(n);
  467. if (pdosflag)
  468. *pdosflag = dosflag;
  469. return n;
  470. }
  471. char *in2ex(n)
  472. char *n; /* internal file name */
  473. /* Convert the zip file name to an external file name, returning the malloc'ed
  474. string or NULL if not enough memory. */
  475. {
  476. char *x; /* external file name */
  477. if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  478. return NULL;
  479. strcpy(x, n);
  480. return x;
  481. }
  482. void stamp(f, d)
  483. char *f; /* name of file to change */
  484. ulg d; /* dos-style time to change it to */
  485. /* Set last updated and accessed time of file f to the DOS time d. */
  486. {
  487. #if defined(__TURBOC__) || defined(__GO32__)
  488. int h; /* file handle */
  489. if ((h = open(f, 0)) != -1)
  490. {
  491. setftime(h, (struct ftime *)(void *)&d);
  492. close(h);
  493. }
  494. #else /* !__TURBOC__ && !__GO32__ */
  495. ztimbuf u; /* argument for utime() */
  496. /* Convert DOS time to time_t format in u.actime and u.modtime */
  497. u.actime = u.modtime = dos2unixtime(d);
  498. /* Set updated and accessed times of f */
  499. utime(f, &u);
  500. #endif /* ?(__TURBOC__ || __GO32__) */
  501. }
  502. ulg filetime(f, a, n, t)
  503. char *f; /* name of file to get info on */
  504. ulg *a; /* return value: file attributes */
  505. long *n; /* return value: file size */
  506. iztimes *t; /* return value: access, modific. and creation times */
  507. /* If file *f does not exist, return 0. Else, return the file's last
  508. modified date and time as an MSDOS date and time. The date and
  509. time is returned in a long with the date most significant to allow
  510. unsigned integer comparison of absolute times. Also, if a is not
  511. a NULL pointer, store the file attributes there, with the high two
  512. bytes being the Unix attributes, and the low byte being a mapping
  513. of that to DOS attributes. If n is not NULL, store the file size
  514. there. If t is not NULL, the file's access, modification and creation
  515. times are stored there as UNIX time_t values.
  516. If f is "-", use standard input as the file. If f is a device, return
  517. a file size of -1 */
  518. {
  519. struct stat s; /* results of stat() */
  520. /* convert FNMAX to malloc - 11/8/04 EG */
  521. char *name;
  522. int len = strlen(f);
  523. int isstdin = !strcmp(f, "-");
  524. if (f == label) {
  525. if (a != NULL)
  526. *a = label_mode;
  527. if (n != NULL)
  528. *n = -2L; /* convention for a label name */
  529. if (t != NULL)
  530. t->atime = t->mtime = t->ctime = label_utim;
  531. return label_time;
  532. }
  533. if ((name = malloc(len + 1)) == NULL) {
  534. ZIPERR(ZE_MEM, "filetime");
  535. }
  536. strcpy(name, f);
  537. if (name[len - 1] == '/')
  538. name[len - 1] = '\0';
  539. /* not all systems allow stat'ing a file with / appended */
  540. if (isstdin) {
  541. if (fstat(fileno(stdin), &s) != 0) {
  542. free(name);
  543. error("fstat(stdin)");
  544. }
  545. time((time_t *)&s.st_mtime); /* some fstat()s return time zero */
  546. } else if (LSSTAT(name, &s) != 0) {
  547. /* Accept about any file kind including directories
  548. * (stored with trailing / with -r option)
  549. */
  550. free(name);
  551. return 0;
  552. }
  553. if (a != NULL) {
  554. *a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name));
  555. #if (S_IFREG != 0x8000)
  556. /* kludge to work around non-standard S_IFREG flag used in DJGPP V2.x */
  557. if ((s.st_mode & S_IFMT) == S_IFREG) *a |= 0x80000000L;
  558. #endif
  559. }
  560. free(name);
  561. if (n != NULL)
  562. *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  563. if (t != NULL) {
  564. t->atime = s.st_atime;
  565. t->mtime = s.st_mtime;
  566. t->ctime = s.st_ctime;
  567. }
  568. return unix2dostime((time_t *)&s.st_mtime);
  569. }
  570. int deletedir(d)
  571. char *d; /* directory to delete */
  572. /* Delete the directory *d if it is empty, do nothing otherwise.
  573. Return the result of rmdir(), delete(), or system().
  574. */
  575. {
  576. return rmdir(d);
  577. }
  578. int set_extra_field(z, z_utim)
  579. struct zlist far *z;
  580. iztimes *z_utim;
  581. /* create extra field and change z->att if desired */
  582. {
  583. #ifdef USE_EF_UT_TIME
  584. #ifdef IZ_CHECK_TZ
  585. if (!zp_tz_is_valid) return ZE_OK; /* skip silently if no valid TZ info */
  586. #endif
  587. if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)
  588. return ZE_MEM;
  589. z->extra[0] = 'U';
  590. z->extra[1] = 'T';
  591. z->extra[2] = EB_UT_LEN(1); /* length of data part of e.f. */
  592. z->extra[3] = 0;
  593. z->extra[4] = EB_UT_FL_MTIME;
  594. z->extra[5] = (char)(z_utim->mtime);
  595. z->extra[6] = (char)(z_utim->mtime >> 8);
  596. z->extra[7] = (char)(z_utim->mtime >> 16);
  597. z->extra[8] = (char)(z_utim->mtime >> 24);
  598. z->cext = z->ext = (EB_HEADSIZE+EB_UT_LEN(1));
  599. z->cextra = z->extra;
  600. return ZE_OK;
  601. #else /* !USE_EF_UT_TIME */
  602. return (int)(z-z);
  603. #endif /* ?USE_EF_UT_TIME */
  604. }
  605. #ifdef MY_ZCALLOC /* Special zcalloc function for MEMORY16 (MSDOS/OS2) */
  606. #if defined(__TURBOC__) && !defined(OS2)
  607. /* Small and medium model are for now limited to near allocation with
  608. * reduced MAX_WBITS and MAX_MEM_LEVEL
  609. */
  610. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  611. * and farmalloc(64K) returns a pointer with an offset of 8, so we
  612. * must fix the pointer. Warning: the pointer must be put back to its
  613. * original form in order to free it, use zcfree().
  614. */
  615. #define MAX_PTR 10
  616. /* 10*64K = 640K */
  617. local int next_ptr = 0;
  618. typedef struct ptr_table_s {
  619. zvoid far *org_ptr;
  620. zvoid far *new_ptr;
  621. } ptr_table;
  622. local ptr_table table[MAX_PTR];
  623. /* This table is used to remember the original form of pointers
  624. * to large buffers (64K). Such pointers are normalized with a zero offset.
  625. * Since MSDOS is not a preemptive multitasking OS, this table is not
  626. * protected from concurrent access. This hack doesn't work anyway on
  627. * a protected system like OS/2. Use Microsoft C instead.
  628. */
  629. zvoid far *zcalloc (unsigned items, unsigned size)
  630. {
  631. zvoid far *buf;
  632. ulg bsize = (ulg)items*size;
  633. if (bsize < (65536L-16L)) {
  634. buf = farmalloc(bsize);
  635. if (*(ush*)&buf != 0) return buf;
  636. } else {
  637. buf = farmalloc(bsize + 16L);
  638. }
  639. if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  640. table[next_ptr].org_ptr = buf;
  641. /* Normalize the pointer to seg:0 */
  642. *((ush*)&buf+1) += ((ush)((uch*)buf-NULL) + 15) >> 4;
  643. *(ush*)&buf = 0;
  644. table[next_ptr++].new_ptr = buf;
  645. return buf;
  646. }
  647. zvoid zcfree (zvoid far *ptr)
  648. {
  649. int n;
  650. if (*(ush*)&ptr != 0) { /* object < 64K */
  651. farfree(ptr);
  652. return;
  653. }
  654. /* Find the original pointer */
  655. for (n = next_ptr - 1; n >= 0; n--) {
  656. if (ptr != table[n].new_ptr) continue;
  657. farfree(table[n].org_ptr);
  658. while (++n < next_ptr) {
  659. table[n-1] = table[n];
  660. }
  661. next_ptr--;
  662. return;
  663. }
  664. ziperr(ZE_MEM, "zcfree: ptr not found");
  665. }
  666. #endif /* __TURBOC__ */
  667. #if defined(MSC) || defined(__WATCOMC__)
  668. #if (!defined(_MSC_VER) || (_MSC_VER < 700))
  669. # define _halloc halloc
  670. # define _hfree hfree
  671. #endif
  672. zvoid far *zcalloc (unsigned items, unsigned size)
  673. {
  674. return (zvoid far *)_halloc((long)items, size);
  675. }
  676. zvoid zcfree (zvoid far *ptr)
  677. {
  678. _hfree((void huge *)ptr);
  679. }
  680. #endif /* MSC || __WATCOMC__ */
  681. #endif /* MY_ZCALLOC */
  682. #if (defined(__WATCOMC__) && defined(ASMV) && !defined(__386__))
  683. /* This is a hack to connect "call _exit" in match.asm to exit() */
  684. #pragma aux xit "_exit" parm caller []
  685. void xit(void)
  686. {
  687. exit(20);
  688. }
  689. #endif
  690. local int is_running_on_windows(void)
  691. {
  692. char * var = getenv("OS");
  693. /* if the OS env.var says 'Windows_NT' then */
  694. /* we're likely running on a variant of WinNT */
  695. if ((NULL != var) && (0 == strcmp("Windows_NT", var)))
  696. {
  697. return 1;
  698. }
  699. /* if the windir env.var is non-null then */
  700. /* we're likely running on a variant of Win9x */
  701. /* DOS mode of Win9x doesn't define windir, only winbootdir */
  702. /* NT's command.com can't see lowercase env. vars */
  703. var = getenv("windir");
  704. if ((NULL != var) && (0 != var[0]))
  705. {
  706. return 1;
  707. }
  708. return 0;
  709. }
  710. void check_for_windows(char *app)
  711. {
  712. /* Print a warning for users running under Windows */
  713. /* to reduce bug reports due to running DOS version */
  714. /* under Windows, when Windows version usually works correctly */
  715. /* This is only called from the DOS version */
  716. if (is_running_on_windows())
  717. {
  718. printf("\nzip warning: You are running MSDOS %s on Windows.\n"
  719. "Try the Windows version before reporting any problems.\n",
  720. app);
  721. }
  722. }
  723. #endif /* !UTIL */
  724. #ifndef WINDLL
  725. /******************************/
  726. /* Function version_local() */
  727. /******************************/
  728. static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s.\n\n";
  729. /* At module level to keep Turbo C++ 1.0 happy !! */
  730. void version_local()
  731. {
  732. #if defined(__DJGPP__) || defined(__WATCOMC__) || \
  733. (defined(_MSC_VER) && (_MSC_VER != 800))
  734. char buf[80];
  735. #endif
  736. /* Define the compiler name and version strings */
  737. #if defined(__GNUC__)
  738. # if defined(__DJGPP__)
  739. sprintf(buf, "djgpp v%d.%02d / gcc ", __DJGPP__, __DJGPP_MINOR__);
  740. # define COMPILER_NAME1 buf
  741. # elif defined(__GO32__) /* __GO32__ is defined as "1" only (sigh) */
  742. # define COMPILER_NAME1 "djgpp v1.x / gcc "
  743. # elif defined(__EMX__) /* ...so is __EMX__ (double sigh) */
  744. # define COMPILER_NAME1 "emx+gcc "
  745. # else
  746. # define COMPILER_NAME1 "gcc "
  747. # endif
  748. # define COMPILER_NAME2 __VERSION__
  749. #elif defined(__WATCOMC__)
  750. # if (__WATCOMC__ % 10 > 0)
  751. /* We do this silly test because __WATCOMC__ gives two digits for the */
  752. /* minor version, but Watcom packaging prefers to show only one digit. */
  753. sprintf(buf, "Watcom C/C++ %d.%02d", __WATCOMC__ / 100,
  754. __WATCOMC__ % 100);
  755. # else
  756. sprintf(buf, "Watcom C/C++ %d.%d", __WATCOMC__ / 100,
  757. (__WATCOMC__ % 100) / 10);
  758. # endif
  759. # define COMPILER_NAME1 buf
  760. # define COMPILER_NAME2 ""
  761. #elif defined(__TURBOC__)
  762. # ifdef __BORLANDC__
  763. # define COMPILER_NAME1 "Borland C++"
  764. # if (__BORLANDC__ < 0x0200)
  765. # define COMPILER_NAME2 " 1.0"
  766. # elif (__BORLANDC__ == 0x0200) /* James: __TURBOC__ = 0x0297 */
  767. # define COMPILER_NAME2 " 2.0"
  768. # elif (__BORLANDC__ == 0x0400)
  769. # define COMPILER_NAME2 " 3.0"
  770. # elif (__BORLANDC__ == 0x0410) /* __BCPLUSPLUS__ = 0x0310 */
  771. # define COMPILER_NAME2 " 3.1"
  772. # elif (__BORLANDC__ == 0x0452) /* __BCPLUSPLUS__ = 0x0320 */
  773. # define COMPILER_NAME2 " 4.0 or 4.02"
  774. # elif (__BORLANDC__ == 0x0460) /* __BCPLUSPLUS__ = 0x0340 */
  775. # define COMPILER_NAME2 " 4.5"
  776. # elif (__BORLANDC__ == 0x0500) /* __TURBOC__ = 0x0500 */
  777. # define COMPILER_NAME2 " 5.0"
  778. # else
  779. # define COMPILER_NAME2 " later than 5.0"
  780. # endif
  781. # else
  782. # define COMPILER_NAME1 "Turbo C"
  783. # if (__TURBOC__ > 0x0401)
  784. # define COMPILER_NAME2 "++ later than 3.0"
  785. # elif (__TURBOC__ == 0x0401) /* Kevin: 3.0 -> 0x0401 */
  786. # define COMPILER_NAME2 "++ 3.0"
  787. # elif (__TURBOC__ == 0x0296) /* [662] checked by SPC */
  788. # define COMPILER_NAME2 "++ 1.01"
  789. # elif (__TURBOC__ == 0x0295) /* [661] vfy'd by Kevin */
  790. # define COMPILER_NAME2 "++ 1.0"
  791. # elif (__TURBOC__ == 0x0201) /* Brian: 2.01 -> 0x0201 */
  792. # define COMPILER_NAME2 " 2.01"
  793. # elif ((__TURBOC__ >= 0x018d) && (__TURBOC__ <= 0x0200)) /* James: 0x0200 */
  794. # define COMPILER_NAME2 " 2.0"
  795. # elif (__TURBOC__ > 0x0100)
  796. # define COMPILER_NAME2 " 1.5" /* James: 0x0105? */
  797. # else
  798. # define COMPILER_NAME2 " 1.0" /* James: 0x0100 */
  799. # endif
  800. # endif
  801. #elif defined(MSC)
  802. # if defined(_QC) && !defined(_MSC_VER)
  803. # define COMPILER_NAME1 "Microsoft Quick C"
  804. # define COMPILER_NAME2 "" /* _QC is defined as 1 */
  805. # else
  806. # define COMPILER_NAME1 "Microsoft C "
  807. # ifdef _MSC_VER
  808. # if (_MSC_VER == 800)
  809. # define COMPILER_NAME2 "8.0/8.0c (Visual C++ 1.0/1.5)"
  810. # else
  811. # define COMPILER_NAME2 \
  812. (sprintf(buf, "%d.%02d", _MSC_VER/100, _MSC_VER%100), buf)
  813. # endif
  814. # else
  815. # define COMPILER_NAME2 "5.1 or earlier"
  816. # endif
  817. # endif
  818. #else
  819. # define COMPILER_NAME1 "unknown compiler"
  820. # define COMPILER_NAME2 ""
  821. #endif
  822. /* Define the OS name and memory environment strings */
  823. #if defined(__WATCOMC__) || defined(__TURBOC__) || defined(MSC) || \
  824. defined(__GNUC__)
  825. # define OS_NAME1 "\nMS-DOS"
  826. #else
  827. # define OS_NAME1 "MS-DOS"
  828. #endif
  829. #if (defined(__GNUC__) || (defined(__WATCOMC__) && defined(__386__)))
  830. # define OS_NAME2 " (32-bit)"
  831. #elif defined(M_I86HM) || defined(__HUGE__)
  832. # define OS_NAME2 " (16-bit, huge)"
  833. #elif defined(M_I86LM) || defined(__LARGE__)
  834. # define OS_NAME2 " (16-bit, large)"
  835. #elif defined(M_I86MM) || defined(__MEDIUM__)
  836. # define OS_NAME2 " (16-bit, medium)"
  837. #elif defined(M_I86CM) || defined(__COMPACT__)
  838. # define OS_NAME2 " (16-bit, compact)"
  839. #elif defined(M_I86SM) || defined(__SMALL__)
  840. # define OS_NAME2 " (16-bit, small)"
  841. #elif defined(M_I86TM) || defined(__TINY__)
  842. # define OS_NAME2 " (16-bit, tiny)"
  843. #else
  844. # define OS_NAME2 " (16-bit)"
  845. #endif
  846. /* Define the compile date string */
  847. #ifdef __DATE__
  848. # define COMPILE_DATE " on " __DATE__
  849. #else
  850. # define COMPILE_DATE ""
  851. #endif
  852. printf(CompiledWith, COMPILER_NAME1, COMPILER_NAME2,
  853. OS_NAME1, OS_NAME2, COMPILE_DATE);
  854. } /* end function version_local() */
  855. #endif /* !WINDLL */
  856. #if 0 /* inserted here for future use (clearing of archive bits) */
  857. #if (defined(__GO32__) && (!defined(__DJGPP__) || (__DJGPP__ < 2)))
  858. #include <errno.h>
  859. int volatile _doserrno;
  860. unsigned _dos_setfileattr(char *name, unsigned attr)
  861. {
  862. #if 0 /* stripping of trailing '/' is not needed for zip-internal use */
  863. unsigned namlen = strlen(name);
  864. char *i_name = alloca(namlen + 1);
  865. strcpy(i_name, name);
  866. if (namlen > 1 && i_name[namlen-1] == '/' && i_name[namlen-2] != ':')
  867. i_name[namlen-1] = '\0';
  868. asm("movl %0, %%edx": : "g" (i_name));
  869. #else
  870. asm("movl %0, %%edx": : "g" (name));
  871. #endif
  872. asm("movl %0, %%ecx": : "g" (attr));
  873. asm("movl $0x4301, %eax");
  874. asm("int $0x21": : : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
  875. _doserrno = 0;
  876. asm("jnc 1f");
  877. asm("movl %%eax, %0": "=m" (_doserrno));
  878. switch (_doserrno) {
  879. case 2:
  880. case 3:
  881. errno = ENOENT;
  882. break;
  883. case 5:
  884. errno = EACCES;
  885. break;
  886. }
  887. asm("1:");
  888. return (unsigned)_doserrno;
  889. }
  890. #endif /* DJGPP v1.x */
  891. #endif /* never (not yet used) */
  892. #if (defined(__DJGPP__) && (__DJGPP__ >= 2))
  893. /* Disable determination of "x" bit in st_mode field for [f]stat() calls. */
  894. int _is_executable (const char *path, int fhandle, const char *ext)
  895. {
  896. return 0;
  897. }
  898. /* Prevent globbing of filenames. This gives the same functionality as
  899. * "stubedit <program> globbing=no" did with DJGPP v1.
  900. */
  901. #ifndef USE_DJGPP_GLOB
  902. char **__crt0_glob_function(char *_arg)
  903. {
  904. return NULL;
  905. }
  906. #endif
  907. /* Reduce the size of the executable and remove the functionality to read
  908. * the program's environment from whatever $DJGPP points to.
  909. */
  910. #if !defined(USE_DJGPP_ENV) || defined(UTIL)
  911. void __crt0_load_environment_file(char *_app_name)
  912. {
  913. }
  914. #endif
  915. #endif /* __DJGPP__ >= 2 */
  916. #if defined(_MSC_VER) && _MSC_VER == 700
  917. /*
  918. * ARGH. MSC 7.0 libraries think times are based on 1899 Dec 31 00:00, not
  919. * 1970 Jan 1 00:00. So we have to diddle time_t's appropriately: add
  920. * 70 years' worth of seconds for localtime() wrapper function;
  921. * (70*365 regular days + 17 leap days + 1 1899 day) * 86400 ==
  922. * (25550 + 17 + 1) * 86400 == 2209075200 seconds.
  923. * Let time() and stat() return seconds since 1970 by using our own
  924. * _dtoxtime() which is the routine that is called by these two functions.
  925. */
  926. #ifdef UTIL
  927. # include <time.h>
  928. #endif
  929. #ifndef UTIL
  930. #undef localtime
  931. struct tm *localtime(const time_t *);
  932. struct tm *msc7_localtime(const time_t *clock)
  933. {
  934. time_t t = *clock;
  935. t += 2209075200L;
  936. return localtime(&t);
  937. }
  938. #endif /* !UTIL */
  939. void __tzset(void);
  940. int _isindst(struct tm *);
  941. extern int _days[];
  942. /* Nonzero if `y' is a leap year, else zero. */
  943. #define leap(y) (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
  944. /* Number of leap years from 1970 to `y' (not including `y' itself). */
  945. #define nleap(y) (((y) - 1969) / 4 - ((y) - 1901) / 100 + ((y) - 1601) / 400)
  946. time_t _dtoxtime(year, month, mday, hour, min, sec)
  947. int year, month, mday, year, hour, min, sec;
  948. {
  949. struct tm tm;
  950. time_t t;
  951. int days;
  952. days = _days[month - 1] + mday;
  953. year += 1980;
  954. if (leap(year) && month > 2)
  955. ++days;
  956. tm.tm_yday = days;
  957. tm.tm_mon = month - 1;
  958. tm.tm_year = year - 1900;
  959. tm.tm_hour = hour;
  960. __tzset();
  961. days += 365 * (year - 1970) + nleap (year);
  962. t = 86400L * days + 3600L * hour + 60 * min + sec + _timezone;
  963. if (_daylight && _isindst(&tm))
  964. t -= 3600;
  965. return t;
  966. }
  967. #endif /* _MSC_VER && _MSC_VER == 700 */
  968. #ifdef __WATCOMC__
  969. /* This papers over a bug in Watcom 10.6's standard library... sigh */
  970. /* Apparently it applies to both the DOS and Win32 stat()s. */
  971. int stat_bandaid(const char *path, struct stat *buf)
  972. {
  973. char newname[4];
  974. if (!stat(path, buf))
  975. return 0;
  976. else if (!strcmp(path, ".") || (path[0] && !strcmp(path + 1, ":."))) {
  977. strcpy(newname, path);
  978. newname[strlen(path) - 1] = '\\'; /* stat(".") fails for root! */
  979. return stat(newname, buf);
  980. } else
  981. return -1;
  982. }
  983. #endif