From 9bd49d6a22a31bd3b5e3e3dcaacdb8a019630506 Mon Sep 17 00:00:00 2001 From: Matt Kraai Date: Tue, 5 Feb 2002 22:31:48 +0000 Subject: * archival/gzip.c (ifname, ofname): Delete. (gzip_main): Handle multiple files. * include/usage.h (gzip_trivial_usage): Allow multiple FILEs. (gzip_full_usage): Ditto. * testsuite/gzip/gzip-accepts-multiple-files: New. * testsuite/gzip/gzip-removes-original-file: New. --- archival/gzip.c | 129 +++++++++++++++++++++++++++----------------------------- 1 file changed, 62 insertions(+), 67 deletions(-) (limited to 'archival') diff --git a/archival/gzip.c b/archival/gzip.c index 732a83e90..fed93907b 100644 --- a/archival/gzip.c +++ b/archival/gzip.c @@ -317,8 +317,6 @@ static long ifile_size; /* input file size, -1 for devices (debug only) */ static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */ static int z_len; /* strlen(z_suffix) */ -static char ifname[MAX_PATH_LEN]; /* input file name */ -static char ofname[MAX_PATH_LEN]; /* output file name */ static int ifd; /* input file descriptor */ static int ofd; /* output file descriptor */ static unsigned insize; /* valid bytes in inbuf */ @@ -1214,7 +1212,6 @@ int gzip_main(int argc, char **argv) struct stat statBuf; char *delFileName; int tostdout = 0; - int fromstdin = 0; int force = 0; int opt; @@ -1241,16 +1238,6 @@ int gzip_main(int argc, char **argv) show_usage(); } } - if ((optind == argc) || (strcmp(argv[optind], "-") == 0)) { - fromstdin = 1; - tostdout = 1; - } - - if (argc - optind > 1) - show_usage (); - - if (isatty(fileno(stdout)) && tostdout==1 && force==0) - error_msg_and_die( "compressed data not written to terminal. Use -f to force it."); foreground = signal(SIGINT, SIG_IGN) != SIG_IGN; if (foreground) { @@ -1277,70 +1264,78 @@ int gzip_main(int argc, char **argv) ALLOC(uch, window, 2L * WSIZE); ALLOC(ush, tab_prefix, 1L << BITS); - if (fromstdin == 1) { - strcpy(ofname, "stdin"); + clear_bufs(); + part_nb = 0; - inFileNum = fileno(stdin); - time_stamp = 0; /* time unknown by default */ - ifile_size = -1L; /* convention for unknown size */ + if (optind == argc) { + time_stamp = 0; + ifile_size = -1L; + zip(STDIN_FILENO, STDOUT_FILENO); } else { - /* Open up the input file */ - strncpy(ifname, argv[optind], MAX_PATH_LEN); - - /* Open input file */ - inFileNum = open(ifname, O_RDONLY); - if (inFileNum < 0 || stat(ifname, &statBuf) < 0) - perror_msg_and_die("%s", ifname); - /* Get the time stamp on the input file. */ - time_stamp = statBuf.st_ctime; - ifile_size = statBuf.st_size; - } + int i; + for (i = optind; i < argc; i++) { + char *path = NULL; - if (tostdout == 1) { - /* And get to work */ - strcpy(ofname, "stdout"); - outFileNum = fileno(stdout); + if (strcmp(argv[i], "-") == 0) { + time_stamp = 0; + ifile_size = -1L; + inFileNum = STDIN_FILENO; + outFileNum = STDOUT_FILENO; + } else { + inFileNum = open(argv[i], O_RDONLY); + if (inFileNum < 0 || fstat (inFileNum, &statBuf) < 0) + perror_msg_and_die("%s", argv[i]); + time_stamp = statBuf.st_ctime; + ifile_size = statBuf.st_size; + + if (!tostdout) { + path = xmalloc(strlen(argv[i]) + 4); + strcpy(path, argv[i]); + strcat(path, ".gz"); + + /* Open output file */ +#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) + outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW); +#else + outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL); +#endif + if (outFileNum < 0) { + perror_msg("%s", path); + free(path); + continue; + } + + /* Set permissions on the file */ + fchmod(outFileNum, statBuf.st_mode); + } else + outFileNum = STDOUT_FILENO; + } - clear_bufs(); /* clear input and output buffers */ - part_nb = 0; + if (path == NULL && force == 0) { + perror_msg("compressed data not written to a terminal. Use -f to force compression."); + free(path); + continue; + } - /* Actually do the compression/decompression. */ - zip(inFileNum, outFileNum); + result = zip(inFileNum, outFileNum); - } else { + if (path != NULL) { + close (inFileNum); + close (outFileNum); - /* And get to work */ - strncpy(ofname, ifname, MAX_PATH_LEN - 4); - strcat(ofname, ".gz"); + /* Delete the original file */ + if (result == OK) + delFileName = argv[i]; + else + delFileName = path; + if (unlink(delFileName) < 0) + perror_msg("%s", delFileName); + } - /* Open output fille */ -#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) - outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW); -#else - outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL); -#endif - if (outFileNum < 0) - perror_msg_and_die("%s", ofname); - /* Set permissions on the file */ - fchmod(outFileNum, statBuf.st_mode); - - clear_bufs(); /* clear input and output buffers */ - part_nb = 0; - - /* Actually do the compression/decompression. */ - result = zip(inFileNum, outFileNum); - close(outFileNum); - close(inFileNum); - /* Delete the original file */ - if (result == OK) - delFileName = ifname; - else - delFileName = ofname; - - if (unlink(delFileName) < 0) - perror_msg_and_die("%s", delFileName); + free(path); + } } return(exit_code); -- cgit v1.2.3