]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Unified warning prompts into new function UTIL_requireUserConfirmationToProceed()
authorsenhuang42 <senhuang96@fb.com>
Tue, 25 Aug 2020 15:25:49 +0000 (11:25 -0400)
committersenhuang42 <senhuang96@fb.com>
Tue, 25 Aug 2020 15:25:49 +0000 (11:25 -0400)
programs/fileio.c
programs/util.c
programs/util.h

index c4737372a75dc3f2d960f2956d53ef1f7dee4522..9eb35fa8f228211ac5699c27a00d624a06a29346 100644 (file)
@@ -605,16 +605,10 @@ FIO_openDstFile(FIO_prefs_t* const prefs,
                             dstFileName);
                     return NULL;
                 }
-                DISPLAY("zstd: %s already exists; overwrite (y/N) ? ",
-                        dstFileName);
-                {   int ch = getchar();
-                    if ((ch!='Y') && (ch!='y')) {
-                        DISPLAY("    not overwritten  \n");
-                        return NULL;
-                    }
-                    /* flush rest of input line */
-                    while ((ch!=EOF) && (ch!='\n')) ch = getchar();
-            }   }
+                DISPLAY("zstd: %s already exists; ", dstFileName);
+                if (UTIL_requireUserConfirmationToProceed("overwrite (y/n) ? ", "Not overwritten  \n", "yY"))
+                    return NULL;
+            }
             /* need to unlink */
             FIO_removeFile(dstFileName);
     }   }
@@ -1683,19 +1677,8 @@ int FIO_compressMultipleFilenames(FIO_prefs_t* const prefs,
             } else {
                 DISPLAY("zstd: WARNING: all input files will be processed and concatenated into a single output file: %s ", outFileName);
             }
-            if (prefs->removeSrcFile) {
-                DISPLAY("Proceed? (y/n): ");
-                {
-                    int ch = getchar();
-                    if ((ch != 'y') && (ch != 'Y')) {
-                        DISPLAY("zstd: aborting...\n");
-                        return 1;
-                    }
-                    /* flush the rest */
-                    while ((ch!=EOF) && (ch!='\n'))
-                        ch = getchar();
-                }
-            }
+            if (prefs->removeSrcFile)
+                error = UTIL_requireUserConfirmationToProceed("Proceed? (y/n): ", "Aborting...", "yY");
             DISPLAY("\n");
         }
         ress.dstFile = FIO_openDstFile(prefs, NULL, outFileName);
index fc88ab35cc8e0bcf6c3f414f44b46fd5e05b73b3..6b220f39ae8c7e9374768c0ec7d95017663917c1 100644 (file)
@@ -87,6 +87,22 @@ UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
 ******************************************/
 int g_utilDisplayLevel;
 
+int UTIL_requireUserConfirmationToProceed(const char* prompt, const char* abortMsg,
+                                          const char* acceptableLetters) {
+    int ch;
+    UTIL_DISPLAY("%s", prompt);
+    ch = getchar();
+    if (strchr(acceptableLetters, ch) == NULL) {
+        UTIL_DISPLAY("%s", abortMsg);
+        return 1;
+    }
+    /* flush the rest */
+    while ((ch!=EOF) && (ch!='\n'))
+        ch = getchar();
+    
+    return 0;
+}
+
 
 /*-*************************************
 *  Constants
index f8eee86ad005c2e139ef567937c78cbe281b48a1..aa48fd47ae362e093174b9a987d854964e01bdc6 100644 (file)
@@ -93,6 +93,12 @@ extern "C" {
 ******************************************/
 extern int g_utilDisplayLevel;
 
+/**
+ * Displays a message prompt and returns success (0) if first character from stdin
+ * matches any from acceptableLetters. Otherwise, returns failure (1) and displays abortMsg.
+ */
+int UTIL_requireUserConfirmationToProceed(const char* const prompt, const char* const abortMsg, const char* const acceptableLetters);
+
 
 /*-****************************************
 *  File functions