Rework per how I did things in version in 0.60.3 so it can

properly uncompress multiple files now.
 -Erik
This commit is contained in:
Eric Andersen 2002-04-13 09:10:34 +00:00
parent 114de55668
commit d75ac02a4f

View File

@ -68,27 +68,22 @@ static char *license_msg[] = {
extern int gunzip_main(int argc, char **argv) extern int gunzip_main(int argc, char **argv)
{ {
FILE *in_file = stdin; int flags = 0;
FILE *out_file = NULL; int opt = 0;
int delete_old_file, file_count;
struct stat stat_buf; struct stat stat_buf;
FILE *in_file, *out_file;
char *if_name = NULL; char *if_name, *of_name, *delete_file_name;
char *of_name = NULL;
char *delete_file_name = NULL;
const int gunzip_to_stdout = 1; const int gunzip_to_stdout = 1;
const int gunzip_force = 2; const int gunzip_force = 2;
const int gunzip_test = 4; const int gunzip_test = 4;
const int gunzip_verbose = 8;
int flags = 0;
int opt = 0;
int delete_old_file = FALSE;
/* if called as zcat */ /* if called as zcat */
if (strcmp(applet_name, "zcat") == 0) if (strcmp(applet_name, "zcat") == 0)
flags |= gunzip_to_stdout; flags |= gunzip_to_stdout;
while ((opt = getopt(argc, argv, "ctfhdq")) != -1) { while ((opt = getopt(argc, argv, "ctfhdqv")) != -1) {
switch (opt) { switch (opt) {
case 'c': case 'c':
flags |= gunzip_to_stdout; flags |= gunzip_to_stdout;
@ -99,6 +94,9 @@ extern int gunzip_main(int argc, char **argv)
case 't': case 't':
flags |= gunzip_test; flags |= gunzip_test;
break; break;
case 'v':
flags |= gunzip_verbose;
break;
case 'd': /* Used to convert gzip to gunzip. */ case 'd': /* Used to convert gzip to gunzip. */
break; break;
case 'q': case 'q':
@ -110,74 +108,95 @@ extern int gunzip_main(int argc, char **argv)
} }
} }
/* Set input filename and number */ file_count = argc - optind;
if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { while (file_count==0 || optind < argc) {
flags |= gunzip_to_stdout; in_file = stdin;
} else { out_file = NULL;
if_name = xstrdup(argv[optind]); if_name = NULL;
/* Open input file */ of_name = NULL;
in_file = xfopen(if_name, "r"); delete_file_name = NULL;
delete_old_file = FALSE;
/* set the buffer size */ /* Set input filename and number */
setvbuf(in_file, NULL, _IOFBF, 0x8000); if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
flags |= gunzip_to_stdout;
/* Get the time stamp on the input file. */ /* Skip to the end */
if (stat(if_name, &stat_buf) < 0) { optind = argc;
error_msg_and_die("Couldn't stat file %s", if_name);
}
}
/* Check that the input is sane. */
if (isatty(fileno(in_file)) && (flags & gunzip_force) == 0)
error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
/* Set output filename and number */
if (flags & gunzip_test) {
out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
} else if (flags & gunzip_to_stdout) {
out_file = stdout;
} else {
char *extension;
int length = strlen(if_name);
delete_old_file = TRUE;
extension = strrchr(if_name, '.');
if (extension && strcmp(extension, ".gz") == 0) {
length -= 3;
} else if (extension && strcmp(extension, ".tgz") == 0) {
length -= 4;
} else { } else {
error_msg_and_die("Invalid extension"); if_name = strdup(argv[optind]);
/* Open input file */
in_file = xfopen(if_name, "r");
if (flags & gunzip_verbose) {
fprintf(stderr, "%s:\t", if_name);
}
/* set the buffer size */
setvbuf(in_file, NULL, _IOFBF, 0x8000);
/* Get the time stamp on the input file. */
if (stat(if_name, &stat_buf) < 0) {
error_msg_and_die("Couldn't stat file %s", if_name);
}
} }
of_name = (char *) xcalloc(sizeof(char), length + 1);
strncpy(of_name, if_name, length);
/* Open output file */ /* Check that the input is sane. */
out_file = xfopen(of_name, "w"); if (isatty(fileno(in_file)) && (flags & gunzip_force) == 0)
error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
/* Set permissions on the file */ /* Set output filename and number */
chmod(of_name, stat_buf.st_mode); if (flags & gunzip_test) {
} out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
} else if (flags & gunzip_to_stdout) {
out_file = stdout;
} else {
char *extension;
int length = strlen(if_name);
/* do the decompression, and cleanup */ delete_old_file = TRUE;
if (unzip(in_file, out_file) == 0) { extension = strrchr(if_name, '.');
/* Success, remove .gz file */ if (extension && strcmp(extension, ".gz") == 0) {
delete_file_name = if_name; length -= 3;
} else { } else if (extension && strcmp(extension, ".tgz") == 0) {
/* remove failed attempt */ length -= 4;
delete_file_name = of_name; } else {
} error_msg_and_die("Invalid extension");
}
of_name = (char *) xcalloc(sizeof(char), length + 1);
strncpy(of_name, if_name, length);
fclose(out_file); /* Open output file */
fclose(in_file); out_file = xfopen(of_name, "w");
if (delete_old_file) { /* Set permissions on the file */
if (unlink(delete_file_name) < 0) { chmod(of_name, stat_buf.st_mode);
error_msg_and_die("Couldnt remove %s", delete_file_name);
} }
}
free(of_name); /* do the decompression, and cleanup */
if (unzip(in_file, out_file) == 0) {
/* Success, remove .gz file */
delete_file_name = if_name;
if (flags & gunzip_verbose) {
fprintf(stderr, "OK\n");
}
} else {
/* remove failed attempt */
delete_file_name = of_name;
}
fclose(out_file);
fclose(in_file);
if (delete_old_file == TRUE && !(flags & gunzip_test)) {
if (unlink(delete_file_name) < 0) {
error_msg_and_die("Couldnt remove %s", delete_file_name);
}
}
free(of_name);
optind++;
} /* while () */
return(EXIT_SUCCESS); return(EXIT_SUCCESS);
} }