home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip51.zip / zipinfo.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  54KB  |  1,392 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   zipinfo.c
  4.  
  5.   This file contains all of the zipfile-listing routines for UnZip, inclu-
  6.   ding the bulk of what used to be the separate program ZipInfo.  All of
  7.   the ZipInfo routines are by Greg Roelofs; SizeOfEAs is by Kai Uwe Rommel;
  8.   and list_files is by the usual mishmash of Info-ZIP contributors (see the
  9.   listing in CONTRIBS).
  10.  
  11.   Contains:  zi_opts()
  12.              zi_end_central()
  13.              zipinfo()
  14.              zi_long()
  15.              zi_short()
  16.              zi_time()
  17.              SizeOfEAs()
  18.              list_files()
  19.              ratio()
  20.              fnprint()
  21.  
  22.   ---------------------------------------------------------------------------*/
  23.  
  24.  
  25. #include "unzip.h"
  26.  
  27. static char unkn[16];
  28.  
  29. static int ratio OF((ulg uc, ulg c));
  30. static void fnprint OF((uch *fn));
  31.  
  32. #ifndef NO_ZIPINFO  /* strings use up too much space in small-memory systems */
  33.  
  34. /* Define OS-specific attributes for use on ALL platforms--the S_xxxx
  35.  * versions of these are defined differently (or not defined) by different
  36.  * compilers and operating systems. */
  37.  
  38. #define UNX_IFMT       0170000     /* Unix file type mask */
  39. #define UNX_IFDIR      0040000     /* Unix directory */
  40. #define UNX_IFREG      0100000     /* Unix regular file */
  41. #define UNX_IFSOCK     0140000     /* Unix socket (BSD, not SysV or Amiga) */
  42. #define UNX_IFLNK      0120000     /* Unix symbolic link (not SysV, Amiga) */
  43. #define UNX_IFBLK      0060000     /* Unix block special       (not Amiga) */
  44. #define UNX_IFCHR      0020000     /* Unix character special   (not Amiga) */
  45. #define UNX_IFIFO      0010000     /* Unix fifo    (BCC, not MSC or Amiga) */
  46. #define UNX_ISUID      04000       /* Unix set user id on execution */
  47. #define UNX_ISGID      02000       /* Unix set group id on execution */
  48. #define UNX_ISVTX      01000       /* Unix directory permissions control */
  49. #define UNX_ENFMT      UNX_ISGID   /* Unix record locking enforcement flag */
  50. #define UNX_IRWXU      00700       /* Unix read, write, execute: owner */
  51. #define UNX_IRUSR      00400       /* Unix read permission: owner */
  52. #define UNX_IWUSR      00200       /* Unix write permission: owner */
  53. #define UNX_IXUSR      00100       /* Unix execute permission: owner */
  54. #define UNX_IRWXG      00070       /* Unix read, write, execute: group */
  55. #define UNX_IRGRP      00040       /* Unix read permission: group */
  56. #define UNX_IWGRP      00020       /* Unix write permission: group */
  57. #define UNX_IXGRP      00010       /* Unix execute permission: group */
  58. #define UNX_IRWXO      00007       /* Unix read, write, execute: other */
  59. #define UNX_IROTH      00004       /* Unix read permission: other */
  60. #define UNX_IWOTH      00002       /* Unix write permission: other */
  61. #define UNX_IXOTH      00001       /* Unix execute permission: other */
  62.  
  63. #define VMS_IRUSR      UNX_IRUSR   /* VMS read/owner */
  64. #define VMS_IWUSR      UNX_IWUSR   /* VMS write/owner */
  65. #define VMS_IXUSR      UNX_IXUSR   /* VMS execute/owner */
  66. #define VMS_IRGRP      UNX_IRGRP   /* VMS read/group */
  67. #define VMS_IWGRP      UNX_IWGRP   /* VMS write/group */
  68. #define VMS_IXGRP      UNX_IXGRP   /* VMS execute/group */
  69. #define VMS_IROTH      UNX_IROTH   /* VMS read/other */
  70. #define VMS_IWOTH      UNX_IWOTH   /* VMS write/other */
  71. #define VMS_IXOTH      UNX_IXOTH   /* VMS execute/other */
  72.  
  73. #define AMI_IFMT       06000       /* Amiga file type mask */
  74. #define AMI_IFDIR      04000       /* Amiga directory */
  75. #define AMI_IFREG      02000       /* Amiga regular file */
  76. #define AMI_IHIDDEN    00200       /* to be supported in AmigaDOS 3.x */
  77. #define AMI_ISCRIPT    00100       /* executable script (text command file) */
  78. #define AMI_IPURE      00040       /* allow loading into resident memory */
  79. #define AMI_IARCHIVE   00020       /* not modified since bit was last set */
  80. #define AMI_IREAD      00010       /* can be opened for reading */
  81. #define AMI_IWRITE     00004       /* can be opened for writing */
  82. #define AMI_IEXECUTE   00002       /* executable image, a loadable runfile */
  83. #define AMI_IDELETE    00001       /* can be deleted */
  84.  
  85. #define LFLAG  3           /* short "ls -l" type listing */
  86.  
  87. static int   zi_long   OF((void));
  88. static int   zi_short  OF((void));
  89. static char *zi_time   OF((ush *datez, ush *timez));
  90.  
  91.  
  92. /************************/
  93. /*  Function zi_opts()  */
  94. /************************/
  95.  
  96. int zi_opts(pargc, pargv)
  97.     int *pargc;
  98.     char ***pargv;
  99. {
  100.     char   **argv, *s;
  101.     int    argc, c, error=FALSE, negative=0;
  102.     int    hflag_slmv=TRUE, hflag_2=FALSE;  /* diff options => diff defaults */
  103.     int    tflag_slm=TRUE, tflag_2v=FALSE;
  104.     int    explicit_h=FALSE, explicit_t=FALSE;
  105.  
  106.  
  107. #ifdef MACOS
  108.     lflag = LFLAG;   /* reset default on each call */
  109. #endif
  110.     argc = *pargc;
  111.     argv = *pargv;
  112.  
  113.     while (--argc > 0 && (*++argv)[0] == '-') {
  114.         s = argv[0] + 1;
  115.         while ((c = *s++) != 0) {    /* "!= 0":  prevent Turbo C warning */
  116.             switch (c) {
  117.                 case '-':
  118.                     ++negative;
  119.                     break;
  120.                 case '1':      /* shortest listing:  JUST filenames */
  121.                     if (negative)
  122.                         lflag = -2, negative = 0;
  123.                     else
  124.                         lflag = 1;
  125.                     break;
  126.                 case '2':      /* just filenames, plus headers if specified */
  127.                     if (negative)
  128.                         lflag = -2, negative = 0;
  129.                     else
  130.                         lflag = 2;
  131.                     break;
  132.                 case 'h':      /* header line */
  133.                     if (negative)
  134.                         hflag_2 = hflag_slmv = FALSE, negative = 0;
  135.                     else {
  136.                         hflag_2 = hflag_slmv = explicit_h = TRUE;
  137.                         if (lflag == -1)
  138.                             lflag = 0;
  139.                     }
  140.                     break;
  141.                 case 'l':      /* longer form of "ls -l" type listing */
  142.                     if (negative)
  143.                         lflag = -2, negative = 0;
  144.                     else
  145.                         lflag = 5;
  146.                     break;
  147.                 case 'm':      /* medium form of "ls -l" type listing */
  148.                     if (negative)
  149.                         lflag = -2, negative = 0;
  150.                     else
  151.                         lflag = 4;
  152.                     break;
  153.                 case 's':      /* default:  shorter "ls -l" type listing */
  154.                     if (negative)
  155.                         lflag = -2, negative = 0;
  156.                     else
  157.                         lflag = 3;
  158.                     break;
  159.                 case 't':      /* totals line */
  160.                     if (negative)
  161.                         tflag_2v = tflag_slm = FALSE, negative = 0;
  162.                     else {
  163.                         tflag_2v = tflag_slm = explicit_t = TRUE;
  164.                         if (lflag == -1)
  165.                             lflag = 0;
  166.                     }
  167.                     break;
  168.                 case 'v':      /* turbo-verbose listing */
  169.                     if (negative)
  170.                         lflag = -2, negative = 0;
  171.                     else
  172.                         lflag = 10;
  173.                     break;
  174.                 case 'Z':      /* ZipInfo mode:  ignore */
  175.                     break;
  176.                 case 'z':      /* print zipfile comment */
  177.                     if (negative)
  178.                         zflag = negative = 0;
  179.                     else
  180.                         zflag = 1;
  181.                     break;
  182.                 default:
  183.                     error = TRUE;
  184.                     break;
  185.             }
  186.         }
  187.     }
  188.     if ((argc-- == 0) || error) {
  189.         *pargc = argc;
  190.         *pargv = argv;
  191.         return usage(error);
  192.     }
  193.  
  194.     /* if no listing options given (or all negated), or if only -h/-t given
  195.      * with individual files specified, use default listing format */
  196.     if ((lflag < 0) || ((argc > 0) && (lflag == 0)))
  197.         lflag = LFLAG;
  198.  
  199.     /* set header and totals flags to default or specified values */
  200.     switch (lflag) {
  201.         case 0:   /* 0:  can only occur if either -t or -h explicitly given; */
  202.         case 2:   /*  therefore set both flags equal to normally false value */
  203.             hflag = hflag_2;
  204.             tflag = tflag_2v;
  205.             break;
  206.         case 1:   /* only filenames, *always* */
  207.             hflag = FALSE;
  208.             tflag = FALSE;
  209.             zflag = FALSE;
  210.             break;
  211.         case 3:
  212.         case 4:
  213.         case 5:
  214.             hflag = ((argc > 0) && !explicit_h)? FALSE : hflag_slmv;
  215.             tflag = ((argc > 0) && !explicit_t)? FALSE : tflag_slm;
  216.             break;
  217.         case 10:
  218.             hflag = hflag_slmv;
  219.             tflag = tflag_2v;
  220.             break;
  221.     }
  222.  
  223.     *pargc = argc;
  224.     *pargv = argv;
  225.     return 0;
  226.  
  227. } /* end function zi_opts() */
  228.  
  229.  
  230.  
  231.  
  232.  
  233. /*******************************/
  234. /*  Function zi_end_central()  */
  235. /*******************************/
  236.  
  237. int zi_end_central()   /* return PK-type error code */
  238. {
  239.     int  error = PK_COOL;
  240.  
  241.  
  242. /*---------------------------------------------------------------------------
  243.     Print out various interesting things about the zipfile.
  244.   ---------------------------------------------------------------------------*/
  245.  
  246.     /* header fits on one line, for anything up to 10GB and 10000 files: */
  247.     if (hflag)
  248.         printf(((int)strlen(zipfn) < 39)?
  249.           "Archive:  %s   %ld bytes   %d file%s\n" :
  250.           "Archive:  %s   %ld   %d\n", zipfn, (long)ziplen,
  251.           ecrec.total_entries_central_dir,
  252.           (ecrec.total_entries_central_dir==1)? "":"s");
  253.  
  254.     /* verbose format */
  255.     if (lflag > 9) {
  256.         printf("\nEnd-of-central-directory record:\n");
  257.         printf("-------------------------------\n\n");
  258.  
  259.         printf("\
  260.   Actual offset of end-of-central-dir record:   %9ld (%.8lXh)\n\
  261.   Expected offset of end-of-central-dir record: %9ld (%.8lXh)\n\
  262.   (based on the length of the central directory and its expected offset)\n\n",
  263.           (long)real_ecrec_offset, (long)real_ecrec_offset,
  264.           (long)expect_ecrec_offset, (long)expect_ecrec_offset);
  265.  
  266.         if (ecrec.number_this_disk == 0) {
  267.             printf("\
  268.   This zipfile constitutes the sole disk of a single-part archive; its\n\
  269.   central directory contains %u %s.  The central directory is %lu\n\
  270.   (%.8lXh) bytes long, and its (expected) offset in bytes from the\n\
  271.   beginning of the zipfile is %lu (%.8lXh).\n\n",
  272.               ecrec.total_entries_central_dir,
  273.               (ecrec.total_entries_central_dir == 1)? "entry" : "entries",
  274.               ecrec.size_central_directory, ecrec.size_central_directory,
  275.               ecrec.offset_start_central_directory,
  276.               ecrec.offset_start_central_directory);
  277.         } else {
  278.             printf("\
  279.   This zipfile constitutes disk %u of a multi-part archive.  The central\n\
  280.   directory starts on disk %u; %u of its entries %s contained within\n\
  281.   this zipfile, out of a total of %u %s.  The entire central\n\
  282.   directory is %lu (%.8lXh) bytes long, and its offset in bytes from\n\
  283.   the beginning of the zipfile in which it begins is %lu (%.8lXh).\n\n",
  284.               ecrec.number_this_disk,
  285.               ecrec.num_disk_with_start_central_dir,
  286.               ecrec.num_entries_centrl_dir_ths_disk,
  287.               (ecrec.num_entries_centrl_dir_ths_disk == 1)? "is" : "are",
  288.               ecrec.total_entries_central_dir,
  289.               (ecrec.total_entries_central_dir == 1) ? "entry" : "entries",
  290.               ecrec.size_central_directory, ecrec.size_central_directory,
  291.               ecrec.offset_start_central_directory,
  292.               ecrec.offset_start_central_directory);
  293.         }
  294.  
  295.     /*-----------------------------------------------------------------------
  296.         Get the zipfile comment, if any, and print it out.  (Comment may be
  297.         up to 64KB long.  May the fleas of a thousand camels infest the arm-
  298.         pits of anyone who actually takes advantage of this fact.)
  299.       -----------------------------------------------------------------------*/
  300.  
  301.         if (!ecrec.zipfile_comment_length)
  302.             printf("  There is no zipfile comment.\n");
  303.         else {
  304.             printf("  The zipfile comment is %u bytes long and contains the following text:\n\n",
  305.               ecrec.zipfile_comment_length );
  306.             printf("======================== zipfile comment begins ==========================\n");
  307.             if (do_string(ecrec.zipfile_comment_length, DISPLAY))
  308.                 error = PK_WARN;
  309.             printf("========================= zipfile comment ends ===========================\n");
  310.             if (error)
  311.                 printf("\n  The zipfile comment is truncated.\n");
  312.         } /* endif (comment exists) */
  313.  
  314.     /* non-verbose mode:  print zipfile comment only if requested */
  315.     } else if (zflag && ecrec.zipfile_comment_length) {
  316.         if (do_string(ecrec.zipfile_comment_length,DISPLAY)) {
  317.             fprintf(stderr, "\ncaution:  zipfile comment truncated\n");
  318.             error = PK_WARN;
  319.         }
  320.     } /* endif (verbose) */
  321.  
  322.     return error;
  323.  
  324. } /* end function zi_end_central() */
  325.  
  326.  
  327.  
  328.  
  329.  
  330. /************************/
  331. /*  Function zipinfo()  */
  332. /************************/
  333.  
  334. int zipinfo()   /* return PK-type error code */
  335. {
  336.     int   j, do_this_file=FALSE, error, error_in_archive=PK_COOL;
  337.     int   *fn_matched=NULL, *xn_matched=NULL;
  338.     ush   members=0;
  339.     ulg   tot_csize=0L, tot_ucsize=0L;
  340.  
  341.  
  342. /*---------------------------------------------------------------------------
  343.     Malloc space for check on unmatched filespecs (no big deal if one or both
  344.     are NULL).
  345.   ---------------------------------------------------------------------------*/
  346.  
  347.     if (filespecs > 0  &&
  348.         (fn_matched=(int *)malloc(filespecs*sizeof(int))) != NULL)
  349.         for (j = 0;  j < filespecs;  ++j)
  350.             fn_matched[j] = FALSE;
  351.  
  352.     if (xfilespecs > 0  &&
  353.         (xn_matched=(int *)malloc(xfilespecs*sizeof(int))) != NULL)
  354.         for (j = 0;  j < xfilespecs;  ++j)
  355.             xn_matched[j] = FALSE;
  356.  
  357. /*---------------------------------------------------------------------------
  358.     Set file pointer to start of central directory, then loop through cen-
  359.     tral directory entries.  Check that directory-entry signature bytes are
  360.     actually there (just a precaution), then process the entry.  We know
  361.     the entire central directory is on this disk:  we wouldn't have any of
  362.     this information unless the end-of-central-directory record was on this
  363.     disk, and we wouldn't have gotten to this routine unless this is also
  364.     the disk on which the central directory starts.  In practice, this had
  365.     better be the *only* disk in the archive, but maybe someday we'll add
  366.     multi-disk support.
  367.   ---------------------------------------------------------------------------*/
  368.  
  369.     pInfo->lcflag = 0;    /* used in do_string():  never TRUE in zipinfo mode */
  370.     pInfo->textmode = 0;  /* so one can read on screen (but is it ever used?) */
  371.  
  372.     for (j = 0;  j < (int)ecrec.total_entries_central_dir;  ++j) {
  373.         if (readbuf(sig, 4) <= 0)
  374.             return PK_EOF;
  375.         if (strncmp(sig, central_hdr_sig, 4)) {  /* just to make sure */
  376.             fprintf(stderr, CentSigMsg, j);  /* sig not found */
  377.             return PK_BADERR;
  378.         }
  379.         if ((error = get_cdir_file_hdr()) != PK_COOL)
  380.             return error;       /* only PK_EOF defined */
  381.  
  382.         /* do Amigas (AMIGA_) also have volume labels? */
  383.         if (IS_VOLID(crec.external_file_attributes) &&
  384.             (pInfo->hostnum == FS_FAT_ || pInfo->hostnum == FS_HPFS_ ||
  385.              pInfo->hostnum == FS_NTFS_ || pInfo->hostnum == ATARI_))
  386.             pInfo->vollabel = TRUE;
  387.         else
  388.             pInfo->vollabel = FALSE;
  389.  
  390.         if ((error = do_string(crec.filename_length, FILENAME)) != PK_COOL) {
  391.           error_in_archive = error;   /* might be warning */
  392.           if (error > PK_WARN)        /* fatal */
  393.               return error;
  394.         }
  395.  
  396.         if (!process_all_files) {    /* check if specified on command line */
  397.             char  **pfn = pfnames-1;
  398.  
  399.             do_this_file = FALSE;
  400.             while (*++pfn)
  401.                 if (match(filename, *pfn, 0)) {
  402.                     do_this_file = TRUE;
  403.                     if (fn_matched)
  404.                         fn_matched[pfn-pfnames] = TRUE;
  405.                     break;       /* found match, so stop looping */
  406.                 }
  407.             if (do_this_file) {  /* check if this is an excluded file */
  408.                 char  **pxn = pxnames-1;
  409.  
  410.                 while (*++pxn)
  411.                     if (match(filename, *pxn, 0)) {
  412.                         do_this_file = FALSE;
  413.                         if (xn_matched)
  414.                             xn_matched[pxn-pxnames] = TRUE;
  415.                         break;
  416.                     }
  417.             }
  418.         }
  419.  
  420.     /*-----------------------------------------------------------------------
  421.         If current file was specified on command line, or if no names were
  422.         specified, do the listing for this file.  Otherwise, get rid of the
  423.         file comment and go back for the next file.
  424.       -----------------------------------------------------------------------*/
  425.  
  426.         if (process_all_files || do_this_file) {
  427.  
  428.             switch (lflag) {
  429.                 case 1:
  430.                 case 2:
  431.                     fnprint((uch *)filename);
  432.                     SKIP_(crec.extra_field_length)
  433.                     SKIP_(crec.file_comment_length)
  434.                     break;
  435.  
  436.                 case 3:
  437.                 case 4:
  438.                 case 5:
  439.                     if ((error = zi_short()) != PK_COOL) {
  440.                         error_in_archive = error;   /* might be warning */
  441.                         if (error > PK_WARN)        /* fatal */
  442.                             return error;
  443.                     }
  444.                     break;
  445.  
  446.                 case 10:
  447. #ifdef T20_VMS  /* GRR:  add cbreak-style "more" */
  448.                     printf("\nCentral directory entry #%d:\n", j);
  449. #else
  450.                     /* formfeed/CR for piping to "more": */
  451.                     printf("%s\nCentral directory entry #%d:\n", "\014", j);
  452. #endif
  453.                     printf("---------------------------\n\n");
  454.  
  455.                     if ((error = zi_long()) != PK_COOL) {
  456.                       error_in_archive = error;   /* might be warning */
  457.                       if (error > PK_WARN)        /* fatal */
  458.                           return error;
  459.                     }
  460.                     break;
  461.  
  462.                 default:
  463.                     SKIP_(crec.extra_field_length)
  464.                     SKIP_(crec.file_comment_length)
  465.                     break;
  466.  
  467.             } /* end switch (lflag) */
  468.  
  469.             tot_csize += crec.csize;
  470.             tot_ucsize += crec.ucsize;
  471.             if (crec.general_purpose_bit_flag & 1)
  472.                 tot_csize -= 12;   /* don't count encryption header */
  473.             ++members;
  474.  
  475.         } else {   /* not listing */
  476.             SKIP_(crec.extra_field_length)
  477.             SKIP_(crec.file_comment_length)
  478.  
  479.         } /* end if (list member?) */
  480.  
  481.     } /* end for-loop (j: member files) */
  482.  
  483. /*---------------------------------------------------------------------------
  484.     Double check that we're back at the end-of-central-directory record.
  485.   ---------------------------------------------------------------------------*/
  486.  
  487.     if (readbuf(sig, 4) <= 0)  /* disk error? */
  488.         return PK_EOF;
  489.     if (strncmp(sig, end_central_sig, 4)) {     /* just to make sure again */
  490.         fprintf(stderr, EndSigMsg);  /* didn't find end-of-central-dir sig */
  491.         error_in_archive = PK_WARN;
  492.     }
  493.  
  494. /*---------------------------------------------------------------------------
  495.     Check that we actually found requested files; if so, print totals.
  496.   ---------------------------------------------------------------------------*/
  497.  
  498.     if (tflag) {
  499.         char *sgn = "";
  500.         int cfactor = ratio(tot_ucsize, tot_csize);
  501.  
  502.         if (cfactor < 0) {
  503.             sgn = "-";
  504.             cfactor = -cfactor;
  505.         }
  506.         printf(
  507.         "%d file%s, %lu bytes uncompressed, %lu bytes compressed:  %s%d.%d%%\n",
  508.           members, (members==1)? "":"s", tot_ucsize, tot_csize, sgn, cfactor/10,
  509.           cfactor%10);
  510.     }
  511.  
  512. /*---------------------------------------------------------------------------
  513.     Check for unmatched filespecs on command line and print warning if any
  514.     found.
  515.   ---------------------------------------------------------------------------*/
  516.  
  517.     if (fn_matched) {
  518.         for (j = 0;  j < filespecs;  ++j)
  519.             if (!fn_matched[j])
  520.                 fprintf(stderr, "caution: filename not matched:  %s\n",
  521.                   pfnames[j]);
  522.         free(fn_matched);
  523.     }
  524.     if (xn_matched) {
  525.         for (j = 0;  j < xfilespecs;  ++j)
  526.             if (!xn_matched[j])
  527.                 fprintf(stderr, "caution: excluded filename not matched:  %s\n",
  528.                   pxnames[j]);
  529.         free(xn_matched);
  530.     }
  531.  
  532.     return error_in_archive;
  533.  
  534. } /* end function zipinfo() */
  535.  
  536.  
  537.  
  538.  
  539.  
  540. /************************/
  541. /*  Function zi_long()  */
  542. /************************/
  543.  
  544. static int zi_long()   /* return PK-type error code */
  545. {
  546.     int          error, error_in_archive=PK_COOL;
  547.     ush          hostnum, hostver, extnum, extver, methnum, xattr;
  548.     char         workspace[12], attribs[22];
  549.     static char  *os[NUM_HOSTS+1] = {"MS-DOS, OS/2 or NT FAT", "Amiga",
  550.                      "VAX VMS", "Unix", "VM/CMS", "Atari ST", "OS/2 or NT HPFS",
  551.                      "Macintosh", "Z-System", "CP/M", "TOPS-20", "NT NTFS",
  552.                      "unknown" };
  553.     static char  *method[NUM_METHODS+1] = {"none (stored)", "shrunk",
  554.                      "reduced (factor 1)", "reduced (factor 2)",
  555.                      "reduced (factor 3)", "reduced (factor 4)",
  556.                      "imploded", "tokenized", "deflated", unkn};
  557.     static char  *dtype[4] = {"normal", "maximum", "fast", "superfast"};
  558.  
  559.  
  560. /*---------------------------------------------------------------------------
  561.     Print out various interesting things about the compressed file.
  562.   ---------------------------------------------------------------------------*/
  563.  
  564.     hostnum = MIN(crec.version_made_by[1], NUM_HOSTS);
  565.     hostver = crec.version_made_by[0];
  566.     extnum = MIN(crec.version_needed_to_extract[1], NUM_HOSTS);
  567.     extver = crec.version_needed_to_extract[0];
  568.     methnum = MIN(crec.compression_method, NUM_METHODS);
  569.     if (methnum == NUM_METHODS)
  570.         sprintf(unkn, "unknown (%d)", crec.compression_method);
  571.  
  572.     putchar(' ');  putchar(' ');  fnprint((uch *)filename);
  573.  
  574.     printf("\n  host operating system (created on):               %s\n",
  575.       os[hostnum]);
  576.     printf("  version of encoding software:                     %d.%d\n",
  577.       hostver/10, hostver%10);
  578.     printf("  minimum operating system compatibility required:  %s\n",
  579.       os[extnum]);
  580.     printf("  minimum software version required to extract:     %d.%d\n",
  581.       extver/10, extver%10);
  582.     printf("  compression method:                               %s\n",
  583.       method[methnum]);
  584.     if (methnum == IMPLODED) {
  585.         printf("  size of sliding dictionary (implosion):           %cK\n",
  586.           (crec.general_purpose_bit_flag & 2)? '8' : '4');
  587.         printf("  number of Shannon-Fano trees (implosion):         %c\n",
  588.           (crec.general_purpose_bit_flag & 4)? '3' : '2');
  589.     } else if (methnum == DEFLATED) {
  590.         ush  dnum=(crec.general_purpose_bit_flag>>1) & 3;
  591.         printf("  compression sub-type (deflation):                 %s\n",
  592.           dtype[dnum]);
  593.     }
  594.     printf("  file security status:                             %sencrypted\n",
  595.       (crec.general_purpose_bit_flag & 1)? "" : "not ");
  596.     printf("  extended local header:                            %s\n",
  597.       (crec.general_purpose_bit_flag & 8)? "yes" : "no");
  598.     /* print upper 3 bits for amusement? */
  599.     printf("  file last modified on:                            %s\n",
  600.       zi_time(&crec.last_mod_file_date, &crec.last_mod_file_time));
  601.     printf("  32-bit CRC value (hex):                           %.8lx\n",
  602.       crec.crc32);
  603.     printf("  compressed size:                                  %lu bytes\n",
  604.       crec.csize);
  605.     printf("  uncompressed size:                                %lu bytes\n",
  606.       crec.ucsize);
  607.     printf("  length of filename:                               %u characters\n",
  608.       crec.filename_length);
  609.     printf("  length of extra field:                            %u bytes\n",
  610.       crec.extra_field_length);
  611.     printf("  length of file comment:                           %u characters\n",
  612.       crec.file_comment_length);
  613.     printf("  disk number on which file begins:                 disk %u\n",
  614.       crec.disk_number_start);
  615.     printf("  apparent file type:                               %s\n",
  616.       (crec.internal_file_attributes & 1)? "text" : "binary");
  617. /*
  618.     printf("  external file attributes (hex):                   %.8lx\n",
  619.       crec.external_file_attributes);
  620.  */
  621.     xattr = (ush)((crec.external_file_attributes >> 16) & 0xFFFF);
  622.     if (hostnum == VMS_) {
  623.         char   *p=attribs, *q=attribs+1;
  624.         int    i, j, k;
  625.  
  626.         for (k = 0;  k < 12;  ++k)
  627.             workspace[k] = 0;
  628.         if (xattr & VMS_IRUSR)
  629.             workspace[0] = 'R';
  630.         if (xattr & VMS_IWUSR) {
  631.             workspace[1] = 'W';
  632.             workspace[3] = 'D';
  633.         }
  634.         if (xattr & VMS_IXUSR)
  635.             workspace[2] = 'E';
  636.         if (xattr & VMS_IRGRP)
  637.             workspace[4] = 'R';
  638.         if (xattr & VMS_IWGRP) {
  639.             workspace[5] = 'W';
  640.             workspace[7] = 'D';
  641.         }
  642.         if (xattr & VMS_IXGRP)
  643.             workspace[6] = 'E';
  644.         if (xattr & VMS_IROTH)
  645.             workspace[8] = 'R';
  646.         if (xattr & VMS_IWOTH) {
  647.             workspace[9] = 'W';
  648.             workspace[11] = 'D';
  649.         }
  650.         if (xattr & VMS_IXOTH)
  651.             workspace[10] = 'E';
  652.  
  653.         *p++ = '(';
  654.         for (k = j = 0;  j < 3;  ++j) {    /* loop over groups of permissions */
  655.             for (i = 0;  i < 4;  ++i, ++k)  /* loop over perms within a group */
  656.                 if (workspace[k])
  657.                     *p++ = workspace[k];
  658.             *p++ = ',';                       /* group separator */
  659.             if (j == 0)
  660.                 while ((*p++ = *q++) != ','); /* system, owner perms are same */
  661.         }
  662.         *p-- = 0;
  663.         *p = ')';   /* overwrite last comma */
  664.         printf("  VMS file attributes (%06o octal):               %s\n",
  665.           xattr, attribs);
  666.  
  667.     } else if (hostnum == AMIGA_) {
  668.         switch (xattr & AMI_IFMT) {
  669.             case AMI_IFDIR:  attribs[0] = 'd';  break;
  670.             case AMI_IFREG:  attribs[0] = '-';  break;
  671.             default:         attribs[0] = '?';  break;
  672.         }
  673.         attribs[1] = (xattr & AMI_IHIDDEN)?   'h' : '-';
  674.         attribs[2] = (xattr & AMI_ISCRIPT)?   's' : '-';
  675.         attribs[3] = (xattr & AMI_IPURE)?     'p' : '-';
  676.         attribs[4] = (xattr & AMI_IARCHIVE)?  'a' : '-';
  677.         attribs[5] = (xattr & AMI_IREAD)?     'r' : '-';
  678.         attribs[6] = (xattr & AMI_IWRITE)?    'w' : '-';
  679.         attribs[7] = (xattr & AMI_IEXECUTE)?  'e' : '-';
  680.         attribs[8] = (xattr & AMI_IDELETE)?   'd' : '-';
  681.         attribs[9] = 0;   /* better dlm the string */
  682.         printf("  Amiga file attributes (%06o octal):             %s\n",
  683.           xattr, attribs);
  684.  
  685.     } else if ((hostnum != FS_FAT_) && (hostnum != FS_HPFS_) &&
  686.         (hostnum != FS_NTFS_)) {
  687.         /* assume Unix-like */
  688.         switch (xattr & UNX_IFMT) {
  689.             case UNX_IFDIR:   attribs[0] = 'd';  break;
  690.             case UNX_IFREG:   attribs[0] = '-';  break;
  691.             case UNX_IFLNK:   attribs[0] = 'l';  break;
  692.             case UNX_IFBLK:   attribs[0] = 'b';  break;
  693.             case UNX_IFCHR:   attribs[0] = 'c';  break;
  694.             case UNX_IFIFO:   attribs[0] = 'p';  break;
  695.             case UNX_IFSOCK:  attribs[0] = 's';  break;
  696.             default:          attribs[0] = '?';  break;
  697.         }
  698.         attribs[1] = (xattr & UNX_IRUSR)? 'r' : '-';
  699.         attribs[4] = (xattr & UNX_IRGRP)? 'r' : '-';
  700.         attribs[7] = (xattr & UNX_IROTH)? 'r' : '-';
  701.  
  702.         attribs[2] = (xattr & UNX_IWUSR)? 'w' : '-';
  703.         attribs[5] = (xattr & UNX_IWGRP)? 'w' : '-';
  704.         attribs[8] = (xattr & UNX_IWOTH)? 'w' : '-';
  705.  
  706.         if (xattr & UNX_IXUSR)
  707.             attribs[3] = (xattr & UNX_ISUID)? 's' : 'x';
  708.         else
  709.             attribs[3] = (xattr & UNX_ISUID)? 'S' : '-';   /* S = undefined */
  710.         if (xattr & UNX_IXGRP)
  711.             attribs[6] = (xattr & UNX_ISGID)? 's' : 'x';   /* == UNX_ENFMT */
  712.         else
  713.             attribs[6] = (xattr & UNX_ISGID)? 'l' : '-';
  714.         if (xattr & UNX_IXOTH)
  715.             attribs[9] = (xattr & UNX_ISVTX)? 't' : 'x';   /* "sticky bit" */
  716.         else
  717.             attribs[9] = (xattr & UNX_ISVTX)? 'T' : '-';   /* T = undefined */
  718.         attribs[10] = 0;
  719.  
  720.         printf("  Unix file attributes (%06o octal):              %s\n",
  721.           xattr, attribs);
  722.  
  723.     } else {
  724.         printf("  non-MSDOS external file attributes:               %06lX hex\n"
  725.           , crec.external_file_attributes >> 8);
  726.  
  727.     } /* endif (hostnum: external attributes format) */
  728.  
  729.     if ((xattr=(ush)(crec.external_file_attributes & 0xFF)) == 0)
  730.         printf("  MS-DOS file attributes (%02X hex):                  none\n",
  731.           xattr);
  732.     else if (xattr == 1)
  733.         printf(
  734.           "  MS-DOS file attributes (%02X hex):                  read-only\n",
  735.           xattr);
  736.     else
  737.         printf(
  738.          "  MS-DOS file attributes (%02X hex):                  %s%s%s%s%s%s\n",
  739.           xattr, (xattr&1)?"rdo ":"", (xattr&2)?"hid ":"", (xattr&4)?"sys ":"",
  740.           (xattr&8)?"lab ":"", (xattr&16)?"dir ":"", (xattr&32)?"arc":"");
  741.     printf(
  742.      "  offset of local header from start of archive:     %lu (%.8lXh) bytes\n",
  743.       crec.relative_offset_local_header, crec.relative_offset_local_header);
  744.  
  745. /*---------------------------------------------------------------------------
  746.     Skip the extra field, if any, and print the file comment, if any (the
  747.     filename has already been printed, above).  That finishes up this file
  748.     entry...
  749.   ---------------------------------------------------------------------------*/
  750.  
  751.     if (crec.extra_field_length > 0) {
  752. /* #ifdef OS2 */
  753. #if TRUE
  754.         ulg ea_size;
  755.         if ((error = do_string(crec.extra_field_length, EXTRA_FIELD)) != 0) {
  756.             error_in_archive = error;
  757.             if (error > PK_WARN)   /* fatal:  can't continue */
  758.                 return error;
  759.         }
  760.         if ((ea_size = SizeOfEAs(extra_field)) != 0)
  761.             printf("\n\
  762.   This file has %lu bytes of OS/2 EA's in the local extra field.\n\
  763.   (May not match OS/2 \"dir\" amount due to storage method.)\n\n",
  764.               ea_size);
  765.         else
  766.             printf("\n  There is an unknown extra field (skipping).\n");
  767. #else
  768.         printf("\n  There is an extra field (skipping).\n");
  769.         SKIP_(crec.extra_field_length)
  770. #endif
  771.     } else
  772.         printf("\n");
  773.  
  774.     if (!crec.file_comment_length)
  775.         printf("  There is no file comment.\n");
  776.     else {
  777.         printf("\
  778. ------------------------- file comment begins ----------------------------\n");
  779.         if ((error = do_string(crec.file_comment_length, DISPLAY)) != PK_COOL) {
  780.           error_in_archive = error;   /* might be warning */
  781.           if (error > PK_WARN)   /* fatal */
  782.               return error;
  783.         }
  784.         printf("\
  785. -------------------------- file comment ends -----------------------------\n");
  786.     }
  787.  
  788.     return error_in_archive;
  789.  
  790. } /* end function zi_long() */
  791.  
  792.  
  793.  
  794.  
  795.  
  796. /*************************/
  797. /*  Function zi_short()  */
  798. /*************************/
  799.  
  800. static int zi_short()   /* return PK-type error code */
  801. {
  802.     int           k, error, error_in_archive=PK_COOL;
  803.     ush           methnum, hostnum, hostver, xattr;
  804.     char          *p, workspace[12], attribs[16];
  805.     static char   impl[5]="i#:#", defl[5]="def#";
  806.     static char   dtype[5]="NXFS";  /* normal, maximum, fast, superfast */
  807.     static char   *os[NUM_HOSTS+1] = {"fat", "ami", "vms", "unx", "cms",
  808.                       "atr", "hpf", "mac", "zzz", "cpm", "t20", "ntf", "???" };
  809.     static char   *method[NUM_METHODS+1] = {"stor", "shrk", "re:1", "re:2",
  810.                       "re:3", "re:4", impl, "tokn", defl, unkn};
  811.  
  812.  
  813. /*---------------------------------------------------------------------------
  814.     Print out various interesting things about the compressed file.
  815.   ---------------------------------------------------------------------------*/
  816.  
  817.     methnum = MIN(crec.compression_method, NUM_METHODS);
  818.     hostnum = MIN(crec.version_made_by[1], NUM_HOSTS);
  819.     hostver = crec.version_made_by[0];
  820. /*
  821.     extnum = MIN(crec.version_needed_to_extract[1], NUM_HOSTS);
  822.     extver = crec.version_needed_to_extract[0];
  823.  */
  824.  
  825.     if (methnum == IMPLODED) {
  826.         impl[1] = (char) ((crec.general_purpose_bit_flag & 2)? '8' : '4');
  827.         impl[3] = (char) ((crec.general_purpose_bit_flag & 4)? '3' : '2');
  828.     } else if (methnum == DEFLATED) {
  829.         ush  dnum=(crec.general_purpose_bit_flag>>1) & 3;
  830.         defl[3] = dtype[dnum];
  831.     } else if (methnum == NUM_METHODS) {   /* unknown */
  832.         sprintf(unkn, "u%03d", crec.compression_method);
  833.     }
  834.  
  835.     for (k = 0;  k < 15;  ++k)
  836.         attribs[k] = ' ';
  837.     attribs[15] = 0;
  838.  
  839.     xattr = (ush)((crec.external_file_attributes >> 16) & 0xFFFF);
  840.     switch (hostnum) {
  841.         case VMS_:
  842.             {   int    i, j;
  843.  
  844.                 for (k = 0;  k < 12;  ++k)
  845.                     workspace[k] = 0;
  846.                 if (xattr & VMS_IRUSR)
  847.                     workspace[0] = 'R';
  848.                 if (xattr & VMS_IWUSR) {
  849.                     workspace[1] = 'W';
  850.                     workspace[3] = 'D';
  851.                 }
  852.                 if (xattr & VMS_IXUSR)
  853.                     workspace[2] = 'E';
  854.                 if (xattr & VMS_IRGRP)
  855.                     workspace[4] = 'R';
  856.                 if (xattr & VMS_IWGRP) {
  857.                     workspace[5] = 'W';
  858.                     workspace[7] = 'D';
  859.                 }
  860.                 if (xattr & VMS_IXGRP)
  861.                   workspace[6] = 'E';
  862.                 if (xattr & VMS_IROTH)
  863.                     workspace[8] = 'R';
  864.                 if (xattr & VMS_IWOTH) {
  865.                     workspace[9] = 'W';
  866.                     workspace[11] = 'D';
  867.                 }
  868.                 if (xattr & VMS_IXOTH)
  869.                     workspace[10] = 'E';
  870.  
  871.                 p = attribs;
  872.                 for (k = j = 0;  j < 3;  ++j) {     /* groups of permissions */
  873.                     for (i = 0;  i < 4;  ++i, ++k)  /* perms within a group */
  874.                         if (workspace[k])
  875.                             *p++ = workspace[k];
  876.                     *p++ = ',';                     /* group separator */
  877.                 }
  878.                 *--p = ' ';   /* overwrite last comma */
  879.                 if ((p - attribs) < 12)
  880.                     sprintf(&attribs[12], "%d.%d", hostver/10, hostver%10);
  881.             }
  882.             break;
  883.  
  884.         case FS_FAT_:
  885.         case FS_HPFS_:
  886.         case FS_NTFS_:
  887.             xattr = (ush)(crec.external_file_attributes & 0xFF);
  888.             sprintf(attribs, ".r.-...     %d.%d", hostver/10, hostver%10);
  889.             attribs[2] = (xattr & 0x01)? '-' : 'w';
  890.             attribs[5] = (xattr & 0x02)? 'h' : '-';
  891.             attribs[6] = (xattr & 0x04)? 's' : '-';
  892.             attribs[0] = (xattr & 0x10)? 'd' : '-';
  893.             attribs[4] = (xattr & 0x20)? 'a' : '-';
  894.             if (IS_VOLID(xattr))
  895.                 attribs[0] = 'V';
  896.             else if ((p = strrchr(filename, '.')) != NULL) {
  897.                 ++p;
  898.                 if (STRNICMP(p, "com", 3) == 0 || STRNICMP(p, "exe", 3) == 0 ||
  899.                     STRNICMP(p, "btm", 3) == 0 || STRNICMP(p, "cmd", 3) == 0 ||
  900.                     STRNICMP(p, "bat", 3) == 0)
  901.                     attribs[3] = 'x';
  902.             }
  903.             break;
  904.  
  905.         case AMIGA_:
  906.             switch (xattr & AMI_IFMT) {
  907.                 case AMI_IFDIR:  attribs[0] = 'd';  break;
  908.                 case AMI_IFREG:  attribs[0] = '-';  break;
  909.                 default:         attribs[0] = '?';  break;
  910.             }
  911.             attribs[1] = (xattr & AMI_IHIDDEN)?   'h' : '-';
  912.             attribs[2] = (xattr & AMI_ISCRIPT)?   's' : '-';
  913.             attribs[3] = (xattr & AMI_IPURE)?     'p' : '-';
  914.             attribs[4] = (xattr & AMI_IARCHIVE)?  'a' : '-';
  915.             attribs[5] = (xattr & AMI_IREAD)?     'r' : '-';
  916.             attribs[6] = (xattr & AMI_IWRITE)?    'w' : '-';
  917.             attribs[7] = (xattr & AMI_IEXECUTE)?  'e' : '-';
  918.             attribs[8] = (xattr & AMI_IDELETE)?   'd' : '-';
  919.             sprintf(&attribs[12], "%d.%d", hostver/10, hostver%10);
  920.             break;
  921.  
  922.         default:   /* assume Unix-like */
  923.             switch (xattr & UNX_IFMT) {
  924.                 case UNX_IFDIR:   attribs[0] = 'd';  break;
  925.                 case UNX_IFREG:   attribs[0] = '-';  break;
  926.                 case UNX_IFLNK:   attribs[0] = 'l';  break;
  927.                 case UNX_IFBLK:   attribs[0] = 'b';  break;
  928.                 case UNX_IFCHR:   attribs[0] = 'c';  break;
  929.                 case UNX_IFIFO:   attribs[0] = 'p';  break;
  930.                 case UNX_IFSOCK:  attribs[0] = 's';  break;
  931.                 default:          attribs[0] = '?';  break;
  932.             }
  933.             attribs[1] = (xattr & UNX_IRUSR)? 'r' : '-';
  934.             attribs[4] = (xattr & UNX_IRGRP)? 'r' : '-';
  935.             attribs[7] = (xattr & UNX_IROTH)? 'r' : '-';
  936.             attribs[2] = (xattr & UNX_IWUSR)? 'w' : '-';
  937.             attribs[5] = (xattr & UNX_IWGRP)? 'w' : '-';
  938.             attribs[8] = (xattr & UNX_IWOTH)? 'w' : '-';
  939.  
  940.             if (xattr & UNX_IXUSR)
  941.                 attribs[3] = (xattr & UNX_ISUID)? 's' : 'x';
  942.             else
  943.                 attribs[3] = (xattr & UNX_ISUID)? 'S' : '-';  /* S==undefined */
  944.             if (xattr & UNX_IXGRP)
  945.                 attribs[6] = (xattr & UNX_ISGID)? 's' : 'x';  /* == UNX_ENFMT */
  946.             else
  947.                 /* attribs[6] = (xattr & UNX_ISGID)? 'l' : '-';  real 4.3BSD */
  948.                 attribs[6] = (xattr & UNX_ISGID)? 'S' : '-';  /* SunOS 4.1.x */
  949.             if (xattr & UNX_IXOTH)
  950.                 attribs[9] = (xattr & UNX_ISVTX)? 't' : 'x';  /* "sticky bit" */
  951.             else
  952.                 attribs[9] = (xattr & UNX_ISVTX)? 'T' : '-';  /* T==undefined */
  953.  
  954.             sprintf(&attribs[12], "%d.%d", hostver/10, hostver%10);
  955.             break;
  956.  
  957.     } /* end switch (hostnum: external attributes format) */
  958.  
  959.     printf("%s %s %7lu %c%c", attribs, os[hostnum], crec.ucsize,
  960.       (crec.general_purpose_bit_flag & 1)?
  961.       ((crec.internal_file_attributes & 1)? 'T' : 'B') :   /* encrypted */
  962.       ((crec.internal_file_attributes & 1)? 't' : 'b'),    /* plaintext */
  963.       (crec.general_purpose_bit_flag & 8)? (crec.extra_field_length? 'X' : 'l')
  964.                                         : (crec.extra_field_length? 'x' : '-'));
  965.     if (lflag == 4) {
  966.         ulg csiz = crec.csize;
  967.  
  968.         if (crec.general_purpose_bit_flag & 1)
  969.             csiz -= 12;    /* if encrypted, don't count encryption header */
  970.         printf("%3d%%", (ratio(crec.ucsize,csiz) + 5)/10);
  971.     } else if (lflag == 5)
  972.         printf(" %7lu", crec.csize);
  973.  
  974.     printf(" %s %s ", method[methnum],
  975.       zi_time(&crec.last_mod_file_date, &crec.last_mod_file_time));
  976.     fnprint((uch *)filename);
  977.  
  978. /*---------------------------------------------------------------------------
  979.     Skip the extra field and/or the file comment, if any (the filename has
  980.     already been printed, above).  That finishes up this file entry...
  981.   ---------------------------------------------------------------------------*/
  982.  
  983.     SKIP_(crec.extra_field_length)
  984.     SKIP_(crec.file_comment_length)
  985.  
  986.     return error_in_archive;
  987.  
  988. } /* end function zi_short() */
  989.  
  990.  
  991.  
  992.  
  993.  
  994. /************************/
  995. /*  Function zi_time()  */
  996. /************************/
  997.  
  998. static char *zi_time(datez, timez)
  999.     ush *datez, *timez;
  1000. {
  1001.     ush          yr, mo, dy, hh, mm, ss;
  1002.     static char  d_t_str[21];
  1003.     static char  *month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  1004.                                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  1005.  
  1006.  
  1007.  
  1008. /*---------------------------------------------------------------------------
  1009.     Convert the file-modification date and time info to a string of the form
  1010.     "1991 Feb 23 17:15:00" or "23-Feb-91 17:15," depending on value of lflag.
  1011.   ---------------------------------------------------------------------------*/
  1012.  
  1013.     yr = ((*datez >> 9) & 0x7f) + 80;
  1014.     mo = ((*datez >> 5) & 0x0f) - 1;
  1015.     dy = *datez & 0x1f;
  1016.  
  1017.     hh = (*timez >> 11) & 0x1f;
  1018.     mm = (*timez >> 5) & 0x3f;
  1019.     ss = (*timez & 0x1f) * 2;
  1020.  
  1021.     if ((lflag >= 3) && (lflag <= 5))
  1022.         sprintf(d_t_str, "%2u-%s-%u %02u:%02u", dy, month[mo], yr, hh, mm);
  1023.     else if (lflag > 9)  /* verbose listing format */
  1024.         sprintf(d_t_str, "%u %s %u %02u:%02u:%02u", yr+1900, month[mo], dy,
  1025.           hh, mm, ss);
  1026.  
  1027.     return d_t_str;
  1028.  
  1029. } /* end function zi_time() */
  1030.  
  1031. #endif /* !NO_ZIPINFO */
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037. #if (!defined(NO_ZIPINFO) || defined(OS2))
  1038.  
  1039. #define EAID   0x0009      /* OS/2 extended-attributes extra-field ID, for */
  1040. typedef struct {           /*  OS/2 zipfile info in both OS/2 and non-OS2 */
  1041.     unsigned short nID;    /*  environments */
  1042.     unsigned short nSize;
  1043.     ulg lSize;             /* GRR:  need to do same for VMS, Mac, etc. */
  1044. } EAHEADER, *PEAHEADER;
  1045.  
  1046. /**************************/
  1047. /*  Function SizeOfEAs()  */
  1048. /**************************/
  1049.  
  1050. ulg SizeOfEAs(extra_field)
  1051.     void   *extra_field;
  1052. {
  1053.     EAHEADER *pEAblock = (PEAHEADER)extra_field;
  1054.  
  1055.     if (extra_field != NULL  &&  pEAblock->nID == EAID)
  1056.         return pEAblock->lSize;
  1057.  
  1058.     return 0L;
  1059. }
  1060.  
  1061. #endif /* !NO_ZIPINFO || OS2 */
  1062.  
  1063.  
  1064.  
  1065.  
  1066.  
  1067. /* also referenced in UpdateListBox() in updatelb.c (Windows version) */
  1068. char *Headers[][2] = {
  1069.     {" Length    Date    Time    Name",
  1070.      " ------    ----    ----    ----"},
  1071.     {" Length  Method   Size  Ratio   Date    Time   CRC-32     Name",
  1072.      " ------  ------   ----  -----   ----    ----   ------     ----"}
  1073. };
  1074.  
  1075. /*************************/
  1076. /* Function list_files() */
  1077. /*************************/
  1078.  
  1079. int list_files()    /* return PK-type error code */
  1080. {
  1081.     char sgn, cfactorstr[10];
  1082.     int do_this_file=FALSE, cfactor, error, error_in_archive=PK_COOL;
  1083.     int longhdr=(vflag>1), date_format, methnum;
  1084.     ush j, yr, mo, dy, hh, mm, members=0;
  1085.     ulg csiz, tot_csize=0L, tot_ucsize=0L;
  1086. #ifdef OS2
  1087.     ulg ea_size, tot_easize=0L, tot_eafiles=0L;
  1088. #endif
  1089. #ifdef MSWIN
  1090.     PSTR psLBEntry;  /* list box entry */
  1091. #endif
  1092.     min_info info;
  1093.     static char defl[]="Defl:#", dtype[]="NXFS";   /* see zi_short() */
  1094.     static char *method[NUM_METHODS+1] =
  1095.         {"Stored", "Shrunk", "Reduce1", "Reduce2", "Reduce3", "Reduce4",
  1096.          "Implode", "Token", defl, unkn};
  1097.  
  1098.  
  1099.  
  1100. /*---------------------------------------------------------------------------
  1101.     Unlike extract_or_test_files(), this routine confines itself to the cen-
  1102.     tral directory.  Thus its structure is somewhat simpler, since we can do
  1103.     just a single loop through the entire directory, listing files as we go.
  1104.  
  1105.     So to start off, print the heading line and then begin main loop through
  1106.     the central directory.  The results will look vaguely like the following:
  1107.  
  1108.   Length  Method   Size  Ratio   Date    Time   CRC-32     Name ("^" ==> case
  1109.   ------  ------   ----  -----   ----    ----   ------     ----   conversion)
  1110.    44004  Implode  13041  71%  11-02-89  19:34  8b4207f7   Makefile.UNIX
  1111.     3438  Shrunk    2209  36%  09-15-90  14:07  a2394fd8  ^dos-file.ext
  1112.   ---------------------------------------------------------------------------*/
  1113.  
  1114.     pInfo = &info;
  1115.     date_format = DATE_FORMAT;
  1116.  
  1117. #ifndef MSWIN
  1118.     if (qflag < 2)
  1119.         if (U_flag)
  1120.             printf("%s\n%s\n", Headers[longhdr][0], Headers[longhdr][1]);
  1121.         else
  1122.             printf("%s (\"^\" ==> case\n%s   conversion)\n",
  1123.               Headers[longhdr][0], Headers[longhdr][1]);
  1124. #endif /* !MSWIN */
  1125.  
  1126.     for (j = 0; j < ecrec.total_entries_central_dir; ++j) {
  1127.  
  1128.         if (readbuf(sig, 4) <= 0)
  1129.             return PK_EOF;
  1130.         if (strncmp(sig, central_hdr_sig, 4)) {  /* just to make sure */
  1131.             fprintf(stderr, CentSigMsg, j);  /* sig not found */
  1132.             fprintf(stderr, ReportMsg);   /* check binary transfers */
  1133.             return PK_BADERR;
  1134.         }
  1135.         /* process_cdir_file_hdr() sets pInfo->lcflag: */
  1136.         if ((error = process_cdir_file_hdr()) != PK_COOL)
  1137.             return error;       /* only PK_EOF defined */
  1138.  
  1139.         /*
  1140.          * We could DISPLAY the filename instead of storing (and possibly trun-
  1141.          * cating, in the case of a very long name) and printing it, but that
  1142.          * has the disadvantage of not allowing case conversion--and it's nice
  1143.          * to be able to see in the listing precisely how you have to type each
  1144.          * filename in order for unzip to consider it a match.  Speaking of
  1145.          * which, if member names were specified on the command line, check in
  1146.          * with match() to see if the current file is one of them, and make a
  1147.          * note of it if it is.
  1148.          */
  1149.  
  1150.         if ((error = do_string(crec.filename_length, FILENAME)) != PK_COOL) {
  1151.             error_in_archive = error;             /*  ^--(uses pInfo->lcflag) */
  1152.             if (error > PK_WARN)   /* fatal:  can't continue */
  1153.                 return error;
  1154.         }
  1155.         if (extra_field != (uch *)NULL) {
  1156.             free(extra_field);
  1157.             extra_field = (uch *)NULL;
  1158.         }
  1159.         if ((error = do_string(crec.extra_field_length, EXTRA_FIELD)) != 0) {
  1160.             error_in_archive = error;  
  1161.             if (error > PK_WARN)      /* fatal */
  1162.                 return error;
  1163.         }
  1164.         if (!process_all_files) {   /* check if specified on command line */
  1165.             char **pfn = pfnames-1;
  1166.  
  1167.             do_this_file = FALSE;
  1168.             while (*++pfn)
  1169.                 if (match(filename, *pfn, pInfo->lcflag)) {
  1170.                     do_this_file = TRUE;
  1171.                     break;       /* found match, so stop looping */
  1172.                 }
  1173.             if (do_this_file) {  /* check if this is an excluded file */
  1174.                 char **pxn = pxnames-1;
  1175.  
  1176.                 while (*++pxn)
  1177.                     if (match(filename, *pxn, pInfo->lcflag)) {
  1178.                         do_this_file = FALSE;
  1179.                         break;
  1180.                     }
  1181.             }
  1182.         }
  1183.         /*
  1184.          * If current file was specified on command line, or if no names were
  1185.          * specified, do the listing for this file.  Otherwise, get rid of the
  1186.          * file comment and go back for the next file.
  1187.          */
  1188.  
  1189.         if (process_all_files || do_this_file) {
  1190.  
  1191.             yr = (((crec.last_mod_file_date >> 9) & 0x7f) + 80) % (unsigned)100;
  1192.             mo = (crec.last_mod_file_date >> 5) & 0x0f;
  1193.             dy = crec.last_mod_file_date & 0x1f;
  1194.  
  1195.             /* permute date so it displays according to national convention */
  1196.             switch (date_format) {
  1197.                 case DF_YMD:
  1198.                     hh = mo; mo = yr; yr = dy; dy = hh;
  1199.                     break;
  1200.                 case DF_DMY:
  1201.                     hh = mo; mo = dy; dy = hh;
  1202.             }
  1203.             hh = (crec.last_mod_file_time >> 11) & 0x1f;
  1204.             mm = (crec.last_mod_file_time >> 5) & 0x3f;
  1205.  
  1206.             csiz = crec.csize;
  1207.             if (crec.general_purpose_bit_flag & 1)
  1208.                 csiz -= 12;   /* if encrypted, don't count encryption header */
  1209.             if ((cfactor = ratio(crec.ucsize, csiz)) < 0) {
  1210.                 sgn = '-';
  1211.                 cfactor = (-cfactor + 5) / 10;
  1212.             } else {
  1213.                 sgn = ' ';
  1214.                 cfactor = (cfactor + 5) / 10;
  1215.             }
  1216.             sprintf(cfactorstr, "%c%d%%", sgn, cfactor);
  1217.  
  1218.             methnum = MIN(crec.compression_method, NUM_METHODS);
  1219.             if (methnum == DEFLATED)
  1220.                 defl[5] = dtype[(crec.general_purpose_bit_flag>>1) & 3];
  1221.             else if (methnum == NUM_METHODS)
  1222.                 sprintf(unkn, "Unk:%03d", crec.compression_method);
  1223.  
  1224. #if 0       /* GRR/Euro:  add this? */
  1225. #if defined(DOS_NT_OS2) || defined(UNIX)
  1226.             for (p = filename;  *p;  ++p)
  1227.                 if (!isprint(*p))
  1228.                     *p = '?';  /* change non-printable chars to '?' */
  1229. #endif /* DOS_NT_OS2 || UNIX */
  1230. #endif /* 0 */
  1231.  
  1232. #ifdef MSWIN
  1233. #ifdef NEED_EARLY_REDRAW
  1234.             /* turn on listbox redrawing just before adding last line */
  1235.             if (j == (ecrec.total_entries_central_dir-1))
  1236.                 (void)SendMessage(hWndList, WM_SETREDRAW, TRUE, 0L);
  1237. #endif /* NEED_EARLY_REDRAW */
  1238.             psLBEntry =
  1239.               (PSTR)LocalAlloc(LMEM_FIXED, FILNAMSIZ+LONG_FORM_FNAME_INX);
  1240.             /* GRR:  does OemToAnsi filter out escape and CR characters? */
  1241.             OemToAnsi(filename, filename);  /* translate to ANSI */
  1242.             if (longhdr) {
  1243.                 wsprintf(psLBEntry, 
  1244.                  "%7lu  %-7s%7lu %4s  %02u-%02u-%02u  %02u:%02u  %08lx  %c%s",
  1245.                   crec.ucsize, (LPSTR)method[methnum], csiz, cfactorstr,
  1246.                   mo, dy, yr, hh, mm, crec.crc32, (pInfo->lcflag?'^':' '),
  1247.                   (LPSTR)filename);
  1248.                 SendMessage(hWndList, LB_ADDSTRING, 0,
  1249.                   (LONG)(LPSTR)psLBEntry);
  1250.             } else {
  1251.                 wsprintf(psLBEntry, "%7lu  %02u-%02u-%02u  %02u:%02u  %c%s",
  1252.                   crec.ucsize, mo, dy, yr, hh, mm, (pInfo->lcflag?'^':' '),
  1253.                   (LPSTR)filename);
  1254.                 SendMessage(hWndList, LB_ADDSTRING, 0,
  1255.                   (LONG)(LPSTR)psLBEntry);
  1256.             }
  1257.             LocalFree((HANDLE)psLBEntry);
  1258. #else /* !MSWIN */
  1259.             if (longhdr)
  1260.                 printf(
  1261.               "%7lu  %-7s%7lu %4s  %02u-%02u-%02u  %02u:%02u  %08lx  %c",
  1262.                   crec.ucsize, method[methnum], csiz, cfactorstr, mo, dy, yr,
  1263.                   hh, mm, crec.crc32, (pInfo->lcflag?'^':' '));
  1264.             else
  1265.                 printf("%7lu  %02u-%02u-%02u  %02u:%02u  %c", crec.ucsize,
  1266.                   mo, dy, yr, hh, mm, (pInfo->lcflag?'^':' '));
  1267.             fnprint((uch *)filename);
  1268. #endif /* ?MSWIN */
  1269.  
  1270.             error = do_string(crec.file_comment_length, QCOND? DISPLAY : SKIP);
  1271.             if (error) {
  1272.                 error_in_archive = error;  /* might be just warning */
  1273.                 if (error > PK_WARN)       /* fatal */
  1274.                     return error;
  1275.             }
  1276.             tot_ucsize += crec.ucsize;
  1277.             tot_csize += csiz;
  1278.             ++members;
  1279. #ifdef OS2
  1280.             if ((ea_size = SizeOfEAs(extra_field)) != 0) {
  1281.                 tot_easize += ea_size;
  1282.                 tot_eafiles++;
  1283.             }
  1284. #endif
  1285.         } else {        /* not listing this file */
  1286.             SKIP_(crec.file_comment_length)
  1287.         }
  1288.     } /* end for-loop (j: files in central directory) */
  1289.  
  1290. /*---------------------------------------------------------------------------
  1291.     Print footer line and totals (compressed size, uncompressed size, number
  1292.     of members in zipfile).
  1293.   ---------------------------------------------------------------------------*/
  1294.  
  1295.     if (qflag < 2) {
  1296.         if ((cfactor = ratio(tot_ucsize, tot_csize)) < 0) {
  1297.             sgn = '-';
  1298.             cfactor = (-cfactor + 5) / 10;
  1299.         } else {
  1300.             sgn = ' ';
  1301.             cfactor = (cfactor + 5) / 10;
  1302.         }
  1303.         sprintf(cfactorstr, "%c%d%%", sgn, cfactor);
  1304. #ifdef MSWIN
  1305.         /* Display just the totals since the dashed lines get displayed
  1306.          * in UpdateListBox(). Get just enough space to display total. */
  1307.         if (longhdr)
  1308.             wsprintf(lpumb->szTotalsLine, 
  1309.               "%7lu         %7lu %4s                              %-7u", 
  1310.               tot_ucsize, tot_csize, cfactorstr, members);
  1311.         else
  1312.             wsprintf(lpumb->szTotalsLine, "%7lu                    %-7u", 
  1313.               tot_ucsize, members);
  1314. #else /* !MSWIN */
  1315.         if (longhdr)
  1316.             printf(" ------          ------  ---                \
  1317.               -------\n%7lu         %7lu %4s                \
  1318.               %-7u\n", tot_ucsize, tot_csize, cfactorstr, members);
  1319.         else
  1320.             printf(" ------                    -------\n%7lu      \
  1321.               %-7u\n", tot_ucsize, members);
  1322. #endif /* ?MSWIN */
  1323. #ifdef OS2
  1324.         if (tot_eafiles && tot_easize)
  1325.             printf("\n%ld file%s %ld bytes of EA's attached.\n", tot_eafiles, 
  1326.               tot_eafiles == 1 ? " has" : "s have a total of", tot_easize);
  1327. #endif
  1328.     }
  1329. /*---------------------------------------------------------------------------
  1330.     Double check that we're back at the end-of-central-directory record.
  1331.   ---------------------------------------------------------------------------*/
  1332.  
  1333.     if (readbuf(sig, 4) <= 0)
  1334.         return PK_EOF;
  1335.     if (strncmp(sig, end_central_sig, 4)) {     /* just to make sure again */
  1336.         fprintf(stderr, EndSigMsg);  /* didn't find end-of-central-dir sig */
  1337.         error_in_archive = PK_WARN;
  1338.     }
  1339.     return error_in_archive;
  1340.  
  1341. } /* end function list_files() */
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347. /********************/
  1348. /* Function ratio() */
  1349. /********************/
  1350.  
  1351. static int ratio(uc, c)
  1352.     ulg uc, c;
  1353. {
  1354.     ulg denom;
  1355.  
  1356.     if (uc == 0)
  1357.         return 0;
  1358.     if (uc > 2000000L) {    /* risk signed overflow if multiply numerator */
  1359.         denom = uc / 1000L;
  1360.         return ((uc >= c) ?
  1361.             (int) ((uc-c + (denom>>1)) / denom) :
  1362.           -((int) ((c-uc + (denom>>1)) / denom)));
  1363.     } else {             /* ^^^^^^^^ rounding */
  1364.         denom = uc;
  1365.         return ((uc >= c) ?
  1366.             (int) ((1000L*(uc-c) + (denom>>1)) / denom) :
  1367.           -((int) ((1000L*(c-uc) + (denom>>1)) / denom)));
  1368.     }                            /* ^^^^^^^^ rounding */
  1369. }
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375. /************************/
  1376. /*  Function fnprint()  */
  1377. /************************/
  1378.  
  1379. static void fnprint(fn)    /* print filename (after filtering) and newline */
  1380.     uch *fn;               /* (GRR:  probably needs EBCDICifying!) */
  1381. {
  1382.     --fn;
  1383.     while (*++fn)
  1384.         if (*fn < 32) {     /* ASCII control character */
  1385.             putchar('^');
  1386.             putchar(*fn+64);
  1387.         } else
  1388.             putchar(*fn);
  1389.     putchar('\n');
  1390.  
  1391. } /* end function fnprint() */
  1392.