summaryrefslogtreecommitdiffstats
path: root/programs/lz4cli.c
diff options
context:
space:
mode:
authorYann Collet <cyan@fb.com>2019-04-20 00:08:40 (GMT)
committerYann Collet <cyan@fb.com>2019-04-20 00:08:40 (GMT)
commit5f8ac02b7707c03f93ff10ac1362d2c618bd3c42 (patch)
tree2b007309534f4a65111bd12711ae29e99b584832 /programs/lz4cli.c
parent7a4e3b1fac5cd9d4ec7c8d0091329ba107ec2131 (diff)
downloadlz4-5f8ac02b7707c03f93ff10ac1362d2c618bd3c42.zip
lz4-5f8ac02b7707c03f93ff10ac1362d2c618bd3c42.tar.gz
lz4-5f8ac02b7707c03f93ff10ac1362d2c618bd3c42.tar.bz2
cli: display a warning whenever default output is stdout while input != stdin
This behavior has been preserved for compatibility with existing ecosystem. But it's problematic, as some environment start `lz4` without identifying stdout as console by default, leading to a change of behavior for a same line of script. A more sensible policy would be to default to stdout only when input is stdin. Soft change for the time being : keep the behavior, just print a warning message. User should prefer `-c` to explicitly select `stdout`. Also : updated tests in Makefile to explicitly select `stdout` with `-c`.
Diffstat (limited to 'programs/lz4cli.c')
-rw-r--r--programs/lz4cli.c36
1 files changed, 25 insertions, 11 deletions
diff --git a/programs/lz4cli.c b/programs/lz4cli.c
index 8bd7042..279aaf1 100644
--- a/programs/lz4cli.c
+++ b/programs/lz4cli.c
@@ -652,7 +652,15 @@ int main(int argc, const char** argv)
/* No output filename ==> try to select one automatically (when possible) */
while ((!output_filename) && (multiple_inputs==0)) {
- if (!IS_CONSOLE(stdout)) { output_filename=stdoutmark; break; } /* Default to stdout whenever possible (i.e. not a console) */
+
+ if (!IS_CONSOLE(stdout)) {
+ /* Default to stdout whenever stdout is not the console.
+ * Note : this policy is likely to change in the future, don't rely on it !
+ * prefer using an explicit `-c` flag */
+ DISPLAYLEVEL(1, "Warning : using stdout as default output. Do not rely on this behavior: use explicit `-c` instead ! \n");
+ output_filename=stdoutmark;
+ break;
+ }
if (mode == om_auto) { /* auto-determine compression or decompression, based on file extension */
mode = determineOpMode(input_filename);
}
@@ -682,10 +690,14 @@ int main(int argc, const char** argv)
break;
}
- /* Check if output is defined as console; trigger an error in this case */
+ if (multiple_inputs==0) assert(output_filename);
+ /* when multiple_inputs==1, output_filename may simply be useless,
+ * however, output_filename must be !NULL for next strcmp() tests */
if (!output_filename) output_filename = "*\\dummy^!//";
+
+ /* Check if output is defined as console; trigger an error in this case */
if (!strcmp(output_filename,stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) {
- DISPLAYLEVEL(1, "refusing to write to console without -c\n");
+ DISPLAYLEVEL(1, "refusing to write to console without -c \n");
exit(1);
}
/* Downgrade notification level in stdout and multiple file mode */
@@ -701,21 +713,23 @@ int main(int argc, const char** argv)
LZ4IO_setNotificationLevel((int)displayLevel);
if (ifnIdx == 0) multiple_inputs = 0;
if (mode == om_decompress) {
- if (multiple_inputs)
- operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION);
- else
+ if (multiple_inputs) {
+ assert(ifnIdx <= INT_MAX);
+ operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION);
+ } else {
operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename);
+ }
} else { /* compression is default action */
if (legacy_format) {
DISPLAYLEVEL(3, "! Generating LZ4 Legacy format (deprecated) ! \n");
LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel);
} else {
- if (multiple_inputs)
- operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION, cLevel);
- else
+ if (multiple_inputs) {
+ assert(ifnIdx <= INT_MAX);
+ operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, (int)ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION, cLevel);
+ } else {
operationResult = DEFAULT_COMPRESSOR(prefs, input_filename, output_filename, cLevel);
- }
- }
+ } } }
_cleanup:
if (main_pause) waitEnter();