Fix a number of problems with argument handling.
This commit is contained in:
parent
54652230d4
commit
96dcd19b8a
@ -80,29 +80,17 @@ extern int gunzip_main(int argc, char **argv)
|
|||||||
char *delete_file_name = NULL;
|
char *delete_file_name = NULL;
|
||||||
|
|
||||||
const int gunzip_to_stdout = 1;
|
const int gunzip_to_stdout = 1;
|
||||||
const int gunzip_from_stdin = 2;
|
const int gunzip_force = 2;
|
||||||
const int gunzip_force = 4;
|
const int gunzip_test = 4;
|
||||||
const int gunzip_test = 8;
|
|
||||||
|
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
int opt = 0;
|
int opt = 0;
|
||||||
int delete_old_file = FALSE;
|
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)
|
||||||
if (argc > 2) {
|
flags |= gunzip_to_stdout;
|
||||||
show_usage();
|
|
||||||
}
|
|
||||||
else if (argc == 2) {
|
|
||||||
/* a filename was specified */
|
|
||||||
flags |= (gunzip_to_stdout | gunzip_force);
|
|
||||||
optind = 1;
|
|
||||||
} else {
|
|
||||||
/* read from stdin, this gets caught below as argv[optind] will be NULL */
|
|
||||||
optind = argc;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* workout flags as regular gunzip */
|
|
||||||
while ((opt = getopt(argc, argv, "ctfh")) != -1) {
|
while ((opt = getopt(argc, argv, "ctfh")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'c':
|
case 'c':
|
||||||
@ -119,19 +107,11 @@ extern int gunzip_main(int argc, char **argv)
|
|||||||
show_usage(); /* exit's inside usage */
|
show_usage(); /* exit's inside usage */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* no filename specified so it must be reading from stdin to stdout */
|
|
||||||
if (argv[optind] == NULL) {
|
|
||||||
flags |= (gunzip_from_stdin |gunzip_to_stdout |gunzip_force);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set input filename and number */
|
/* Set input filename and number */
|
||||||
if (flags & gunzip_from_stdin) {
|
if (argv[optind] == NULL) {
|
||||||
|
flags |= gunzip_to_stdout;
|
||||||
in_file = stdin;
|
in_file = stdin;
|
||||||
if ((flags & gunzip_force) == 0) {
|
|
||||||
error_msg_and_die("data not written to terminal. Use -f to force it.");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if_name = strdup(argv[optind]);
|
if_name = strdup(argv[optind]);
|
||||||
/* Open input file */
|
/* Open input file */
|
||||||
@ -139,19 +119,19 @@ extern int gunzip_main(int argc, char **argv)
|
|||||||
|
|
||||||
/* Get the time stamp on the input file. */
|
/* Get the time stamp on the input file. */
|
||||||
if (stat(if_name, &stat_buf) < 0) {
|
if (stat(if_name, &stat_buf) < 0) {
|
||||||
error_msg_and_die("Couldnt stat file %s",if_name);
|
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 */
|
/* Set output filename and number */
|
||||||
if (flags & gunzip_to_stdout) {
|
if (flags & gunzip_test) {
|
||||||
out_file = stdout;
|
|
||||||
/* whats the best way to do this with streams ? */
|
|
||||||
if (isatty(fileno(out_file)) && ((flags & gunzip_force) == 0)) {
|
|
||||||
error_msg_and_die("data not written to terminal. Use -f to force it.");
|
|
||||||
}
|
|
||||||
} else if (flags & gunzip_test) {
|
|
||||||
out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
|
out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
|
||||||
|
} else if (flags & gunzip_to_stdout) {
|
||||||
|
out_file = stdout;
|
||||||
} else {
|
} else {
|
||||||
char *extension;
|
char *extension;
|
||||||
int length = strlen(if_name);
|
int length = strlen(if_name);
|
||||||
|
50
gunzip.c
50
gunzip.c
@ -80,29 +80,17 @@ extern int gunzip_main(int argc, char **argv)
|
|||||||
char *delete_file_name = NULL;
|
char *delete_file_name = NULL;
|
||||||
|
|
||||||
const int gunzip_to_stdout = 1;
|
const int gunzip_to_stdout = 1;
|
||||||
const int gunzip_from_stdin = 2;
|
const int gunzip_force = 2;
|
||||||
const int gunzip_force = 4;
|
const int gunzip_test = 4;
|
||||||
const int gunzip_test = 8;
|
|
||||||
|
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
int opt = 0;
|
int opt = 0;
|
||||||
int delete_old_file = FALSE;
|
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)
|
||||||
if (argc > 2) {
|
flags |= gunzip_to_stdout;
|
||||||
show_usage();
|
|
||||||
}
|
|
||||||
else if (argc == 2) {
|
|
||||||
/* a filename was specified */
|
|
||||||
flags |= (gunzip_to_stdout | gunzip_force);
|
|
||||||
optind = 1;
|
|
||||||
} else {
|
|
||||||
/* read from stdin, this gets caught below as argv[optind] will be NULL */
|
|
||||||
optind = argc;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* workout flags as regular gunzip */
|
|
||||||
while ((opt = getopt(argc, argv, "ctfh")) != -1) {
|
while ((opt = getopt(argc, argv, "ctfh")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'c':
|
case 'c':
|
||||||
@ -119,19 +107,11 @@ extern int gunzip_main(int argc, char **argv)
|
|||||||
show_usage(); /* exit's inside usage */
|
show_usage(); /* exit's inside usage */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* no filename specified so it must be reading from stdin to stdout */
|
|
||||||
if (argv[optind] == NULL) {
|
|
||||||
flags |= (gunzip_from_stdin |gunzip_to_stdout |gunzip_force);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set input filename and number */
|
/* Set input filename and number */
|
||||||
if (flags & gunzip_from_stdin) {
|
if (argv[optind] == NULL) {
|
||||||
|
flags |= gunzip_to_stdout;
|
||||||
in_file = stdin;
|
in_file = stdin;
|
||||||
if ((flags & gunzip_force) == 0) {
|
|
||||||
error_msg_and_die("data not written to terminal. Use -f to force it.");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if_name = strdup(argv[optind]);
|
if_name = strdup(argv[optind]);
|
||||||
/* Open input file */
|
/* Open input file */
|
||||||
@ -139,19 +119,19 @@ extern int gunzip_main(int argc, char **argv)
|
|||||||
|
|
||||||
/* Get the time stamp on the input file. */
|
/* Get the time stamp on the input file. */
|
||||||
if (stat(if_name, &stat_buf) < 0) {
|
if (stat(if_name, &stat_buf) < 0) {
|
||||||
error_msg_and_die("Couldnt stat file %s",if_name);
|
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 */
|
/* Set output filename and number */
|
||||||
if (flags & gunzip_to_stdout) {
|
if (flags & gunzip_test) {
|
||||||
out_file = stdout;
|
|
||||||
/* whats the best way to do this with streams ? */
|
|
||||||
if (isatty(fileno(out_file)) && ((flags & gunzip_force) == 0)) {
|
|
||||||
error_msg_and_die("data not written to terminal. Use -f to force it.");
|
|
||||||
}
|
|
||||||
} else if (flags & gunzip_test) {
|
|
||||||
out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
|
out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
|
||||||
|
} else if (flags & gunzip_to_stdout) {
|
||||||
|
out_file = stdout;
|
||||||
} else {
|
} else {
|
||||||
char *extension;
|
char *extension;
|
||||||
int length = strlen(if_name);
|
int length = strlen(if_name);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user