home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unarj32.lzh / unarj.c < prev    next >
Text File  |  1994-01-29  |  30KB  |  1,191 lines

  1. /* UNARJ.C, UNARJ, R JUNG, 02/17/93
  2.  * Main Extractor routine
  3.  * Copyright (c) 1991-93 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   I am holding the copyright on the source code, so please do not
  13.  *   delete my name from the program files or from the documentation.
  14.  *
  15.  *   I wish to give credit to Haruhiko Okumura for providing the
  16.  *   basic ideas for ARJ and UNARJ in his program AR.  Please note
  17.  *   that UNARJ is significantly different from AR from an archive
  18.  *   structural point of view.
  19.  *
  20.  * Modification history:
  21.  * Date      Programmer  Description of modification.
  22.  * 04/05/91  R. Jung     Rewrote code.
  23.  * 04/23/91  M. Adler    Portabilized.
  24.  * 04/29/91  R. Jung     Added l command.  Removed 16 bit dependency in
  25.  *                       fillbuf().
  26.  * 05/19/91  R. Jung     Fixed extended header skipping code.
  27.  * 05/25/91  R. Jung     Improved find_header().
  28.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  29.  *                       set_ftime_mode().
  30.  * 06/19/81  R. Jung     Added two more %c in printf() in list_arc().
  31.  * 07/07/91  R. Jung     Added default_case_path() to extract().
  32.  *                       Added strlower().
  33.  * 07/20/91  R. Jung     Changed uint ratio() to static uint ratio().
  34.  * 07/21/91  R. Jung     Added #ifdef VMS.
  35.  * 08/28/91  R. Jung     Changed M_DIFFHOST message.
  36.  * 08/31/91  R. Jung     Added changes to support MAC THINK_C compiler
  37.  *                       per Eric Larson.
  38.  * 10/07/91  R. Jung     Added missing ; to THINK_C additions.
  39.  * 11/11/91  R. Jung     Added host_os test to fwrite_txt_crc().
  40.  * 11/24/91  R. Jung     Added more error_count processing.
  41.  * 12/03/91  R. Jung     Added backup file processing.
  42.  * 02/17/93  R. Jung     Added archive modified date support.
  43.  * 09/02/93  K. Flint    Added wildcards and extraction paths.
  44.  * 01/24/94  K. Flint    Added wildcard expansion to archive file path
  45.  *
  46.  */
  47.  
  48. #include "unarj.h"
  49.  
  50. #ifdef MODERN
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <ctype.h>
  54. #else /* !MODERN */
  55. extern void free();
  56. extern void exit();
  57. extern char *strcat();
  58. extern char *strcpy();
  59. extern char *strncpy();
  60. extern char *strchr();
  61. extern char *strrchr();
  62. extern int strlen();
  63. extern int strcmp();
  64. #ifdef VMS
  65. #include <ssdef.h>
  66. #define EXIT_FAILURE SS$_ABORT
  67. #define EXIT_SUCCESS SS$_NORMAL
  68. #else
  69. #define EXIT_FAILURE (1)
  70. #define EXIT_SUCCESS (0)
  71. #endif
  72. #define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  73. #define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
  74. #endif /* ?MODERN */
  75.  
  76. #ifdef THINK_C
  77. #include <console.h>
  78. #endif
  79.  
  80. /* DosFindFirst() stuff */
  81. #ifdef _OS2
  82. #define INCL_DOSFILEMGR
  83. #include <os2.h>
  84. #define NORMAL_FILES 0L
  85. #endif
  86.  
  87. /* Global variables */
  88.  
  89. UCRC   crc;
  90. FILE   *arcfile;
  91. FILE   *outfile;
  92. ushort bitbuf;
  93. long   compsize;
  94. long   origsize;
  95. uchar  subbitbuf;
  96. uchar  header[HEADERSIZE_MAX];
  97. char   arc_name[FNAME_MAX];
  98. int    command;
  99. int    bitcount;
  100. int    file_type;
  101. int    no_output;
  102. int    error_count;
  103.  
  104. /* Messages */
  105.  
  106. static char *M_USAGE  [] =
  107. {
  108. "Usage: UNARJ <archive>[.arj]    (list archive) or \n",
  109. "       UNARJ <Command> <archive>[.arj] [outpath][fs1] [path][fs2] ...\n",
  110. "       Commands:   E    (extract archive)     L    (list archive)\n",
  111. "                   T    (test archive)        X    (extract with pathnames)\n",
  112. "       outpath: Changes extract path for all files (default = current dir)\n",
  113. "       path: overides outpath for next filespec only\n",
  114. "       fs: filespec - file[s] to extract (wildcards *,? OK)\n",
  115. "\n",
  116. "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
  117. "You may freely use, copy and distribute this program, provided that no fee\n",
  118. "is charged for such use, copying or distribution, and it is distributed\n",
  119. "ONLY in its original unmodified state.  UNARJ is provided as is without\n",
  120. "warranty of any kind, express or implied, including but not limited to\n",
  121. "the implied warranties of merchantability and fitness for a particular\n",
  122. "purpose.  Refer to UNARJ.DOC for more warranty information.\n",
  123. "\n",
  124. "ARJ Software                    Internet address :  robjung@world.std.com\n",
  125. "Robert and Susan Jung           CompuServe userid:  72077,445\n",
  126. "2606 Village Road West\n",
  127. "Norwood, Massachusetts 02062 USA\n",
  128. NULL
  129. };
  130.  
  131. char M_VERSION [] = "UNARJ (Modified Demo version) 2.41.kkf.a Copyright (c) 1991-93 Robert K Jung\nModifed by Kerry Flint\n\n";
  132.  
  133. char M_ARCDATE [] = "Archive created: %s";
  134. char M_ARCDATEM[] = ", modified: %s";
  135. char M_BADCOMND[] = "Bad UNARJ command: %s";
  136. char M_BADCOMNT[] = "Invalid comment header";
  137. char M_BADHEADR[] = "Bad header";
  138. char M_BADTABLE[] = "Bad Huffman code";
  139. char M_CANTOPEN[] = "Can't open %s";
  140. char M_CANTREAD[] = "Can't read file or unexpected end of file";
  141. char M_CANTWRIT[] = "Can't write file. Disk full?";
  142. char M_CRCERROR[] = "CRC error!\n";
  143. char M_CRCOK   [] = "CRC OK\n";
  144. char M_DIFFHOST[] = "  Binary file!";
  145. char M_ENCRYPT [] = "File is password encrypted, ";
  146. char M_ERRORCNT[] = "%sFound %5d error(s)!";
  147. char M_EXTRACT [] = "Extracting %-25s";
  148. char M_FEXISTS [] = "%-25s exists, ";
  149. char M_HEADRCRC[] = "Header CRC error!";
  150. char M_NBRFILES[] = "%5d file(s)\n";
  151. char M_NOMEMORY[] = "Out of memory";
  152. char M_NOTARJ  [] = "%s is not an ARJ archive";
  153. char M_PROCARC [] = "Processing archive: %s\n";
  154. char M_SKIPPED [] = "Skipped %s\n";
  155. char M_SUFFIX  [] = ARJ_SUFFIX;
  156. char M_TESTING [] = "Testing    %-25s";
  157. char M_UNKNMETH[] = "Unsupported method: %d, ";
  158. char M_UNKNTYPE[] = "Unsupported file type: %d, ";
  159. char M_UNKNVERS[] = "Unsupported version: %d, ";
  160.  
  161. #define get_crc()       get_longword()
  162. #define fget_crc(f)     fget_longword(f)
  163.  
  164. #define setup_get(PTR)  (get_ptr = (PTR))
  165. #define get_byte()      ((uchar)(*get_ptr++ & 0xff))
  166.  
  167. #define BUFFERSIZE      4096
  168.  
  169. #define ASCII_MASK      0x7F
  170.  
  171. #define CRCPOLY         0xEDB88320L
  172.  
  173. #define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)
  174.  
  175. /* Local functions */
  176.  
  177. #ifdef MODERN
  178. static void  make_crctable(void);
  179. static void  crc_buf(char *str, int len);
  180. static void  strparity(uchar *p);
  181. static FILE  *fopen_msg(char *name, char *mode);
  182. static int   fget_byte(FILE *f);
  183. static uint  fget_word(FILE *f);
  184. static ulong fget_longword(FILE *f);
  185. static void  fread_crc(uchar *p, int n, FILE *f);
  186. static void  decode_path(char *name);
  187. static void  get_date_str(char *str, ulong tstamp);
  188. static int   parse_path(char *pathname, char *path, char *entry);
  189. static void  strncopy(char *to, char *from, int len);
  190. static uint  get_word(void);
  191. static ulong get_longword(void);
  192. static long  find_header(FILE *fd);
  193. static int   read_header(int first, FILE *fd, char *name);
  194. static void  skip(void);
  195. static void  unstore(void);
  196. static int   check_flags(void);
  197. static int   extract(void);
  198. static int   test(void);
  199. static uint  ratio(long a, long b);
  200. static void  list_start(void);
  201. static void  list_arc(int count);
  202. static void  execute_cmd(void);
  203. static void  help(void);
  204. #endif /* MODERN */
  205.  
  206. /* Local variables */
  207.  
  208. static char   filename[FNAME_MAX];
  209. static char   extract_to_dir[FNAME_MAX];
  210. static char   comment[COMMENT_MAX];
  211. static char   *hdr_filename;
  212. static char   *hdr_comment;
  213. static char   **extract_files;
  214. static int    num_extract_files = 0;
  215.  
  216. static ushort headersize;
  217. static uchar  first_hdr_size;
  218. static uchar  arj_nbr;
  219. static uchar  arj_x_nbr;
  220. static uchar  host_os;
  221. static uchar  arj_flags;
  222. static short  method;
  223. static uint   file_mode;
  224. static ulong  time_stamp;
  225. static short  entry_pos;
  226. static ushort host_data;
  227. static uchar  *get_ptr;
  228. static UCRC   file_crc;
  229. static UCRC   header_crc;
  230.  
  231. static long   first_hdr_pos;
  232. static long   torigsize;
  233. static long   tcompsize;
  234.  
  235. static int    clock_inx;
  236.  
  237. static char   *writemode[2]  = { "wb",  "w" };
  238.  
  239. static UCRC   crctable[UCHAR_MAX + 1];
  240.  
  241. /* Functions */
  242.  
  243. static void
  244. make_crctable()
  245. {
  246.     uint i, j;
  247.     UCRC r;
  248.  
  249.     for (i = 0; i <= UCHAR_MAX; i++)
  250.     {
  251.         r = i;
  252.         for (j = CHAR_BIT; j > 0; j--)
  253.         {
  254.             if (r & 1)
  255.                 r = (r >> 1) ^ CRCPOLY;
  256.             else
  257.                 r >>= 1;
  258.         }
  259.         crctable[i] = r;
  260.     }
  261. }
  262.  
  263. static void
  264. crc_buf(str, len)
  265. char *str;
  266. int  len;
  267. {
  268.     while (len--)
  269.         UPDATE_CRC(crc, *str++);
  270. }
  271.  
  272. void
  273. disp_clock()
  274. {
  275.     static char clock_str[4] = { '|', '/', '-', '\\' };
  276.  
  277.     printf("(%c)\b\b\b", clock_str[clock_inx]);
  278.     clock_inx = (clock_inx + 1) & 0x03;
  279. }
  280.  
  281. void
  282. error(fmt, arg)
  283. char *fmt;
  284. char *arg;
  285. {
  286.     putc('\n', stdout);
  287.     printf(fmt, arg, error_count);
  288.     putc('\n', stdout);
  289.     exit(EXIT_FAILURE);
  290. }
  291.  
  292. static void
  293. strparity(p)
  294. uchar *p;
  295. {
  296.     while (*p)
  297.     {
  298.         FIX_PARITY(*p);
  299.         p++;
  300.     }
  301. }
  302.  
  303. static FILE *
  304. fopen_msg(name, mode)
  305. char *name;
  306. char *mode;
  307. {
  308.     FILE *fd;
  309.  
  310.     fd = file_open(name, mode);
  311.     if (fd == NULL)
  312.         error(M_CANTOPEN, name);
  313.     return fd;
  314. }
  315.  
  316. static int
  317. fget_byte(f)
  318. FILE *f;
  319. {
  320.     int c;
  321.  
  322.     if ((c = getc(f)) == EOF)
  323.         error(M_CANTREAD, "");
  324.     return c & 0xFF;
  325. }
  326.  
  327. static uint
  328. fget_word(f)
  329. FILE *f;
  330. {
  331.     uint b0, b1;
  332.  
  333.     b0 = fget_byte(f);
  334.     b1 = fget_byte(f);
  335.     return (b1 << 8) + b0;
  336. }
  337.  
  338. static ulong
  339. fget_longword(f)
  340. FILE *f;
  341. {
  342.     ulong b0, b1, b2, b3;
  343.  
  344.     b0 = fget_byte(f);
  345.     b1 = fget_byte(f);
  346.     b2 = fget_byte(f);
  347.     b3 = fget_byte(f);
  348.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  349. }
  350.  
  351. static void
  352. fread_crc(p, n, f)
  353. uchar *p;
  354. int   n;
  355. FILE  *f;
  356. {
  357.     n = file_read((char *)p, 1, n, f);
  358.     origsize += n;
  359.     crc_buf((char *)p, n);
  360. }
  361.  
  362. void
  363. fwrite_txt_crc(p, n)
  364. uchar *p;
  365. int   n;
  366. {
  367.     uchar c;
  368.  
  369.     crc_buf((char *)p, n);
  370.     if (no_output)
  371.         return;
  372.  
  373.     if (file_type == TEXT_TYPE)
  374.     {
  375.         while (n--)
  376.         {
  377.             c = *p++;
  378.             if (host_os != OS)
  379.             {
  380.                 FIX_PARITY(c);
  381.             }
  382.             if (putc((int) c, outfile) == EOF)
  383.                 error(M_CANTWRIT, "");
  384.         }
  385.     }
  386.     else
  387.     {
  388.         if (file_write((char *)p, 1, n, outfile) != n)
  389.             error(M_CANTWRIT, "");
  390.     }
  391. }
  392.  
  393. void
  394. init_getbits()
  395. {
  396.     bitbuf = 0;
  397.     subbitbuf = 0;
  398.     bitcount = 0;
  399.     fillbuf(2 * CHAR_BIT);
  400. }
  401.  
  402. void
  403. fillbuf(n)                /* Shift bitbuf n bits left, read n bits */
  404. int n;
  405. {
  406.     bitbuf = (bitbuf << n) & 0xFFFF;  /* lose the first n bits */
  407.     while (n > bitcount)
  408.     {
  409.         bitbuf |= subbitbuf << (n -= bitcount);
  410.         if (compsize != 0)
  411.         {
  412.             compsize--;
  413.             subbitbuf = (uchar) getc(arcfile);
  414.         }
  415.         else
  416.             subbitbuf = 0;
  417.         bitcount = CHAR_BIT;
  418.     }
  419.     bitbuf |= subbitbuf >> (bitcount -= n);
  420. }
  421.  
  422. ushort
  423. getbits(n)
  424. int n;
  425. {
  426.     ushort x;
  427.  
  428.     x = bitbuf >> (2 * CHAR_BIT - n);
  429.     fillbuf(n);
  430.     return x;
  431. }
  432.  
  433. static void
  434. decode_path(name)
  435. char *name;
  436. {
  437.     for ( ; *name; name++)
  438.     {
  439.         if (*name == ARJ_PATH_CHAR)
  440.             *name = PATH_CHAR;
  441.     }
  442. }
  443.  
  444. static void
  445. get_date_str(str, tstamp)
  446. char  *str;
  447. ulong tstamp;
  448. {
  449.     sprintf(str, "%04u-%02u-%02u %02u:%02u:%02u",
  450.            ts_year(tstamp), ts_month(tstamp), ts_day(tstamp),
  451.            ts_hour(tstamp), ts_min(tstamp), ts_sec(tstamp));
  452. }
  453.  
  454. static int
  455. parse_path(pathname, path, entry)
  456. char *pathname;
  457. char *path;
  458. char *entry;
  459. {
  460.     char *cptr, *ptr, *fptr;
  461.     short pos;
  462.  
  463.     fptr = NULL;
  464.     for (cptr = PATH_SEPARATORS; *cptr; cptr++)
  465.     {
  466.         if ((ptr = strrchr(pathname, *cptr)) != NULL &&
  467.                 (fptr == NULL || ptr > fptr))
  468.             fptr = ptr;
  469.     }
  470.     if (fptr == NULL)
  471.         pos = 0;
  472.     else
  473.         pos = fptr + 1 - pathname;
  474.     if (path != NULL)
  475.     {
  476.        strncpy(path, pathname, pos);
  477.        path[pos] = NULL_CHAR;
  478.     }
  479.     if (entry != NULL)
  480.        strcpy(entry, &pathname[pos]);
  481.     return pos;
  482. }
  483.  
  484. static void
  485. strncopy(to, from, len)
  486. char *to;
  487. char *from;
  488. int  len;
  489. {
  490.     int i;
  491.  
  492.     for (i = 1; i < len && *from; i++)
  493.         *to++ = *from++;
  494.     *to = NULL_CHAR;
  495. }
  496.  
  497. void
  498. strlower(s)
  499. char *s;
  500. {
  501.     while (*s)
  502.     {
  503.         *s = (char) tolower(*s);
  504.         s++;
  505.     }
  506. }
  507.  
  508. void
  509. strupper(s)
  510. char *s;
  511. {
  512.     while (*s)
  513.     {
  514.         *s = (char) toupper(*s);
  515.         s++;
  516.     }
  517. }
  518.  
  519. voidp *
  520. malloc_msg(size)
  521. int size;
  522. {
  523.     char *p;
  524.  
  525.     if ((p = (char *)xmalloc(size)) == NULL)
  526.         error(M_NOMEMORY, "");
  527.     return (voidp *)p;
  528. }
  529.  
  530. static uint
  531. get_word()
  532. {
  533.     uint b0, b1;
  534.  
  535.     b0 = get_byte();
  536.     b1 = get_byte();
  537.     return (b1 << 8) + b0;
  538. }
  539.  
  540. static ulong
  541. get_longword()
  542. {
  543.     ulong b0, b1, b2, b3;
  544.  
  545.     b0 = get_byte();
  546.     b1 = get_byte();
  547.     b2 = get_byte();
  548.     b3 = get_byte();
  549.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  550. }
  551.  
  552. static long
  553. find_header(fd)
  554. FILE *fd;
  555. {
  556.     long arcpos, lastpos;
  557.     int c;
  558.  
  559.     arcpos = file_tell(fd);
  560.     file_seek(fd, 0L, SEEK_END);
  561.     lastpos = file_tell(fd) - 2;
  562.     if (lastpos > MAXSFX)
  563.         lastpos = MAXSFX;
  564.     for ( ; arcpos < lastpos; arcpos++)
  565.     {
  566.         file_seek(fd, arcpos, SEEK_SET);
  567.         c = fget_byte(fd);
  568.         while (arcpos < lastpos)
  569.         {
  570.             if (c != HEADER_ID_LO)  /* low order first */
  571.                 c = fget_byte(fd);
  572.             else if ((c = fget_byte(fd)) == HEADER_ID_HI)
  573.                 break;
  574.             arcpos++;
  575.         }
  576.         if (arcpos >= lastpos)
  577.             break;
  578.         if ((headersize = fget_word(fd)) <= HEADERSIZE_MAX)
  579.         {
  580.             crc = CRC_MASK;
  581.             fread_crc(header, (int) headersize, fd);
  582.             if ((crc ^ CRC_MASK) == fget_crc(fd))
  583.             {
  584.                 file_seek(fd, arcpos, SEEK_SET);
  585.                 return arcpos;
  586.             }
  587.         }
  588.     }
  589.     return -1;          /* could not find a valid header */
  590. }
  591.  
  592. static int
  593. read_header(first, fd, name)
  594. int  first;
  595. FILE *fd;
  596. char *name;
  597. {
  598.     ushort extheadersize, header_id;
  599.  
  600.     header_id = fget_word(fd);
  601.     if (header_id != HEADER_ID)
  602.     {
  603.         if (first)
  604.             error(M_NOTARJ, name);
  605.         else
  606.             error(M_BADHEADR, "");
  607.     }
  608.  
  609.     headersize = fget_word(fd);
  610.     if (headersize == 0)
  611.         return 0;               /* end of archive */
  612.     if (headersize > HEADERSIZE_MAX)
  613.         error(M_BADHEADR, "");
  614.  
  615.     crc = CRC_MASK;
  616.     fread_crc(header, (int) headersize, fd);
  617.     header_crc = fget_crc(fd);
  618.     if ((crc ^ CRC_MASK) != header_crc)
  619.         error(M_HEADRCRC, "");
  620.  
  621.     setup_get(header);
  622.     first_hdr_size = get_byte();
  623.     arj_nbr = get_byte();
  624.     arj_x_nbr = get_byte();
  625.     host_os = get_byte();
  626.     arj_flags = get_byte();
  627.     method = get_byte();
  628.     file_type = get_byte();
  629.     (void)get_byte();
  630.     time_stamp = get_longword();
  631.     compsize = get_longword();
  632.     origsize = get_longword();
  633.     file_crc = get_crc();
  634.     entry_pos = get_word();
  635.     file_mode = get_word();
  636.     host_data = get_word();
  637.  
  638.     hdr_filename = (char *)&header[first_hdr_size];
  639.     strncopy(filename, hdr_filename, sizeof(filename));
  640.     if (host_os != OS)
  641.         strparity((uchar *)filename);
  642.     if ((arj_flags & PATHSYM_FLAG) != 0)
  643.         decode_path(filename);
  644.  
  645.     hdr_comment = (char *)&header[first_hdr_size + strlen(hdr_filename) + 1];
  646.     strncopy(comment, hdr_comment, sizeof(comment));
  647.     if (host_os != OS)
  648.         strparity((uchar *)comment);
  649.  
  650.     /* if extheadersize == 0 then no CRC */
  651.     /* otherwise read extheader data and read 4 bytes for CRC */
  652.  
  653.     while ((extheadersize = fget_word(fd)) != 0)
  654.         file_seek(fd, (long) (extheadersize + 4), SEEK_CUR);
  655.  
  656.     return 1;                   /* success */
  657. }
  658.  
  659. static void
  660. skip()
  661. {
  662.     file_seek(arcfile, compsize, SEEK_CUR);
  663. }
  664.  
  665. static void
  666. unstore()
  667. {
  668.     int n;
  669.     long pos;
  670.     char *buffer;
  671.  
  672.     buffer = (char *)malloc_msg(BUFFERSIZE);
  673.     pos = file_tell(arcfile);
  674.     disp_clock();
  675.     n = (int)(BUFFERSIZE - (pos % BUFFERSIZE));
  676.     n = compsize > (long)n ? n : (int)compsize;
  677.     while (compsize > 0)
  678.     {
  679.         if (file_read(buffer, 1, n, arcfile) != n)
  680.             error(M_CANTREAD, "");
  681.         disp_clock();
  682.         compsize -= n;
  683.         fwrite_txt_crc((uchar *)buffer, n);
  684.         n = compsize > BUFFERSIZE ? BUFFERSIZE : (int)compsize;
  685.     }
  686.     free(buffer);
  687. }
  688.  
  689. static int
  690. check_flags()
  691. {
  692.     if (arj_x_nbr > ARJ_X_VERSION)
  693.     {
  694.         printf(M_UNKNVERS, arj_x_nbr);
  695.         printf(M_SKIPPED, filename);
  696.         skip();
  697.         return -1;
  698.     }
  699.     if ((arj_flags & GARBLE_FLAG) != 0)
  700.     {
  701.         printf(M_ENCRYPT);
  702.         printf(M_SKIPPED, filename);
  703.         skip();
  704.         return -1;
  705.     }
  706.     if (method < 0 || method > MAXMETHOD || (method == 4 && arj_nbr == 1))
  707.     {
  708.         printf(M_UNKNMETH, method);
  709.         printf(M_SKIPPED, filename);
  710.         skip();
  711.         return -1;
  712.     }
  713.     if (file_type != BINARY_TYPE && file_type != TEXT_TYPE)
  714.     {
  715.         printf(M_UNKNTYPE, file_type);
  716.         printf(M_SKIPPED, filename);
  717.         skip();
  718.         return -1;
  719.     }
  720.     return 0;
  721. }
  722.  
  723. char myupper(char c){
  724.    return(toupper(c));
  725. }
  726.  
  727. static int check_wild(char *wild, char *check){
  728. /* compare a wildcard string with another string */
  729.  
  730.    char *tmp;
  731.  
  732.    tmp = strrchr(wild, '\\'); /* split off path */
  733.    if (tmp) 
  734.       wild = tmp + 1;
  735.  
  736.    if (command == 'D')   printf("Comparing %s and %s : ", wild, check);
  737.    while ( ((myupper(*wild) == myupper(*check) ) || *wild == '*' || *wild == '?') && *wild) {
  738.       if (myupper(*wild) == myupper(*check)) {
  739.          wild++;
  740.          check++;
  741.       } else { /* wild card */
  742.          if (*wild == '?') { /* match any single char */
  743.             if (*check != '.' && *check ){ /* don't increment if next char . or \0 */
  744.                check++;
  745.             }
  746.             wild++;
  747.          } else { /* * match any number of characters */
  748.             while (*wild == '*' || *wild == '?') { /* skip to next non wild */
  749.                wild++;
  750.             } /* endwhile */
  751.             if (!*wild) { /* matches to EOS */
  752.                while (*check) {
  753.                   check++;
  754.                } /* endwhile */
  755.             } else {
  756.                tmp = check;
  757.                while (*tmp) { /* find last char that matches */
  758.                   if (myupper(*tmp) == myupper(*wild)){
  759.                      check = tmp;
  760.                   }
  761.                   tmp++;
  762.                }
  763.                if (*wild == '.' && *check != '.'){ /* make EOS dot pos */
  764.                   check = tmp;
  765.                   wild++;
  766.                 }
  767.             } /* endif */
  768.          } /* endif */
  769.       } /* endif */
  770.    } /* endwhile */
  771.    if (*wild == *check) {
  772.        if (command == 'D')      printf("Match\n");
  773.       return(1);
  774.    } else {
  775.       if (command == 'D')      printf("No-Wild=%s & Check =%s\n", wild, check);
  776.       return(0);
  777.    } /* endif */
  778. }
  779.  
  780. static int file_check(char *name){
  781.  
  782.    int i, len, n_len, e_len;
  783.    char *ext;
  784.  
  785.    if (!num_extract_files) {
  786.       return(1);
  787.    } /* endif */
  788.    for (i=0;i < num_extract_files; i++ ) {
  789. #ifdef SIMPLE_WILDS
  790.       /* exact match */
  791.       if (stricmp(name, extract_files[i]) == 0)
  792.          return(1);
  793.       /* Every file wild card */
  794.       if (strcmp("*.*", extract_files[i]) == 0 ||
  795.           strcmp("*", extract_files[i]) == 0) {
  796.          return(1);
  797.       } /* endif */
  798.       /* check just extensions */
  799.       if (strncmp("*.", extract_files[i], 2) == 0) {
  800.          ext = strrchr(name, '.');
  801.          if (ext){
  802.             if (stricmp(ext, &extract_files[i][2]) == 0){
  803.                return (1);
  804.             }
  805.          }
  806.       } else {
  807.          /* any extension */
  808.          len = strlen(extract_files[i]);
  809.          if (strcmp(".*", &extract_files[i][len -2]) == 0){
  810.             ext = strrchr(name, '.');
  811.             if (ext) {
  812.                e_len = strlen(ext);
  813.             } else {
  814.                e_len = 0;
  815.             } /* endif */
  816.             n_len = strlen(name) - e_len;
  817.             if (strnicmp(name, extract_files[i], len-2) == 0 && (n_len == len - 2)){
  818.                return(1);
  819.             }
  820.          }
  821.       } /* endif */
  822. #else
  823.       if (check_wild(extract_files[i], name)){
  824.          strcpy(extract_to_dir, extract_files[i]); /* figure out which extract path to use */
  825.          ext = strrchr(extract_to_dir, '\\');
  826.          if (ext) {
  827.             *(ext+1) = '\0';
  828.          } else {
  829.             strcpy(extract_to_dir, extract_files[0]);
  830.             ext = strrchr(extract_to_dir, '\\');
  831.             if (ext) {
  832.                *(ext+1) = '\0';
  833.             } else {
  834.                strcpy(extract_to_dir, DEFAULT_DIR);
  835.             }
  836.          } /* endif */
  837.          return(1);
  838.       }
  839. #endif
  840.    } /* endfor */
  841.    return(0);
  842. }
  843.  
  844. static int
  845. extract()
  846. {
  847.     char name[FNAME_MAX];
  848.  
  849.     if (check_flags())
  850.     {
  851.         error_count++;
  852.         return 0;
  853.     }
  854.  
  855.     no_output = 0;
  856.     if (command == 'E'){
  857.         strcpy(name, extract_to_dir);
  858.         strcat(name, &filename[entry_pos]);
  859.     } else
  860.     {
  861.         strcpy(name, extract_to_dir);
  862.         strcat(name, filename);
  863.     }
  864.  
  865.     if (host_os != OS)
  866.         default_case_path(name);
  867.  
  868.  
  869.     if (file_exists(name))
  870.     {
  871.         printf(M_FEXISTS, name);
  872.         printf(M_SKIPPED, name);
  873.         skip();
  874.         error_count++;
  875.         return 0;
  876.     }
  877.     outfile = file_open(name, writemode[file_type & 1]);
  878.     if (outfile == NULL)
  879.     {
  880.         printf(M_CANTOPEN, name);
  881.         putchar('\n');
  882.         skip();
  883.         error_count++;
  884.         return 0;
  885.     }
  886.     printf(M_EXTRACT, name);
  887.     if (host_os != OS && file_type == BINARY_TYPE)
  888.         printf(M_DIFFHOST);
  889.     printf("  ");
  890.  
  891.     crc = CRC_MASK;
  892.  
  893.     if (method == 0)
  894.         unstore();
  895.     else if (method == 1 || method == 2 || method == 3)
  896.         decode();
  897.     else if (method == 4)
  898.         decode_f();
  899.     fclose(outfile);
  900.  
  901.     set_ftime_mode(name, time_stamp, file_mode, (uint) host_os);
  902.  
  903.     if ((crc ^ CRC_MASK) == file_crc)
  904.         printf(M_CRCOK);
  905.     else
  906.     {
  907.         printf(M_CRCERROR);
  908.         error_count++;
  909.     }
  910.     return 1;
  911. }
  912.  
  913. static int
  914. test()
  915. {
  916.     if (check_flags())
  917.         return 0;
  918.  
  919.     no_output = 1;
  920.     printf(M_TESTING, filename);
  921.     printf("  ");
  922.  
  923.     crc = CRC_MASK;
  924.  
  925.     if (method == 0)
  926.         unstore();
  927.     else if (method == 1 || method == 2 || method == 3)
  928.         decode();
  929.     else if (method == 4)
  930.         decode_f();
  931.  
  932.     if ((crc ^ CRC_MASK) == file_crc)
  933.         printf(M_CRCOK);
  934.     else
  935.     {
  936.         printf(M_CRCERROR);
  937.         error_count++;
  938.     }
  939.     return 1;
  940. }
  941.  
  942. static uint
  943. ratio(a, b)
  944. long a, b;
  945. {
  946.    int i;
  947.  
  948.    for (i = 0; i < 3; i++)
  949.        if (a <= LONG_MAX / 10)
  950.            a *= 10;
  951.        else
  952.            b /= 10;
  953.    if ((long) (a + (b >> 1)) < a)
  954.    {
  955.        a >>= 1;
  956.        b >>= 1;
  957.    }
  958.    if (b == 0)
  959.        return 0;
  960.    return (uint) ((a + (b >> 1)) / b);
  961. }
  962.  
  963. static void
  964. list_start()
  965. {
  966.     printf("Filename       Original Compressed Ratio DateTime modified CRC-32   AttrBTPMGVX\n");
  967.     printf("------------ ---------- ---------- ----- ----------------- -------- -----------\n");
  968. }
  969.  
  970. static void
  971. list_arc(count)
  972. int count;
  973. {
  974.     uint r;
  975.     int garble_mode, path_mode, volume_mode, extfil_mode, ftype, bckf_mode;
  976.     char date_str[20], fmode_str[10];
  977.     static char mode[5] = { 'B', 'T', '?', 'D', 'V' };
  978.     static char pthf[2] = { ' ', '+' };
  979.     static char pwdf[2] = { ' ', 'G' };  /* plain, encrypted */
  980.     static char volf[2] = { ' ', 'V' };
  981.     static char extf[2] = { ' ', 'X' };
  982.     static char bckf[2] = { ' ', '*' };
  983.  
  984.     if (count == 0)
  985.         list_start();
  986.  
  987.     garble_mode = ((arj_flags & GARBLE_FLAG) != 0);
  988.     volume_mode = ((arj_flags & VOLUME_FLAG) != 0);
  989.     extfil_mode = ((arj_flags & EXTFILE_FLAG) != 0);
  990.     bckf_mode   = ((arj_flags & BACKUP_FLAG) != 0);
  991.     path_mode   = (entry_pos > 0);
  992.     r = ratio(compsize, origsize);
  993.     torigsize += origsize;
  994.     tcompsize += compsize;
  995.     ftype = file_type;
  996.     if (ftype != BINARY_TYPE && ftype != TEXT_TYPE && ftype != DIR_TYPE &&
  997.             ftype != LABEL_TYPE)
  998.         ftype = 3;
  999.     get_date_str(date_str, time_stamp);
  1000.     strcpy(fmode_str, "    ");
  1001.     if (host_os == OS)
  1002.         get_mode_str(fmode_str, (uint) file_mode);
  1003.     if (strlen(&filename[entry_pos]) > 12)
  1004.         printf("%-12s\n             ", &filename[entry_pos]);
  1005.     else
  1006.         printf("%-12s ", &filename[entry_pos]);
  1007.     printf("%10ld %10ld %u.%03u %s %08lX %4s%c%c%c%u%c%c%c\n",
  1008.         origsize, compsize, r / 1000, r % 1000, &date_str[2], file_crc,
  1009.         fmode_str, bckf[bckf_mode], mode[ftype], pthf[path_mode], method,
  1010.         pwdf[garble_mode], volf[volume_mode], extf[extfil_mode]);
  1011. }
  1012.  
  1013. static void
  1014. execute_cmd()
  1015. {
  1016.     int file_count;
  1017.     char date_str[22];
  1018.     uint r;
  1019.     int doit;
  1020.    #ifdef _OS2
  1021.    HDIR FindHandle;
  1022.    FILEFINDBUF3 FindBuffer;
  1023.    ULONG FindCount, File_Attribute;
  1024.    APIRET rc;
  1025.    char FileSpec[FNAME_MAX];
  1026.    char PathSpec[FNAME_MAX];
  1027.  
  1028.    FindHandle = 0x0001;
  1029.    FindCount = 1;
  1030.    File_Attribute = NORMAL_FILES | FILE_ARCHIVED | FILE_HIDDEN | FILE_READONLY;  /* All files, no dirs */
  1031.  
  1032.    strncopy(FileSpec, arc_name, FNAME_MAX);
  1033.    parse_path(FileSpec, PathSpec, NULL);
  1034.    rc = DosFindFirst(FileSpec, &FindHandle, File_Attribute, (PVOID) &FindBuffer,
  1035.                      sizeof(FindBuffer), &FindCount, FIL_STANDARD);
  1036.    if (rc != 0)
  1037.         error(M_CANTOPEN, arc_name);
  1038.    while (rc == 0) {
  1039.       strncopy(arc_name, PathSpec, FNAME_MAX);
  1040.       strncat(arc_name, FindBuffer.achName, FNAME_MAX - strlen(PathSpec) - 1);
  1041.       arc_name[FNAME_MAX -1] = '\0';
  1042.    #endif 
  1043.       first_hdr_pos = 0;
  1044.       time_stamp = 0;
  1045.       first_hdr_size = FIRST_HDR_SIZE;
  1046.  
  1047.       arcfile = fopen_msg(arc_name, "rb");
  1048.  
  1049.       printf(M_PROCARC, arc_name);
  1050.  
  1051.       first_hdr_pos = find_header(arcfile);
  1052.       if (first_hdr_pos < 0)
  1053.          error(M_NOTARJ, arc_name);
  1054.       file_seek(arcfile, first_hdr_pos, SEEK_SET);
  1055.       if (!read_header(1, arcfile, arc_name))
  1056.           error(M_BADCOMNT, "");
  1057.       get_date_str(date_str, time_stamp);
  1058.       printf(M_ARCDATE, date_str);
  1059.       if (arj_nbr >= ARJ_M_VERSION)
  1060.       {
  1061.          get_date_str(date_str, (ulong) compsize);
  1062.          printf(M_ARCDATEM, date_str);
  1063.       }
  1064.       printf("\n");
  1065.  
  1066.       file_count = 0;
  1067.       while (read_header(0, arcfile, arc_name))
  1068.       {
  1069.          strcpy(extract_to_dir, DEFAULT_DIR);
  1070.          doit = file_check(filename+entry_pos);
  1071.          if (!doit)
  1072.             skip();
  1073.          else switch (command)
  1074.          {
  1075.          case 'E':
  1076.          case 'X':
  1077.             if (extract())
  1078.                 file_count++;
  1079.             break;
  1080.          case 'L':
  1081.             list_arc(file_count++);
  1082.             skip();
  1083.             break;
  1084.          case 'T':
  1085.             if (test())
  1086.                 file_count++;
  1087.             break;
  1088.          case 'D': /* Debug Wildcards only */
  1089.             skip();
  1090.             file_count++;
  1091.             break;
  1092.          }
  1093.       }
  1094.  
  1095.       if (command == 'L')
  1096.       {
  1097.          printf("------------ ---------- ---------- ----- -----------------\n");
  1098.          r = ratio(tcompsize, torigsize);
  1099.          printf(" %5d files %10ld %10ld %u.%03u %s\n",
  1100.             file_count, torigsize, tcompsize, r / 1000, r % 1000, &date_str[2]);
  1101.       }
  1102.       else
  1103.          printf(M_NBRFILES, file_count);
  1104.  
  1105.       fclose(arcfile);
  1106.    #ifdef _OS2
  1107.       rc = DosFindNext(FindHandle, (PVOID) &FindBuffer, sizeof(FindBuffer), &FindCount);
  1108.    } /* endwhile */
  1109.    rc = DosFindClose(FindHandle);
  1110.    #endif
  1111. }
  1112.  
  1113. static void
  1114. help()
  1115. {
  1116.     int i;
  1117.  
  1118.     for (i = 0; M_USAGE[i] != NULL; i++)
  1119.         printf(M_USAGE[i]);
  1120. }
  1121.  
  1122. int
  1123. main(argc, argv)
  1124. int  argc;
  1125. char *argv[];
  1126. {
  1127.     int i, j, lastc;
  1128.     char *arc_p;
  1129.  
  1130. #ifdef THINK_C
  1131.     argc = ccommand(&argv);
  1132. #endif
  1133. #ifdef __IBMC__
  1134.     setvbuf(stdout, NULL, _IONBF, 0L);
  1135. #endif
  1136.     printf(M_VERSION);
  1137.  
  1138.     if (argc == 1)
  1139.     {
  1140.         help();
  1141.         return EXIT_SUCCESS;
  1142.     }
  1143.     else if (argc == 2)
  1144.     {
  1145.         command = 'L';
  1146.         arc_p = argv[1];
  1147.     }
  1148.     else if (argc >= 3)
  1149.     {
  1150.         if (strlen(argv[1]) > 1)
  1151.             error(M_BADCOMND, argv[1]);
  1152.         command = toupper(*argv[1]);
  1153.         if (strchr("ELTXD", command) == NULL)
  1154.             error(M_BADCOMND, argv[1]);
  1155.         arc_p = argv[2];
  1156.         extract_files = &argv[3];
  1157.         num_extract_files = argc - 3;
  1158.     }
  1159.     else
  1160.     {
  1161.         help();
  1162.         return EXIT_FAILURE;
  1163.     }
  1164.  
  1165.     strncopy(arc_name, arc_p, FNAME_MAX);
  1166.     case_path(arc_name);
  1167.     i = strlen(arc_name);
  1168.     j = parse_path(arc_name, (char *)NULL, (char *)NULL);
  1169.     lastc = arc_name[i - 1];
  1170.     if (lastc == ARJ_DOT)
  1171.         arc_name[i - 1] = NULL_CHAR;
  1172.     else if (strchr(&arc_name[j], ARJ_DOT) == NULL)
  1173.         strcat(arc_name, M_SUFFIX);
  1174.  
  1175.     make_crctable();
  1176.  
  1177.     error_count = 0;
  1178.     clock_inx = 0;
  1179.     arcfile = NULL;
  1180.     outfile = NULL;
  1181.  
  1182.     execute_cmd();
  1183.  
  1184.     if (error_count > 0)
  1185.         error(M_ERRORCNT, "");
  1186.  
  1187.     return EXIT_SUCCESS;
  1188. }
  1189.  
  1190. /* end UNARJ.C */
  1191.