From 540b52ea7b8819535ec5f456cf7c6b4792ff1675 Mon Sep 17 00:00:00 2001 From: WHR Date: Tue, 14 Dec 2021 14:06:52 +0800 Subject: Allow '--list' with stdin if it is a regular file --- programs/lz4cli.c | 7 +------ programs/lz4io.c | 2 +- programs/util.h | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 57a6ab9..49fa492 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -712,11 +712,6 @@ int main(int argc, const char** argv) } if (mode == om_list){ - /* Exit if trying to read from stdin as this isn't supported in this mode */ - if(!strcmp(input_filename, stdinmark)){ - DISPLAYLEVEL(1, "refusing to read from standard input in --list mode\n"); - exit(1); - } if(!multiple_inputs){ inFileNames[ifnIdx++] = input_filename; } @@ -729,7 +724,7 @@ int main(int argc, const char** argv) 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) { + if (!strcmp(output_filename,stdoutmark) && mode != om_list && IS_CONSOLE(stdout) && !forceStdout) { DISPLAYLEVEL(1, "refusing to write to console without -c \n"); exit(1); } diff --git a/programs/lz4io.c b/programs/lz4io.c index c01374b..4cdd3cc 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1639,7 +1639,7 @@ int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, size_t ifnIdx) /* Get file info */ LZ4IO_cFileInfo_t cfinfo = LZ4IO_INIT_CFILEINFO; cfinfo.fileName = LZ4IO_baseName(inFileNames[idx]); - if (!UTIL_isRegFile(inFileNames[idx])) { + if (strcmp(inFileNames[idx], stdinmark) == 0 ? !UTIL_isRegFD(0) : !UTIL_isRegFile(inFileNames[idx])) { DISPLAYLEVEL(1, "lz4: %s is not a regular file \n", inFileNames[idx]); return 0; } diff --git a/programs/util.h b/programs/util.h index 394837e..2840179 100644 --- a/programs/util.h +++ b/programs/util.h @@ -317,6 +317,7 @@ UTIL_STATIC void UTIL_waitForNextTick(void) UTIL_STATIC int UTIL_isRegFile(const char* infilename); +UTIL_STATIC int UTIL_isRegFD(int fd); UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf) @@ -352,6 +353,19 @@ UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf) } +UTIL_STATIC int UTIL_getFDStat(int fd, stat_t *statbuf) +{ + int r; +#if defined(_MSC_VER) + r = _fstat64(fd, statbuf); + if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */ +#else + r = fstat(fd, statbuf); + if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */ +#endif + return 1; +} + UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf) { int r; @@ -366,6 +380,17 @@ UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf) } +UTIL_STATIC int UTIL_isRegFD(int fd) +{ + stat_t statbuf; +#ifdef _WIN32 + /* Windows runtime library always open file descriptors 0, 1 and 2 in text mode, therefore we can't use them for binary I/O */ + if(fd < 3) return 0; +#endif + return UTIL_getFDStat(fd, &statbuf); /* Only need to know whether it is a regular file */ +} + + UTIL_STATIC int UTIL_isRegFile(const char* infilename) { stat_t statbuf; -- cgit v0.12