human68k.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. Copyright (c) 1990-2007 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. human68k.c
  10. Human68k-specific routines for use with Info-ZIP's UnZip 5.41 and later.
  11. Contains: do_wild()
  12. mapattr()
  13. mapname()
  14. checkdir()
  15. close_outfile()
  16. stamp_file() (TIMESTAMP only)
  17. version()
  18. main() (for UnZipSFX)
  19. ---------------------------------------------------------------------------*/
  20. #include <dirent.h>
  21. #include <string.h>
  22. #include <sys/dos.h>
  23. #include <sys/xunistd.h>
  24. #ifdef HAVE_TWONCALL_H
  25. #include <twoncall.h>
  26. #endif
  27. #define UNZIP_INTERNAL
  28. #include "unzip.h"
  29. #if defined (SFX) && defined (MAIN)
  30. #include <sys/xstart.h>
  31. int MAIN(int argc, char *argv[]);
  32. #endif
  33. static void map2fat(char *pathcomp, char *last_dot);
  34. static char *trunc_name(char *name, int maxlen);
  35. static int created_dir; /* used in mapname(), checkdir() */
  36. static int renamed_fullpath; /* ditto */
  37. static char multi_period, special_char;
  38. #ifndef SFX
  39. /**********************/
  40. /* Function do_wild() */
  41. /**********************/
  42. char *do_wild(__G__ wildspec)
  43. __GDEF
  44. ZCONST char *wildspec; /* only used first time on a given dir */
  45. {
  46. static DIR *wild_dir = (DIR *)NULL;
  47. static ZCONST char *wildname;
  48. static char *dirname, matchname[FILNAMSIZ];
  49. static int notfirstcall=FALSE, have_dirname, dirnamelen;
  50. struct dirent *file;
  51. /* Even when we're just returning wildspec, we *always* do so in
  52. * matchname[]--calling routine is allowed to append four characters
  53. * to the returned string, and wildspec may be a pointer to argv[].
  54. */
  55. if (!notfirstcall) { /* first call: must initialize everything */
  56. notfirstcall = TRUE;
  57. if (!iswild(wildspec)) {
  58. strncpy(matchname, wildspec, FILNAMSIZ);
  59. matchname[FILNAMSIZ-1] = '\0';
  60. have_dirname = FALSE;
  61. wild_dir = (DIR *)NULL;
  62. return matchname;
  63. }
  64. /* break the wildspec into a directory part and a wildcard filename */
  65. if ((wildname = strrchr(wildspec, '/')) == NULL) {
  66. dirname = ".";
  67. dirnamelen = 1;
  68. have_dirname = FALSE;
  69. wildname = wildspec;
  70. } else {
  71. ++wildname; /* point at character after '/' */
  72. dirnamelen = wildname - wildspec;
  73. if ((dirname = (char *)malloc(dirnamelen+1)) == NULL) {
  74. Info(slide, 1, ((char *)slide,
  75. "warning: cannot allocate wildcard buffers\n"));
  76. strcpy(matchname, wildspec);
  77. return matchname; /* but maybe filespec was not a wildcard */
  78. }
  79. strncpy(dirname, wildspec, dirnamelen);
  80. dirname[dirnamelen] = '\0'; /* terminate for strcpy below */
  81. have_dirname = TRUE;
  82. }
  83. Trace((stderr, "do_wild: dirname = [%s]\n", FnFilter1(dirname)));
  84. if ((wild_dir = opendir(dirname)) != (DIR *)NULL) {
  85. while ((file = readdir(wild_dir)) != (struct dirent *)NULL) {
  86. Trace((stderr, "do_wild: readdir returns %s\n",
  87. FnFilter1(file->d_name)));
  88. if (file->d_name[0] == '.' && wildname[0] != '.')
  89. continue; /* Unix: '*' and '?' do not match leading dot */
  90. if (match(file->d_name, wildname, 0 WISEP) && /* 0=case sens.*/
  91. /* skip "." and ".." directory entries */
  92. strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
  93. Trace((stderr, "do_wild: match() succeeds\n"));
  94. if (have_dirname) {
  95. strcpy(matchname, dirname);
  96. strcpy(matchname+dirnamelen, file->d_name);
  97. } else
  98. strcpy(matchname, file->d_name);
  99. return matchname;
  100. }
  101. }
  102. /* if we get to here directory is exhausted, so close it */
  103. closedir(wild_dir);
  104. wild_dir = (DIR *)NULL;
  105. }
  106. #ifdef DEBUG
  107. else {
  108. Trace((stderr, "do_wild: Opendir(%s) returns NULL\n",
  109. FnFilter1(dirname)));
  110. }
  111. #endif /* DEBUG */
  112. /* return the raw wildspec in case that works (e.g., directory not
  113. * searchable, but filespec was not wild and file is readable) */
  114. strncpy(matchname, wildspec, FILNAMSIZ);
  115. matchname[FILNAMSIZ-1] = '\0';
  116. return matchname;
  117. }
  118. /* last time through, might have failed opendir but returned raw wildspec */
  119. if (wild_dir == (DIR *)NULL) {
  120. notfirstcall = FALSE; /* nothing left to try--reset for new wildspec */
  121. if (have_dirname)
  122. free(dirname);
  123. return (char *)NULL;
  124. }
  125. /* If we've gotten this far, we've read and matched at least one entry
  126. * successfully (in a previous call), so dirname has been copied into
  127. * matchname already.
  128. */
  129. while ((file = readdir(wild_dir)) != (struct dirent *)NULL) {
  130. Trace((stderr, "do_wild: readdir returns %s\n",
  131. FnFilter1(file->d_name)));
  132. if (file->d_name[0] == '.' && wildname[0] != '.')
  133. continue; /* Unix: '*' and '?' do not match leading dot */
  134. if (match(file->d_name, wildname, 0 WISEP)) { /* 0 == case sens. */
  135. Trace((stderr, "do_wild: match() succeeds\n"));
  136. if (have_dirname) {
  137. /* strcpy(matchname, dirname); */
  138. strcpy(matchname+dirnamelen, file->d_name);
  139. } else
  140. strcpy(matchname, file->d_name);
  141. return matchname;
  142. }
  143. }
  144. closedir(wild_dir); /* have read at least one entry; nothing left */
  145. wild_dir = (DIR *)NULL;
  146. notfirstcall = FALSE; /* reset for new wildspec */
  147. if (have_dirname)
  148. free(dirname);
  149. return (char *)NULL;
  150. } /* end function do_wild() */
  151. #endif /* !SFX */
  152. /**********************/
  153. /* Function mapattr() */
  154. /**********************/
  155. int mapattr(__G)
  156. __GDEF
  157. {
  158. ulg tmp = G.crec.external_file_attributes;
  159. switch (G.pInfo->hostnum) {
  160. case UNIX_:
  161. if (tmp & 0xff)
  162. break;
  163. /* fall through */
  164. case VMS_:
  165. case ACORN_:
  166. case ATARI_:
  167. case ATHEOS_:
  168. case BEOS_:
  169. case QDOS_:
  170. G.pInfo->file_attr = _mode2dos(tmp >> 16);
  171. return 0;
  172. default:
  173. break;
  174. }
  175. /* set archive bit (file is not backed up) */
  176. if((tmp & 0x08) == 0)
  177. tmp |= 0x20;
  178. G.pInfo->file_attr = tmp & 0xff;
  179. return 0;
  180. } /* end function mapattr() */
  181. /**********************/
  182. /* Function mapname() */
  183. /**********************/
  184. int mapname(__G__ renamed)
  185. __GDEF
  186. int renamed;
  187. /*
  188. * returns:
  189. * MPN_OK - no problem detected
  190. * MPN_INF_TRUNC - caution (truncated filename)
  191. * MPN_INF_SKIP - info "skip entry" (dir doesn't exist)
  192. * MPN_ERR_SKIP - error -> skip entry
  193. * MPN_ERR_TOOLONG - error -> path is too long
  194. * MPN_NOMEM - error (memory allocation failed) -> skip entry
  195. * [also MPN_VOL_LABEL, MPN_CREATED_DIR]
  196. */
  197. {
  198. char pathcomp[FILNAMSIZ]; /* path-component buffer */
  199. char *pp, *cp=(char *)NULL; /* character pointers */
  200. char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  201. char *last_dot=(char *)NULL; /* last dot */
  202. int error = MPN_OK;
  203. register unsigned workch; /* hold the character being tested */
  204. #ifdef HAVE_TWONCALL_H
  205. static char twentyone_flag;
  206. /* Get TwentyOne options */
  207. if (twentyone_flag == 0) {
  208. twentyone_flag++;
  209. if (GetTwentyOneID () == TWON_ID) {
  210. int flags = GetTwentyOneOptions ();
  211. if (flags & (1 << TWON_PERIOD_BIT))
  212. multi_period = TRUE;
  213. if (flags & (1 << TWON_SPECIAL_BIT))
  214. special_char = TRUE;
  215. }
  216. }
  217. #endif
  218. /*---------------------------------------------------------------------------
  219. Initialize various pointers and counters and stuff.
  220. ---------------------------------------------------------------------------*/
  221. /* can create path as long as not just freshening, or if user told us */
  222. G.create_dirs = (!uO.fflag || renamed);
  223. created_dir = FALSE; /* not yet */
  224. renamed_fullpath = FALSE;
  225. if (renamed) {
  226. cp = G.filename - 1; /* point to beginning of renamed name... */
  227. while (*++cp)
  228. if (*cp == '\\') /* convert backslashes to forward */
  229. *cp = '/';
  230. cp = G.filename;
  231. if ((G.filename[0] == '/')
  232. || (isalpha(G.filename[0]) && G.filename[1] == ':')) {
  233. /* user gave full pathname: don't prepend rootpath */
  234. renamed_fullpath = TRUE;
  235. }
  236. }
  237. if ((error = checkdir(__G__ (char *)NULL, INIT)) != 0)
  238. return error; /* initialize path buffer, unless no memory */
  239. *pathcomp = '\0'; /* initialize translation buffer */
  240. pp = pathcomp; /* point to translation buffer */
  241. if (uO.jflag) /* junking directories */
  242. cp = (char *)strrchr(G.filename, '/');
  243. if (cp == (char *)NULL) /* no '/' or not junking dirs */
  244. cp = G.filename; /* point to internal zipfile-member pathname */
  245. else
  246. ++cp; /* point to start of last component of path */
  247. /*---------------------------------------------------------------------------
  248. Begin main loop through characters in filename.
  249. ---------------------------------------------------------------------------*/
  250. while ((workch = (uch)*cp++) != 0) {
  251. if (_ismbblead((unsigned char)workch)) {
  252. if (*cp) {
  253. *pp++ = (char)workch;
  254. *pp++ = (char)*cp++;
  255. }
  256. else
  257. *pp++ = '_';
  258. continue;
  259. }
  260. switch (workch) {
  261. case '/': /* can assume -j flag not given */
  262. *pp = '\0';
  263. map2fat(pathcomp, last_dot); /* 18.3 trunc. (in place) */
  264. if (strcmp(pathcomp, ".") == 0) {
  265. /* don't bother appending "./" to the path */
  266. *pathcomp = '\0';
  267. } else if (!uO.ddotflag && strcmp(pathcomp, "..") == 0) {
  268. /* "../" dir traversal detected, skip over it */
  269. *pathcomp = '\0';
  270. killed_ddot = TRUE; /* set "show message" flag */
  271. }
  272. /* when path component is not empty, append it now */
  273. if (*pathcomp != '\0' &&
  274. ((error = checkdir(__G__ pathcomp, APPEND_DIR))
  275. & MPN_MASK) > MPN_INF_TRUNC)
  276. return error;
  277. pp = pathcomp; /* reset conversion buffer for next piece */
  278. lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
  279. break;
  280. /* drive names are not stored in zipfile, so no colons allowed;
  281. * no brackets or most other punctuation either (all of which
  282. * can appear in Unix-created archives; backslash is particularly
  283. * bad unless all necessary directories exist) */
  284. case '[': /* these punctuation characters forbidden */
  285. case ']': /* only on plain FAT file systems */
  286. case '+':
  287. case ',':
  288. case '=':
  289. case '<':
  290. case '>':
  291. case '|':
  292. case '\"':
  293. case '\'':
  294. if (!special_char)
  295. workch = '_';
  296. *pp++ = (char)workch;
  297. break;
  298. case '-':
  299. if (pp == pathcomp && !special_char)
  300. workch = '_';
  301. *pp++ = (char)workch;
  302. break;
  303. case ':':
  304. case '\\':
  305. case '*':
  306. case '?':
  307. *pp++ = '_';
  308. break;
  309. case ';': /* VMS version (or DEC-20 attrib?) */
  310. lastsemi = pp;
  311. if (!special_char)
  312. workch = '_';
  313. *pp++ = (char)workch; /* keep for now; remove VMS ";##" */
  314. break; /* later, if requested */
  315. case ' ': /* change spaces to underscores */
  316. #if 0 /* do it always */
  317. if (uO.sflag) /* only if requested */
  318. #endif
  319. workch = '_';
  320. *pp++ = (char)workch;
  321. break;
  322. default:
  323. /* allow European characters in filenames: */
  324. if (isprint(workch) || workch >= 128)
  325. *pp++ = (char)workch;
  326. } /* end switch */
  327. } /* end while loop */
  328. /* Show warning when stripping insecure "parent dir" path components */
  329. if (killed_ddot && QCOND2) {
  330. Info(slide, 0, ((char *)slide,
  331. "warning: skipped \"../\" path component(s) in %s\n",
  332. FnFilter1(G.filename)));
  333. if (!(error & ~MPN_MASK))
  334. error = (error & MPN_MASK) | PK_WARN;
  335. }
  336. /*---------------------------------------------------------------------------
  337. Report if directory was created (and no file to create: filename ended
  338. in '/'), check name to be sure it exists, and combine path and name be-
  339. fore exiting.
  340. ---------------------------------------------------------------------------*/
  341. if (G.filename[strlen(G.filename) - 1] == '/') {
  342. checkdir(__G__ G.filename, GETPATH);
  343. if (created_dir) {
  344. if (QCOND2) {
  345. Info(slide, 0, ((char *)slide, " creating: %s\n",
  346. FnFilter1(G.filename)));
  347. }
  348. /* set dir time (note trailing '/') */
  349. return (error & ~MPN_MASK) | MPN_CREATED_DIR;
  350. }
  351. /* dir existed already; don't look for data to extract */
  352. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  353. }
  354. *pp = '\0'; /* done with pathcomp: terminate it */
  355. /* if not saving them, remove VMS version numbers (appended ";###") */
  356. if (!uO.V_flag && lastsemi) {
  357. pp = lastsemi + 1;
  358. while (isdigit((uch)(*pp)))
  359. ++pp;
  360. if (*pp == '\0') /* only digits between ';' and end: nuke */
  361. *lastsemi = '\0';
  362. }
  363. map2fat(pathcomp, last_dot); /* 18.3 truncation (in place) */
  364. if (*pathcomp == '\0') {
  365. Info(slide, 1, ((char *)slide, "mapname: conversion of %s failed\n",
  366. FnFilter1(G.filename)));
  367. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  368. }
  369. checkdir(__G__ pathcomp, APPEND_NAME); /* returns 1 if truncated: care? */
  370. checkdir(__G__ G.filename, GETPATH);
  371. if (G.pInfo->vollabel) { /* set the volume label now */
  372. int fd;
  373. if (QCOND2)
  374. Info(slide, 0, ((char *)slide, " labelling: %s\n",
  375. FnFilter1(G.filename)));
  376. if ((fd = _dos_newfile(G.filename, G.pInfo->file_attr)) < 0) {
  377. Info(slide, 1, ((char *)slide,
  378. "mapname: error setting volume label\n"));
  379. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  380. }
  381. _dos_close(fd);
  382. /* success: skip the "extraction" quietly */
  383. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  384. }
  385. return error;
  386. } /* end function mapname() */
  387. /**********************/
  388. /* Function map2fat() */
  389. /**********************/
  390. static void map2fat(pathcomp, last_dot)
  391. char *pathcomp, *last_dot;
  392. {
  393. char *np;
  394. if (pathcomp == last_dot) { /* dotfile(e.g. ".foo") */
  395. pathcomp = last_dot;
  396. last_dot = (char *)NULL;
  397. }
  398. if (multi_period) {
  399. if (strlen(pathcomp) <= 18)
  400. return;
  401. }
  402. else {
  403. char *p;
  404. for (p = pathcomp; *p; p++)
  405. if (*p == (char)'.' && p != last_dot)
  406. *p = '_';
  407. }
  408. if (last_dot) {
  409. *last_dot++ = '\0';
  410. trunc_name(last_dot, 3);
  411. }
  412. np = trunc_name(pathcomp, 18);
  413. if (last_dot) {
  414. *--last_dot = '.';
  415. if (np)
  416. strcpy(np, last_dot);
  417. }
  418. } /* end function map2fat() */
  419. static char *trunc_name(char *name, int maxlen)
  420. {
  421. if (strlen(name) <= maxlen)
  422. return (char *)NULL;
  423. do {
  424. if (_ismbblead((unsigned char)*name)) {
  425. if (--maxlen == 0)
  426. break;
  427. name++;
  428. }
  429. name++;
  430. maxlen--;
  431. } while (maxlen > 0);
  432. *name = '\0';
  433. return name;
  434. }
  435. /***********************/
  436. /* Function checkdir() */
  437. /***********************/
  438. int checkdir(__G__ pathcomp, flag)
  439. __GDEF
  440. char *pathcomp;
  441. int flag;
  442. /*
  443. * returns:
  444. * MPN_OK - no problem detected
  445. * MPN_INF_TRUNC - (on APPEND_NAME) truncated filename
  446. * MPN_INF_SKIP - path doesn't exist, not allowed to create
  447. * MPN_ERR_SKIP - path doesn't exist, tried to create and failed; or path
  448. * exists and is not a directory, but is supposed to be
  449. * MPN_ERR_TOOLONG - path is too long
  450. * MPN_NOMEM - can't allocate memory for filename buffers
  451. */
  452. {
  453. static int rootlen = 0; /* length of rootpath */
  454. static char *rootpath; /* user's "extract-to" directory */
  455. static char *buildpath; /* full path (so far) to extracted file */
  456. static char *end; /* pointer to end of buildpath ('\0') */
  457. # define FN_MASK 7
  458. # define FUNCTION (flag & FN_MASK)
  459. /*---------------------------------------------------------------------------
  460. APPEND_DIR: append the path component to the path being built and check
  461. for its existence. If doesn't exist and we are creating directories, do
  462. so for this one; else signal success or error as appropriate.
  463. ---------------------------------------------------------------------------*/
  464. if (FUNCTION == APPEND_DIR) {
  465. int too_long = FALSE;
  466. Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
  467. while ((*end = *pathcomp++) != '\0')
  468. ++end;
  469. /* GRR: could do better check, see if overrunning buffer as we go:
  470. * check end-buildpath after each append, set warning variable if
  471. * within 20 of FILNAMSIZ; then if var set, do careful check when
  472. * appending. Clear variable when begin new path. */
  473. if ((end-buildpath) > FILNAMSIZ-3) /* need '/', one-char name, '\0' */
  474. too_long = TRUE; /* check if extracting directory? */
  475. if (SSTAT(buildpath, &G.statbuf)) /* path doesn't exist */
  476. {
  477. if (!G.create_dirs) { /* told not to create (freshening) */
  478. free(buildpath);
  479. return MPN_INF_SKIP; /* path doesn't exist: nothing to do */
  480. }
  481. if (too_long) {
  482. Info(slide, 1, ((char *)slide,
  483. "checkdir error: path too long: %s\n",
  484. FnFilter1(buildpath)));
  485. free(buildpath);
  486. /* no room for filenames: fatal */
  487. return MPN_ERR_TOOLONG;
  488. }
  489. if (mkdir(buildpath, 0777) == -1) { /* create the directory */
  490. Info(slide, 1, ((char *)slide,
  491. "checkdir error: cannot create %s\n\
  492. unable to process %s.\n",
  493. FnFilter2(buildpath), FnFilter1(G.filename)));
  494. free(buildpath);
  495. /* path didn't exist, tried to create, failed */
  496. return MPN_ERR_SKIP;
  497. }
  498. created_dir = TRUE;
  499. } else if (!S_ISDIR(G.statbuf.st_mode)) {
  500. Info(slide, 1, ((char *)slide,
  501. "checkdir error: %s exists but is not directory\n\
  502. unable to process %s.\n",
  503. FnFilter2(buildpath), FnFilter1(G.filename)));
  504. free(buildpath);
  505. /* path existed but wasn't dir */
  506. return MPN_ERR_SKIP;
  507. }
  508. if (too_long) {
  509. Info(slide, 1, ((char *)slide,
  510. "checkdir error: path too long: %s\n", FnFilter1(buildpath)));
  511. free(buildpath);
  512. /* no room for filenames: fatal */
  513. return MPN_ERR_TOOLONG;
  514. }
  515. *end++ = '/';
  516. *end = '\0';
  517. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  518. return MPN_OK;
  519. } /* end if (FUNCTION == APPEND_DIR) */
  520. /*---------------------------------------------------------------------------
  521. GETPATH: copy full path to the string pointed at by pathcomp, and free
  522. buildpath.
  523. ---------------------------------------------------------------------------*/
  524. if (FUNCTION == GETPATH) {
  525. strcpy(pathcomp, buildpath);
  526. Trace((stderr, "getting and freeing path [%s]\n",
  527. FnFilter1(pathcomp)));
  528. free(buildpath);
  529. buildpath = end = (char *)NULL;
  530. return MPN_OK;
  531. }
  532. /*---------------------------------------------------------------------------
  533. APPEND_NAME: assume the path component is the filename; append it and
  534. return without checking for existence.
  535. ---------------------------------------------------------------------------*/
  536. if (FUNCTION == APPEND_NAME) {
  537. Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  538. while ((*end = *pathcomp++) != '\0') {
  539. ++end;
  540. if ((end-buildpath) >= FILNAMSIZ) {
  541. *--end = '\0';
  542. Info(slide, 1, ((char *)slide,
  543. "checkdir warning: path too long; truncating\n\
  544. %s\n -> %s\n",
  545. FnFilter1(G.filename), FnFilter2(buildpath)));
  546. return MPN_INF_TRUNC; /* filename truncated */
  547. }
  548. }
  549. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  550. /* could check for existence here, prompt for new name... */
  551. return MPN_OK;
  552. }
  553. /*---------------------------------------------------------------------------
  554. INIT: allocate and initialize buffer space for the file currently being
  555. extracted. If file was renamed with an absolute path, don't prepend the
  556. extract-to path.
  557. ---------------------------------------------------------------------------*/
  558. if (FUNCTION == INIT) {
  559. Trace((stderr, "initializing buildpath to "));
  560. /* allocate space for full filename, root path, and maybe "./" */
  561. if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+3)) ==
  562. (char *)NULL)
  563. return MPN_NOMEM;
  564. if ((rootlen > 0) && !renamed_fullpath) {
  565. strcpy(buildpath, rootpath);
  566. end = buildpath + rootlen;
  567. } else {
  568. *buildpath = '\0';
  569. end = buildpath;
  570. }
  571. Trace((stderr, "[%s]\n", FnFilter1(buildpath)));
  572. return MPN_OK;
  573. }
  574. /*---------------------------------------------------------------------------
  575. ROOT: if appropriate, store the path in rootpath and create it if neces-
  576. sary; else assume it's a zipfile member and return. This path segment
  577. gets used in extracting all members from every zipfile specified on the
  578. command line.
  579. ---------------------------------------------------------------------------*/
  580. #if (!defined(SFX) || defined(SFX_EXDIR))
  581. if (FUNCTION == ROOT) {
  582. Trace((stderr, "initializing root path to [%s]\n",
  583. FnFilter1(pathcomp)));
  584. if (pathcomp == (char *)NULL) {
  585. rootlen = 0;
  586. return MPN_OK;
  587. }
  588. if (rootlen > 0) /* rootpath was already set, nothing to do */
  589. return MPN_OK;
  590. if ((rootlen = strlen(pathcomp)) > 0) {
  591. int had_trailing_pathsep=FALSE, has_drive=FALSE, add_dot=FALSE;
  592. char *tmproot;
  593. if ((tmproot = (char *)malloc(rootlen+3)) == (char *)NULL) {
  594. rootlen = 0;
  595. return MPN_NOMEM;
  596. }
  597. strcpy(tmproot, pathcomp);
  598. if (isalpha((uch)tmproot[0]) && tmproot[1] == ':')
  599. has_drive = TRUE; /* drive designator */
  600. if (tmproot[rootlen-1] == '/' || tmproot[rootlen-1] == '\\') {
  601. tmproot[--rootlen] = '\0';
  602. had_trailing_pathsep = TRUE;
  603. }
  604. if (has_drive && (rootlen == 2)) {
  605. if (!had_trailing_pathsep) /* i.e., original wasn't "x:/" */
  606. add_dot = TRUE; /* relative path: add '.' before '/' */
  607. } else if (rootlen > 0 && (SSTAT(tmproot, &G.statbuf) ||
  608. !S_ISDIR(G.statbuf.st_mode))) /* path does not exist */
  609. {
  610. if (!G.create_dirs /* || iswild(tmproot) */ ) {
  611. free(tmproot);
  612. rootlen = 0;
  613. /* skip (or treat as stored file) */
  614. return MPN_INF_SKIP;
  615. }
  616. /* create the directory (could add loop here scanning tmproot
  617. * to create more than one level, but why really necessary?) */
  618. if (mkdir(tmproot, 0777) == -1) {
  619. Info(slide, 1, ((char *)slide,
  620. "checkdir: cannot create extraction directory: %s\n",
  621. FnFilter1(tmproot)));
  622. free(tmproot);
  623. rootlen = 0;
  624. /* path didn't exist, tried to create, and failed: */
  625. /* file exists, or 2+ subdir levels required */
  626. return MPN_ERR_SKIP;
  627. }
  628. }
  629. if (add_dot) /* had just "x:", make "x:." */
  630. tmproot[rootlen++] = '.';
  631. tmproot[rootlen++] = '/';
  632. tmproot[rootlen] = '\0';
  633. if ((rootpath = (char *)realloc(tmproot, rootlen+1)) == NULL) {
  634. free(tmproot);
  635. rootlen = 0;
  636. return MPN_NOMEM;
  637. }
  638. Trace((stderr, "rootpath now = [%s]\n", FnFilter1(rootpath)));
  639. }
  640. return MPN_OK;
  641. }
  642. #endif /* !SFX || SFX_EXDIR */
  643. /*---------------------------------------------------------------------------
  644. END: free rootpath, immediately prior to program exit.
  645. ---------------------------------------------------------------------------*/
  646. if (FUNCTION == END) {
  647. Trace((stderr, "freeing rootpath\n"));
  648. if (rootlen > 0) {
  649. free(rootpath);
  650. rootlen = 0;
  651. }
  652. return MPN_OK;
  653. }
  654. return MPN_INVALID; /* should never reach */
  655. } /* end function checkdir() */
  656. #if (defined(USE_EF_UT_TIME) || defined(TIMESTAMP))
  657. /* The following DOS date/time structure is machine-dependent as it
  658. * assumes "little-endian" byte order. For MSDOS-specific code, which
  659. * is run on ix86 CPUs (or emulators), this assumption is valid; but
  660. * care should be taken when using this code as template for other ports.
  661. */
  662. typedef union {
  663. ulg z_dostime;
  664. struct { /* date and time words */
  665. ush ztime; /* DOS file modification time word */
  666. ush zdate; /* DOS file modification date word */
  667. } zft;
  668. struct { /* DOS date/time components bitfield */
  669. unsigned zt_se : 5;
  670. unsigned zt_mi : 6;
  671. unsigned zt_hr : 5;
  672. unsigned zd_dy : 5;
  673. unsigned zd_mo : 4;
  674. unsigned zd_yr : 7;
  675. } z_dtf;
  676. } dos_fdatetime;
  677. #endif /* USE_EF_UT_TIME || TIMESTAMP */
  678. /****************************/
  679. /* Function close_outfile() */
  680. /****************************/
  681. void close_outfile(__G)
  682. __GDEF
  683. {
  684. /* skip restoring time stamps on user's request */
  685. if (uO.D_flag <= 1) {
  686. #ifdef USE_EF_UT_TIME
  687. dos_fdatetime dos_dt;
  688. iztimes z_utime;
  689. struct tm *t;
  690. #endif /* USE_EF_UT_TIME */
  691. #ifdef USE_EF_UT_TIME
  692. if (G.extra_field &&
  693. #ifdef IZ_CHECK_TZ
  694. G.tz_is_valid &&
  695. #endif
  696. (ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length, 0,
  697. G.lrec.last_mod_dos_datetime, &z_utime, NULL)
  698. & EB_UT_FL_MTIME))
  699. {
  700. TTrace((stderr, "close_outfile: Unix e.f. modif. time = %ld\n",
  701. z_utime.mtime));
  702. /* round up (down if "up" overflows) to even seconds */
  703. if (z_utime.mtime & 1)
  704. z_utime.mtime = (z_utime.mtime + 1 > z_utime.mtime) ?
  705. z_utime.mtime + 1 : z_utime.mtime - 1;
  706. TIMET_TO_NATIVE(z_utime.mtime) /* NOP unless MSC 7 or Macintosh */
  707. t = localtime(&(z_utime.mtime));
  708. } else
  709. t = (struct tm *)NULL;
  710. if (t != (struct tm *)NULL) {
  711. if (t->tm_year < 80) {
  712. dos_dt.z_dtf.zt_se = 0;
  713. dos_dt.z_dtf.zt_mi = 0;
  714. dos_dt.z_dtf.zt_hr = 0;
  715. dos_dt.z_dtf.zd_dy = 1;
  716. dos_dt.z_dtf.zd_mo = 1;
  717. dos_dt.z_dtf.zd_yr = 0;
  718. } else {
  719. dos_dt.z_dtf.zt_se = t->tm_sec >> 1;
  720. dos_dt.z_dtf.zt_mi = t->tm_min;
  721. dos_dt.z_dtf.zt_hr = t->tm_hour;
  722. dos_dt.z_dtf.zd_dy = t->tm_mday;
  723. dos_dt.z_dtf.zd_mo = t->tm_mon + 1;
  724. dos_dt.z_dtf.zd_yr = t->tm_year - 80;
  725. }
  726. } else {
  727. dos_dt.z_dostime = G.lrec.last_mod_dos_datetime;
  728. }
  729. _dos_filedate(fileno(G.outfile), dos_dt.z_dostime);
  730. #else /* !USE_EF_UT_TIME */
  731. _dos_filedate(fileno(G.outfile), G.lrec.last_mod_dos_datetime);
  732. #endif /* ?USE_EF_UT_TIME */
  733. }
  734. fclose(G.outfile);
  735. _dos_chmod(G.filename, G.pInfo->file_attr);
  736. } /* end function close_outfile() */
  737. #ifdef TIMESTAMP
  738. /*************************/
  739. /* Function stamp_file() */
  740. /*************************/
  741. int stamp_file(fname, modtime)
  742. ZCONST char *fname;
  743. time_t modtime;
  744. {
  745. dos_fdatetime dos_dt;
  746. time_t t_even;
  747. struct tm *t;
  748. int fd; /* file handle */
  749. /* round up (down if "up" overflows) to even seconds */
  750. t_even = ((modtime + 1 > modtime) ? modtime + 1 : modtime) & (~1);
  751. TIMET_TO_NATIVE(t_even) /* NOP unless MSC 7.0 or Macintosh */
  752. t = localtime(&t_even);
  753. if (t == (struct tm *)NULL)
  754. return -1; /* time conversion error */
  755. if (t->tm_year < 80) {
  756. dos_dt.z_dtf.zt_se = 0;
  757. dos_dt.z_dtf.zt_mi = 0;
  758. dos_dt.z_dtf.zt_hr = 0;
  759. dos_dt.z_dtf.zd_dy = 1;
  760. dos_dt.z_dtf.zd_mo = 1;
  761. dos_dt.z_dtf.zd_yr = 0;
  762. } else {
  763. dos_dt.z_dtf.zt_se = t->tm_sec >> 1;
  764. dos_dt.z_dtf.zt_mi = t->tm_min;
  765. dos_dt.z_dtf.zt_hr = t->tm_hour;
  766. dos_dt.z_dtf.zd_dy = t->tm_mday;
  767. dos_dt.z_dtf.zd_mo = t->tm_mon + 1;
  768. dos_dt.z_dtf.zd_yr = t->tm_year - 80;
  769. }
  770. if (((fd = open((char *)fname, 0)) == -1) ||
  771. (_dos_filedate(fd, dos_dt.z_dostime)))
  772. {
  773. if (fd != -1)
  774. close(fd);
  775. return -1;
  776. }
  777. close(fd);
  778. return 0;
  779. } /* end function stamp_file() */
  780. #endif /* TIMESTAMP */
  781. #ifndef SFX
  782. /************************/
  783. /* Function version() */
  784. /************************/
  785. void version(__G)
  786. __GDEF
  787. {
  788. int len;
  789. #if 0
  790. char buf[40];
  791. #endif
  792. len = sprintf((char *)slide, LoadFarString(CompiledWith),
  793. #ifdef __GNUC__
  794. "gcc ", __VERSION__,
  795. #else
  796. # if 0
  797. "cc ", (sprintf(buf, " version %d", _RELEASE), buf),
  798. # else
  799. "unknown compiler", "",
  800. # endif
  801. #endif
  802. "Human68k",
  803. #ifdef __MC68020__
  804. " (X68030)",
  805. #else
  806. " (X680x0)",
  807. #endif
  808. #ifdef __DATE__
  809. " on ", __DATE__
  810. #else
  811. "", ""
  812. #endif
  813. );
  814. (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  815. } /* end function version() */
  816. #endif /* !SFX */
  817. #if defined (SFX) && defined (MAIN)
  818. int main(int argc, char *argv[])
  819. {
  820. char argv0[92];
  821. /* make true argv[0] (startup routine makes it inaccuracy) */
  822. argv[0] = strcat (strcpy (argv0, _procp->exe_path), _procp->exe_name);
  823. return MAIN(argc, argv);
  824. }
  825. #endif /* SFX && MAIN */