home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unar2412.zip / unarj.c < prev    next >
C/C++ Source or Header  |  1993-06-23  |  23KB  |  1,015 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.  *
  44.  */
  45.  
  46. #include "unarj.h"
  47.  
  48. #ifdef MODERN
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <ctype.h>
  52. #else /* !MODERN */
  53. extern void free();
  54. extern void exit();
  55. extern char *strcat();
  56. extern char *strcpy();
  57. extern char *strncpy();
  58. extern char *strchr();
  59. extern char *strrchr();
  60. extern int strlen();
  61. extern int strcmp();
  62. #ifdef VMS
  63. #include <ssdef.h>
  64. #define EXIT_FAILURE SS$_ABORT
  65. #define EXIT_SUCCESS SS$_NORMAL
  66. #else
  67. #define EXIT_FAILURE (1)
  68. #define EXIT_SUCCESS (0)
  69. #endif
  70. #define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  71. #define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
  72. #endif /* ?MODERN */
  73.  
  74. #ifdef THINK_C
  75. #include <console.h>
  76. #endif
  77.  
  78. /* Global variables */
  79.  
  80. UCRC   crc;
  81. FILE   *arcfile;
  82. FILE   *outfile;
  83. ushort bitbuf;
  84. long   compsize;
  85. long   origsize;
  86. uchar  subbitbuf;
  87. uchar  header[HEADERSIZE_MAX];
  88. char   arc_name[FNAME_MAX];
  89. int    command;
  90. int    bitcount;
  91. int    file_type;
  92. int    no_output;
  93. int    error_count;
  94.  
  95. /* Messages */
  96.  
  97. static char *M_USAGE  [] =
  98. {
  99. "Usage:  UNARJ archive[.arj]    (list archive)\n",
  100. "        UNARJ e archive        (extract archive)\n",
  101. "        UNARJ l archive        (list archive)\n",
  102. "        UNARJ t archive        (test archive)\n",
  103. "        UNARJ x archive        (extract with pathnames)\n",
  104. "\n",
  105. "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
  106. "You may freely use, copy and distribute this program, provided that no fee\n",
  107. "is charged for such use, copying or distribution, and it is distributed\n",
  108. "ONLY in its original unmodified state.  UNARJ is provided as is without\n",
  109. "warranty of any kind, express or implied, including but not limited to\n",
  110. "the implied warranties of merchantability and fitness for a particular\n",
  111. "purpose.  Refer to UNARJ.DOC for more warranty information.\n",
  112. "\n",
  113. "ARJ Software                    Internet address :  robjung@world.std.com\n",
  114. "Robert and Susan Jung           CompuServe userid:  72077,445\n",
  115. "2606 Village Road West\n",
  116. "Norwood, Massachusetts 02062\n",
  117. "USA\n",
  118. NULL
  119. };
  120.  
  121. char M_VERSION [] = "UNARJ (Demo version) 2.41 Copyright (c) 1991-93 Robert K Jung\n\n";
  122.  
  123. char M_ARCDATE [] = "Archive created: %s";
  124. char M_ARCDATEM[] = ", modified: %s";
  125. char M_BADCOMND[] = "Bad UNARJ command: %s";
  126. char M_BADCOMNT[] = "Invalid comment header";
  127. char M_BADHEADR[] = "Bad header";
  128. char M_BADTABLE[] = "Bad Huffman code";
  129. char M_CANTOPEN[] = "Can't open %s";
  130. char M_CANTREAD[] = "Can't read file or unexpected end of file";
  131. char M_CANTWRIT[] = "Can't write file. Disk full?";
  132. char M_CRCERROR[] = "CRC error!\n";
  133. char M_CRCOK   [] = "CRC OK\n";
  134. char M_DIFFHOST[] = "  Binary file!";
  135. char M_ENCRYPT [] = "File is password encrypted, ";
  136. char M_ERRORCNT[] = "%sFound %5d error(s)!";
  137. char M_EXTRACT [] = "Extracting %-25s";
  138. char M_FEXISTS [] = "%-25s exists, ";
  139. char M_HEADRCRC[] = "Header CRC error!";
  140. char M_NBRFILES[] = "%5d file(s)\n";
  141. char M_NOMEMORY[] = "Out of memory";
  142. char M_NOTARJ  [] = "%s is not an ARJ archive";
  143. char M_PROCARC [] = "Processing archive: %s\n";
  144. char M_SKIPPED [] = "Skipped %s\n";
  145. char M_SUFFIX  [] = ARJ_SUFFIX;
  146. char M_TESTING [] = "Testing    %-25s";
  147. char M_UNKNMETH[] = "Unsupported method: %d, ";
  148. char M_UNKNTYPE[] = "Unsupported file type: %d, ";
  149. char M_UNKNVERS[] = "Unsupported version: %d, ";
  150.  
  151. #define get_crc()       get_longword()
  152. #define fget_crc(f)     fget_longword(f)
  153.  
  154. #define setup_get(PTR)  (get_ptr = (PTR))
  155. #define get_byte()      ((uchar)(*get_ptr++ & 0xff))
  156.  
  157. #define BUFFERSIZE      4096
  158.  
  159. #define ASCII_MASK      0x7F
  160.  
  161. #define CRCPOLY         0xEDB88320L
  162.  
  163. #define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)
  164.  
  165. /* Local functions */
  166.  
  167. #ifdef MODERN
  168. static void  make_crctable(void);
  169. static void  crc_buf(char *str, int len);
  170. static void  strparity(uchar *p);
  171. static FILE  *fopen_msg(char *name, char *mode);
  172. static int   fget_byte(FILE *f);
  173. static uint  fget_word(FILE *f);
  174. static ulong fget_longword(FILE *f);
  175. static void  fread_crc(uchar *p, int n, FILE *f);
  176. static void  decode_path(char *name);
  177. static void  get_date_str(char *str, ulong tstamp);
  178. static int   parse_path(char *pathname, char *path, char *entry);
  179. static void  strncopy(char *to, char *from, int len);
  180. static uint  get_word(void);
  181. static ulong get_longword(void);
  182. static long  find_header(FILE *fd);
  183. static int   read_header(int first, FILE *fd, char *name);
  184. static void  skip(void);
  185. static void  unstore(void);
  186. static int   check_flags(void);
  187. static int   extract(void);
  188. static int   test(void);
  189. static uint  ratio(long a, long b);
  190. static void  list_start(void);
  191. static void  list_arc(int count);
  192. static void  execute_cmd(void);
  193. static void  help(void);
  194. #endif /* MODERN */
  195.  
  196. /* Local variables */
  197.  
  198. static char   filename[FNAME_MAX];
  199. static char   comment[COMMENT_MAX];
  200. static char   *hdr_filename;
  201. static char   *hdr_comment;
  202.  
  203. static ushort headersize;
  204. static uchar  first_hdr_size;
  205. static uchar  arj_nbr;
  206. static uchar  arj_x_nbr;
  207. static uchar  host_os;
  208. static uchar  arj_flags;
  209. static short  method;
  210. static uint   file_mode;
  211. static ulong  time_stamp;
  212. static short  entry_pos;
  213. static ushort host_data;
  214. static uchar  *get_ptr;
  215. static UCRC   file_crc;
  216. static UCRC   header_crc;
  217.  
  218. static long   first_hdr_pos;
  219. static long   torigsize;
  220. static long   tcompsize;
  221.  
  222. static int    clock_inx;
  223.  
  224. static char   *writemode[2]  = { "wb",  "w" };
  225.  
  226. static UCRC   crctable[UCHAR_MAX + 1];
  227.  
  228. /* Functions */
  229.  
  230. static void
  231. make_crctable()
  232. {
  233.     uint i, j;
  234.     UCRC r;
  235.  
  236.     for (i = 0; i <= UCHAR_MAX; i++)
  237.     {
  238.         r = i;
  239.         for (j = CHAR_BIT; j > 0; j--)
  240.         {
  241.             if (r & 1)
  242.                 r = (r >> 1) ^ CRCPOLY;
  243.             else
  244.                 r >>= 1;
  245.         }
  246.         crctable[i] = r;
  247.     }
  248. }
  249.  
  250. static void
  251. crc_buf(str, len)
  252. char *str;
  253. int  len;
  254. {
  255.     while (len--)
  256.         UPDATE_CRC(crc, *str++);
  257. }
  258.  
  259. void
  260. disp_clock()
  261. {
  262.     static char clock_str[4] = { '|', '/', '-', '\\' };
  263.  
  264.     printf("(%c)\b\b\b", clock_str[clock_inx]);
  265.     clock_inx = (clock_inx + 1) & 0x03;
  266. }
  267.  
  268. void
  269. error(fmt, arg)
  270. char *fmt;
  271. char *arg;
  272. {
  273.     putc('\n', stdout);
  274.     printf(fmt, arg, error_count);
  275.     putc('\n', stdout);
  276.     exit(EXIT_FAILURE);
  277. }
  278.  
  279. static void
  280. strparity(p)
  281. uchar *p;
  282. {
  283.     while (*p)
  284.     {
  285.         FIX_PARITY(*p);
  286.         p++;
  287.     }
  288. }
  289.  
  290. static FILE *
  291. fopen_msg(name, mode)
  292. char *name;
  293. char *mode;
  294. {
  295.     FILE *fd;
  296.  
  297.     fd = file_open(name, mode);
  298.     if (fd == NULL)
  299.         error(M_CANTOPEN, name);
  300.     return fd;
  301. }
  302.  
  303. static int
  304. fget_byte(f)
  305. FILE *f;
  306. {
  307.     int c;
  308.  
  309.     if ((c = getc(f)) == EOF)
  310.         error(M_CANTREAD, "");
  311.     return c & 0xFF;
  312. }
  313.  
  314. static uint
  315. fget_word(f)
  316. FILE *f;
  317. {
  318.     uint b0, b1;
  319.  
  320.     b0 = fget_byte(f);
  321.     b1 = fget_byte(f);
  322.     return (b1 << 8) + b0;
  323. }
  324.  
  325. static ulong
  326. fget_longword(f)
  327. FILE *f;
  328. {
  329.     ulong b0, b1, b2, b3;
  330.  
  331.     b0 = fget_byte(f);
  332.     b1 = fget_byte(f);
  333.     b2 = fget_byte(f);
  334.     b3 = fget_byte(f);
  335.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  336. }
  337.  
  338. static void
  339. fread_crc(p, n, f)
  340. uchar *p;
  341. int   n;
  342. FILE  *f;
  343. {
  344.     n = file_read((char *)p, 1, n, f);
  345.     origsize += n;
  346.     crc_buf((char *)p, n);
  347. }
  348.  
  349. void
  350. fwrite_txt_crc(p, n)
  351. uchar *p;
  352. int   n;
  353. {
  354.     uchar c;
  355.  
  356.     crc_buf((char *)p, n);
  357.     if (no_output)
  358.         return;
  359.  
  360.     if (file_type == TEXT_TYPE)
  361.     {
  362.         while (n--)
  363.         {
  364.             c = *p++;
  365.             if (host_os != OS)
  366.             {
  367.                 FIX_PARITY(c);
  368.             }
  369.             if (putc((int) c, outfile) == EOF)
  370.                 error(M_CANTWRIT, "");
  371.         }
  372.     }
  373.     else
  374.     {
  375.         if (file_write((char *)p, 1, n, outfile) != n)
  376.             error(M_CANTWRIT, "");
  377.     }
  378. }
  379.  
  380. void
  381. init_getbits()
  382. {
  383.     bitbuf = 0;
  384.     subbitbuf = 0;
  385.     bitcount = 0;
  386.     fillbuf(2 * CHAR_BIT);
  387. }
  388.  
  389. void
  390. fillbuf(n)                /* Shift bitbuf n bits left, read n bits */
  391. int n;
  392. {
  393.     bitbuf = (bitbuf << n) & 0xFFFF;  /* lose the first n bits */
  394.     while (n > bitcount)
  395.     {
  396.         bitbuf |= subbitbuf << (n -= bitcount);
  397.         if (compsize != 0)
  398.         {
  399.             compsize--;
  400.             subbitbuf = (uchar) getc(arcfile);
  401.         }
  402.         else
  403.             subbitbuf = 0;
  404.         bitcount = CHAR_BIT;
  405.     }
  406.     bitbuf |= subbitbuf >> (bitcount -= n);
  407. }
  408.  
  409. ushort
  410. getbits(n)
  411. int n;
  412. {
  413.     ushort x;
  414.  
  415.     x = bitbuf >> (2 * CHAR_BIT - n);
  416.     fillbuf(n);
  417.     return x;
  418. }
  419.  
  420. static void
  421. decode_path(name)
  422. char *name;
  423. {
  424.     for ( ; *name; name++)
  425.     {
  426.         if (*name == ARJ_PATH_CHAR)
  427.             *name = PATH_CHAR;
  428.     }
  429. }
  430.  
  431. static void
  432. get_date_str(str, tstamp)
  433. char  *str;
  434. ulong tstamp;
  435. {
  436.     sprintf(str, "%04u-%02u-%02u %02u:%02u:%02u",
  437.            ts_year(tstamp), ts_month(tstamp), ts_day(tstamp),
  438.            ts_hour(tstamp), ts_min(tstamp), ts_sec(tstamp));
  439. }
  440.  
  441. static int
  442. parse_path(pathname, path, entry)
  443. char *pathname;
  444. char *path;
  445. char *entry;
  446. {
  447.     char *cptr, *ptr, *fptr;
  448.     short pos;
  449.  
  450.     fptr = NULL;
  451.     for (cptr = PATH_SEPARATORS; *cptr; cptr++)
  452.     {
  453.         if ((ptr = strrchr(pathname, *cptr)) != NULL &&
  454.                 (fptr == NULL || ptr > fptr))
  455.             fptr = ptr;
  456.     }
  457.     if (fptr == NULL)
  458.         pos = 0;
  459.     else
  460.         pos = fptr + 1 - pathname;
  461.     if (path != NULL)
  462.     {
  463.        strncpy(path, pathname, pos);
  464.        path[pos] = NULL_CHAR;
  465.     }
  466.     if (entry != NULL)
  467.        strcpy(entry, &pathname[pos]);
  468.     return pos;
  469. }
  470.  
  471. static void
  472. strncopy(to, from, len)
  473. char *to;
  474. char *from;
  475. int  len;
  476. {
  477.     int i;
  478.  
  479.     for (i = 1; i < len && *from; i++)
  480.         *to++ = *from++;
  481.     *to = NULL_CHAR;
  482. }
  483.  
  484. void
  485. strlower(s)
  486. char *s;
  487. {
  488.     while (*s)
  489.     {
  490.         *s = (char) tolower(*s);
  491.         s++;
  492.     }
  493. }
  494.  
  495. void
  496. strupper(s)
  497. char *s;
  498. {
  499.     while (*s)
  500.     {
  501.         *s = (char) toupper(*s);
  502.         s++;
  503.     }
  504. }
  505.  
  506. voidp *
  507. malloc_msg(size)
  508. int size;
  509. {
  510.     char *p;
  511.  
  512.     if ((p = (char *)xmalloc(size)) == NULL)
  513.         error(M_NOMEMORY, "");
  514.     return (voidp *)p;
  515. }
  516.  
  517. static uint
  518. get_word()
  519. {
  520.     uint b0, b1;
  521.  
  522.     b0 = get_byte();
  523.     b1 = get_byte();
  524.     return (b1 << 8) + b0;
  525. }
  526.  
  527. static ulong
  528. get_longword()
  529. {
  530.     ulong b0, b1, b2, b3;
  531.  
  532.     b0 = get_byte();
  533.     b1 = get_byte();
  534.     b2 = get_byte();
  535.     b3 = get_byte();
  536.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  537. }
  538.  
  539. static long
  540. find_header(fd)
  541. FILE *fd;
  542. {
  543.     long arcpos, lastpos;
  544.     int c;
  545.  
  546.     arcpos = file_tell(fd);
  547.     file_seek(fd, 0L, SEEK_END);
  548.     lastpos = file_tell(fd) - 2;
  549.     if (lastpos > MAXSFX)
  550.         lastpos = MAXSFX;
  551.     for ( ; arcpos < lastpos; arcpos++)
  552.     {
  553.         file_seek(fd, arcpos, SEEK_SET);
  554.         c = fget_byte(fd);
  555.         while (arcpos < lastpos)
  556.         {
  557.             if (c != HEADER_ID_LO)  /* low order first */
  558.                 c = fget_byte(fd);
  559.             else if ((c = fget_byte(fd)) == HEADER_ID_HI)
  560.                 break;
  561.             arcpos++;
  562.         }
  563.         if (arcpos >= lastpos)
  564.             break;
  565.         if ((headersize = fget_word(fd)) <= HEADERSIZE_MAX)
  566.         {
  567.             crc = CRC_MASK;
  568.             fread_crc(header, (int) headersize, fd);
  569.             if ((crc ^ CRC_MASK) == fget_crc(fd))
  570.             {
  571.                 file_seek(fd, arcpos, SEEK_SET);
  572.                 return arcpos;
  573.             }
  574.         }
  575.     }
  576.     return -1;          /* could not find a valid header */
  577. }
  578.  
  579. static int
  580. read_header(first, fd, name)
  581. int  first;
  582. FILE *fd;
  583. char *name;
  584. {
  585.     ushort extheadersize, header_id;
  586.  
  587.     header_id = fget_word(fd);
  588.     if (header_id != HEADER_ID)
  589.     {
  590.         if (first)
  591.             error(M_NOTARJ, name);
  592.         else
  593.             error(M_BADHEADR, "");
  594.     }
  595.  
  596.     headersize = fget_word(fd);
  597.     if (headersize == 0)
  598.         return 0;               /* end of archive */
  599.     if (headersize > HEADERSIZE_MAX)
  600.         error(M_BADHEADR, "");
  601.  
  602.     crc = CRC_MASK;
  603.     fread_crc(header, (int) headersize, fd);
  604.     header_crc = fget_crc(fd);
  605.     if ((crc ^ CRC_MASK) != header_crc)
  606.         error(M_HEADRCRC, "");
  607.  
  608.     setup_get(header);
  609.     first_hdr_size = get_byte();
  610.     arj_nbr = get_byte();
  611.     arj_x_nbr = get_byte();
  612.     host_os = get_byte();
  613.     arj_flags = get_byte();
  614.     method = get_byte();
  615.     file_type = get_byte();
  616.     (void)get_byte();
  617.     time_stamp = get_longword();
  618.     compsize = get_longword();
  619.     origsize = get_longword();
  620.     file_crc = get_crc();
  621.     entry_pos = get_word();
  622.     file_mode = get_word();
  623.     host_data = get_word();
  624.  
  625.     hdr_filename = (char *)&header[first_hdr_size];
  626.     strncopy(filename, hdr_filename, sizeof(filename));
  627.     if (host_os != OS)
  628.         strparity((uchar *)filename);
  629.     if ((arj_flags & PATHSYM_FLAG) != 0)
  630.         decode_path(filename);
  631.  
  632.     hdr_comment = (char *)&header[first_hdr_size + strlen(hdr_filename) + 1];
  633.     strncopy(comment, hdr_comment, sizeof(comment));
  634.     if (host_os != OS)
  635.         strparity((uchar *)comment);
  636.  
  637.     /* if extheadersize == 0 then no CRC */
  638.     /* otherwise read extheader data and read 4 bytes for CRC */
  639.  
  640.     while ((extheadersize = fget_word(fd)) != 0)
  641.         file_seek(fd, (long) (extheadersize + 4), SEEK_CUR);
  642.  
  643.     return 1;                   /* success */
  644. }
  645.  
  646. static void
  647. skip()
  648. {
  649.     file_seek(arcfile, compsize, SEEK_CUR);
  650. }
  651.  
  652. static void
  653. unstore()
  654. {
  655.     int n;
  656.     long pos;
  657.     char *buffer;
  658.  
  659.     buffer = (char *)malloc_msg(BUFFERSIZE);
  660.     pos = file_tell(arcfile);
  661.     disp_clock();
  662.     n = (int)(BUFFERSIZE - (pos % BUFFERSIZE));
  663.     n = compsize > (long)n ? n : (int)compsize;
  664.     while (compsize > 0)
  665.     {
  666.         if (file_read(buffer, 1, n, arcfile) != n)
  667.             error(M_CANTREAD, "");
  668.         disp_clock();
  669.         compsize -= n;
  670.         fwrite_txt_crc((uchar *)buffer, n);
  671.         n = compsize > BUFFERSIZE ? BUFFERSIZE : (int)compsize;
  672.     }
  673.     free(buffer);
  674. }
  675.  
  676. static int
  677. check_flags()
  678. {
  679.     if (arj_x_nbr > ARJ_X_VERSION)
  680.     {
  681.         printf(M_UNKNVERS, arj_x_nbr);
  682.         printf(M_SKIPPED, filename);
  683.         skip();
  684.         return -1;
  685.     }
  686.     if ((arj_flags & GARBLE_FLAG) != 0)
  687.     {
  688.         printf(M_ENCRYPT);
  689.         printf(M_SKIPPED, filename);
  690.         skip();
  691.         return -1;
  692.     }
  693.     if (method < 0 || method > MAXMETHOD || (method == 4 && arj_nbr == 1))
  694.     {
  695.         printf(M_UNKNMETH, method);
  696.         printf(M_SKIPPED, filename);
  697.         skip();
  698.         return -1;
  699.     }
  700.     if (file_type != BINARY_TYPE && file_type != TEXT_TYPE)
  701.     {
  702.         printf(M_UNKNTYPE, file_type);
  703.         printf(M_SKIPPED, filename);
  704.         skip();
  705.         return -1;
  706.     }
  707.     return 0;
  708. }
  709.  
  710. static int
  711. extract()
  712. {
  713.     char name[FNAME_MAX];
  714.  
  715.     if (check_flags())
  716.     {
  717.         error_count++;
  718.         return 0;
  719.     }
  720.  
  721.     no_output = 0;
  722.     if (command == 'E')
  723.         strcpy(name, &filename[entry_pos]);
  724.     else
  725.     {
  726.         strcpy(name, DEFAULT_DIR);
  727.         strcat(name, filename);
  728.     }
  729.  
  730.     if (host_os != OS)
  731.         default_case_path(name);
  732.  
  733.     if (file_exists(name))
  734.     {
  735.         printf(M_FEXISTS, name);
  736.         printf(M_SKIPPED, name);
  737.         skip();
  738.         error_count++;
  739.         return 0;
  740.     }
  741.     outfile = file_open(name, writemode[file_type & 1]);
  742.     if (outfile == NULL)
  743.     {
  744.         printf(M_CANTOPEN, name);
  745.         putchar('\n');
  746.         skip();
  747.         error_count++;
  748.         return 0;
  749.     }
  750.     printf(M_EXTRACT, name);
  751.     if (host_os != OS && file_type == BINARY_TYPE)
  752.         printf(M_DIFFHOST);
  753.     printf("  ");
  754.  
  755.     crc = CRC_MASK;
  756.  
  757.     if (method == 0)
  758.         unstore();
  759.     else if (method == 1 || method == 2 || method == 3)
  760.         decode();
  761.     else if (method == 4)
  762.         decode_f();
  763.     fclose(outfile);
  764.  
  765.     set_ftime_mode(name, time_stamp, file_mode, (uint) host_os);
  766.  
  767.     if ((crc ^ CRC_MASK) == file_crc)
  768.         printf(M_CRCOK);
  769.     else
  770.     {
  771.         printf(M_CRCERROR);
  772.         error_count++;
  773.     }
  774.     return 1;
  775. }
  776.  
  777. static int
  778. test()
  779. {
  780.     if (check_flags())
  781.         return 0;
  782.  
  783.     no_output = 1;
  784.     printf(M_TESTING, filename);
  785.     printf("  ");
  786.  
  787.     crc = CRC_MASK;
  788.  
  789.     if (method == 0)
  790.         unstore();
  791.     else if (method == 1 || method == 2 || method == 3)
  792.         decode();
  793.     else if (method == 4)
  794.         decode_f();
  795.  
  796.     if ((crc ^ CRC_MASK) == file_crc)
  797.         printf(M_CRCOK);
  798.     else
  799.     {
  800.         printf(M_CRCERROR);
  801.         error_count++;
  802.     }
  803.     return 1;
  804. }
  805.  
  806. static uint
  807. ratio(a, b)
  808. long a, b;
  809. {
  810.    int i;
  811.  
  812.    for (i = 0; i < 3; i++)
  813.        if (a <= LONG_MAX / 10)
  814.            a *= 10;
  815.        else
  816.            b /= 10;
  817.    if ((long) (a + (b >> 1)) < a)
  818.    {
  819.        a >>= 1;
  820.        b >>= 1;
  821.    }
  822.    if (b == 0)
  823.        return 0;
  824.    return (uint) ((a + (b >> 1)) / b);
  825. }
  826.  
  827. static void
  828. list_start()
  829. {
  830.     printf("Filename       Original Compressed Ratio DateTime modified CRC-32   AttrBTPMGVX\n");
  831.     printf("------------ ---------- ---------- ----- ----------------- -------- -----------\n");
  832. }
  833.  
  834. static void
  835. list_arc(count)
  836. int count;
  837. {
  838.     uint r;
  839.     int garble_mode, path_mode, volume_mode, extfil_mode, ftype, bckf_mode;
  840.     char date_str[20], fmode_str[10];
  841.     static char mode[5] = { 'B', 'T', '?', 'D', 'V' };
  842.     static char pthf[2] = { ' ', '+' };
  843.     static char pwdf[2] = { ' ', 'G' };  /* plain, encrypted */
  844.     static char volf[2] = { ' ', 'V' };
  845.     static char extf[2] = { ' ', 'X' };
  846.     static char bckf[2] = { ' ', '*' };
  847.  
  848.     if (count == 0)
  849.         list_start();
  850.  
  851.     garble_mode = ((arj_flags & GARBLE_FLAG) != 0);
  852.     volume_mode = ((arj_flags & VOLUME_FLAG) != 0);
  853.     extfil_mode = ((arj_flags & EXTFILE_FLAG) != 0);
  854.     bckf_mode   = ((arj_flags & BACKUP_FLAG) != 0);
  855.     path_mode   = (entry_pos > 0);
  856.     r = ratio(compsize, origsize);
  857.     torigsize += origsize;
  858.     tcompsize += compsize;
  859.     ftype = file_type;
  860.     if (ftype != BINARY_TYPE && ftype != TEXT_TYPE && ftype != DIR_TYPE &&
  861.             ftype != LABEL_TYPE)
  862.         ftype = 3;
  863.     get_date_str(date_str, time_stamp);
  864.     strcpy(fmode_str, "    ");
  865.     if (host_os == OS)
  866.         get_mode_str(fmode_str, (uint) file_mode);
  867.     if (strlen(&filename[entry_pos]) > 12)
  868.         printf("%-12s\n             ", &filename[entry_pos]);
  869.     else
  870.         printf("%-12s ", &filename[entry_pos]);
  871.     printf("%10ld %10ld %u.%03u %s %08lX %4s%c%c%c%u%c%c%c\n",
  872.         origsize, compsize, r / 1000, r % 1000, &date_str[2], file_crc,
  873.         fmode_str, bckf[bckf_mode], mode[ftype], pthf[path_mode], method,
  874.         pwdf[garble_mode], volf[volume_mode], extf[extfil_mode]);
  875. }
  876.  
  877. static void
  878. execute_cmd()
  879. {
  880.     int file_count;
  881.     char date_str[22];
  882.     uint r;
  883.  
  884.     first_hdr_pos = 0;
  885.     time_stamp = 0;
  886.     first_hdr_size = FIRST_HDR_SIZE;
  887.  
  888.     arcfile = fopen_msg(arc_name, "rb");
  889.  
  890.     printf(M_PROCARC, arc_name);
  891.  
  892.     first_hdr_pos = find_header(arcfile);
  893.     if (first_hdr_pos < 0)
  894.         error(M_NOTARJ, arc_name);
  895.     file_seek(arcfile, first_hdr_pos, SEEK_SET);
  896.     if (!read_header(1, arcfile, arc_name))
  897.         error(M_BADCOMNT, "");
  898.     get_date_str(date_str, time_stamp);
  899.     printf(M_ARCDATE, date_str);
  900.     if (arj_nbr >= ARJ_M_VERSION)
  901.     {
  902.         get_date_str(date_str, (ulong) compsize);
  903.         printf(M_ARCDATEM, date_str);
  904.     }
  905.     printf("\n");
  906.  
  907.     file_count = 0;
  908.     while (read_header(0, arcfile, arc_name))
  909.     {
  910.         switch (command)
  911.         {
  912.         case 'E':
  913.         case 'X':
  914.             if (extract())
  915.                 file_count++;
  916.             break;
  917.         case 'L':
  918.             list_arc(file_count++);
  919.             skip();
  920.             break;
  921.         case 'T':
  922.             if (test())
  923.                 file_count++;
  924.             break;
  925.         }
  926.     }
  927.  
  928.     if (command == 'L')
  929.     {
  930.         printf("------------ ---------- ---------- ----- -----------------\n");
  931.         r = ratio(tcompsize, torigsize);
  932.         printf(" %5d files %10ld %10ld %u.%03u %s\n",
  933.             file_count, torigsize, tcompsize, r / 1000, r % 1000, &date_str[2]);
  934.     }
  935.     else
  936.         printf(M_NBRFILES, file_count);
  937.  
  938.     fclose(arcfile);
  939. }
  940.  
  941. static void
  942. help()
  943. {
  944.     int i;
  945.  
  946.     for (i = 0; M_USAGE[i] != NULL; i++)
  947.         printf(M_USAGE[i]);
  948. }
  949.  
  950. int
  951. main(argc, argv)
  952. int  argc;
  953. char *argv[];
  954. {
  955.     int i, j, lastc;
  956.     char *arc_p;
  957.  
  958. #ifdef THINK_C
  959.     argc = ccommand(&argv);
  960. #endif
  961.  
  962.     printf(M_VERSION);
  963.  
  964.     if (argc == 1)
  965.     {
  966.         help();
  967.         return EXIT_SUCCESS;
  968.     }
  969.     else if (argc == 2)
  970.     {
  971.         command = 'L';
  972.         arc_p = argv[1];
  973.     }
  974.     else if (argc == 3)
  975.     {
  976.         if (strlen(argv[1]) > 1)
  977.             error(M_BADCOMND, argv[1]);
  978.         command = toupper(*argv[1]);
  979.         if (strchr("ELTX", command) == NULL)
  980.             error(M_BADCOMND, argv[1]);
  981.         arc_p = argv[2];
  982.     }
  983.     else
  984.     {
  985.         help();
  986.         return EXIT_FAILURE;
  987.     }
  988.  
  989.     strncopy(arc_name, arc_p, FNAME_MAX);
  990.     case_path(arc_name);
  991.     i = strlen(arc_name);
  992.     j = parse_path(arc_name, (char *)NULL, (char *)NULL);
  993.     lastc = arc_name[i - 1];
  994.     if (lastc == ARJ_DOT)
  995.         arc_name[i - 1] = NULL_CHAR;
  996.     else if (strchr(&arc_name[j], ARJ_DOT) == NULL)
  997.         strcat(arc_name, M_SUFFIX);
  998.  
  999.     make_crctable();
  1000.  
  1001.     error_count = 0;
  1002.     clock_inx = 0;
  1003.     arcfile = NULL;
  1004.     outfile = NULL;
  1005.  
  1006.     execute_cmd();
  1007.  
  1008.     if (error_count > 0)
  1009.         error(M_ERRORCNT, "");
  1010.  
  1011.     return EXIT_SUCCESS;
  1012. }
  1013.  
  1014. /* end UNARJ.C */
  1015.