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);
} }
} 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);
******************************************/
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
******************************************/
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