tanunz.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. Copyright (c) 1990-2008 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 unzip.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. /*---------------------------------------------------------------------------
  9. tanunz.c
  10. Tandem/NSK routines for use with Info-ZIP's UnZip 5.3 and later.
  11. Contains: do_wild() <-- generic enough to put in fileio.c?
  12. ef_scan_for_tandem()
  13. open_outfile()
  14. mapattr()
  15. mapname()
  16. checkdir()
  17. mkdir()
  18. close_outfile()
  19. version()
  20. ---------------------------------------------------------------------------*/
  21. #define UNZIP_INTERNAL
  22. #include "unzip.h"
  23. #include <tal.h>
  24. #include "$system.zsysdefs.zsysc" nolist
  25. #include <cextdecs> nolist
  26. #include "tannsk.h"
  27. char *in2ex OF((__GPRO__ char *));
  28. static nsk_file_attrs *ef_scan_for_tandem (
  29. uch *ef_buf,
  30. unsigned ef_len
  31. );
  32. static int created_dir; /* used in mapname(), checkdir() */
  33. static int renamed_fullpath; /* ditto */
  34. /****************************/
  35. /* Strings used in tanunz.c */
  36. /****************************/
  37. static ZCONST char Far CannotDeleteOldFile[] =
  38. "error: cannot delete old %s\n";
  39. static ZCONST char Far CannotCreateFile[] = "error: cannot create %s\n";
  40. #ifndef SFX
  41. /**********************/
  42. /* Function do_wild() */ /* for porting: dir separator; match(ignore_case) */
  43. /**********************/
  44. char *do_wild(__G__ wildspec)
  45. __GDEF
  46. ZCONST char *wildspec; /* only used first time on a given dir */
  47. {
  48. static DIR *wild_dir = (DIR *)NULL;
  49. static ZCONST char *wildname;
  50. static char *dirname, matchname[FILNAMSIZ];
  51. static int notfirstcall=FALSE, have_dirname, dirnamelen;
  52. struct dirent *file;
  53. static char *intname;
  54. int isdir = 0;
  55. int pdosflag = 0;
  56. /* Even when we're just returning wildspec, we *always* do so in
  57. * matchname[]--calling routine is allowed to append four characters
  58. * to the returned string, and wildspec may be a pointer to argv[].
  59. */
  60. if (!notfirstcall) { /* first call: must initialize everything */
  61. notfirstcall = TRUE;
  62. if (!iswild(wildspec)) {
  63. strncpy(matchname, wildspec, FILNAMSIZ);
  64. matchname[FILNAMSIZ-1] = '\0';
  65. have_dirname = FALSE;
  66. wild_dir = NULL;
  67. return matchname;
  68. }
  69. dirnamelen = strlen(wildspec);
  70. if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  71. Info(slide, 0x201, ((char *)slide,
  72. "warning: cannot allocate wildcard buffers\n"));
  73. strncpy(matchname, wildspec, FILNAMSIZ);
  74. matchname[FILNAMSIZ-1] = '\0';
  75. return matchname; /* but maybe filespec was not a wildcard */
  76. }
  77. strcpy(dirname, wildspec);
  78. wildname = wildspec;
  79. have_dirname = FALSE;
  80. if ((wild_dir = opendir(dirname)) != (DIR *)NULL) {
  81. while ((file = readdir(wild_dir)) != (struct dirent *)NULL) {
  82. Trace((stderr, "do_wild: readdir returns %s\n",
  83. FnFilter1(file->d_name)));
  84. if (file->d_name[0] == '.' && wildname[0] != '.')
  85. continue; /* Unix: '*' and '?' do not match leading dot */
  86. if (match(file->d_name, wildname, 0 WISEP) && /* 0=case sens.*/
  87. /* skip "." and ".." directory entries */
  88. strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
  89. Trace((stderr, "do_wild: match() succeeds\n"));
  90. if (have_dirname) {
  91. strcpy(matchname, dirname);
  92. strcpy(matchname+dirnamelen, file->d_name);
  93. } else
  94. strcpy(matchname, file->d_name);
  95. return matchname;
  96. }
  97. }
  98. /* if we get to here directory is exhausted, so close it */
  99. closedir(wild_dir);
  100. wild_dir = (DIR *)NULL;
  101. }
  102. /* return the raw wildspec in case that works (e.g., directory not
  103. * searchable, but filespec was not wild and file is readable) */
  104. strncpy(matchname, wildspec, FILNAMSIZ);
  105. matchname[FILNAMSIZ-1] = '\0';
  106. return matchname;
  107. }
  108. /* last time through, might have failed opendir but returned raw wildspec */
  109. if (wild_dir == (DIR *)NULL) {
  110. notfirstcall = FALSE; /* nothing left to try--reset for new wildspec */
  111. if (have_dirname)
  112. free(dirname);
  113. return (char *)NULL;
  114. }
  115. /* If we've gotten this far, we've read and matched at least one entry
  116. * successfully (in a previous call), so dirname has been copied into
  117. * matchname already.
  118. */
  119. while ((file = readdir(wild_dir)) != (struct dirent *)NULL) {
  120. Trace((stderr, "do_wild: readdir returns %s\n",
  121. FnFilter1(file->d_name)));
  122. if (file->d_name[0] == '.' && wildname[0] != '.')
  123. continue; /* Unix: '*' and '?' do not match leading dot */
  124. if (match(file->d_name, wildname, 0 WISEP)) { /* 0 == case sens. */
  125. if (have_dirname) {
  126. /* strcpy(matchname, dirname); */
  127. strcpy(matchname+dirnamelen, file->d_name);
  128. } else
  129. strcpy(matchname, file->d_name);
  130. return matchname;
  131. }
  132. }
  133. closedir(wild_dir); /* have read at least one entry; nothing left */
  134. wild_dir = (DIR *)NULL;
  135. notfirstcall = FALSE; /* reset for new wildspec */
  136. if (have_dirname)
  137. free(dirname);
  138. return (char *)NULL;
  139. } /* end function do_wild() */
  140. #endif /* !SFX */
  141. /*********************************/
  142. /* Function ef_scan_for_tandem() */
  143. /*********************************/
  144. static nsk_file_attrs *ef_scan_for_tandem(ef_buf, ef_len)
  145. uch *ef_buf; /* buffer containing extra field */
  146. unsigned ef_len; /* total length of extra field */
  147. {
  148. unsigned eb_id;
  149. unsigned eb_len;
  150. /*---------------------------------------------------------------------------
  151. This function scans the extra field for EF_TANDEM
  152. -------------------------------------------------------------------------*/
  153. if (ef_buf == NULL)
  154. return NULL;
  155. while (ef_len >= EB_HEADSIZE) {
  156. eb_id = makeword(EB_ID + ef_buf);
  157. eb_len = makeword(EB_LEN + ef_buf);
  158. if (eb_len > (ef_len - EB_HEADSIZE)) {
  159. /* discovered some extra field inconsistency! */
  160. TTrace((stderr,
  161. "ef_scan_for_tandem: block length %u > rest ef_size %u\n", eb_len,
  162. ef_len - EB_HEADSIZE));
  163. break;
  164. }
  165. switch (eb_id) {
  166. case EF_TANDEM:
  167. return (nsk_file_attrs *)(char *)(ef_buf + EB_HEADSIZE);
  168. break;
  169. default:
  170. break;
  171. }
  172. /* Skip this extra field block */
  173. ef_buf += (eb_len + EB_HEADSIZE);
  174. ef_len -= (eb_len + EB_HEADSIZE);
  175. }
  176. return NULL;
  177. }
  178. /***************************/
  179. /* Function open_outfile() */
  180. /***************************/
  181. int open_outfile(__G) /* return 1 if fail */
  182. __GDEF
  183. {
  184. int fdesc;
  185. short fnum, err, len;
  186. int priext, secext;
  187. short maxext, filecode, blocksize;
  188. #define alist_items 1
  189. #define vlist_bytes 2
  190. short alist[alist_items]={42};
  191. unsigned short vlist[alist_items];
  192. short extra, *err_item=&extra;
  193. nsk_file_attrs *znsk_attr;
  194. ulg eof, pages;
  195. char nsk_work[FILENAME_MAX + 1], *nsk_fname=&nsk_work[0];
  196. #ifdef DLL
  197. if (G.redirect_data)
  198. return (redirect_outfile(__G) == FALSE);
  199. #endif
  200. if (SSTAT(G.filename, &G.statbuf) == 0) {
  201. Trace((stderr, "open_outfile: stat(%s) returns 0: file exists\n",
  202. FnFilter1(G.filename)));
  203. if (unlink(G.filename) != 0) {
  204. Trace((stderr, "open_outfile: existing file %s is read-only\n",
  205. FnFilter1(G.filename)));
  206. chmod(G.filename, S_IRUSR | S_IWUSR);
  207. Trace((stderr, "open_outfile: %s now writable\n",
  208. FnFilter1(G.filename)));
  209. if (unlink(G.filename) != 0) {
  210. Info(slide, 0x401, ((char *)slide,
  211. LoadFarString(CannotDeleteOldFile), FnFilter1(G.filename)));
  212. return 1;
  213. }
  214. }
  215. Trace((stderr, "open_outfile: %s now deleted\n",
  216. FnFilter1(G.filename)));
  217. }
  218. /* Set up Tandem specific file information if present */
  219. znsk_attr = ef_scan_for_tandem(G.extra_field, G.lrec.extra_field_length);
  220. if (znsk_attr != NULL) {
  221. /* Set extent sizes */
  222. priext = znsk_attr->priext;
  223. secext = znsk_attr->secext;
  224. maxext = (int) znsk_attr->maxext;
  225. /* If original file was Enscribe and text then recreate as Edit */
  226. filecode = (znsk_attr->filetype != NSK_UNSTRUCTURED ?
  227. (G.pInfo->textmode ? NSK_EDITFILECODE : NSK_UNSTRUCTURED) :
  228. znsk_attr->filecode);
  229. blocksize = znsk_attr->block;
  230. } else {
  231. /* Try to work out some decent sizes based on how big the file is */
  232. eof = G.lrec.ucsize;
  233. pages = (eof/2048) + 2;
  234. if (pages <= 500) {
  235. priext = pages; /* fits into one extent */
  236. maxext = 16;
  237. } else {
  238. priext = 500; /* Try and fit into 500 page chunks */
  239. maxext = pages/(priext - 50); /* Allow for Enscribe overhead */
  240. if (maxext > 978) {
  241. priext = eof >> 10; /* 512 equal extents */
  242. maxext = 978; /* 2048 * 512 == 2^10 */
  243. }
  244. }
  245. secext = priext;
  246. filecode = (G.pInfo->textmode ? NSK_EDITFILECODE : NSK_UNSTRUCTURED);
  247. blocksize = TANDEM_BLOCKSIZE;
  248. }
  249. if ((fdesc = creat(G.filename,,priext,secext)) != -1){
  250. fnum = fdtogfn ((short)fdesc);
  251. err = (SETMODE (fnum, SET_FILE_BUFFERSIZE, blocksize) != CCE);
  252. err = (SETMODE (fnum, SET_FILE_BUFFERED, 0, 0) != CCE);
  253. err = (SETMODE (fnum, SET_FILE_BUFFERED, 0, 1) != CCE);
  254. err = (SETMODE (fnum, SET_FILE_MAXEXTENTS, maxext) != CCE);
  255. err = close(fdesc);
  256. vlist[0] = filecode;
  257. /* Note that FILE_ALTERLIST_ expects uppercase names */
  258. /* Need to call strlen and upshift */
  259. len = strlen(G.filename);
  260. err = STRING_UPSHIFT_(G.filename,
  261. len,
  262. nsk_fname,
  263. len);
  264. err = FILE_ALTERLIST_(nsk_fname,
  265. len,
  266. alist,
  267. alist_items,
  268. vlist,
  269. vlist_bytes,
  270. ,
  271. err_item);
  272. };
  273. G.outfile = fopen(G.filename, (G.pInfo->textmode ? FOPWT : FOPW));
  274. if (G.outfile == (FILE *)NULL) {
  275. Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  276. FnFilter1(G.filename)));
  277. return 1;
  278. }
  279. #ifdef USE_FWRITE
  280. #ifdef _IOFBF /* make output fully buffered (works just about like write()) */
  281. setvbuf(G.outfile, (char *)slide, _IOFBF, WSIZE);
  282. #else
  283. setbuf(G.outfile, (char *)slide);
  284. #endif
  285. #endif /* USE_FWRITE */
  286. return 0;
  287. } /* end function open_outfile() */
  288. /**********************/
  289. /* Function mapattr() */
  290. /**********************/
  291. int mapattr(__G)
  292. __GDEF
  293. {
  294. ulg tmp = G.crec.external_file_attributes;
  295. switch (G.pInfo->hostnum) {
  296. case AMIGA_:
  297. tmp = (unsigned)(tmp>>17 & 7); /* Amiga RWE bits */
  298. G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  299. break;
  300. case THEOS_:
  301. tmp &= 0xF1FFFFFFL;
  302. if ((tmp & 0xF0000000L) != 0x40000000L)
  303. tmp &= 0x01FFFFFFL; /* not a dir, mask all ftype bits */
  304. else
  305. tmp &= 0x41FFFFFFL; /* leave directory bit as set */
  306. /* fall through! */
  307. case TANDEM_:
  308. case UNIX_:
  309. case VMS_:
  310. case ACORN_:
  311. case ATARI_:
  312. case ATHEOS_:
  313. case BEOS_:
  314. case QDOS_:
  315. G.pInfo->file_attr = (unsigned)(tmp >> 16);
  316. if (G.pInfo->file_attr != 0 || !G.extra_field) {
  317. return 0;
  318. } else {
  319. /* Some (non-Info-ZIP) implementations of Zip for Unix and
  320. VMS (and probably others ??) leave 0 in the upper 16-bit
  321. part of the external_file_attributes field. Instead, they
  322. store file permission attributes in some extra field.
  323. As a work-around, we search for the presence of one of
  324. these extra fields and fall back to the MSDOS compatible
  325. part of external_file_attributes if one of the known
  326. e.f. types has been detected.
  327. Later, we might implement extraction of the permission
  328. bits from the VMS extra field. But for now, the work-around
  329. should be sufficient to provide "readable" extracted files.
  330. (For ASI Unix e.f., an experimental remap of the e.f.
  331. mode value IS already provided!)
  332. */
  333. ush ebID;
  334. unsigned ebLen;
  335. uch *ef = G.extra_field;
  336. unsigned ef_len = G.crec.extra_field_length;
  337. int r = FALSE;
  338. while (!r && ef_len >= EB_HEADSIZE) {
  339. ebID = makeword(ef);
  340. ebLen = (unsigned)makeword(ef+EB_LEN);
  341. if (ebLen > (ef_len - EB_HEADSIZE))
  342. /* discoverd some e.f. inconsistency! */
  343. break;
  344. switch (ebID) {
  345. case EF_ASIUNIX:
  346. if (ebLen >= (EB_ASI_MODE+2)) {
  347. G.pInfo->file_attr =
  348. (unsigned)makeword(ef+(EB_HEADSIZE+EB_ASI_MODE));
  349. /* force stop of loop: */
  350. ef_len = (ebLen + EB_HEADSIZE);
  351. break;
  352. }
  353. /* else: fall through! */
  354. case EF_PKVMS:
  355. /* "found nondecypherable e.f. with perm. attr" */
  356. r = TRUE;
  357. default:
  358. break;
  359. }
  360. ef_len -= (ebLen + EB_HEADSIZE);
  361. ef += (ebLen + EB_HEADSIZE);
  362. }
  363. if (!r)
  364. return 0;
  365. }
  366. /* fall through! */
  367. /* all remaining cases: expand MSDOS read-only bit into write perms */
  368. case FS_FAT_:
  369. /* PKWARE's PKZip for Unix marks entries as FS_FAT_, but stores the
  370. * Unix attributes in the upper 16 bits of the external attributes
  371. * field, just like Info-ZIP's Zip for Unix. We try to use that
  372. * value, after a check for consistency with the MSDOS attribute
  373. * bits (see below).
  374. */
  375. G.pInfo->file_attr = (unsigned)(tmp >> 16);
  376. /* fall through! */
  377. case FS_HPFS_:
  378. case FS_NTFS_:
  379. case MAC_:
  380. case TOPS20_:
  381. default:
  382. /* Ensure that DOS subdir bit is set when the entry's name ends
  383. * in a '/'. Some third-party Zip programs fail to set the subdir
  384. * bit for directory entries.
  385. */
  386. if ((tmp & 0x10) == 0) {
  387. extent fnlen = strlen(G.filename);
  388. if (fnlen > 0 && G.filename[fnlen-1] == '/')
  389. tmp |= 0x10;
  390. }
  391. /* read-only bit --> write perms; subdir bit --> dir exec bit */
  392. tmp = !(tmp & 1) << 1 | (tmp & 0x10) >> 4;
  393. if ((G.pInfo->file_attr & 0700) == (unsigned)(0400 | tmp<<6))
  394. /* keep previous G.pInfo->file_attr setting, when its "owner"
  395. * part appears to be consistent with DOS attribute flags!
  396. */
  397. return 0;
  398. G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  399. break;
  400. } /* end switch (host-OS-created-by) */
  401. /* for originating systems without concept of "group," "other," "system" */
  402. umask( (int)(tmp=umask(0)) ); /* apply mask to expanded r/w(/x) perms */
  403. G.pInfo->file_attr &= ~tmp;
  404. return 0;
  405. } /* end function mapattr() */
  406. /************************/
  407. /* Function mapname() */
  408. /************************/
  409. int mapname(__G__ renamed)
  410. __GDEF
  411. int renamed;
  412. /*
  413. * returns:
  414. * MPN_OK - no problem detected
  415. * MPN_INF_TRUNC - caution (truncated filename)
  416. * MPN_INF_SKIP - info "skip entry" (dir doesn't exist)
  417. * MPN_ERR_SKIP - error -> skip entry
  418. * MPN_ERR_TOOLONG - error -> path is too long
  419. * MPN_NOMEM - error (memory allocation failed) -> skip entry
  420. * [also MPN_VOL_LABEL, MPN_CREATED_DIR]
  421. */
  422. {
  423. char pathcomp[FILNAMSIZ]; /* path-component buffer */
  424. char *pp, *cp; /* character pointers */
  425. char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  426. int error = MPN_OK;
  427. register unsigned workch; /* hold the character being tested */
  428. /*---------------------------------------------------------------------------
  429. Initialize various pointers and counters and stuff.
  430. ---------------------------------------------------------------------------*/
  431. if (G.pInfo->vollabel)
  432. return MPN_VOL_LABEL; /* can't set disk volume labels on Tandem */
  433. /* can create path as long as not just freshening, or if user told us */
  434. G.create_dirs = (!uO.fflag || renamed);
  435. created_dir = FALSE; /* not yet */
  436. /* user gave full pathname: don't prepend rootpath */
  437. renamed_fullpath = (renamed && (*G.filename == '/'));
  438. if (checkdir(__G__ (char *)NULL, INIT) == MPN_NOMEM)
  439. return MPN_NOMEM; /* initialize path buffer, unless no memory */
  440. /* TANDEM - call in2ex */
  441. pp = in2ex(__G__ G.filename);
  442. if (pp == (char *)NULL)
  443. return MPN_NOMEM;
  444. strcpy(G.filename, pp);
  445. free(pp);
  446. *pathcomp = '\0'; /* initialize translation buffer */
  447. pp = pathcomp; /* point to translation buffer */
  448. /* directories have already been junked in in2ex() */
  449. cp = G.filename; /* point to internal zipfile-member pathname */
  450. /*---------------------------------------------------------------------------
  451. Begin main loop through characters in filename.
  452. ---------------------------------------------------------------------------*/
  453. while ((workch = (uch)*cp++) != 0) {
  454. switch (workch) { /* includes space char, let checkdir handle it */
  455. case TANDEM_DELIMITER: /* can assume -j flag not given */
  456. *pp = '\0';
  457. if (((error = checkdir(__G__ pathcomp, APPEND_DIR))
  458. & MPN_MASK) > MPN_INF_TRUNC)
  459. return error;
  460. pp = pathcomp; /* reset conversion buffer for next piece */
  461. lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
  462. break;
  463. case ';': /* VMS version (or DEC-20 attrib?) */
  464. lastsemi = pp;
  465. *pp++ = ';'; /* keep for now; remove VMS ";##" */
  466. break; /* later, if requested */
  467. default:
  468. /* allow European characters in filenames: */
  469. if (isprint(workch) || (128 <= workch && workch <= 254))
  470. *pp++ = (char)workch;
  471. } /* end switch */
  472. } /* end while loop */
  473. /*---------------------------------------------------------------------------
  474. Report if directory was created (and no file to create: filename ended
  475. in '/'), check name to be sure it exists, and combine path and name be-
  476. fore exiting.
  477. ---------------------------------------------------------------------------*/
  478. if (G.filename[strlen(G.filename) - 1] == TANDEM_DELIMITER) {
  479. checkdir(__G__ G.filename, GETPATH);
  480. if (created_dir) {
  481. if (QCOND2) {
  482. Info(slide, 0, ((char *)slide, " creating: %s\n",
  483. FnFilter1(G.filename)));
  484. }
  485. /* set dir time (note trailing '/') */
  486. return (error & ~MPN_MASK) | MPN_CREATED_DIR;
  487. }
  488. /* dir existed already; don't look for data to extract */
  489. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  490. }
  491. *pp = '\0'; /* done with pathcomp: terminate it */
  492. /* if not saving them, remove VMS version numbers (appended ";###") */
  493. if (!uO.V_flag && lastsemi) {
  494. pp = lastsemi + 1;
  495. while (isdigit((uch)(*pp)))
  496. ++pp;
  497. if (*pp == '\0') /* only digits between ';' and end: nuke */
  498. *lastsemi = '\0';
  499. }
  500. if (*pathcomp == '\0') {
  501. Info(slide, 1, ((char *)slide, "mapname: conversion of %s failed\n",
  502. FnFilter1(G.filename)));
  503. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  504. }
  505. checkdir(__G__ pathcomp, APPEND_NAME); /* returns 1 if truncated: care? */
  506. checkdir(__G__ G.filename, GETPATH);
  507. return error;
  508. } /* end function mapname() */
  509. /***********************/
  510. /* Function checkdir() */
  511. /***********************/
  512. int checkdir(__G__ pathcomp, flag)
  513. __GDEF
  514. char *pathcomp;
  515. int flag;
  516. /*
  517. * returns:
  518. * MPN_OK - no problem detected
  519. * MPN_INF_TRUNC - (on APPEND_NAME) truncated filename
  520. * MPN_INF_SKIP - path doesn't exist, not allowed to create
  521. * MPN_ERR_SKIP - path doesn't exist, tried to create and failed; or path
  522. * exists and is not a directory, but is supposed to be
  523. * MPN_ERR_TOOLONG - path is too long
  524. * MPN_NOMEM - can't allocate memory for filename buffers
  525. */
  526. /* By using in2ex() to pre-process the filename we do not need to worry
  527. about the lengths of filename parts. We just need to cope with mapping the
  528. pseudo file extensions properly of the actual filename (last part of name).
  529. e.g. "tandem c" -> tandemc
  530. "revsion.h" -> revisioh
  531. */
  532. {
  533. static int rootlen = 0; /* length of rootpath */
  534. static char *rootpath; /* user's "extract-to" directory */
  535. static char *buildpath; /* full path (so far) to extracted file */
  536. static char *end; /* pointer to end of buildpath ('\0') */
  537. # define FN_MASK 7
  538. # define FUNCTION (flag & FN_MASK)
  539. char fname[FILENAME_MAX + 1];
  540. short fnamelen, extlen, trunclen, i;
  541. char ext[EXTENSION_MAX + 1];
  542. char *ptr;
  543. /*---------------------------------------------------------------------------
  544. APPEND_DIR: append the path component to the path being built and check
  545. for its existence. If doesn't exist and we are creating directories, do
  546. so for this one; else signal success or error as appropriate.
  547. ---------------------------------------------------------------------------*/
  548. if (FUNCTION == APPEND_DIR) {
  549. int too_long = FALSE;
  550. Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
  551. while ((*end = *pathcomp++) != '\0')
  552. ++end;
  553. if (stat(buildpath, &G.statbuf)) { /* path doesn't exist */
  554. if (!G.create_dirs) { /* told not to create (freshening) */
  555. free(buildpath);
  556. return MPN_INF_SKIP; /* path doesn't exist: nothing to do */
  557. }
  558. if (mkdir(buildpath, 0777) == -1) { /* create the directory */
  559. Info(slide, 1, ((char *)slide,
  560. "checkdir error: cannot create %s\n\
  561. unable to process %s.\n",
  562. FnFilter2(buildpath), FnFilter1(G.filename)));
  563. free(buildpath);
  564. /* path didn't exist, tried to create, failed */
  565. return MPN_ERR_SKIP;
  566. }
  567. created_dir = TRUE;
  568. } else if (!S_ISDIR(G.statbuf.st_mode)) {
  569. Info(slide, 1, ((char *)slide,
  570. "checkdir error: %s exists but is not directory\n\
  571. unable to process %s.\n",
  572. FnFilter2(buildpath), FnFilter1(G.filename)));
  573. free(buildpath);
  574. /* path existed but wasn't dir */
  575. return MPN_ERR_SKIP;
  576. }
  577. *end++ = TANDEM_DELIMITER;
  578. *end = '\0';
  579. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  580. return MPN_OK;
  581. } /* end if (FUNCTION == APPEND_DIR) */
  582. /*---------------------------------------------------------------------------
  583. GETPATH: copy full path to the string pointed at by pathcomp, and free
  584. buildpath.
  585. ---------------------------------------------------------------------------*/
  586. if (FUNCTION == GETPATH) {
  587. strcpy(pathcomp, buildpath);
  588. Trace((stderr, "getting and freeing path [%s]\n",
  589. FnFilter1(pathcomp)));
  590. free(buildpath);
  591. buildpath = end = (char *)NULL;
  592. return MPN_OK;
  593. }
  594. /*---------------------------------------------------------------------------
  595. APPEND_NAME: assume the path component is the filename; append it and
  596. return without checking for existence.
  597. ---------------------------------------------------------------------------*/
  598. if (FUNCTION == APPEND_NAME) {
  599. Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  600. if (!uO.rflag /* Do not add extension if asked */
  601. && parsename(pathcomp, fname, ext))
  602. {
  603. fnamelen = strlen(fname);
  604. extlen = strlen(ext);
  605. if (fnamelen+extlen > MAXFILEPARTLEN) {
  606. /* Doesn't fit. Best approx is to use up to three characters
  607. from extension and place these on the end of as much of the
  608. start of the filename part as possible.
  609. */
  610. if (extlen > EXTENSION_MAX)
  611. extlen = EXTENSION_MAX;
  612. trunclen = MAXFILEPARTLEN - extlen;
  613. ptr = fname;
  614. for (i=0; i < trunclen; i++)
  615. *end++ = *ptr++;
  616. ptr = ext;
  617. for (i=0; i < extlen; i++)
  618. *end++ = *ptr++;
  619. *end = '\0'; /* mark end of string */
  620. }
  621. else {
  622. /* Just join parts end to end */
  623. ptr = fname;
  624. while ((*end = *ptr++) != '\0')
  625. ++end;
  626. ptr = ext;
  627. while ((*end = *ptr++) != '\0')
  628. ++end;
  629. }
  630. }
  631. else
  632. while ((*end = *pathcomp++) != '\0')
  633. ++end;
  634. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  635. /* could check for existence here, prompt for new name... */
  636. return MPN_OK;
  637. }
  638. /*---------------------------------------------------------------------------
  639. INIT: allocate and initialize buffer space for the file currently being
  640. extracted. If file was renamed with an absolute path, don't prepend the
  641. extract-to path.
  642. ---------------------------------------------------------------------------*/
  643. /* GRR: for VMS and TOPS-20, add up to 13 to strlen */
  644. if (FUNCTION == INIT) {
  645. Trace((stderr, "initializing buildpath to "));
  646. if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+1))
  647. == (char *)NULL)
  648. return MPN_NOMEM;
  649. if ((rootlen > 0) && !renamed_fullpath) {
  650. strcpy(buildpath, rootpath);
  651. end = buildpath + rootlen;
  652. } else {
  653. *buildpath = '\0';
  654. end = buildpath;
  655. }
  656. Trace((stderr, "[%s]\n", FnFilter1(buildpath)));
  657. return MPN_OK;
  658. }
  659. /*---------------------------------------------------------------------------
  660. ROOT: if appropriate, store the path in rootpath and create it if
  661. necessary; else assume it's a zipfile member and return. This path
  662. segment gets used in extracting all members from every zipfile specified
  663. on the command line.
  664. ---------------------------------------------------------------------------*/
  665. #if (!defined(SFX) || defined(SFX_EXDIR))
  666. if (FUNCTION == ROOT) {
  667. Trace((stderr, "initializing root path to [%s]\n",
  668. FnFilter1(pathcomp)));
  669. if (pathcomp == (char *)NULL) {
  670. rootlen = 0;
  671. return MPN_OK;
  672. }
  673. if (rootlen > 0) /* rootpath was already set, nothing to do */
  674. return MPN_OK;
  675. if ((rootlen = strlen(pathcomp)) > 0) {
  676. char *tmproot;
  677. if ((tmproot = (char *)malloc(rootlen+2)) == (char *)NULL) {
  678. rootlen = 0;
  679. return MPN_NOMEM;
  680. }
  681. strcpy(tmproot, pathcomp);
  682. if (tmproot[rootlen-1] == TANDEM_DELIMITER) {
  683. tmproot[--rootlen] = '\0';
  684. }
  685. if (rootlen > 0 && (stat(tmproot, &G.statbuf) ||
  686. !S_ISDIR(G.statbuf.st_mode)))
  687. { /* path does not exist */
  688. if (!G.create_dirs /* || iswild(tmproot) */ ) {
  689. free(tmproot);
  690. rootlen = 0;
  691. /* skip (or treat as stored file) */
  692. return MPN_INF_SKIP;
  693. }
  694. /* create the directory (could add loop here scanning tmproot
  695. * to create more than one level, but why really necessary?) */
  696. if (mkdir(tmproot, 0777) == -1) {
  697. Info(slide, 1, ((char *)slide,
  698. "checkdir: cannot create extraction directory: %s\n",
  699. FnFilter1(tmproot)));
  700. free(tmproot);
  701. rootlen = 0;
  702. /* path didn't exist, tried to create, and failed: */
  703. /* file exists, or 2+ subdir levels required */
  704. return MPN_ERR_SKIP;
  705. }
  706. }
  707. tmproot[rootlen++] = TANDEM_DELIMITER;
  708. tmproot[rootlen] = '\0';
  709. if ((rootpath = (char *)realloc(tmproot, rootlen+1)) == NULL) {
  710. free(tmproot);
  711. rootlen = 0;
  712. return MPN_NOMEM;
  713. }
  714. Trace((stderr, "rootpath now = [%s]\n", FnFilter1(rootpath)));
  715. }
  716. return MPN_OK;
  717. }
  718. #endif /* !SFX || SFX_EXDIR */
  719. /*---------------------------------------------------------------------------
  720. END: free rootpath, immediately prior to program exit.
  721. ---------------------------------------------------------------------------*/
  722. if (FUNCTION == END) {
  723. Trace((stderr, "freeing rootpath\n"));
  724. if (rootlen > 0) {
  725. free(rootpath);
  726. rootlen = 0;
  727. }
  728. return MPN_OK;
  729. }
  730. return MPN_INVALID; /* should never reach */
  731. } /* end function checkdir() */
  732. /********************/
  733. /* Function mkdir() */
  734. /********************/
  735. int mkdir(path, mode)
  736. const char *path; /* both */
  737. mode_t mode; /* ignored */
  738. /*
  739. * returns: 0 - successful
  740. * -1 - failed (errno not set, however)
  741. */
  742. {
  743. return 0;
  744. }
  745. /****************************/
  746. /* Function close_outfile() */
  747. /****************************/
  748. void close_outfile(__G) /* GRR: change to return PK-style warning level */
  749. __GDEF
  750. {
  751. union {
  752. iztimes t3; /* mtime, atime, ctime */
  753. ztimbuf t2; /* modtime, actime */
  754. } zt;
  755. ulg z_uidgid[2];
  756. unsigned eb_izux_flg;
  757. nsk_file_attrs *znsk_attr;
  758. short err;
  759. unsigned short len;
  760. #define alist_items 1
  761. #define vlist_bytes 2
  762. short alist[alist_items]={42 };
  763. unsigned short vlist[alist_items];
  764. short extra, *err_item=&extra;
  765. char nsk_work[FILENAME_MAX + 1], *nsk_fname=&nsk_work[0];
  766. fclose(G.outfile);
  767. /* Set up Tandem specific file information if present */
  768. znsk_attr = ef_scan_for_tandem(G.extra_field, G.lrec.extra_field_length);
  769. /* Currently we do not create 'proper' Tandem files in unzip - we only */
  770. /* create unstructured (180) or edit (101) files. This code allows */
  771. /* some limited support for restoration of Tandem file attributes */
  772. if (znsk_attr != NULL) {
  773. /* Reset File Code */
  774. if (!uO.bflag && !G.pInfo->textmode
  775. && znsk_attr->filetype == NSK_UNSTRUCTURED) {
  776. /* leave type 101 files alone */
  777. vlist[0] = znsk_attr->filecode;
  778. /* Note that FILE_ALTERLIST_ expect uppercase names and that */
  779. /* G.lrec.filename_length is the length of original zipped filename */
  780. /* not any 'rename' name. Need to re call strlen and upshift */
  781. len = strlen(G.filename);
  782. err = STRING_UPSHIFT_(G.filename,
  783. len,
  784. nsk_fname,
  785. len);
  786. err = FILE_ALTERLIST_(nsk_fname,
  787. len,
  788. alist,
  789. alist_items,
  790. vlist,
  791. vlist_bytes,
  792. ,
  793. err_item);
  794. }
  795. }
  796. /*---------------------------------------------------------------------------
  797. Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  798. time: adjust base year from 1980 to 1970, do usual conversions from
  799. yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  800. light savings time differences. If we have a Unix extra field, however,
  801. we're laughing: both mtime and atime are ours. On the other hand, we
  802. then have to check for restoration of UID/GID.
  803. ---------------------------------------------------------------------------*/
  804. eb_izux_flg = (G.extra_field ? ef_scan_for_izux(G.extra_field,
  805. G.lrec.extra_field_length, 0, G.lrec.last_mod_dos_datetime,
  806. #ifdef IZ_CHECK_TZ
  807. (G.tz_is_valid ? &(zt.t3) : NULL),
  808. #else
  809. &(zt.t3),
  810. #endif
  811. z_uidgid) : 0);
  812. if (eb_izux_flg & EB_UT_FL_MTIME) {
  813. TTrace((stderr, "\nclose_outfile: Unix e.f. modif. time = %ld\n",
  814. zt.t3.mtime));
  815. } else {
  816. zt.t3.mtime = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
  817. }
  818. if (eb_izux_flg & EB_UT_FL_ATIME) {
  819. TTrace((stderr, "close_outfile: Unix e.f. access time = %ld\n",
  820. zt.t3.atime));
  821. } else {
  822. zt.t3.atime = zt.t3.mtime;
  823. TTrace((stderr, "\nclose_outfile: modification/access times = %ld\n",
  824. zt.t3.mtime));
  825. }
  826. /*---------------------------------------------------------------------------
  827. Change the file's last modified time to that stored in the zipfile.
  828. Not sure how (yet) or whether it's a good idea to set the last open time
  829. ---------------------------------------------------------------------------*/
  830. /* skip restoring time stamps on user's request */
  831. if (uO.D_flag <= 1) {
  832. /* set the file's access and modification times */
  833. if (utime(G.filename, &(zt.t2)))
  834. if (uO.qflag)
  835. Info(slide, 0x201, ((char *)slide,
  836. "warning: cannot set times for %s\n",
  837. FnFilter1(G.filename)));
  838. else
  839. Info(slide, 0x201, ((char *)slide,
  840. " (warning) cannot set times"));
  841. }
  842. /*---------------------------------------------------------------------------
  843. Change the file permissions from default ones to those stored in the
  844. zipfile.
  845. ---------------------------------------------------------------------------*/
  846. #ifndef NO_CHMOD
  847. if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
  848. perror("chmod (file attributes) error");
  849. #endif
  850. /*---------------------------------------------------------------------------
  851. if -X option was specified and we have UID/GID info, restore it
  852. this must come after the file security and modtimes changes - since once
  853. we have secured the file to somebody else we cannot access it again.
  854. ---------------------------------------------------------------------------*/
  855. if (uO.X_flag && eb_izux_flg & EB_UX2_VALID) {
  856. TTrace((stderr, "close_outfile: restoring Unix UID/GID info\n"));
  857. if (chown(G.filename, (uid_t)z_uidgid[0], (gid_t)z_uidgid[1]))
  858. {
  859. if (uO.qflag)
  860. Info(slide, 0x201, ((char *)slide,
  861. "warning: cannot set UID %lu and/or GID %lu for %s\n",
  862. z_uidgid[0], z_uidgid[1], FnFilter1(G.filename)));
  863. else
  864. Info(slide, 0x201, ((char *)slide,
  865. " (warning) cannot set UID %lu and/or GID %lu",
  866. z_uidgid[0], z_uidgid[1]));
  867. }
  868. }
  869. } /* end function close_outfile() */
  870. #ifndef SFX
  871. /************************/
  872. /* Function version() */
  873. /************************/
  874. void version(__G)
  875. __GDEF
  876. {
  877. /* Pyramid, NeXT have problems with huge macro expansion, too: no Info() */
  878. sprintf((char *)slide, LoadFarString(CompiledWith),
  879. "C ",
  880. "T9255D44 - (16OCT98)",
  881. "NonStop ",
  882. "(Tandem/NSK)",
  883. #ifdef __DATE__
  884. " on ", __DATE__
  885. #else
  886. "", ""
  887. #endif
  888. );
  889. (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
  890. } /* end function version() */
  891. #endif /* !SFX */