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