* 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.
This commit is contained in:
Matt Kraai 2002-02-05 22:31:48 +00:00
parent 592a3e63ee
commit 9bd49d6a22
4 changed files with 68 additions and 67 deletions

View File

@ -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 char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
static int z_len; /* strlen(z_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 ifd; /* input file descriptor */
static int ofd; /* output file descriptor */ static int ofd; /* output file descriptor */
static unsigned insize; /* valid bytes in inbuf */ static unsigned insize; /* valid bytes in inbuf */
@ -1214,7 +1212,6 @@ int gzip_main(int argc, char **argv)
struct stat statBuf; struct stat statBuf;
char *delFileName; char *delFileName;
int tostdout = 0; int tostdout = 0;
int fromstdin = 0;
int force = 0; int force = 0;
int opt; int opt;
@ -1241,16 +1238,6 @@ int gzip_main(int argc, char **argv)
show_usage(); 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; foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
if (foreground) { if (foreground) {
@ -1277,70 +1264,78 @@ int gzip_main(int argc, char **argv)
ALLOC(uch, window, 2L * WSIZE); ALLOC(uch, window, 2L * WSIZE);
ALLOC(ush, tab_prefix, 1L << BITS); ALLOC(ush, tab_prefix, 1L << BITS);
if (fromstdin == 1) { clear_bufs();
strcpy(ofname, "stdin"); part_nb = 0;
inFileNum = fileno(stdin); if (optind == argc) {
time_stamp = 0; /* time unknown by default */ time_stamp = 0;
ifile_size = -1L; /* convention for unknown size */ ifile_size = -1L;
zip(STDIN_FILENO, STDOUT_FILENO);
} else { } else {
/* Open up the input file */ int i;
strncpy(ifname, argv[optind], MAX_PATH_LEN);
/* Open input file */ for (i = optind; i < argc; i++) {
inFileNum = open(ifname, O_RDONLY); char *path = NULL;
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;
}
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 == 1) { if (!tostdout) {
/* And get to work */ path = xmalloc(strlen(argv[i]) + 4);
strcpy(ofname, "stdout"); strcpy(path, argv[i]);
outFileNum = fileno(stdout); strcat(path, ".gz");
clear_bufs(); /* clear input and output buffers */ /* Open output file */
part_nb = 0;
/* Actually do the compression/decompression. */
zip(inFileNum, outFileNum);
} else {
/* And get to work */
strncpy(ofname, ifname, MAX_PATH_LEN - 4);
strcat(ofname, ".gz");
/* Open output fille */
#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW); outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
#else #else
outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL); outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL);
#endif #endif
if (outFileNum < 0) if (outFileNum < 0) {
perror_msg_and_die("%s", ofname); perror_msg("%s", path);
/* Set permissions on the file */ free(path);
fchmod(outFileNum, statBuf.st_mode); continue;
}
clear_bufs(); /* clear input and output buffers */ /* Set permissions on the file */
part_nb = 0; fchmod(outFileNum, statBuf.st_mode);
} else
outFileNum = STDOUT_FILENO;
}
/* Actually do the compression/decompression. */ if (path == NULL && force == 0) {
result = zip(inFileNum, outFileNum); perror_msg("compressed data not written to a terminal. Use -f to force compression.");
close(outFileNum); free(path);
close(inFileNum); continue;
/* Delete the original file */ }
if (result == OK)
delFileName = ifname;
else
delFileName = ofname;
if (unlink(delFileName) < 0) result = zip(inFileNum, outFileNum);
perror_msg_and_die("%s", delFileName);
if (path != NULL) {
close (inFileNum);
close (outFileNum);
/* Delete the original file */
if (result == OK)
delFileName = argv[i];
else
delFileName = path;
if (unlink(delFileName) < 0)
perror_msg("%s", delFileName);
}
free(path);
}
} }
return(exit_code); return(exit_code);

View File

@ -628,9 +628,9 @@
"-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n" "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
#define gzip_trivial_usage \ #define gzip_trivial_usage \
"[OPTION]... [FILE]" "[OPTION]... [FILE]..."
#define gzip_full_usage \ #define gzip_full_usage \
"Compress FILE with maximum compression.\n" \ "Compress FILE(s) with maximum compression.\n" \
"When FILE is '-' or unspecified, reads standard input. Implies -c.\n\n" \ "When FILE is '-' or unspecified, reads standard input. Implies -c.\n\n" \
"Options:\n" \ "Options:\n" \
"\t-c\tWrite output to standard output instead of FILE.gz\n" \ "\t-c\tWrite output to standard output instead of FILE.gz\n" \

View File

@ -0,0 +1,3 @@
touch foo bar
busybox gzip foo bar
test -f foo.gz -a -f bar.gz

View File

@ -0,0 +1,3 @@
touch foo
busybox gzip foo
test ! -f foo