acornzip.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2000-Apr-09 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 <stdlib.h>
  9. #include <string.h>
  10. #include "zip.h"
  11. #ifndef UTIL
  12. #define PAD 0
  13. #define PATH_END '/'
  14. local int wild_recurse(char *whole, char *wildtail);
  15. local int uxtime2acornftime(unsigned *pexadr, unsigned *pldadr, time_t ut);
  16. extern char *label;
  17. local ulg label_time = 0;
  18. local ulg label_mode = 0;
  19. local time_t label_utim = 0;
  20. char *readd(DIR *d)
  21. /* Return a pointer to the next name in the directory stream d, or NULL if
  22. no more entries or an error occurs. */
  23. {
  24. struct dirent *e;
  25. e = readdir(d);
  26. return (e == NULL ? (char *) NULL : e->d_name);
  27. }
  28. /* What we have here is a mostly-generic routine using opend()/readd() and */
  29. /* isshexp()/MATCH() to find all the files matching a multi-part filespec */
  30. /* using the portable pattern syntax. It shouldn't take too much fiddling */
  31. /* to make it usable for any other platform that has directory hierarchies */
  32. /* but no shell-level pattern matching. It works for patterns throughout */
  33. /* the pathname, such as "foo:*.?/source/x*.[ch]". */
  34. /* whole is a pathname with wildcards, wildtail points somewhere in the */
  35. /* middle of it. All wildcards to be expanded must come AFTER wildtail. */
  36. local int wild_recurse(whole, wildtail) char *whole; char *wildtail;
  37. {
  38. DIR *dir;
  39. char *subwild, *name, *newwhole = NULL, *glue = NULL, plug = 0, plug2;
  40. ush newlen, amatch = 0;
  41. struct stat statb;
  42. int disk_not_mounted=0;
  43. int e = ZE_MISS;
  44. if (!isshexp(wildtail)) {
  45. if (stat(whole,&statb)==0 && (statb.st_mode & S_IREAD)!=0) {
  46. return procname(whole, 0);
  47. } else
  48. return ZE_MISS; /* woops, no wildcards! */
  49. }
  50. /* back up thru path components till existing dir found */
  51. do {
  52. name = wildtail + strlen(wildtail) - 1;
  53. for (;;)
  54. if (name-- <= wildtail || *name == '.') {
  55. subwild = name + 1;
  56. plug2 = *subwild;
  57. *subwild = 0;
  58. break;
  59. }
  60. if (glue)
  61. *glue = plug;
  62. glue = subwild;
  63. plug = plug2;
  64. dir = opendir(whole);
  65. } while (!dir && !disk_not_mounted && subwild > wildtail);
  66. wildtail = subwild; /* skip past non-wild components */
  67. if ((subwild = strchr(wildtail + 1, '.')) != NULL) {
  68. /* this "+ 1" dodges the ^^^ hole left by *glue == 0 */
  69. *(subwild++) = 0; /* wildtail = one component pattern */
  70. newlen = strlen(whole) + strlen(subwild) + 32;
  71. } else
  72. newlen = strlen(whole) + 31;
  73. if (!dir || !(newwhole = malloc(newlen))) {
  74. if (glue)
  75. *glue = plug;
  76. e = dir ? ZE_MEM : ZE_MISS;
  77. goto ohforgetit;
  78. }
  79. strcpy(newwhole, whole);
  80. newlen = strlen(newwhole);
  81. if (glue)
  82. *glue = plug; /* repair damage to whole */
  83. if (!isshexp(wildtail)) {
  84. e = ZE_MISS; /* non-wild name not found */
  85. goto ohforgetit;
  86. }
  87. while (name = readd(dir)) {
  88. if (MATCH(wildtail, name, 0)) {
  89. strcpy(newwhole + newlen, name);
  90. if (subwild) {
  91. name = newwhole + strlen(newwhole);
  92. *(name++) = '.';
  93. strcpy(name, subwild);
  94. e = wild_recurse(newwhole, name);
  95. } else
  96. e = procname(newwhole, 0);
  97. newwhole[newlen] = 0;
  98. if (e == ZE_OK)
  99. amatch = 1;
  100. else if (e != ZE_MISS)
  101. break;
  102. }
  103. }
  104. ohforgetit:
  105. if (dir) closedir(dir);
  106. if (subwild) *--subwild = '.';
  107. if (newwhole) free(newwhole);
  108. if (e == ZE_MISS && amatch)
  109. e = ZE_OK;
  110. return e;
  111. }
  112. int wild(p)
  113. char *p;
  114. {
  115. char *path;
  116. int toret;
  117. /* special handling of stdin request */
  118. if (strcmp(p, "-") == 0) /* if compressing stdin */
  119. return newname(p, 0, 0);
  120. path=p;
  121. if (strchr(p, ':')==NULL && *p!='@') {
  122. if (!(path=malloc(strlen(p)+3))) {
  123. return ZE_MEM;
  124. }
  125. strcpy(path,"@.");
  126. strcat(path,p);
  127. }
  128. toret=wild_recurse(path, path);
  129. if (path!=p) {
  130. free(path);
  131. }
  132. return toret;
  133. }
  134. int procname(n, caseflag)
  135. char *n; /* name to process */
  136. int caseflag; /* true to force case-sensitive match */
  137. /* Process a name or sh expression to operate on (or exclude). Return
  138. an error code in the ZE_ class. */
  139. {
  140. char *a; /* path and name for recursion */
  141. DIR *d; /* directory stream from opendir() */
  142. char *e; /* pointer to name from readd() */
  143. int m; /* matched flag */
  144. char *p; /* path for recursion */
  145. struct stat s; /* result of stat() */
  146. struct zlist far *z; /* steps through zfiles list */
  147. if (strcmp(n, "-") == 0) /* if compressing stdin */
  148. return newname(n, 0, caseflag);
  149. else if (LSSTAT(n, &s))
  150. {
  151. /* Not a file or directory--search for shell expression in zip file */
  152. p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
  153. m = 1;
  154. for (z = zfiles; z != NULL; z = z->nxt) {
  155. if (MATCH(p, z->iname, caseflag))
  156. {
  157. z->mark = pcount ? filter(z->zname, caseflag) : 1;
  158. if (verbose)
  159. fprintf(mesg, "zip diagnostic: %scluding %s\n",
  160. z->mark ? "in" : "ex", z->name);
  161. m = 0;
  162. }
  163. }
  164. free((zvoid *)p);
  165. return m ? ZE_MISS : ZE_OK;
  166. }
  167. /* Live name--use if file, recurse if directory */
  168. if ((s.st_mode & S_IFDIR) == 0)
  169. {
  170. /* add or remove name of file */
  171. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  172. return m;
  173. } else {
  174. /* Add trailing / to the directory name */
  175. if ((p = malloc(strlen(n)+2)) == NULL)
  176. return ZE_MEM;
  177. if (strcmp(n, ".") == 0) {
  178. *p = '\0'; /* avoid "./" prefix and do not create zip entry */
  179. } else {
  180. strcpy(p, n);
  181. a = p + strlen(p);
  182. if (a[-1] != '.')
  183. strcpy(a, ".");
  184. if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
  185. free((zvoid *)p);
  186. return m;
  187. }
  188. }
  189. /* recurse into directory */
  190. if (recurse && (d = opendir(n)) != NULL)
  191. {
  192. while ((e = readd(d)) != NULL) {
  193. if (strcmp(e, ".") && strcmp(e, ".."))
  194. {
  195. if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  196. {
  197. closedir(d);
  198. free((zvoid *)p);
  199. return ZE_MEM;
  200. }
  201. strcat(strcpy(a, p), e);
  202. if ((m = procname(a, caseflag)) != ZE_OK) /* recurse on name */
  203. {
  204. if (m == ZE_MISS)
  205. zipwarn("name not matched: ", a);
  206. else
  207. ziperr(m, a);
  208. }
  209. free((zvoid *)a);
  210. }
  211. }
  212. closedir(d);
  213. }
  214. free((zvoid *)p);
  215. } /* (s.st_mode & S_IFDIR) == 0) */
  216. return ZE_OK;
  217. }
  218. char *ex2in(x, isdir, pdosflag)
  219. char *x; /* external file name */
  220. int isdir; /* input: x is a directory */
  221. int *pdosflag; /* output: force MSDOS file attributes? */
  222. /* Convert the external file name to a zip file name, returning the malloc'ed
  223. string or NULL if not enough memory. */
  224. {
  225. char *n; /* internal file name (malloc'ed) */
  226. char *t; /* shortened name */
  227. char *tmp;
  228. int dosflag;
  229. char *lastlastdir=NULL; /* pointer to 2 dirs before... */
  230. char *lastdir=NULL; /* pointer to last dir... */
  231. /* Malloc space for internal name and copy it */
  232. if ((tmp = malloc(strlen(x) + 1)) == NULL)
  233. return NULL;
  234. strcpy(tmp, x);
  235. dosflag = dosify; /* default for non-DOS and non-OS/2 */
  236. /* Find starting point in name before doing malloc */
  237. for(t=tmp;*t;t++) {
  238. if (*t=='/') {
  239. *t='.';
  240. }
  241. else if (*t=='.') {
  242. *t='/';
  243. lastlastdir=lastdir;
  244. lastdir=t+1;
  245. }
  246. }
  247. t=strchr(tmp,'$'); /* skip FS name */
  248. if (t!=NULL)
  249. t+=2; /* skip '.' after '$' */
  250. else
  251. t=tmp;
  252. if (*t=='@') /* skip @. at the beginning of filenames */
  253. t+=2;
  254. /* Make changes, if any, to the copied name (leave original intact) */
  255. /* return a pointer to '\0' if the file is a directory with the same
  256. same name as an extension to swap (eg. 'c', 'h', etc.) */
  257. if (isdir && exts2swap!=NULL) {
  258. if (lastlastdir==NULL)
  259. lastlastdir=t;
  260. if (checkext(lastlastdir)) {
  261. free((void *)tmp);
  262. n=malloc(1);
  263. if (n!=NULL)
  264. *n='\0';
  265. return n;
  266. }
  267. }
  268. if (exts2swap!=NULL && lastdir!=NULL) {
  269. if (lastlastdir==NULL)
  270. lastlastdir=t;
  271. if (checkext(lastlastdir)) {
  272. if (swapext(lastlastdir,lastdir-1)) {
  273. free((void *)tmp);
  274. return NULL;
  275. }
  276. }
  277. }
  278. if (!pathput)
  279. t = last(t, PATH_END);
  280. /* Malloc space for internal name and copy it */
  281. if ((n = malloc(strlen(t) + 1)) == NULL) {
  282. free((void *)tmp);
  283. return NULL;
  284. }
  285. strcpy(n, t);
  286. free((void *)tmp);
  287. if (dosify)
  288. msname(n);
  289. /* Returned malloc'ed name */
  290. if (pdosflag)
  291. *pdosflag = dosflag;
  292. return n;
  293. }
  294. char *in2ex(n)
  295. char *n; /* internal file name */
  296. /* Convert the zip file name to an external file name, returning the malloc'ed
  297. string or NULL if not enough memory. */
  298. {
  299. char *x; /* external file name */
  300. char *t; /* scans name */
  301. char *lastext=NULL; /* pointer to last extension */
  302. char *lastdir=NULL; /* pointer to last dir */
  303. if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  304. return NULL;
  305. strcpy(x, n);
  306. for(t=x;*t;t++) {
  307. if (*t=='.') {
  308. *t='/';
  309. lastext=t+1;
  310. }
  311. else if (*t=='/') {
  312. *t='.';
  313. lastdir=t+1;
  314. }
  315. }
  316. if (exts2swap!=NULL && (int)lastext>(int)lastdir) {
  317. if (lastdir==NULL)
  318. lastdir=x;
  319. if (checkext(lastext)) {
  320. if (swapext(lastdir,lastext-1)) {
  321. free((void *)x);
  322. return NULL;
  323. }
  324. }
  325. }
  326. return x;
  327. }
  328. local int uxtime2acornftime(unsigned *pexadr, unsigned *pldadr, time_t ut)
  329. {
  330. unsigned timlo; /* 3 lower bytes of acorn file-time plus carry byte */
  331. unsigned timhi; /* 2 high bytes of acorn file-time */
  332. timlo = ((unsigned)ut & 0x00ffffffU) * 100 + 0x00996a00U;
  333. timhi = ((unsigned)ut >> 24);
  334. timhi = timhi * 100 + 0x0000336eU + (timlo >> 24);
  335. if (timhi & 0xffff0000U)
  336. return 1; /* calculation overflow, do not change time */
  337. /* insert the five time bytes into loadaddr and execaddr variables */
  338. *pexadr = (timlo & 0x00ffffffU) | ((timhi & 0x000000ffU) << 24);
  339. *pldadr = (*pldadr & 0xffffff00U) | ((timhi >> 8) & 0x000000ffU);
  340. return 0; /* subject to future extension to signal overflow */
  341. }
  342. void stamp(f, d)
  343. char *f; /* name of file to change */
  344. ulg d; /* dos-style time to change it to */
  345. /* Set last updated and accessed time of file f to the DOS time d. */
  346. {
  347. time_t m_time;
  348. unsigned int loadaddr, execaddr;
  349. int attr;
  350. /* Convert DOS time to time_t format in m_time */
  351. m_time = dos2unixtime(d);
  352. /* set the file's modification time */
  353. SWI_OS_File_5(f,NULL,&loadaddr,NULL,NULL,&attr);
  354. if (uxtime2acornftime(&execaddr, &loadaddr, m_time) != 0)
  355. return;
  356. SWI_OS_File_1(f,loadaddr,execaddr,attr);
  357. }
  358. ulg filetime(f, a, n, t)
  359. char *f; /* name of file to get info on */
  360. ulg *a; /* return value: file attributes */
  361. long *n; /* return value: file size */
  362. iztimes *t; /* return value: access, modific. and creation times */
  363. /* If file *f does not exist, return 0. Else, return the file's last
  364. modified date and time as an MSDOS date and time. The date and
  365. time is returned in a long with the date most significant to allow
  366. unsigned integer comparison of absolute times. Also, if a is not
  367. a NULL pointer, store the file attributes there, with the high two
  368. bytes being the Unix attributes, and the low byte being a mapping
  369. of that to DOS attributes. If n is not NULL, store the file size
  370. there. If t is not NULL, the file's access, modification and creation
  371. times are stored there as UNIX time_t values.
  372. If f is "-", use standard input as the file. If f is a device, return
  373. a file size of -1 */
  374. {
  375. struct stat s; /* results of stat() */
  376. /* convert FNMAX to malloc - 11/8/04 EG */
  377. char *name;
  378. unsigned int len = strlen(f);
  379. if (f == label) {
  380. if (a != NULL)
  381. *a = label_mode;
  382. if (n != NULL)
  383. *n = -2L; /* convention for a label name */
  384. if (t != NULL)
  385. t->atime = t->mtime = t->ctime = label_utim;
  386. return label_time;
  387. }
  388. if ((name = malloc(len + 1)) == NULL) {
  389. ZIPERR(ZE_MEM, "filetime");
  390. }
  391. strcpy(name, f);
  392. if (name[len - 1] == '.')
  393. name[len - 1] = '\0';
  394. /* not all systems allow stat'ing a file with / appended */
  395. if (strcmp(f, "-") == 0) {
  396. /* forge stat values for stdin since Amiga and RISCOS have no fstat() */
  397. s.st_mode = (S_IREAD|S_IWRITE|S_IFREG);
  398. s.st_size = -1;
  399. s.st_mtime = time(&s.st_mtime);
  400. } else if (LSSTAT(name, &s) != 0) {
  401. /* Accept about any file kind including directories
  402. * (stored with trailing / with -r option)
  403. */
  404. free(name);
  405. return 0;
  406. }
  407. free(name);
  408. if (a != NULL) {
  409. *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  410. if ((s.st_mode & S_IFDIR) != 0) {
  411. *a |= MSDOS_DIR_ATTR;
  412. }
  413. }
  414. if (n != NULL)
  415. *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  416. if (t != NULL) {
  417. t->atime = s.st_atime;
  418. t->mtime = s.st_mtime;
  419. t->ctime = s.st_ctime;
  420. }
  421. return unix2dostime((time_t *) &s.st_mtime);
  422. }
  423. int set_extra_field(z, z_utim)
  424. struct zlist far *z;
  425. iztimes *z_utim;
  426. {
  427. #ifdef USE_EF_UT_TIME
  428. char *eb_ptr;
  429. #endif /* USE_EF_UT_TIME */
  430. char *name;
  431. extra_block *block;
  432. #define EB_SPARK_LEN 20
  433. #define EB_SPARK_SIZE (EB_HEADSIZE+EB_SPARK_LEN)
  434. #ifdef USE_EF_UT_TIME
  435. # ifdef IZ_CHECK_TZ
  436. # define EB_UTTIME_SIZE (zp_tz_is_valid ? EB_HEADSIZE+EB_UT_LEN(1) : 0)
  437. # else
  438. # define EB_UTTIME_SIZE (EB_HEADSIZE+EB_UT_LEN(1))
  439. # endif
  440. #else
  441. # define EB_UTTIME_SIZE 0
  442. #endif
  443. #define EF_SPARK_TOTALSIZE (EB_SPARK_SIZE + EB_UTTIME_SIZE)
  444. if ((name=(char *)malloc(strlen(z->name)+1))==NULL) {
  445. fprintf(stderr," set_extra_field: not enough memory for directory name\n");
  446. return ZE_MEM;
  447. }
  448. strcpy(name,z->name);
  449. if (name[strlen(name)-1]=='.') { /* remove the last '.' in directory names */
  450. name[strlen(name)-1]=0;
  451. }
  452. z->extra=(char *)malloc(EF_SPARK_TOTALSIZE);
  453. if (z->extra==NULL) {
  454. fprintf(stderr," set_extra_field: not enough memory\n");
  455. free(name);
  456. return ZE_MEM;
  457. }
  458. z->cextra = z->extra;
  459. z->cext = z->ext = EF_SPARK_TOTALSIZE;
  460. block=(extra_block *)z->extra;
  461. block->ID=SPARKID;
  462. block->size=EB_SPARK_LEN;
  463. block->ID_2=SPARKID_2;
  464. block->zero=0;
  465. if (SWI_OS_File_5(name,NULL,&block->loadaddr,&block->execaddr,
  466. NULL,&block->attr) != NULL) {
  467. fprintf(stderr," OS error while set_extra_field of %s\n",name);
  468. }
  469. free(name);
  470. #ifdef USE_EF_UT_TIME
  471. # ifdef IZ_CHECK_TZ
  472. if (zp_tz_is_valid) {
  473. # endif
  474. eb_ptr = z->extra + EB_SPARK_SIZE;
  475. eb_ptr[0] = 'U';
  476. eb_ptr[1] = 'T';
  477. eb_ptr[2] = EB_UT_LEN(1); /* length of data part of e.f. */
  478. eb_ptr[3] = 0;
  479. eb_ptr[4] = EB_UT_FL_MTIME;
  480. eb_ptr[5] = (char)(z_utim->mtime);
  481. eb_ptr[6] = (char)(z_utim->mtime >> 8);
  482. eb_ptr[7] = (char)(z_utim->mtime >> 16);
  483. eb_ptr[8] = (char)(z_utim->mtime >> 24);
  484. # ifdef IZ_CHECK_TZ
  485. }
  486. # endif
  487. #endif /* USE_EF_UT_TIME */
  488. return ZE_OK;
  489. }
  490. #endif /* !UTIL */
  491. /******************************/
  492. /* Function version_local() */
  493. /******************************/
  494. void version_local()
  495. {
  496. static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  497. printf(CompiledWith,
  498. #ifdef __GNUC__
  499. "gcc ", __VERSION__,
  500. #else
  501. # ifdef __CC_NORCROFT
  502. "Norcroft ", "cc",
  503. # else
  504. "cc", "",
  505. # endif
  506. #endif
  507. "RISC OS",
  508. " (Acorn Computers Ltd)",
  509. #ifdef __DATE__
  510. " on ", __DATE__
  511. #else
  512. "", ""
  513. #endif
  514. );
  515. } /* end function version_local() */