list.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. Copyright (c) 1990-2009 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2009-Jan-02 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. list.c
  10. This file contains the non-ZipInfo-specific listing routines for UnZip.
  11. Contains: list_files()
  12. get_time_stamp() [optional feature]
  13. ratio()
  14. fnprint()
  15. ---------------------------------------------------------------------------*/
  16. #define UNZIP_INTERNAL
  17. #include "unzip.h"
  18. #ifdef WINDLL
  19. # ifdef POCKET_UNZIP
  20. # include "wince/intrface.h"
  21. # else
  22. # include "windll/windll.h"
  23. # endif
  24. #endif
  25. #ifdef TIMESTAMP
  26. static int fn_is_dir OF((__GPRO));
  27. #endif
  28. #ifndef WINDLL
  29. static ZCONST char Far CompFactorStr[] = "%c%d%%";
  30. static ZCONST char Far CompFactor100[] = "100%%";
  31. #ifdef OS2_EAS
  32. static ZCONST char Far HeadersS[] =
  33. " Length EAs ACLs Date Time Name";
  34. static ZCONST char Far HeadersS1[] =
  35. "--------- --- ---- ---------- ----- ----";
  36. #else
  37. static ZCONST char Far HeadersS[] =
  38. " Length Date Time Name";
  39. static ZCONST char Far HeadersS1[] =
  40. "--------- ---------- ----- ----";
  41. #endif
  42. static ZCONST char Far HeadersL[] =
  43. " Length Method Size Cmpr Date Time CRC-32 Name";
  44. static ZCONST char Far HeadersL1[] =
  45. "-------- ------ ------- ---- ---------- ----- -------- ----";
  46. static ZCONST char Far *Headers[][2] =
  47. { {HeadersS, HeadersS1}, {HeadersL, HeadersL1} };
  48. static ZCONST char Far CaseConversion[] =
  49. "%s (\"^\" ==> case\n%s conversion)\n";
  50. static ZCONST char Far LongHdrStats[] =
  51. "%s %-7s%s %4s %02u%c%02u%c%02u %02u:%02u %08lx %c";
  52. static ZCONST char Far LongFileTrailer[] =
  53. "-------- ------- --- \
  54. -------\n%s %s %4s %lu file%s\n";
  55. #ifdef OS2_EAS
  56. static ZCONST char Far ShortHdrStats[] =
  57. "%s %6lu %6lu %02u%c%02u%c%02u %02u:%02u %c";
  58. static ZCONST char Far ShortFileTrailer[] =
  59. "--------- ----- ----- \
  60. -------\n%s %6lu %6lu %lu file%s\n";
  61. static ZCONST char Far OS2ExtAttrTrailer[] =
  62. "%lu file%s %lu bytes of OS/2 extended attributes attached.\n";
  63. static ZCONST char Far OS2ACLTrailer[] =
  64. "%lu file%s %lu bytes of access control lists attached.\n";
  65. #else
  66. static ZCONST char Far ShortHdrStats[] =
  67. "%s %02u%c%02u%c%02u %02u:%02u %c";
  68. static ZCONST char Far ShortFileTrailer[] =
  69. "--------- -------\n%s\
  70. %lu file%s\n";
  71. #endif /* ?OS2_EAS */
  72. #endif /* !WINDLL */
  73. /*************************/
  74. /* Function list_files() */
  75. /*************************/
  76. int list_files(__G) /* return PK-type error code */
  77. __GDEF
  78. {
  79. int do_this_file=FALSE, cfactor, error, error_in_archive=PK_COOL;
  80. #ifndef WINDLL
  81. char sgn, cfactorstr[10];
  82. int longhdr=(uO.vflag>1);
  83. #endif
  84. int date_format;
  85. char dt_sepchar;
  86. ulg members=0L;
  87. zusz_t j;
  88. unsigned methnum;
  89. #ifdef USE_EF_UT_TIME
  90. iztimes z_utime;
  91. struct tm *t;
  92. #endif
  93. unsigned yr, mo, dy, hh, mm;
  94. zusz_t csiz, tot_csize=0L, tot_ucsize=0L;
  95. #ifdef OS2_EAS
  96. ulg ea_size, tot_easize=0L, tot_eafiles=0L;
  97. ulg acl_size, tot_aclsize=0L, tot_aclfiles=0L;
  98. #endif
  99. min_info info;
  100. char methbuf[8];
  101. static ZCONST char dtype[]="NXFS"; /* see zi_short() */
  102. static ZCONST char Far method[NUM_METHODS+1][8] =
  103. {"Stored", "Shrunk", "Reduce1", "Reduce2", "Reduce3", "Reduce4",
  104. "Implode", "Token", "Defl:#", "Def64#", "ImplDCL", "BZip2",
  105. "LZMA", "Terse", "IBMLZ77", "WavPack", "PPMd", "Unk:###"};
  106. /*---------------------------------------------------------------------------
  107. Unlike extract_or_test_files(), this routine confines itself to the cen-
  108. tral directory. Thus its structure is somewhat simpler, since we can do
  109. just a single loop through the entire directory, listing files as we go.
  110. So to start off, print the heading line and then begin main loop through
  111. the central directory. The results will look vaguely like the following:
  112. Length Method Size Ratio Date Time CRC-32 Name ("^" ==> case
  113. -------- ------ ------- ----- ---- ---- ------ ---- conversion)
  114. 44004 Implode 13041 71% 11-02-89 19:34 8b4207f7 Makefile.UNIX
  115. 3438 Shrunk 2209 36% 09-15-90 14:07 a2394fd8 ^dos-file.ext
  116. 16717 Defl:X 5252 69% 11-03-97 06:40 1ce0f189 WHERE
  117. -------- ------- --- -------
  118. 64159 20502 68% 3 files
  119. ---------------------------------------------------------------------------*/
  120. G.pInfo = &info;
  121. date_format = DATE_FORMAT;
  122. dt_sepchar = DATE_SEPCHAR;
  123. #ifndef WINDLL
  124. if (uO.qflag < 2) {
  125. if (uO.L_flag)
  126. Info(slide, 0, ((char *)slide, LoadFarString(CaseConversion),
  127. LoadFarStringSmall(Headers[longhdr][0]),
  128. LoadFarStringSmall2(Headers[longhdr][1])));
  129. else
  130. Info(slide, 0, ((char *)slide, "%s\n%s\n",
  131. LoadFarString(Headers[longhdr][0]),
  132. LoadFarStringSmall(Headers[longhdr][1])));
  133. }
  134. #endif /* !WINDLL */
  135. for (j = 1L;;j++) {
  136. if (readbuf(__G__ G.sig, 4) == 0)
  137. return PK_EOF;
  138. if (memcmp(G.sig, central_hdr_sig, 4)) { /* is it a CentDir entry? */
  139. /* no new central directory entry
  140. * -> is the number of processed entries compatible with the
  141. * number of entries as stored in the end_central record?
  142. */
  143. if (((j - 1) &
  144. (ulg)(G.ecrec.have_ecr64 ? MASK_ZUCN64 : MASK_ZUCN16))
  145. == (ulg)G.ecrec.total_entries_central_dir)
  146. {
  147. /* "j modulus 4T/64k" matches the reported 64/16-bit-unsigned
  148. * number of directory entries -> probably, the regular
  149. * end of the central directory has been reached
  150. */
  151. break;
  152. } else {
  153. Info(slide, 0x401,
  154. ((char *)slide, LoadFarString(CentSigMsg), j));
  155. Info(slide, 0x401,
  156. ((char *)slide, LoadFarString(ReportMsg)));
  157. return PK_BADERR; /* sig not found */
  158. }
  159. }
  160. /* process_cdir_file_hdr() sets pInfo->hostnum, pInfo->lcflag, ...: */
  161. if ((error = process_cdir_file_hdr(__G)) != PK_COOL)
  162. return error; /* only PK_EOF defined */
  163. /*
  164. * We could DISPLAY the filename instead of storing (and possibly trun-
  165. * cating, in the case of a very long name) and printing it, but that
  166. * has the disadvantage of not allowing case conversion--and it's nice
  167. * to be able to see in the listing precisely how you have to type each
  168. * filename in order for unzip to consider it a match. Speaking of
  169. * which, if member names were specified on the command line, check in
  170. * with match() to see if the current file is one of them, and make a
  171. * note of it if it is.
  172. */
  173. if ((error = do_string(__G__ G.crec.filename_length, DS_FN)) !=
  174. PK_COOL) /* ^--(uses pInfo->lcflag) */
  175. {
  176. error_in_archive = error;
  177. if (error > PK_WARN) /* fatal: can't continue */
  178. return error;
  179. }
  180. if (G.extra_field != (uch *)NULL) {
  181. free(G.extra_field);
  182. G.extra_field = (uch *)NULL;
  183. }
  184. if ((error = do_string(__G__ G.crec.extra_field_length, EXTRA_FIELD))
  185. != 0)
  186. {
  187. error_in_archive = error;
  188. if (error > PK_WARN) /* fatal */
  189. return error;
  190. }
  191. if (!G.process_all_files) { /* check if specified on command line */
  192. unsigned i;
  193. if (G.filespecs == 0)
  194. do_this_file = TRUE;
  195. else { /* check if this entry matches an `include' argument */
  196. do_this_file = FALSE;
  197. for (i = 0; i < G.filespecs; i++)
  198. if (match(G.filename, G.pfnames[i], uO.C_flag WISEP)) {
  199. do_this_file = TRUE;
  200. break; /* found match, so stop looping */
  201. }
  202. }
  203. if (do_this_file) { /* check if this is an excluded file */
  204. for (i = 0; i < G.xfilespecs; i++)
  205. if (match(G.filename, G.pxnames[i], uO.C_flag WISEP)) {
  206. do_this_file = FALSE; /* ^-- ignore case in match */
  207. break;
  208. }
  209. }
  210. }
  211. /*
  212. * If current file was specified on command line, or if no names were
  213. * specified, do the listing for this file. Otherwise, get rid of the
  214. * file comment and go back for the next file.
  215. */
  216. if (G.process_all_files || do_this_file) {
  217. #ifdef OS2DLL
  218. /* this is used by UzpFileTree() to allow easy processing of lists
  219. * of zip directory contents */
  220. if (G.processExternally) {
  221. if ((G.processExternally)(G.filename, &G.crec))
  222. break;
  223. ++members;
  224. } else {
  225. #endif
  226. #ifdef OS2_EAS
  227. {
  228. uch *ef_ptr = G.extra_field;
  229. int ef_size, ef_len = G.crec.extra_field_length;
  230. ea_size = acl_size = 0;
  231. while (ef_len >= EB_HEADSIZE) {
  232. ef_size = makeword(&ef_ptr[EB_LEN]);
  233. switch (makeword(&ef_ptr[EB_ID])) {
  234. case EF_OS2:
  235. ea_size = makelong(&ef_ptr[EB_HEADSIZE]);
  236. break;
  237. case EF_ACL:
  238. acl_size = makelong(&ef_ptr[EB_HEADSIZE]);
  239. break;
  240. }
  241. ef_ptr += (ef_size + EB_HEADSIZE);
  242. ef_len -= (ef_size + EB_HEADSIZE);
  243. }
  244. }
  245. #endif
  246. #ifdef USE_EF_UT_TIME
  247. if (G.extra_field &&
  248. #ifdef IZ_CHECK_TZ
  249. G.tz_is_valid &&
  250. #endif
  251. (ef_scan_for_izux(G.extra_field, G.crec.extra_field_length, 1,
  252. G.crec.last_mod_dos_datetime, &z_utime, NULL)
  253. & EB_UT_FL_MTIME))
  254. {
  255. TIMET_TO_NATIVE(z_utime.mtime) /* NOP unless MSC 7.0, Mac */
  256. t = localtime(&(z_utime.mtime));
  257. } else
  258. t = (struct tm *)NULL;
  259. if (t != (struct tm *)NULL) {
  260. mo = (unsigned)(t->tm_mon + 1);
  261. dy = (unsigned)(t->tm_mday);
  262. yr = (unsigned)(t->tm_year + 1900);
  263. hh = (unsigned)(t->tm_hour);
  264. mm = (unsigned)(t->tm_min);
  265. } else
  266. #endif /* USE_EF_UT_TIME */
  267. {
  268. yr = ((((unsigned)(G.crec.last_mod_dos_datetime >> 25) & 0x7f)
  269. + 1980));
  270. mo = ((unsigned)(G.crec.last_mod_dos_datetime >> 21) & 0x0f);
  271. dy = ((unsigned)(G.crec.last_mod_dos_datetime >> 16) & 0x1f);
  272. hh = (((unsigned)G.crec.last_mod_dos_datetime >> 11) & 0x1f);
  273. mm = (((unsigned)G.crec.last_mod_dos_datetime >> 5) & 0x3f);
  274. }
  275. /* permute date so it displays according to nat'l convention
  276. * ('methnum' is not yet set, it is used as temporary buffer) */
  277. switch (date_format) {
  278. case DF_YMD:
  279. methnum = mo;
  280. mo = yr; yr = dy; dy = methnum;
  281. break;
  282. case DF_DMY:
  283. methnum = mo;
  284. mo = dy; dy = methnum;
  285. }
  286. csiz = G.crec.csize;
  287. if (G.crec.general_purpose_bit_flag & 1)
  288. csiz -= 12; /* if encrypted, don't count encryption header */
  289. if ((cfactor = ratio(G.crec.ucsize, csiz)) < 0) {
  290. #ifndef WINDLL
  291. sgn = '-';
  292. #endif
  293. cfactor = (-cfactor + 5) / 10;
  294. } else {
  295. #ifndef WINDLL
  296. sgn = ' ';
  297. #endif
  298. cfactor = (cfactor + 5) / 10;
  299. }
  300. methnum = find_compr_idx(G.crec.compression_method);
  301. zfstrcpy(methbuf, method[methnum]);
  302. if (G.crec.compression_method == DEFLATED ||
  303. G.crec.compression_method == ENHDEFLATED) {
  304. methbuf[5] = dtype[(G.crec.general_purpose_bit_flag>>1) & 3];
  305. } else if (methnum >= NUM_METHODS) {
  306. sprintf(&methbuf[4], "%03u", G.crec.compression_method);
  307. }
  308. #if 0 /* GRR/Euro: add this? */
  309. #if defined(DOS_FLX_NLM_OS2_W32) || defined(THEOS) || defined(UNIX)
  310. for (p = G.filename; *p; ++p)
  311. if (!isprint(*p))
  312. *p = '?'; /* change non-printable chars to '?' */
  313. #endif /* DOS_FLX_NLM_OS2_W32 || THEOS || UNIX */
  314. #endif /* 0 */
  315. #ifdef WINDLL
  316. /* send data to application for formatting and printing */
  317. if (G.lpUserFunctions->SendApplicationMessage != NULL)
  318. (*G.lpUserFunctions->SendApplicationMessage)(G.crec.ucsize,
  319. csiz, (unsigned)cfactor, mo, dy, yr, hh, mm,
  320. (char)(G.pInfo->lcflag ? '^' : ' '),
  321. (LPCSTR)fnfilter(G.filename, slide, (WSIZE>>1)),
  322. (LPCSTR)methbuf, G.crec.crc32,
  323. (char)((G.crec.general_purpose_bit_flag & 1) ? 'E' : ' '));
  324. else if (G.lpUserFunctions->SendApplicationMessage_i32 != NULL) {
  325. unsigned long ucsize_lo, csiz_lo;
  326. unsigned long ucsize_hi=0L, csiz_hi=0L;
  327. ucsize_lo = (unsigned long)(G.crec.ucsize);
  328. csiz_lo = (unsigned long)(csiz);
  329. #ifdef ZIP64_SUPPORT
  330. ucsize_hi = (unsigned long)(G.crec.ucsize >> 32);
  331. csiz_hi = (unsigned long)(csiz >> 32);
  332. #endif /* ZIP64_SUPPORT */
  333. (*G.lpUserFunctions->SendApplicationMessage_i32)(ucsize_lo,
  334. ucsize_hi, csiz_lo, csiz_hi, (unsigned)cfactor,
  335. mo, dy, yr, hh, mm,
  336. (char)(G.pInfo->lcflag ? '^' : ' '),
  337. (LPCSTR)fnfilter(G.filename, slide, (WSIZE>>1)),
  338. (LPCSTR)methbuf, G.crec.crc32,
  339. (char)((G.crec.general_purpose_bit_flag & 1) ? 'E' : ' '));
  340. }
  341. #else /* !WINDLL */
  342. if (cfactor == 100)
  343. sprintf(cfactorstr, LoadFarString(CompFactor100));
  344. else
  345. sprintf(cfactorstr, LoadFarString(CompFactorStr), sgn, cfactor);
  346. if (longhdr)
  347. Info(slide, 0, ((char *)slide, LoadFarString(LongHdrStats),
  348. FmZofft(G.crec.ucsize, "8", "u"), methbuf,
  349. FmZofft(csiz, "8", "u"), cfactorstr,
  350. mo, dt_sepchar, dy, dt_sepchar, yr, hh, mm,
  351. G.crec.crc32, (G.pInfo->lcflag? '^':' ')));
  352. else
  353. #ifdef OS2_EAS
  354. Info(slide, 0, ((char *)slide, LoadFarString(ShortHdrStats),
  355. FmZofft(G.crec.ucsize, "9", "u"), ea_size, acl_size,
  356. mo, dt_sepchar, dy, dt_sepchar, yr, hh, mm,
  357. (G.pInfo->lcflag? '^':' ')));
  358. #else
  359. Info(slide, 0, ((char *)slide, LoadFarString(ShortHdrStats),
  360. FmZofft(G.crec.ucsize, "9", "u"),
  361. mo, dt_sepchar, dy, dt_sepchar, yr, hh, mm,
  362. (G.pInfo->lcflag? '^':' ')));
  363. #endif
  364. fnprint(__G);
  365. #endif /* ?WINDLL */
  366. if ((error = do_string(__G__ G.crec.file_comment_length,
  367. QCOND? DISPL_8 : SKIP)) != 0)
  368. {
  369. error_in_archive = error; /* might be just warning */
  370. if (error > PK_WARN) /* fatal */
  371. return error;
  372. }
  373. tot_ucsize += G.crec.ucsize;
  374. tot_csize += csiz;
  375. ++members;
  376. #ifdef OS2_EAS
  377. if (ea_size) {
  378. tot_easize += ea_size;
  379. ++tot_eafiles;
  380. }
  381. if (acl_size) {
  382. tot_aclsize += acl_size;
  383. ++tot_aclfiles;
  384. }
  385. #endif
  386. #ifdef OS2DLL
  387. } /* end of "if (G.processExternally) {...} else {..." */
  388. #endif
  389. } else { /* not listing this file */
  390. SKIP_(G.crec.file_comment_length)
  391. }
  392. } /* end for-loop (j: files in central directory) */
  393. /*---------------------------------------------------------------------------
  394. Print footer line and totals (compressed size, uncompressed size, number
  395. of members in zipfile).
  396. ---------------------------------------------------------------------------*/
  397. if (uO.qflag < 2
  398. #ifdef OS2DLL
  399. && !G.processExternally
  400. #endif
  401. ) {
  402. if ((cfactor = ratio(tot_ucsize, tot_csize)) < 0) {
  403. #ifndef WINDLL
  404. sgn = '-';
  405. #endif
  406. cfactor = (-cfactor + 5) / 10;
  407. } else {
  408. #ifndef WINDLL
  409. sgn = ' ';
  410. #endif
  411. cfactor = (cfactor + 5) / 10;
  412. }
  413. #ifdef WINDLL
  414. /* pass the totals back to the calling application */
  415. G.lpUserFunctions->TotalSizeComp = tot_csize;
  416. G.lpUserFunctions->TotalSize = tot_ucsize;
  417. G.lpUserFunctions->CompFactor = (ulg)cfactor;
  418. G.lpUserFunctions->NumMembers = members;
  419. #else /* !WINDLL */
  420. if (cfactor == 100)
  421. sprintf(cfactorstr, LoadFarString(CompFactor100));
  422. else
  423. sprintf(cfactorstr, LoadFarString(CompFactorStr), sgn, cfactor);
  424. if (longhdr) {
  425. Info(slide, 0, ((char *)slide, LoadFarString(LongFileTrailer),
  426. FmZofft(tot_ucsize, "8", "u"), FmZofft(tot_csize, "8", "u"),
  427. cfactorstr, members, members==1? "":"s"));
  428. #ifdef OS2_EAS
  429. if (tot_easize || tot_aclsize)
  430. Info(slide, 0, ((char *)slide, "\n"));
  431. if (tot_eafiles && tot_easize)
  432. Info(slide, 0, ((char *)slide, LoadFarString(OS2ExtAttrTrailer),
  433. tot_eafiles, tot_eafiles == 1? " has" : "s have a total of",
  434. tot_easize));
  435. if (tot_aclfiles && tot_aclsize)
  436. Info(slide, 0, ((char *)slide, LoadFarString(OS2ACLTrailer),
  437. tot_aclfiles,
  438. tot_aclfiles == 1 ? " has" : "s have a total of",
  439. tot_aclsize));
  440. #endif /* OS2_EAS */
  441. } else
  442. #ifdef OS2_EAS
  443. Info(slide, 0, ((char *)slide, LoadFarString(ShortFileTrailer),
  444. FmZofft(tot_ucsize, "9", "u"), tot_easize, tot_aclsize,
  445. members, members == 1 ? "" : "s"));
  446. #else
  447. Info(slide, 0, ((char *)slide, LoadFarString(ShortFileTrailer),
  448. FmZofft(tot_ucsize, "9", "u"),
  449. members, members == 1 ? "" : "s"));
  450. #endif /* OS2_EAS */
  451. #endif /* ?WINDLL */
  452. }
  453. /* Skip the following checks in case of a premature listing break. */
  454. if (error_in_archive <= PK_WARN) {
  455. /*---------------------------------------------------------------------------
  456. Double check that we're back at the end-of-central-directory record.
  457. ---------------------------------------------------------------------------*/
  458. if ( (memcmp(G.sig,
  459. (G.ecrec.have_ecr64 ?
  460. end_central64_sig : end_central_sig),
  461. 4) != 0)
  462. && (!G.ecrec.is_zip64_archive)
  463. && (memcmp(G.sig, end_central_sig, 4) != 0)
  464. ) { /* just to make sure again */
  465. Info(slide, 0x401, ((char *)slide, LoadFarString(EndSigMsg)));
  466. error_in_archive = PK_WARN; /* didn't find sig */
  467. }
  468. /* Set specific return code when no files have been found. */
  469. if (members == 0L && error_in_archive <= PK_WARN)
  470. error_in_archive = PK_FIND;
  471. }
  472. return error_in_archive;
  473. } /* end function list_files() */
  474. #ifdef TIMESTAMP
  475. /************************/
  476. /* Function fn_is_dir() */
  477. /************************/
  478. static int fn_is_dir(__G) /* returns TRUE if G.filename is directory */
  479. __GDEF
  480. {
  481. extent fn_len = strlen(G.filename);
  482. register char endc;
  483. return fn_len > 0 &&
  484. ((endc = lastchar(G.filename, fn_len)) == '/' ||
  485. (G.pInfo->hostnum == FS_FAT_ && !MBSCHR(G.filename, '/') &&
  486. endc == '\\'));
  487. }
  488. /*****************************/
  489. /* Function get_time_stamp() */
  490. /*****************************/
  491. int get_time_stamp(__G__ last_modtime, nmember) /* return PK-type error code */
  492. __GDEF
  493. time_t *last_modtime;
  494. ulg *nmember;
  495. {
  496. int do_this_file=FALSE, error, error_in_archive=PK_COOL;
  497. ulg j;
  498. #ifdef USE_EF_UT_TIME
  499. iztimes z_utime;
  500. #endif
  501. min_info info;
  502. /*---------------------------------------------------------------------------
  503. Unlike extract_or_test_files() but like list_files(), this function works
  504. on information in the central directory alone. Thus we have a single,
  505. large loop through the entire directory, searching for the latest time
  506. stamp.
  507. ---------------------------------------------------------------------------*/
  508. *last_modtime = 0L; /* assuming no zipfile data older than 1970 */
  509. *nmember = 0L;
  510. G.pInfo = &info;
  511. for (j = 1L;; j++) {
  512. if (readbuf(__G__ G.sig, 4) == 0)
  513. return PK_EOF;
  514. if (memcmp(G.sig, central_hdr_sig, 4)) { /* is it a CentDir entry? */
  515. if (((unsigned)(j - 1) & (unsigned)0xFFFF) ==
  516. (unsigned)G.ecrec.total_entries_central_dir) {
  517. /* "j modulus 64k" matches the reported 16-bit-unsigned
  518. * number of directory entries -> probably, the regular
  519. * end of the central directory has been reached
  520. */
  521. break;
  522. } else {
  523. Info(slide, 0x401,
  524. ((char *)slide, LoadFarString(CentSigMsg), j));
  525. Info(slide, 0x401,
  526. ((char *)slide, LoadFarString(ReportMsg)));
  527. return PK_BADERR; /* sig not found */
  528. }
  529. }
  530. /* process_cdir_file_hdr() sets pInfo->lcflag: */
  531. if ((error = process_cdir_file_hdr(__G)) != PK_COOL)
  532. return error; /* only PK_EOF defined */
  533. if ((error = do_string(__G__ G.crec.filename_length, DS_FN)) != PK_OK)
  534. { /* ^-- (uses pInfo->lcflag) */
  535. error_in_archive = error;
  536. if (error > PK_WARN) /* fatal: can't continue */
  537. return error;
  538. }
  539. if (G.extra_field != (uch *)NULL) {
  540. free(G.extra_field);
  541. G.extra_field = (uch *)NULL;
  542. }
  543. if ((error = do_string(__G__ G.crec.extra_field_length, EXTRA_FIELD))
  544. != 0)
  545. {
  546. error_in_archive = error;
  547. if (error > PK_WARN) /* fatal */
  548. return error;
  549. }
  550. if (!G.process_all_files) { /* check if specified on command line */
  551. unsigned i;
  552. if (G.filespecs == 0)
  553. do_this_file = TRUE;
  554. else { /* check if this entry matches an `include' argument */
  555. do_this_file = FALSE;
  556. for (i = 0; i < G.filespecs; i++)
  557. if (match(G.filename, G.pfnames[i], uO.C_flag WISEP)) {
  558. do_this_file = TRUE;
  559. break; /* found match, so stop looping */
  560. }
  561. }
  562. if (do_this_file) { /* check if this is an excluded file */
  563. for (i = 0; i < G.xfilespecs; i++)
  564. if (match(G.filename, G.pxnames[i], uO.C_flag WISEP)) {
  565. do_this_file = FALSE; /* ^-- ignore case in match */
  566. break;
  567. }
  568. }
  569. }
  570. /* If current file was specified on command line, or if no names were
  571. * specified, check the time for this file. Either way, get rid of the
  572. * file comment and go back for the next file.
  573. * Directory entries are always ignored, to stay compatible with both
  574. * Zip and PKZIP.
  575. */
  576. if ((G.process_all_files || do_this_file) && !fn_is_dir(__G)) {
  577. #ifdef USE_EF_UT_TIME
  578. if (G.extra_field &&
  579. #ifdef IZ_CHECK_TZ
  580. G.tz_is_valid &&
  581. #endif
  582. (ef_scan_for_izux(G.extra_field, G.crec.extra_field_length, 1,
  583. G.crec.last_mod_dos_datetime, &z_utime, NULL)
  584. & EB_UT_FL_MTIME))
  585. {
  586. if (*last_modtime < z_utime.mtime)
  587. *last_modtime = z_utime.mtime;
  588. } else
  589. #endif /* USE_EF_UT_TIME */
  590. {
  591. time_t modtime = dos_to_unix_time(G.crec.last_mod_dos_datetime);
  592. if (*last_modtime < modtime)
  593. *last_modtime = modtime;
  594. }
  595. ++*nmember;
  596. }
  597. SKIP_(G.crec.file_comment_length)
  598. } /* end for-loop (j: files in central directory) */
  599. /*---------------------------------------------------------------------------
  600. Double check that we're back at the end-of-central-directory record.
  601. ---------------------------------------------------------------------------*/
  602. if (memcmp(G.sig, end_central_sig, 4)) { /* just to make sure again */
  603. Info(slide, 0x401, ((char *)slide, LoadFarString(EndSigMsg)));
  604. error_in_archive = PK_WARN;
  605. }
  606. if (*nmember == 0L && error_in_archive <= PK_WARN)
  607. error_in_archive = PK_FIND;
  608. return error_in_archive;
  609. } /* end function get_time_stamp() */
  610. #endif /* TIMESTAMP */
  611. /********************/
  612. /* Function ratio() */ /* also used by ZipInfo routines */
  613. /********************/
  614. int ratio(uc, c)
  615. zusz_t uc, c;
  616. {
  617. zusz_t denom;
  618. if (uc == 0)
  619. return 0;
  620. if (uc > 2000000L) { /* risk signed overflow if multiply numerator */
  621. denom = uc / 1000L;
  622. return ((uc >= c) ?
  623. (int) ((uc-c + (denom>>1)) / denom) :
  624. -((int) ((c-uc + (denom>>1)) / denom)));
  625. } else { /* ^^^^^^^^ rounding */
  626. denom = uc;
  627. return ((uc >= c) ?
  628. (int) ((1000L*(uc-c) + (denom>>1)) / denom) :
  629. -((int) ((1000L*(c-uc) + (denom>>1)) / denom)));
  630. } /* ^^^^^^^^ rounding */
  631. }
  632. /************************/
  633. /* Function fnprint() */ /* also used by ZipInfo routines */
  634. /************************/
  635. void fnprint(__G) /* print filename (after filtering) and newline */
  636. __GDEF
  637. {
  638. char *name = fnfilter(G.filename, slide, (extent)(WSIZE>>1));
  639. (*G.message)((zvoid *)&G, (uch *)name, (ulg)strlen(name), 0);
  640. (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0);
  641. } /* end function fnprint() */