aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJia Tan <jiat0218@gmail.com>2023-11-15 23:40:13 +0800
committerJia Tan <jiat0218@gmail.com>2023-11-15 23:40:13 +0800
commitf481523baac946fa3bc13d79186ffaf0c0b818a7 (patch)
tree4daff5030d1425cc3e85b5c89fa01e6a3b7dd894
parentxz: Move suffix check after stdout mode is detected. (diff)
downloadxz-f481523baac946fa3bc13d79186ffaf0c0b818a7.tar.xz
xz: Refactor suffix test with raw format.
The previous version set opt_stdout, but this caused an issue with copying an input file to standard out when decompressing an unknown file type. The following needs to result in an error: echo foo | xz -df since -c, --stdout is not used. This fixes the previous error by not setting opt_stdout.
-rw-r--r--src/xz/args.c38
1 files changed, 13 insertions, 25 deletions
diff --git a/src/xz/args.c b/src/xz/args.c
index bdeaed67..cee5f202 100644
--- a/src/xz/args.c
+++ b/src/xz/args.c
@@ -834,34 +834,22 @@ args_parse(args_info *args, int argc, char **argv)
args->arg_count = (unsigned int)(argc - optind);
}
- // If all of the filenames provided are "-" (more than one "-"
- // could be specified) or no filenames are provided, then we are
- // only going to be writing to standard out. However if --files or
- // --files0 is used, then we will not be writing to standard out.
- if (!opt_stdout && args->files_name == NULL) {
- uint32_t i;
-
- for (i = 0; i < args->arg_count; i++) {
- const char *name = args->arg_names[i];
-
- // getopt_long() will not give us an empty string
- // as an argument name here so we don't need to
- // check if name[0] is a NULL terminator.
- if (name[0] != '-' && name[1] != '\0')
- break;
- }
-
- // Set opt_stdout if the loop did not exit early.
- opt_stdout = i == args->arg_count;
- }
-
// If raw format is used and a custom suffix is not provided,
- // then only stdout mode can be used when compressing or decompressing.
+ // then only stdout mode can be used when compressing or
+ // decompressing.
if (opt_format == FORMAT_RAW && !suffix_is_set() && !opt_stdout
&& (opt_mode == MODE_COMPRESS
- || opt_mode == MODE_DECOMPRESS))
- message_fatal(_("With --format=raw, --suffix=.SUF is "
- "required unless writing to stdout"));
+ || opt_mode == MODE_DECOMPRESS)) {
+ // If all of the filenames provided are "-" (more than one
+ // "-" could be specified) or no filenames are provided,
+ // then we are only going to be writing to standard out.
+ for (unsigned int i = 0; i < args->arg_count; i++) {
+ if (strcmp(args->arg_names[i], "-") != 0)
+ message_fatal(_("With --format=raw, "
+ "--suffix=.SUF is required "
+ "unless writing to stdout"));
+ }
+ }
return;
}