qdos.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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, 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. qdos.c
  10. QDOS-specific routines for use with Info-ZIP's UnZip 5.3 and later.
  11. Contains: Qstrfix()
  12. QFilename()
  13. QMatch()
  14. chowner()
  15. Qgetch()
  16. QReturn()
  17. LastDir()
  18. screensize()
  19. do_wild() <-- generic enough to put in file_io.c?
  20. mapattr()
  21. mapname()
  22. checkdir()
  23. qfix()
  24. close_outfile()
  25. stamp_file()
  26. getp()
  27. version()
  28. ---------------------------------------------------------------------------*/
  29. #define UNZIP_INTERNAL
  30. #include "unzip.h"
  31. #include "crypt.h"
  32. #include "ttyio.h"
  33. #include <dirent.h>
  34. #include "izqdos.h"
  35. #include "unzvers.h"
  36. #ifndef SFX
  37. char _prog_name[] = "UnZip";
  38. #else
  39. char _prog_name[] = "??Special Flag for unzipsfx hack ??";
  40. #endif
  41. /* sorrid hack at request of GRR follows; hope the compiler stays kind to us */
  42. char _version[] = {UZ_MAJORVER+'0','.',UZ_MINORVER+'0',UZ_PATCHLEVEL+'0'};
  43. char _extra[] = " " UZ_BETALEVEL;
  44. char _copyright[] = "(c) Info-ZIP Group";
  45. char * _endmsg = NULL;
  46. long _stack = 16*1024; /* huge stack (for qdos) */
  47. extern void consetup_title(chanid_t,struct WINDOWDEF *);
  48. void (*_consetup)(chanid_t,struct WINDOWDEF *) = consetup_title;
  49. struct WINDOWDEF _condetails =
  50. {
  51. 2,
  52. 1,
  53. 0,
  54. 7,
  55. 500,
  56. 220,
  57. 2,
  58. 30
  59. };
  60. static jobid_t chowner(chanid_t chan)
  61. {
  62. extern char *_sys_var;
  63. char *scht;
  64. long *cdb;
  65. long jid;
  66. scht = *((char **)(_sys_var + 0x78));
  67. cdb = *(long **)((long *)scht + (chan & 0xffff));
  68. jid = *(cdb + 2);
  69. return jid;
  70. }
  71. int QReturn(int err)
  72. {
  73. jobid_t me,you;
  74. me = getpid();
  75. you = chowner(getchid(0));
  76. if((me == you) && ((qlflag & 4) == 0))
  77. {
  78. if(isatty(0) && isatty(2) && qlwait)
  79. {
  80. char c = 0;
  81. fputs("Press a key to exit", stderr);
  82. if((io_fbyte(getchid(0), qlwait, &c) == 0) && c == 27)
  83. {
  84. io_fbyte(getchid(0), -1, &c);
  85. }
  86. }
  87. }
  88. if(err > 0) err = -err; /* We like -ve err nos (exclusively, alas) */
  89. exit(err);
  90. }
  91. #ifndef FUNZIP
  92. static int created_dir; /* used in mapname(), checkdir() */
  93. static int renamed_fullpath; /* ditto */
  94. char *Qstrfix (char *p)
  95. {
  96. char *q;
  97. for (q = p; (q = strstr(q, ".zip"));)
  98. {
  99. *q = '_';
  100. q += 4;
  101. }
  102. return p;
  103. }
  104. void QFilename(char *f)
  105. {
  106. char *o,*p,*q = strdup(f);
  107. p = q;
  108. if(*q == '.' && *(q+1) == '/') q += 2;
  109. o = q;
  110. for(;*q;q++)
  111. {
  112. if(*q == '/') *q = '_';
  113. if((qlflag & 1) == 0)
  114. {
  115. if(*q == '.') *q = '_';
  116. }
  117. }
  118. strcpy(f,o);
  119. free(p);
  120. }
  121. int QMatch(uch c1, uch c2)
  122. {
  123. int m =0;
  124. if(c1 != c2)
  125. {
  126. if(c1 == '_' && (c2 == '.' || c2 == '/'))
  127. {
  128. m = 1;
  129. }
  130. }
  131. else
  132. {
  133. m = 1;
  134. }
  135. return m;
  136. }
  137. int Qgetch(void)
  138. {
  139. char ch;
  140. if(io_fbyte(getchid(0), -1, &ch) < 0)
  141. {
  142. return EOF;
  143. }
  144. else
  145. {
  146. return (int) ch;
  147. }
  148. }
  149. int screensize(int *tt_rows, int *tt_cols)
  150. {
  151. QLRECT_t rect;
  152. if(0 == sd_chenq(getchid(1), -1, &rect))
  153. {
  154. if(tt_cols)
  155. *tt_cols = rect.q_width;
  156. if(tt_rows)
  157. *tt_rows = rect.q_height;
  158. }
  159. else
  160. {
  161. if(tt_cols)
  162. *tt_cols = 80;
  163. if(tt_rows)
  164. *tt_rows = 24;
  165. }
  166. return 0;
  167. }
  168. #ifndef SFX
  169. char *LastDir(char *ws)
  170. {
  171. char *p;
  172. char *q = ws;
  173. struct stat s;
  174. for(p = ws; *p; p++)
  175. {
  176. if(*p == '_')
  177. {
  178. char c;
  179. p++;
  180. c = *p;
  181. *p = 0;
  182. if(stat(ws, &s) == 0 && S_ISDIR(s.st_mode))
  183. {
  184. q = p;
  185. }
  186. *p = c;
  187. }
  188. }
  189. return q;
  190. }
  191. /**********************/
  192. /* Function do_wild() */ /* for porting: dir separator; match(ignore_case) */
  193. /**********************/
  194. char *do_wild(__G__ wildspec)
  195. __GDEF
  196. ZCONST char *wildspec; /* only used first time on a given dir */
  197. {
  198. static DIR *wild_dir = (DIR *)NULL;
  199. static ZCONST char *wildname;
  200. static char *dirname, matchname[FILNAMSIZ];
  201. static int notfirstcall=FALSE, have_dirname, dirnamelen;
  202. struct dirent *file;
  203. char basedir[40];
  204. /* Even when we're just returning wildspec, we *always* do so in
  205. * matchname[]--calling routine is allowed to append four characters
  206. * to the returned string, and wildspec may be a pointer to argv[].
  207. */
  208. if (!notfirstcall) { /* first call: must initialize everything */
  209. char *ws = NULL, *us = NULL;
  210. notfirstcall = TRUE;
  211. /* break the wildspec into a directory part and a wildcard filename */
  212. ws = (char *) iswild(wildspec);
  213. if(ws == NULL)
  214. {
  215. strncpy(matchname, wildspec, FILNAMSIZ);
  216. matchname[FILNAMSIZ-1] = '\0';
  217. return matchname;
  218. }
  219. us = LastDir(wildspec);
  220. if(us == wildspec)
  221. {
  222. dirname = basedir;
  223. getcwd(basedir, sizeof(basedir)-1);
  224. dirnamelen = strlen(basedir);
  225. have_dirname = FALSE;
  226. wildname = wildspec;
  227. } else {
  228. wildname = us; /* point at character after '/' */
  229. dirnamelen = wildname - wildspec;
  230. if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  231. Info(slide, 0x201, ((char *)slide,
  232. "warning: cannot allocate wildcard buffers\n"));
  233. strncpy(matchname, wildspec, FILNAMSIZ);
  234. matchname[FILNAMSIZ-1] = '\0';
  235. return matchname; /* but maybe filespec was not a wildcard */
  236. }
  237. strncpy(dirname, wildspec, dirnamelen);
  238. dirname[dirnamelen] = '\0'; /* terminate for strcpy below */
  239. have_dirname = TRUE;
  240. }
  241. if ((wild_dir = opendir(dirname)) != (DIR *)NULL) {
  242. while ((file = readdir(wild_dir)) != (struct dirent *)NULL) {
  243. if (match(file->d_name, wildname, 0 WISEP)) { /* 0=case sens.*/
  244. if (have_dirname) {
  245. strcpy(matchname, dirname);
  246. strcpy(matchname+dirnamelen, file->d_name);
  247. } else
  248. strcpy(matchname, file->d_name);
  249. return matchname;
  250. }
  251. }
  252. /* if we get to here directory is exhausted, so close it */
  253. closedir(wild_dir);
  254. wild_dir = (DIR *)NULL;
  255. }
  256. /* return the raw wildspec in case that works (e.g., directory not
  257. * searchable, but filespec was not wild and file is readable) */
  258. strncpy(matchname, wildspec, FILNAMSIZ);
  259. matchname[FILNAMSIZ-1] = '\0';
  260. return matchname;
  261. }
  262. /* last time through, might have failed opendir but returned raw wildspec */
  263. if (wild_dir == (DIR *)NULL) {
  264. notfirstcall = FALSE; /* nothing left to try--reset for new wildspec */
  265. if (have_dirname)
  266. free(dirname);
  267. return (char *)NULL;
  268. }
  269. /* If we've gotten this far, we've read and matched at least one entry
  270. * successfully (in a previous call), so dirname has been copied into
  271. * matchname already.
  272. */
  273. while ((file = readdir(wild_dir)) != (struct dirent *)NULL) {
  274. if (match(file->d_name, wildname, 0 WISEP)) { /* 0 == case sens. */
  275. if (have_dirname) {
  276. /* strcpy(matchname, dirname); */
  277. strcpy(matchname+dirnamelen, file->d_name);
  278. } else
  279. strcpy(matchname, file->d_name);
  280. return matchname;
  281. }
  282. }
  283. closedir(wild_dir); /* have read at least one entry; nothing left */
  284. wild_dir = (DIR *)NULL;
  285. notfirstcall = FALSE; /* reset for new wildspec */
  286. if (have_dirname)
  287. free(dirname);
  288. return (char *)NULL;
  289. } /* end function do_wild() */
  290. #endif /* !SFX */
  291. /**********************/
  292. /* Function mapattr() */
  293. /**********************/
  294. int mapattr(__G)
  295. __GDEF
  296. {
  297. ulg tmp = G.crec.external_file_attributes;
  298. switch (G.pInfo->hostnum) {
  299. case AMIGA_:
  300. tmp = (unsigned)(tmp>>17 & 7); /* Amiga RWE bits */
  301. G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  302. break;
  303. case THEOS_:
  304. tmp &= 0xF1FFFFFFL;
  305. if ((tmp & 0xF0000000L) != 0x40000000L)
  306. tmp &= 0x01FFFFFFL; /* not a dir, mask all ftype bits */
  307. else
  308. tmp &= 0x41FFFFFFL; /* leave directory bit as set */
  309. /* fall through! */
  310. case QDOS_:
  311. case UNIX_:
  312. case VMS_:
  313. case ACORN_:
  314. case ATARI_:
  315. case ATHEOS_:
  316. case BEOS_:
  317. case TANDEM_:
  318. G.pInfo->file_attr = (unsigned)(tmp >> 16);
  319. if (G.pInfo->file_attr != 0 || !G.extra_field) {
  320. return 0;
  321. } else {
  322. /* Some (non-Info-ZIP) implementations of Zip for Unix and
  323. VMS (and probably others ??) leave 0 in the upper 16-bit
  324. part of the external_file_attributes field. Instead, they
  325. store file permission attributes in some extra field.
  326. As a work-around, we search for the presence of one of
  327. these extra fields and fall back to the MSDOS compatible
  328. part of external_file_attributes if one of the known
  329. e.f. types has been detected.
  330. Later, we might implement extraction of the permission
  331. bits from the VMS extra field. But for now, the work-around
  332. should be sufficient to provide "readable" extracted files.
  333. (For ASI Unix e.f., an experimental remap of the e.f.
  334. mode value IS already provided!)
  335. */
  336. ush ebID;
  337. unsigned ebLen;
  338. uch *ef = G.extra_field;
  339. unsigned ef_len = G.crec.extra_field_length;
  340. int r = FALSE;
  341. while (!r && ef_len >= EB_HEADSIZE) {
  342. ebID = makeword(ef);
  343. ebLen = (unsigned)makeword(ef+EB_LEN);
  344. if (ebLen > (ef_len - EB_HEADSIZE))
  345. /* discoverd some e.f. inconsistency! */
  346. break;
  347. switch (ebID) {
  348. case EF_ASIUNIX:
  349. if (ebLen >= (EB_ASI_MODE+2)) {
  350. G.pInfo->file_attr =
  351. (unsigned)makeword(ef+(EB_HEADSIZE+EB_ASI_MODE));
  352. /* force stop of loop: */
  353. ef_len = (ebLen + EB_HEADSIZE);
  354. break;
  355. }
  356. /* else: fall through! */
  357. case EF_PKVMS:
  358. /* "found nondecypherable e.f. with perm. attr" */
  359. r = TRUE;
  360. default:
  361. break;
  362. }
  363. ef_len -= (ebLen + EB_HEADSIZE);
  364. ef += (ebLen + EB_HEADSIZE);
  365. }
  366. if (!r)
  367. return 0;
  368. }
  369. /* fall through! */
  370. /* all remaining cases: expand MSDOS read-only bit into write perms */
  371. case FS_FAT_:
  372. /* PKWARE's PKZip for Unix marks entries as FS_FAT_, but stores the
  373. * Unix attributes in the upper 16 bits of the external attributes
  374. * field, just like Info-ZIP's Zip for Unix. We try to use that
  375. * value, after a check for consistency with the MSDOS attribute
  376. * bits (see below).
  377. */
  378. G.pInfo->file_attr = (unsigned)(tmp >> 16);
  379. /* fall through! */
  380. case FS_HPFS_:
  381. case FS_NTFS_:
  382. case MAC_:
  383. case TOPS20_:
  384. default:
  385. /* Ensure that DOS subdir bit is set when the entry's name ends
  386. * in a '/'. Some third-party Zip programs fail to set the subdir
  387. * bit for directory entries.
  388. */
  389. if ((tmp & 0x10) == 0) {
  390. extent fnlen = strlen(G.filename);
  391. if (fnlen > 0 && G.filename[fnlen-1] == '/')
  392. tmp |= 0x10;
  393. }
  394. /* read-only bit --> write perms; subdir bit --> dir exec bit */
  395. tmp = !(tmp & 1) << 1 | (tmp & 0x10) >> 4;
  396. if ((G.pInfo->file_attr & 0700) == (unsigned)(0400 | tmp<<6))
  397. /* keep previous G.pInfo->file_attr setting, when its "owner"
  398. * part appears to be consistent with DOS attribute flags!
  399. */
  400. return 0;
  401. G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  402. break;
  403. } /* end switch (host-OS-created-by) */
  404. /* for originating systems with no concept of "group," "other," "system": */
  405. umask( (int)(tmp=umask(0)) ); /* apply mask to expanded r/w(/x) perms */
  406. G.pInfo->file_attr &= ~tmp;
  407. return 0;
  408. } /* end function mapattr() */
  409. /************************/
  410. /* Function mapname() */
  411. /************************/
  412. int mapname(__G__ renamed)
  413. __GDEF
  414. int renamed;
  415. /*
  416. * returns:
  417. * MPN_OK - no problem detected
  418. * MPN_INF_TRUNC - caution (truncated filename)
  419. * MPN_INF_SKIP - info "skip entry" (dir doesn't exist)
  420. * MPN_ERR_SKIP - error -> skip entry
  421. * MPN_ERR_TOOLONG - error -> path is too long
  422. * MPN_NOMEM - error (memory allocation failed) -> skip entry
  423. * [also MPN_VOL_LABEL, MPN_CREATED_DIR]
  424. */
  425. {
  426. char pathcomp[FILNAMSIZ]; /* path-component buffer */
  427. char *pp, *cp=(char *)NULL; /* character pointers */
  428. char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  429. int killed_ddot = FALSE; /* is set when skipping "../" pathcomp */
  430. int error = MPN_OK;
  431. register unsigned workch; /* hold the character being tested */
  432. /*---------------------------------------------------------------------------
  433. Initialize various pointers and counters and stuff.
  434. ---------------------------------------------------------------------------*/
  435. if (G.pInfo->vollabel)
  436. return MPN_VOL_LABEL; /* can't set disk volume labels in SMS/QDOS */
  437. /* can create path as long as not just freshening, or if user told us */
  438. G.create_dirs = (!uO.fflag || renamed);
  439. created_dir = FALSE; /* not yet */
  440. /* user gave full pathname: don't prepend rootpath */
  441. renamed_fullpath = (renamed && (*G.filename == '/'));
  442. if (checkdir(__G__ (char *)NULL, INIT) == MPN_NOMEM)
  443. return MPN_NOMEM; /* initialize path buffer, unless no memory */
  444. *pathcomp = '\0'; /* initialize translation buffer */
  445. pp = pathcomp; /* point to translation buffer */
  446. if (uO.jflag) /* junking directories */
  447. cp = (char *)strrchr(G.filename, '/');
  448. if (cp == (char *)NULL) /* no '/' or not junking dirs */
  449. cp = G.filename; /* point to internal zipfile-member pathname */
  450. else
  451. ++cp; /* point to start of last component of path */
  452. /*---------------------------------------------------------------------------
  453. Begin main loop through characters in filename.
  454. ---------------------------------------------------------------------------*/
  455. while ((workch = (uch)*cp++) != 0) {
  456. switch (workch) {
  457. case '/': /* can assume -j flag not given */
  458. *pp = '\0';
  459. if (((error = checkdir(__G__ pathcomp, APPEND_DIR))
  460. & MPN_MASK) > MPN_INF_TRUNC)
  461. return error;
  462. pp = pathcomp; /* reset conversion buffer for next piece */
  463. lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
  464. break;
  465. case '.':
  466. if (pp == pathcomp) { /* nothing appended yet... */
  467. if (*cp == '/') { /* don't bother appending "./" to */
  468. ++cp; /* the path: skip behind the '/' */
  469. break;
  470. } else if (!uO.ddotflag && *cp == '.' && cp[1] == '/') {
  471. /* "../" dir traversal detected */
  472. cp += 2; /* skip over behind the '/' */
  473. killed_ddot = TRUE; /* set "show message" flag */
  474. break;
  475. }
  476. }
  477. *pp++ = (((qlflag & 1) == 0) ? '_' : '.');
  478. break;
  479. case ';': /* VMS version (or DEC-20 attrib?) */
  480. lastsemi = pp;
  481. *pp++ = ';'; /* keep for now; remove VMS ";##" */
  482. break; /* later, if requested */
  483. default:
  484. /* allow European characters in filenames: */
  485. if (isprint(workch) || (128 <= workch && workch <= 254))
  486. *pp++ = (char)workch;
  487. } /* end switch */
  488. } /* end while loop */
  489. /* Show warning when stripping insecure "parent dir" path components */
  490. if (killed_ddot && QCOND2) {
  491. Info(slide, 0, ((char *)slide,
  492. "warning: skipped \"../\" path component(s) in %s\n",
  493. FnFilter1(G.filename)));
  494. if (!(error & ~MPN_MASK))
  495. error = (error & MPN_MASK) | PK_WARN;
  496. }
  497. /*---------------------------------------------------------------------------
  498. Report if directory was created (and no file to create: filename ended
  499. in '/'), check name to be sure it exists, and combine path and name be-
  500. fore exiting.
  501. ---------------------------------------------------------------------------*/
  502. if (G.filename[strlen(G.filename) - 1] == '/') {
  503. G.filename[strlen(G.filename) - 1] = '_';
  504. checkdir(__G__ G.filename, GETPATH);
  505. if (created_dir) {
  506. if (QCOND2) {
  507. Info(slide, 0, ((char *)slide, " creating: %s\n",
  508. FnFilter1(G.filename)));
  509. }
  510. /* set dir time (note trailing '/') */
  511. return (error & ~MPN_MASK) | MPN_CREATED_DIR;
  512. }
  513. /* dir existed already; don't look for data to extract */
  514. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  515. }
  516. *pp = '\0'; /* done with pathcomp: terminate it */
  517. /* if not saving them, remove VMS version numbers (appended ";###") */
  518. if (!uO.V_flag && lastsemi) {
  519. pp = lastsemi + 1;
  520. while (isdigit((uch)(*pp)))
  521. ++pp;
  522. if (*pp == '\0') /* only digits between ';' and end: nuke */
  523. *lastsemi = '\0';
  524. }
  525. if (*pathcomp == '\0') {
  526. Info(slide, 1, ((char *)slide, "mapname: conversion of %s failed\n",
  527. FnFilter1(G.filename)));
  528. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  529. }
  530. checkdir(__G__ pathcomp, APPEND_NAME); /* returns 1 if truncated: care? */
  531. checkdir(__G__ G.filename, GETPATH);
  532. return error;
  533. } /* end function mapname() */
  534. /***********************/
  535. /* Function checkdir() */
  536. /***********************/
  537. int checkdir(__G__ pathcomp, flag)
  538. __GDEF
  539. char *pathcomp;
  540. int flag;
  541. /*
  542. * returns:
  543. * MPN_OK - no problem detected
  544. * MPN_INF_TRUNC - (on APPEND_NAME) truncated filename
  545. * MPN_INF_SKIP - path doesn't exist, not allowed to create
  546. * MPN_ERR_SKIP - path doesn't exist, tried to create and failed; or path
  547. * exists and is not a directory, but is supposed to be
  548. * MPN_ERR_TOOLONG - path is too long
  549. * MPN_NOMEM - can't allocate memory for filename buffers
  550. */
  551. {
  552. static int rootlen = 0; /* length of rootpath */
  553. static char *rootpath; /* user's "extract-to" directory */
  554. static char *buildpath; /* full path (so far) to extracted file */
  555. static char *end; /* pointer to end of buildpath ('\0') */
  556. # define FN_MASK 7
  557. # define FUNCTION (flag & FN_MASK)
  558. /*---------------------------------------------------------------------------
  559. APPEND_DIR: append the path component to the path being built and check
  560. for its existence. If doesn't exist and we are creating directories, do
  561. so for this one; else signal success or error as appropriate.
  562. ---------------------------------------------------------------------------*/
  563. if (FUNCTION == APPEND_DIR) {
  564. int too_long = FALSE;
  565. #ifdef SHORT_NAMES
  566. char *old_end = end;
  567. #endif
  568. Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
  569. while ((*end = *pathcomp++) != '\0')
  570. ++end;
  571. #ifdef SHORT_NAMES /* path components restricted to 14 chars, typically */
  572. if ((end-old_end) > FILENAME_MAX) /* GRR: proper constant? */
  573. *(end = old_end + FILENAME_MAX) = '\0';
  574. #endif
  575. /* GRR: could do better check, see if overrunning buffer as we go:
  576. * check end-buildpath after each append, set warning variable if
  577. * within 20 of FILNAMSIZ; then if var set, do careful check when
  578. * appending. Clear variable when begin new path. */
  579. if ((end-buildpath) > FILNAMSIZ-2) /* need '/', one-char name, '\0' */
  580. too_long = TRUE; /* check if extracting directory? */
  581. if (stat(buildpath, &G.statbuf)) { /* path doesn't exist */
  582. if (!G.create_dirs) { /* told not to create (freshening) */
  583. free(buildpath);
  584. return MPN_INF_SKIP; /* path doesn't exist: nothing to do */
  585. }
  586. if (too_long) {
  587. Info(slide, 1, ((char *)slide,
  588. "checkdir error: path too long: %s\n",
  589. FnFilter1(buildpath)));
  590. free(buildpath);
  591. /* no room for filenames: fatal */
  592. return MPN_ERR_TOOLONG;
  593. }
  594. if (mkdir(buildpath, 0777) == -1) { /* create the directory */
  595. Info(slide, 1, ((char *)slide,
  596. "checkdir error: cannot create %s\n\
  597. unable to process %s.\n",
  598. FnFilter2(buildpath), FnFilter1(G.filename)));
  599. free(buildpath);
  600. /* path didn't exist, tried to create, failed */
  601. return MPN_ERR_SKIP;
  602. }
  603. created_dir = TRUE;
  604. } else if (!S_ISDIR(G.statbuf.st_mode)) {
  605. Info(slide, 1, ((char *)slide,
  606. "checkdir error: %s exists but is not directory\n\
  607. unable to process %s.\n",
  608. FnFilter2(buildpath), FnFilter1(G.filename)));
  609. free(buildpath);
  610. /* path existed but wasn't dir */
  611. return MPN_ERR_SKIP;
  612. }
  613. if (too_long) {
  614. Info(slide, 1, ((char *)slide,
  615. "checkdir error: path too long: %s\n", FnFilter1(buildpath)));
  616. free(buildpath);
  617. /* no room for filenames: fatal */
  618. return MPN_ERR_TOOLONG;
  619. }
  620. *end++ = '_';
  621. *end = '\0';
  622. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  623. return MPN_OK;
  624. } /* end if (FUNCTION == APPEND_DIR) */
  625. /*---------------------------------------------------------------------------
  626. GETPATH: copy full path to the string pointed at by pathcomp, and free
  627. buildpath.
  628. ---------------------------------------------------------------------------*/
  629. if (FUNCTION == GETPATH) {
  630. strcpy(pathcomp, buildpath);
  631. Trace((stderr, "getting and freeing path [%s]\n",
  632. FnFilter1(pathcomp)));
  633. free(buildpath);
  634. buildpath = end = (char *)NULL;
  635. return MPN_OK;
  636. }
  637. /*---------------------------------------------------------------------------
  638. APPEND_NAME: assume the path component is the filename; append it and
  639. return without checking for existence.
  640. ---------------------------------------------------------------------------*/
  641. if (FUNCTION == APPEND_NAME) {
  642. #ifdef SHORT_NAMES
  643. char *old_end = end;
  644. #endif
  645. short dlen;
  646. Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  647. while ((*end = *pathcomp++) != '\0') {
  648. ++end;
  649. #ifdef SHORT_NAMES /* truncate name at 14 characters, typically */
  650. if ((end-old_end) > FILENAME_MAX) /* GRR: proper constant? */
  651. *(end = old_end + FILENAME_MAX) = '\0';
  652. #endif
  653. if (isdirdev(buildpath))
  654. {
  655. dlen = 5;
  656. }
  657. else
  658. {
  659. dlen = 0;
  660. }
  661. if ((end-buildpath-dlen) >= FILNAMSIZ) {
  662. *--end = '\0';
  663. Info(slide, 0x201, ((char *)slide,
  664. "checkdir warning: path too long; truncating\n\
  665. %s\n -> %s\n",
  666. FnFilter1(G.filename), FnFilter2(buildpath)));
  667. return MPN_INF_TRUNC; /* filename truncated */
  668. }
  669. }
  670. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath)));
  671. /* could check for existence here, prompt for new name... */
  672. return MPN_OK;
  673. }
  674. /*---------------------------------------------------------------------------
  675. INIT: allocate and initialize buffer space for the file currently being
  676. extracted. If file was renamed with an absolute path, don't prepend the
  677. extract-to path.
  678. ---------------------------------------------------------------------------*/
  679. /* GRR: for VMS and TOPS-20, add up to 13 to strlen */
  680. if (FUNCTION == INIT) {
  681. Trace((stderr, "initializing buildpath to "));
  682. if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+1))
  683. == (char *)NULL)
  684. return MPN_NOMEM;
  685. if ((rootlen > 0) && !renamed_fullpath) {
  686. strcpy(buildpath, rootpath);
  687. end = buildpath + rootlen;
  688. } else {
  689. *buildpath = '\0';
  690. end = buildpath;
  691. }
  692. Trace((stderr, "[%s]\n", FnFilter1(buildpath)));
  693. return MPN_OK;
  694. }
  695. /*---------------------------------------------------------------------------
  696. ROOT: if appropriate, store the path in rootpath and create it if
  697. necessary; else assume it's a zipfile member and return. This path
  698. segment gets used in extracting all members from every zipfile specified
  699. on the command line.
  700. ---------------------------------------------------------------------------*/
  701. #if (!defined(SFX) || defined(SFX_EXDIR))
  702. if (FUNCTION == ROOT) {
  703. Trace((stderr, "initializing root path to [%s]\n",
  704. FnFilter1(pathcomp)));
  705. if (pathcomp == (char *)NULL) {
  706. rootlen = 0;
  707. return MPN_OK;
  708. }
  709. if (rootlen > 0) /* rootpath was already set, nothing to do */
  710. return MPN_OK;
  711. if ((rootlen = strlen(pathcomp)) > 0) {
  712. char *tmproot;
  713. if ((tmproot = (char *)malloc(rootlen+2)) == (char *)NULL) {
  714. rootlen = 0;
  715. return MPN_NOMEM;
  716. }
  717. strcpy(tmproot, pathcomp);
  718. if ((stat(tmproot, &G.statbuf) ||
  719. !S_ISDIR(G.statbuf.st_mode)))
  720. { /* path does not exist */
  721. if (!G.create_dirs /* || iswild(tmproot) */ ) {
  722. free(tmproot);
  723. rootlen = 0;
  724. /* skip (or treat as stored file) */
  725. return MPN_INF_SKIP;
  726. }
  727. /* create the directory (could add loop here scanning tmproot
  728. * to create more than one level, but why really necessary?) */
  729. if (mkdir(tmproot, 0777) == -1) {
  730. Info(slide, 1, ((char *)slide,
  731. "checkdir: cannot create extraction directory: %s\n",
  732. FnFilter1(tmproot)));
  733. free(tmproot);
  734. rootlen = 0;
  735. /* path didn't exist, tried to create, and failed: */
  736. /* file exists, or 2+ subdir levels required */
  737. return MPN_ERR_SKIP;
  738. }
  739. }
  740. if (tmproot[rootlen-1] == '/' || tmproot[rootlen-1] == '_') {
  741. tmproot[--rootlen] = '\0';
  742. }
  743. tmproot[rootlen++] = '_';
  744. tmproot[rootlen] = '\0';
  745. if ((rootpath = (char *)realloc(tmproot, rootlen+1)) == NULL) {
  746. free(tmproot);
  747. rootlen = 0;
  748. return MPN_NOMEM;
  749. }
  750. }
  751. Trace((stderr, "rootpath now = [%s]\n", FnFilter1(rootpath)));
  752. return MPN_OK;
  753. }
  754. #endif /* !SFX || SFX_EXDIR */
  755. /*---------------------------------------------------------------------------
  756. END: free rootpath, immediately prior to program exit.
  757. ---------------------------------------------------------------------------*/
  758. if (FUNCTION == END) {
  759. Trace((stderr, "freeing rootpath\n"));
  760. if (rootlen > 0) {
  761. free(rootpath);
  762. rootlen = 0;
  763. }
  764. return MPN_OK;
  765. }
  766. return MPN_INVALID; /* should never reach */
  767. } /* end function checkdir() */
  768. static void qfix(__G__ ef_ptr, ef_len)
  769. __GDEF
  770. uch *ef_ptr;
  771. unsigned ef_len;
  772. {
  773. qdosextra qextra;
  774. while (ef_len >= EB_HEADSIZE)
  775. {
  776. qdosextra *extra = &qextra;
  777. jbextra *jbp = (jbextra *)&qextra;
  778. unsigned eb_len = makeword(EB_LEN + ef_ptr);
  779. if (eb_len > (ef_len - EB_HEADSIZE)) {
  780. /* discovered some extra field inconsistency! */
  781. Trace((stderr,
  782. "qfix: block length %u > rest ef_size %u\n", eb_len,
  783. ef_len - EB_HEADSIZE));
  784. break;
  785. }
  786. /* Must ensure that we don't use ODD addresses here */
  787. memcpy(&qextra, ef_ptr, sizeof(qdosextra));
  788. switch (extra->shortid) {
  789. case SHORTID:
  790. if (!strncmp(extra->longid, LONGID, strlen(LONGID)))
  791. {
  792. if (eb_len != EXTRALEN)
  793. fputs("warning: invalid length in Qdos field", stderr);
  794. if (extra->header.d_type)
  795. {
  796. fs_heads(fgetchid(G.outfile), (timeout_t)-1,
  797. &extra->header, 14);
  798. G.pInfo->file_attr |= S_IXUSR;
  799. }
  800. }
  801. if (!strncmp(jbp->longid, JBLONGID, strlen(JBLONGID)))
  802. {
  803. if (eb_len != JBEXTRALEN)
  804. fputs("warning: invalid length in QZ field", stderr);
  805. if (jbp->header.d_type)
  806. {
  807. fs_heads(fgetchid(G.outfile), (timeout_t)-1,
  808. &jbp->header, 14);
  809. G.pInfo->file_attr |= S_IXUSR;
  810. }
  811. }
  812. break;
  813. default:
  814. Trace((stderr,"qfix: unknown extra field block, ID=%d\n",
  815. extra->shortid));
  816. break;
  817. }
  818. /* Skip this extra field block */
  819. ef_ptr += (eb_len + EB_HEADSIZE);
  820. ef_len -= (eb_len + EB_HEADSIZE);
  821. }
  822. }
  823. #ifdef QDOS
  824. # include <utime.h>
  825. long timezone = 0;
  826. #endif
  827. /****************************/
  828. /* Function close_outfile() */
  829. /****************************/
  830. void close_outfile(__G)
  831. __GDEF
  832. {
  833. union {
  834. iztimes t3; /* mtime, atime, ctime */
  835. struct utimbuf t2; /* modtime, actime */
  836. } zt;
  837. #ifdef USE_EF_UT_TIME
  838. unsigned eb_izux_flg;
  839. #endif
  840. if (G.extra_field) {
  841. qfix(__G__ G.extra_field, G.lrec.extra_field_length);
  842. }
  843. fclose(G.outfile);
  844. /*---------------------------------------------------------------------------
  845. Change the file permissions from default ones to those stored in the
  846. zipfile.
  847. ---------------------------------------------------------------------------*/
  848. #ifndef NO_CHMOD
  849. if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
  850. perror("chmod (file attributes) error");
  851. #endif
  852. /*---------------------------------------------------------------------------
  853. Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  854. time: adjust base year from 1980 to 1970, do usual conversions from
  855. yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  856. light savings time differences. If we have a Unix extra field, however,
  857. we're laughing: both mtime and atime are ours.
  858. ---------------------------------------------------------------------------*/
  859. /* skip restoring time stamps on user's request */
  860. if (uO.D_flag <= 1) {
  861. #ifdef USE_EF_UT_TIME
  862. eb_izux_flg = (G.extra_field ? ef_scan_for_izux(G.extra_field,
  863. G.lrec.extra_field_length, 0,
  864. G.lrec.last_mod_dos_datetime,
  865. #ifdef IZ_CHECK_TZ
  866. (G.tz_is_valid ? &(zt.t3) : NULL),
  867. #else
  868. &(zt.t3),
  869. #endif
  870. NULL) : 0);
  871. if (eb_izux_flg & EB_UT_FL_MTIME) {
  872. TTrace((stderr,
  873. "\nclose_outfile: Unix e.f. modif. time = %ld\n",
  874. zt.t3.mtime));
  875. } else {
  876. zt.t3.mtime = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
  877. }
  878. if (eb_izux_flg & EB_UT_FL_ATIME) {
  879. TTrace((stderr,
  880. "close_outfile: Unix e.f. access time = %ld\n",
  881. zt.t3.atime));
  882. } else {
  883. zt.t3.atime = zt.t3.mtime;
  884. TTrace((stderr,
  885. "\nclose_outfile: modification/access times = %ld\n",
  886. zt.t3.mtime));
  887. }
  888. #else
  889. zt.t3.atime = zt.t3.mtime
  890. = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
  891. #endif
  892. /* set the file's access and modification times */
  893. if (utime(G.filename, &(zt.t2))) {
  894. Info(slide, 0x201, ((char *)slide,
  895. "warning: cannot set the time for %s\n",
  896. FnFilter1(G.filename)));
  897. }
  898. }
  899. } /* end function close_outfile() */
  900. #ifdef TIMESTAMP
  901. /***************************/
  902. /* Function stamp_file() */
  903. /***************************/
  904. int stamp_file(fname, modtime)
  905. ZCONST char *fname;
  906. time_t modtime;
  907. {
  908. struct utimbuf tp;
  909. tp.modtime = tp.actime = modtime;
  910. return (utime(fname, &tp));
  911. } /* end function stamp_file() */
  912. #endif /* TIMESTAMP */
  913. #ifndef SFX
  914. /************************/
  915. /* Function version() */
  916. /************************/
  917. void version(__G)
  918. __GDEF
  919. {
  920. sprintf((char *)slide, LoadFarString(CompiledWith),
  921. "c68", " v4.2x", "SMS/QDOS",
  922. " on ", __DATE__, "","");
  923. (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
  924. } /* end function version() */
  925. #endif /* !SFX */
  926. #endif /* !FUNZIP */
  927. #if CRYPT
  928. char *getp(__G__ m, p, n)
  929. __GDEF
  930. const char *m; /* prompt for password */
  931. char *p; /* return value: line input */
  932. int n; /* bytes available in p[] */
  933. {
  934. int c; /* one-byte buffer for read() to use */
  935. int i; /* number of characters input */
  936. char *w; /* warning on retry */
  937. /* get password */
  938. w = "";
  939. sd_cure(getchid(0), -1); /* enable cursor */
  940. do {
  941. fputs(w, stderr); /* warning if back again */
  942. fputs(m, stderr); /* display prompt and flush */
  943. fflush(stderr);
  944. i = 0;
  945. do {
  946. c = getch();
  947. if (c == 0xc2) {
  948. if (i > 0) {
  949. i--; /* the `del' keys works */
  950. fputs("\b \b", stderr);
  951. }
  952. }
  953. else if (i < n) {
  954. p[i++] = c; /* truncate past n */
  955. if(c != '\n') putc('*', stderr);
  956. }
  957. } while (c != '\n');
  958. putc('\n', stderr); fflush(stderr);
  959. w = "(line too long--try again)\n";
  960. } while (p[i-1] != '\n');
  961. p[i-1] = 0; /* terminate at newline */
  962. sd_curs(getchid(0), -1); /* suppress cursor */
  963. return p; /* return pointer to password */
  964. } /* end function getp() */
  965. #endif /* CRYPT */