]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Multi-OS support for --list detecting stream input
authorTopher Lubaway <asimplechris@gmail.com>
Tue, 12 Jun 2018 14:59:17 +0000 (07:59 -0700)
committerTopher Lubaway <asimplechris@gmail.com>
Tue, 12 Jun 2018 14:59:17 +0000 (07:59 -0700)
IS_CONSOLE stolen wholesale from Options.cpp
not sure if i should have extracted that code for DRY-ness
tested in OSX and functionality seems appropriate
unstested in a windows environment

programs/fileio.c

index df68b235b87eadeae93451723baf8f7c8103e7d8..d33c7f423cbc665210cf429e93aeb7f08bf70b64 100644 (file)
 #include <string.h>     /* strcmp, strlen */
 #include <errno.h>      /* errno */
 
-#if defined (_MSC_VER)
-#  include <sys/stat.h>
-#  include <io.h>
-#else
-#  include <unistd.h>     /* isatty */
-#endif
-
 #include "mem.h"
 #include "fileio.h"
 #include "util.h"
 #  include <lz4.h>
 #endif
 
+// Multi-OS support for determining if terminal is tty
+#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) ||     \
+    defined(__CYGWIN__)
+#include <io.h> /* _isatty */
+#define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
+#elif defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE) || (defined(__APPLE__) && defined(__MACH__)) || \
+      defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)  /* https://sourceforge.net/p/predef/wiki/OperatingSystems/ */
+#include <unistd.h> /* isatty */
+#define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
+#else
+#define IS_CONSOLE(stdStream) 0
+#endif
+
 
 /*-*************************************
 *  Constants
@@ -2032,14 +2038,11 @@ static int FIO_listFile(fileInfo_t* total, const char* inFileName, int displayLe
 }
 
 int FIO_listMultipleFiles(unsigned numFiles, const char** filenameTable, int displayLevel){
-    // isatty comes from the header unistd.h
-    // windows has no equilivant
-    #if !defined(_MSC_VER)
-    if (!isatty(0)) {
+
+    if (!IS_CONSOLE(stdin)) {
         DISPLAYOUT("zstd: --list does not support reading from standard input\n");
         return 1;
     }
-    #endif
 
     if (numFiles == 0) {
         DISPLAYOUT("No files given\n");