diff -ur unarj-2.43.orig/unarj.c unarj-2.43/unarj.c --- unarj-2.43.orig/unarj.c Mon Sep 29 13:00:24 1997 +++ unarj-2.43/unarj.c Mon Aug 13 12:56:50 2001 @@ -49,6 +49,7 @@ #ifdef MODERN #include +#include #include #include #else /* !MODERN */ @@ -98,11 +99,14 @@ static char *M_USAGE [] = { -"Usage: UNARJ archive[.arj] (list archive)\n", -" UNARJ e archive (extract archive)\n", -" UNARJ l archive (list archive)\n", -" UNARJ t archive (test archive)\n", -" UNARJ x archive (extract with pathnames)\n", +"Usage: UNARJ [elptx][q] archive_file [file]\n", +"commands: options:\n", +" (none) list archive q quiet\n", +" e extract archive\n", +" l list archive\n", +" p extract to stdout\n", +" t test archive\n", +" x extract with pathnames\n", "\n", "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n", "You may freely use, copy and distribute this program, provided that no fee\n", @@ -191,6 +195,7 @@ static void list_start(void); static void list_arc(int count); static void execute_cmd(void); +static void print_stdout(char *, ...); static void help(void); #endif /* MODERN */ @@ -200,6 +205,7 @@ static char comment[COMMENT_MAX]; static char *hdr_filename; static char *hdr_comment; +static char *member_to_extract; static ushort headersize; static uchar first_hdr_size; @@ -221,6 +227,7 @@ static long tcompsize; static int clock_inx; +static int be_quiet; static char *writemode[2] = { "wb", "w" }; @@ -262,7 +269,7 @@ { static char clock_str[4] = { '|', '/', '-', '\\' }; - printf("(%c)\b\b\b", clock_str[clock_inx]); + print_stdout("(%c)\b\b\b", clock_str[clock_inx]); clock_inx = (clock_inx + 1) & 0x03; } @@ -272,7 +279,7 @@ char *arg; { putc('\n', stdout); - printf(fmt, arg, error_count); + fprintf(stderr, fmt, arg, error_count); putc('\n', stdout); exit(EXIT_FAILURE); } @@ -679,29 +686,29 @@ { if (arj_x_nbr > ARJ_X_VERSION) { - printf(M_UNKNVERS, arj_x_nbr); - printf(M_SKIPPED, filename); + print_stdout(M_UNKNVERS, arj_x_nbr); + print_stdout(M_SKIPPED, filename); skip(); return -1; } if ((arj_flags & GARBLE_FLAG) != 0) { - printf(M_ENCRYPT); - printf(M_SKIPPED, filename); + print_stdout(M_ENCRYPT); + print_stdout(M_SKIPPED, filename); skip(); return -1; } if (method < 0 || method > MAXMETHOD || (method == 4 && arj_nbr == 1)) { - printf(M_UNKNMETH, method); - printf(M_SKIPPED, filename); + print_stdout(M_UNKNMETH, method); + print_stdout(M_SKIPPED, filename); skip(); return -1; } if (file_type != BINARY_TYPE && file_type != TEXT_TYPE) { - printf(M_UNKNTYPE, file_type); - printf(M_SKIPPED, filename); + print_stdout(M_UNKNTYPE, file_type); + print_stdout(M_SKIPPED, filename); skip(); return -1; } @@ -720,7 +727,7 @@ } no_output = 0; - if (command == 'E') + if (command == 'E' || command == 'P') strcpy(name, &filename[entry_pos]); else { @@ -731,27 +738,36 @@ if (host_os != OS) default_case_path(name); - if (file_exists(name)) + if (member_to_extract && strcmp(name, member_to_extract)) { + skip(); + return 1; + } + + if (command != 'P' && file_exists(name)) { - printf(M_FEXISTS, name); - printf(M_SKIPPED, name); + print_stdout(M_FEXISTS, name); + print_stdout(M_SKIPPED, name); skip(); error_count++; return 0; } - outfile = file_open(name, writemode[file_type & 1]); + if (command == 'P') + outfile = stdout; + else + outfile = file_open(name, writemode[file_type & 1]); + if (outfile == NULL) { - printf(M_CANTOPEN, name); + print_stdout(M_CANTOPEN, name); putchar('\n'); skip(); error_count++; return 0; } - printf(M_EXTRACT, name); + print_stdout(M_EXTRACT, name); if (host_os != OS && file_type == BINARY_TYPE) - printf(M_DIFFHOST); - printf(" "); + print_stdout(M_DIFFHOST); + print_stdout(" "); crc = CRC_MASK; @@ -761,15 +777,18 @@ decode(); else if (method == 4) decode_f(); - fclose(outfile); + if (outfile != stdout) + { + fclose(outfile); - set_ftime_mode(name, time_stamp, file_mode, (uint) host_os); + set_ftime_mode(name, time_stamp, file_mode, (uint) host_os); + } if ((crc ^ CRC_MASK) == file_crc) - printf(M_CRCOK); + print_stdout(M_CRCOK); else { - printf(M_CRCERROR); + print_stdout(M_CRCERROR); error_count++; } return 1; @@ -782,8 +801,8 @@ return 0; no_output = 1; - printf(M_TESTING, filename); - printf(" "); + print_stdout(M_TESTING, filename); + print_stdout(" "); crc = CRC_MASK; @@ -795,10 +814,10 @@ decode_f(); if ((crc ^ CRC_MASK) == file_crc) - printf(M_CRCOK); + print_stdout(M_CRCOK); else { - printf(M_CRCERROR); + print_stdout(M_CRCERROR); error_count++; } return 1; @@ -828,8 +847,8 @@ static void list_start() { - printf("Filename Original Compressed Ratio DateTime modified CRC-32 AttrBTPMGVX\n"); - printf("------------ ---------- ---------- ----- ----------------- -------- -----------\n"); + print_stdout("Filename Original Compressed Ratio DateTime modified CRC-32 AttrBTPMGVX\n"); + print_stdout("------------ ---------- ---------- ----- ----------------- -------- -----------\n"); } static void @@ -888,7 +907,7 @@ arcfile = fopen_msg(arc_name, "rb"); - printf(M_PROCARC, arc_name); + print_stdout(M_PROCARC, arc_name); first_hdr_pos = find_header(arcfile); if (first_hdr_pos < 0) @@ -897,13 +916,13 @@ if (!read_header(1, arcfile, arc_name)) error(M_BADCOMNT, ""); get_date_str(date_str, time_stamp); - printf(M_ARCDATE, date_str); + print_stdout(M_ARCDATE, date_str); if (arj_nbr >= ARJ_M_VERSION) { get_date_str(date_str, (ulong) compsize); - printf(M_ARCDATEM, date_str); + print_stdout(M_ARCDATEM, date_str); } - printf("\n"); + print_stdout("\n"); file_count = 0; while (read_header(0, arcfile, arc_name)) @@ -911,6 +930,7 @@ switch (command) { case 'E': + case 'P': case 'X': if (extract()) file_count++; @@ -928,18 +948,30 @@ if (command == 'L') { - printf("------------ ---------- ---------- ----- -----------------\n"); + print_stdout("------------ ---------- ---------- ----- -----------------\n"); r = ratio(tcompsize, torigsize); - printf(" %5d files %10ld %10ld %u.%03u %s\n", + print_stdout(" %5d files %10ld %10ld %u.%03u %s\n", file_count, torigsize, tcompsize, r / 1000, r % 1000, &date_str[2]); } else - printf(M_NBRFILES, file_count); + print_stdout(M_NBRFILES, file_count); fclose(arcfile); } static void +print_stdout(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + + if (!be_quiet) + (void)vprintf(fmt, ap); + + va_end(ap); +} + +static void help() { int i; @@ -960,7 +992,10 @@ argc = ccommand(&argv); #endif - printf(M_VERSION); + if (argc > 2 && argv[1][1] && toupper(argv[1][1]) == 'Q') + be_quiet = 1; + + print_stdout(M_VERSION); if (argc == 1) { @@ -972,14 +1007,16 @@ command = 'L'; arc_p = argv[1]; } - else if (argc == 3) + else if (argc == 3 || argc == 4) { - if (strlen(argv[1]) > 1) + if (strlen(argv[1]) > 2) error(M_BADCOMND, argv[1]); command = toupper(*argv[1]); - if (strchr("ELTX", command) == NULL) + if (strchr("ELPTX", command) == NULL) error(M_BADCOMND, argv[1]); arc_p = argv[2]; + if (argc == 4) + member_to_extract = argv[3]; } else {