amigazip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 "zip.h"
  9. #include "amiga/amiga.h"
  10. #ifndef UTIL /* the companion #endif is a bit of ways down ... */
  11. #define utime FileDate
  12. #define PAD 0
  13. #define PATH_END '/'
  14. /* Local globals (kinda like "military intelligence" or "broadcast quality") */
  15. extern char *label; /* still declared in fileio.c */
  16. local ulg label_time = 0;
  17. local ulg label_mode = 0;
  18. local time_t label_utim = 0;
  19. /* Local functions */
  20. local char *readd OF((DIR *));
  21. local int wild_recurse OF((char *, char *));
  22. local char *readd(d)
  23. DIR *d; /* directory stream to read from */
  24. /* Return a pointer to the next name in the directory stream d, or NULL if
  25. no more entries or an error occurs. */
  26. {
  27. struct dirent *e = readdir(d);
  28. return e == NULL ? (char *) NULL : e->d_name;
  29. }
  30. /* What we have here is a mostly-generic routine using opendir()/readd() and */
  31. /* isshexp()/MATCH() to find all the files matching a multi-part filespec */
  32. /* using the portable pattern syntax. It shouldn't take too much fiddling */
  33. /* to make it usable for any other platform that has directory hierarchies */
  34. /* but no shell-level pattern matching. It works for patterns throughout */
  35. /* the pathname, such as "foo:*.?/source/x*.[ch]". */
  36. #define ONENAMELEN 30
  37. /* the length of one filename component on the Amiga */
  38. /* whole is a pathname with wildcards, wildtail points somewhere in the */
  39. /* middle of it. All wildcards to be expanded must come AFTER wildtail. */
  40. local int wild_recurse(whole, wildtail) char *whole; char *wildtail;
  41. {
  42. DIR *dir;
  43. char *subwild, *name, *newwhole = NULL, *glue = NULL, plug = 0, plug2;
  44. ush newlen, amatch = 0;
  45. BPTR lok;
  46. int e = ZE_MISS;
  47. if (!isshexp(wildtail))
  48. if (lok = Lock(whole, ACCESS_READ)) { /* p exists? */
  49. UnLock(lok);
  50. return procname(whole, 0);
  51. } else
  52. return ZE_MISS; /* woops, no wildcards! */
  53. /* back up thru path components till existing dir found */
  54. do {
  55. name = wildtail + strlen(wildtail) - 1;
  56. for (;;)
  57. if (name-- <= wildtail || *name == PATH_END) {
  58. subwild = name + 1;
  59. plug2 = *subwild;
  60. *subwild = 0;
  61. break;
  62. }
  63. if (glue)
  64. *glue = plug;
  65. glue = subwild;
  66. plug = plug2;
  67. dir = opendir(whole);
  68. } while (!dir && !disk_not_mounted && subwild > wildtail);
  69. wildtail = subwild; /* skip past non-wild components */
  70. if ((subwild = strchr(wildtail + 1, PATH_END)) != NULL) {
  71. /* this "+ 1" dodges the ^^^ hole left by *glue == 0 */
  72. *(subwild++) = 0; /* wildtail = one component pattern */
  73. newlen = strlen(whole) + strlen(subwild) + (ONENAMELEN + 2);
  74. } else
  75. newlen = strlen(whole) + (ONENAMELEN + 1);
  76. if (!dir || !(newwhole = malloc(newlen))) {
  77. if (glue)
  78. *glue = plug;
  79. e = dir ? ZE_MEM : ZE_MISS;
  80. goto ohforgetit;
  81. }
  82. strcpy(newwhole, whole);
  83. newlen = strlen(newwhole);
  84. if (glue)
  85. *glue = plug; /* repair damage to whole */
  86. if (!isshexp(wildtail)) {
  87. e = ZE_MISS; /* non-wild name not found */
  88. goto ohforgetit;
  89. }
  90. while (name = readd(dir)) {
  91. if (MATCH(wildtail, name, 0)) {
  92. strcpy(newwhole + newlen, name);
  93. if (subwild) {
  94. name = newwhole + strlen(newwhole);
  95. *(name++) = PATH_END;
  96. strcpy(name, subwild);
  97. e = wild_recurse(newwhole, name);
  98. } else
  99. e = procname(newwhole, 0);
  100. newwhole[newlen] = 0;
  101. if (e == ZE_OK)
  102. amatch = 1;
  103. else if (e != ZE_MISS)
  104. break;
  105. }
  106. }
  107. ohforgetit:
  108. if (dir) closedir(dir);
  109. if (subwild) *--subwild = PATH_END;
  110. if (newwhole) free(newwhole);
  111. if (e == ZE_MISS && amatch)
  112. e = ZE_OK;
  113. return e;
  114. }
  115. int wild(p) char *p;
  116. {
  117. char *use;
  118. /* special handling of stdin request */
  119. if (strcmp(p, "-") == 0) /* if compressing stdin */
  120. return newname(p, 0, 0);
  121. /* wild_recurse() can't handle colons in wildcard part: */
  122. if (use = strchr(p, ':')) {
  123. if (strchr(++use, ':'))
  124. return ZE_PARMS;
  125. } else
  126. use = p;
  127. return wild_recurse(p, use);
  128. }
  129. int procname(n, caseflag)
  130. char *n; /* name to process */
  131. int caseflag; /* true to force case-sensitive match */
  132. /* Process a name or sh expression to operate on (or exclude). Return
  133. an error code in the ZE_ class. */
  134. {
  135. char *a; /* path and name for recursion */
  136. DIR *d; /* directory stream from opendir() */
  137. char *e; /* pointer to name from readd() */
  138. int m; /* matched flag */
  139. char *p; /* path for recursion */
  140. struct stat s; /* result of stat() */
  141. struct zlist far *z; /* steps through zfiles list */
  142. if (strcmp(n, "-") == 0) /* if compressing stdin */
  143. return newname(n, 0, caseflag);
  144. else if (LSSTAT(n, &s))
  145. {
  146. /* Not a file or directory--search for shell expression in zip file */
  147. p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
  148. m = 1;
  149. for (z = zfiles; z != NULL; z = z->nxt) {
  150. if (MATCH(p, z->iname, caseflag))
  151. {
  152. z->mark = pcount ? filter(z->zname, caseflag) : 1;
  153. if (verbose)
  154. fprintf(mesg, "zip diagnostic: %scluding %s\n",
  155. z->mark ? "in" : "ex", z->name);
  156. m = 0;
  157. }
  158. }
  159. free((zvoid *)p);
  160. return m ? ZE_MISS : ZE_OK;
  161. }
  162. /* Live name--use if file, recurse if directory */
  163. if ((s.st_mode & S_IFDIR) == 0)
  164. {
  165. /* add or remove name of file */
  166. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  167. return m;
  168. } else {
  169. /* Add trailing / to the directory name */
  170. if ((p = malloc(strlen(n)+2)) == NULL)
  171. return ZE_MEM;
  172. strcpy(p, n);
  173. a = p + strlen(p);
  174. if (*p && a[-1] != '/' && a[-1] != ':')
  175. strcpy(a, "/");
  176. if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
  177. free((zvoid *)p);
  178. return m;
  179. }
  180. /* recurse into directory */
  181. if (recurse && (d = opendir(n)) != NULL)
  182. {
  183. while ((e = readd(d)) != NULL) {
  184. if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  185. {
  186. closedir(d);
  187. free((zvoid *)p);
  188. return ZE_MEM;
  189. }
  190. strcat(strcpy(a, p), e);
  191. if ((m = procname(a, caseflag)) != ZE_OK) /* recurse on name */
  192. {
  193. if (m == ZE_MISS)
  194. zipwarn("name not matched: ", a);
  195. else
  196. ziperr(m, a);
  197. }
  198. free((zvoid *)a);
  199. }
  200. closedir(d);
  201. }
  202. free((zvoid *)p);
  203. } /* (s.st_mode & S_IFDIR) == 0) */
  204. return ZE_OK;
  205. }
  206. char *ex2in(x, isdir, pdosflag)
  207. char *x; /* external file name */
  208. int isdir; /* input: x is a directory */
  209. int *pdosflag; /* output: force MSDOS file attributes? */
  210. /* Convert the external file name to a zip file name, returning the malloc'ed
  211. string or NULL if not enough memory. */
  212. {
  213. char *n; /* internal file name (malloc'ed) */
  214. char *t; /* shortened name */
  215. int dosflag;
  216. dosflag = dosify; /* default for non-DOS and non-OS/2 */
  217. /* Find starting point in name before doing malloc */
  218. if ((t = strrchr(x, ':')) != NULL) /* reject ":" */
  219. t++;
  220. else
  221. t = x;
  222. { /* reject "//" */
  223. char *tt = t;
  224. while (tt = strchr(tt, '/'))
  225. while (*++tt == '/')
  226. t = tt;
  227. }
  228. while (*t == '/') /* reject leading "/" on what's left */
  229. t++;
  230. if (!pathput)
  231. t = last(t, PATH_END);
  232. /* Malloc space for internal name and copy it */
  233. if ((n = malloc(strlen(t) + 1)) == NULL)
  234. return NULL;
  235. strcpy(n, t);
  236. if (dosify)
  237. msname(n);
  238. /* Returned malloc'ed name */
  239. if (pdosflag)
  240. *pdosflag = dosflag;
  241. return n;
  242. }
  243. char *in2ex(n)
  244. char *n; /* internal file name */
  245. /* Convert the zip file name to an external file name, returning the malloc'ed
  246. string or NULL if not enough memory. */
  247. {
  248. char *x; /* external file name */
  249. if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  250. return NULL;
  251. strcpy(x, n);
  252. return x;
  253. }
  254. void stamp(f, d)
  255. char *f; /* name of file to change */
  256. ulg d; /* dos-style time to change it to */
  257. /* Set last updated and accessed time of file f to the DOS time d. */
  258. {
  259. time_t u[2]; /* argument for utime() */
  260. /* Convert DOS time to time_t format in u */
  261. u[0] = u[1] = dos2unixtime(d);
  262. /* Set updated and accessed times of f */
  263. utime(f, u);
  264. }
  265. ulg filetime(f, a, n, t)
  266. char *f; /* name of file to get info on */
  267. ulg *a; /* return value: file attributes */
  268. long *n; /* return value: file size */
  269. iztimes *t; /* return value: access, modific. and creation times */
  270. /* If file *f does not exist, return 0. Else, return the file's last
  271. modified date and time as an MSDOS date and time. The date and
  272. time is returned in a long with the date most significant to allow
  273. unsigned integer comparison of absolute times. Also, if a is not
  274. a NULL pointer, store the file attributes there, with the high two
  275. bytes being the Unix attributes, and the low byte being a mapping
  276. of that to DOS attributes. If n is not NULL, store the file size
  277. there. If t is not NULL, the file's access, modification and creation
  278. times are stored there as UNIX time_t values.
  279. If f is "-", use standard input as the file. If f is a device, return
  280. a file size of -1 */
  281. {
  282. struct stat s; /* results of stat() */
  283. /* convert FNMAX to malloc - 11/8/04 EG */
  284. char *name;
  285. int len = strlen(f);
  286. if (f == label) {
  287. if (a != NULL)
  288. *a = label_mode;
  289. if (n != NULL)
  290. *n = -2L; /* convention for a label name */
  291. if (t != NULL)
  292. t->atime = t->mtime = t->ctime = label_utim;
  293. return label_time;
  294. }
  295. if ((name = malloc(len + 1)) == NULL) {
  296. ZIPERR(ZE_MEM, "filetime");
  297. }
  298. strcpy(name, f);
  299. if (name[len - 1] == '/')
  300. name[len - 1] = '\0';
  301. /* not all systems allow stat'ing a file with / appended */
  302. if (strcmp(f, "-") == 0) {
  303. if (fstat(fileno(stdin), &s) != 0)
  304. error("fstat(stdin)");
  305. } else if (SSTAT(name, &s) != 0) {
  306. /* Accept about any file kind including directories
  307. * (stored with trailing / with -r option)
  308. */
  309. free(name);
  310. return 0;
  311. }
  312. free(name);
  313. if (a != NULL) {
  314. *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  315. if ((s.st_mode & S_IFDIR) != 0) {
  316. *a |= MSDOS_DIR_ATTR;
  317. }
  318. }
  319. if (n != NULL)
  320. *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  321. if (t != NULL) {
  322. t->atime = s.st_atime;
  323. t->mtime = s.st_mtime;
  324. t->ctime = s.st_ctime;
  325. }
  326. return unix2dostime(&s.st_mtime);
  327. }
  328. int set_extra_field(z, z_utim)
  329. struct zlist far *z;
  330. iztimes *z_utim;
  331. /* create extra field and change z->att if desired */
  332. {
  333. #ifdef USE_EF_UT_TIME
  334. #ifdef IZ_CHECK_TZ
  335. if (!zp_tz_is_valid) return ZE_OK; /* skip silently if no valid TZ info */
  336. #endif
  337. if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)
  338. return ZE_MEM;
  339. z->extra[0] = 'U';
  340. z->extra[1] = 'T';
  341. z->extra[2] = EB_UT_LEN(1); /* length of data part of e.f. */
  342. z->extra[3] = 0;
  343. z->extra[4] = EB_UT_FL_MTIME;
  344. z->extra[5] = (char)(z_utim->mtime);
  345. z->extra[6] = (char)(z_utim->mtime >> 8);
  346. z->extra[7] = (char)(z_utim->mtime >> 16);
  347. z->extra[8] = (char)(z_utim->mtime >> 24);
  348. z->cextra = z->extra;
  349. z->cext = z->ext = (EB_HEADSIZE+EB_UT_LEN(1));
  350. return ZE_OK;
  351. #else /* !USE_EF_UT_TIME */
  352. return (int)(z-z);
  353. #endif /* ?USE_EF_UT_TIME */
  354. }
  355. int deletedir(d)
  356. char *d; /* directory to delete */
  357. /* Delete the directory *d if it is empty, do nothing otherwise.
  358. Return the result of rmdir(), delete(), or system().
  359. For VMS, d must be in format [x.y]z.dir;1 (not [x.y.z]).
  360. */
  361. {
  362. return rmdir(d);
  363. }
  364. #endif /* !UTIL */
  365. /******************************/
  366. /* Function version_local() */
  367. /******************************/
  368. /* NOTE: the following include depends upon the environment
  369. * variable $Workbench to be set correctly. (Set by
  370. * default, by Version command in Startup-sequence.)
  371. */
  372. int WBversion = (int)
  373. #include "ENV:Workbench"
  374. ;
  375. void version_local()
  376. {
  377. static ZCONST char CompiledWith[] = "Compiled with %s%s under %s%s%s%s.\n\n";
  378. /* Define buffers. */
  379. char buf1[16]; /* compiler name */
  380. char buf2[16]; /* revstamp */
  381. char buf3[16]; /* OS */
  382. char buf4[16]; /* Date */
  383. /* char buf5[16]; /* Time */
  384. /* format "with" name strings */
  385. #ifdef AMIGA
  386. # ifdef __SASC
  387. strcpy(buf1,"SAS/C ");
  388. # else
  389. # ifdef LATTICE
  390. strcpy(buf1,"Lattice C ");
  391. # else
  392. # ifdef AZTEC_C
  393. strcpy(buf1,"Manx Aztec C ");
  394. # else
  395. strcpy(buf1,"UNKNOWN ");
  396. # endif
  397. # endif
  398. # endif
  399. /* "under" */
  400. sprintf(buf3,"AmigaDOS v%d",WBversion);
  401. #else
  402. strcpy(buf1,"Unknown compiler ");
  403. strcpy(buf3,"Unknown OS");
  404. #endif
  405. /* Define revision, date, and time strings.
  406. * NOTE: Do not calculate run time, be sure to use time compiled.
  407. * Pass these strings via your makefile if undefined.
  408. */
  409. #if defined(__VERSION__) && defined(__REVISION__)
  410. sprintf(buf2,"version %d.%d",__VERSION__,__REVISION__);
  411. #else
  412. # ifdef __VERSION__
  413. sprintf(buf2,"version %d",__VERSION__);
  414. # else
  415. sprintf(buf2,"unknown version");
  416. # endif
  417. #endif
  418. #ifdef __DATE__
  419. sprintf(buf4," on %s",__DATE__);
  420. #else
  421. strcpy(buf4," unknown date");
  422. #endif
  423. /******
  424. #ifdef __TIME__
  425. sprintf(buf5," at %s",__TIME__);
  426. #else
  427. strcpy(buf5," unknown time");
  428. #endif
  429. ******/
  430. /* Print strings using "CompiledWith" mask defined above.
  431. * ("Compiled with %s%s under %s%s%s%s.")
  432. */
  433. printf(CompiledWith,
  434. buf1,
  435. buf2,
  436. buf3,
  437. buf4,
  438. /* buf5, */ "",
  439. "" ); /* buf6 not used */
  440. } /* end function version_local() */