OSDN Git Service

Improved error handling on Win32.
authorLoRd_MuldeR <mulder2@gmx.de>
Fri, 26 Jan 2018 19:40:49 +0000 (20:40 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Fri, 26 Jan 2018 19:40:49 +0000 (20:40 +0100)
src/main++.cpp
src/main.c
src/utilities.h

index a7cb2b4..d218801 100644 (file)
 /*Constants*/
 #define BUFF_SIZE 4096
 
-/*Error handler*/
-#define PRINT_ERROR(X) do \
-{ \
-               if (param->ignore_errors && multi_file) \
-               { \
-                       FPRINTF(stderr, T("Skipped file: %s\n"), file_name ? file_name : T("<STDIN>")); \
-               } \
-               else \
-               { \
-                       print_logo();  \
-                       FPRINTF(stderr, T("%s:\n%s\n\n>>> %s <<<\n\n"), (X),file_name ? file_name : T("<STDIN>"), STRERROR(errno));  \
-               } \
-} \
-while(0)
-
+/*process a single file*/
 static int process_file(const int multi_file, const param_t *const param, uint64_t *const bytes_total, CHAR *const file_name)
 {
        FILE *source;
@@ -59,6 +45,9 @@ static int process_file(const int multi_file, const param_t *const param, uint64
        uint_fast32_t count;
        uint_fast16_t update_iter;
 
+       /*clear error indicators first*/
+       CLEAR_ERRORS();
+
        /*check if file is accessible*/
        if (file_name && ACCESS(file_name, R_OK))
        {
@@ -139,15 +128,7 @@ static int process_file(const int multi_file, const param_t *const param, uint64
        /*output result as Hex string*/
        if (param->raw_output)
        {
-               if (fwrite(result.data(), sizeof(uint8_t), result.size(), stdout) != result.size())
-               {
-                       FPUTS(T("Failed to write digest to standard output!\n"), stderr);
-                       if (source != stdin)
-                       {
-                               fclose(source);
-                       }
-                       return 0;
-               }
+               fwrite(result, sizeof(uint8_t), MY_HASH_LENGTH, stdout);
        }
        else
        {
@@ -159,6 +140,9 @@ static int process_file(const int multi_file, const param_t *const param, uint64
                FPUTC(T('\n'), stdout);
        }
 
+       /*flush*/
+       fflush(stdout);
+
        /*check for interruption*/
        if (g_interrupted)
        {
@@ -167,9 +151,6 @@ static int process_file(const int multi_file, const param_t *const param, uint64
                FORCE_EXIT(SIGINT);
        }
 
-       /*flush*/
-       fflush(stdout);
-
        /*clean up*/
        if (source != stdin)
        {
index d6b6ea8..ddfc87d 100644 (file)
 /*Constants*/
 #define BUFF_SIZE 4096
 
-/*Error handler*/
-#define PRINT_ERROR(X) do \
-{ \
-               if (param->ignore_errors && multi_file) \
-               { \
-                       FPRINTF(stderr, T("Skipped file: %s\n"), file_name ? file_name : T("<STDIN>")); \
-               } \
-               else \
-               { \
-                       print_logo();  \
-                       FPRINTF(stderr, T("%s:\n%s\n\n>>> %s <<<\n\n"), (X),file_name ? file_name : T("<STDIN>"), STRERROR(errno));  \
-               } \
-} \
-while(0)
-
+/*process a single file*/
 static int process_file(const int multi_file, const param_t *const param, uint64_t *const bytes_total, CHAR *const file_name)
 {
        FILE *source;
@@ -60,6 +46,9 @@ static int process_file(const int multi_file, const param_t *const param, uint64
        uint_fast32_t count;
        uint_fast16_t update_iter;
 
+       /*clear error indicators first*/
+       CLEAR_ERRORS();
+
        /*check if file is accessible*/
        if (file_name && ACCESS(file_name, R_OK))
        {
@@ -140,15 +129,7 @@ static int process_file(const int multi_file, const param_t *const param, uint64
        /*output result as Hex string*/
        if (param->raw_output)
        {
-               if (fwrite(result, sizeof(uint8_t), MY_HASH_LENGTH, stdout) != MY_HASH_LENGTH)
-               {
-                       FPUTS(T("Failed to write digest to standard output!\n"), stderr);
-                       if (source != stdin)
-                       {
-                               fclose(source);
-                       }
-                       return 0;
-               }
+               fwrite(result, sizeof(uint8_t), MY_HASH_LENGTH, stdout);
        }
        else
        {
@@ -160,6 +141,9 @@ static int process_file(const int multi_file, const param_t *const param, uint64
                FPUTC(T('\n'), stdout);
        }
 
+       /*flush*/
+       fflush(stdout);
+
        /*check for interruption*/
        if (g_interrupted)
        {
@@ -168,9 +152,6 @@ static int process_file(const int multi_file, const param_t *const param, uint64
                FORCE_EXIT(SIGINT);
        }
 
-       /*flush*/
-       fflush(stdout);
-
        /*clean up*/
        if (source != stdin)
        {
index 662871a..b4bfa8b 100644 (file)
@@ -308,4 +308,33 @@ static void sigint_handler(int sig_no)
        fclose(stdin);
 }
 
+/*clear error indicators*/
+#if defined(_WIN32) || defined(_WIN64)
+#define CLEAR_ERRORS() do { errno = _doserrno = 0; } while(0)
+#else
+#define CLEAR_ERRORS() do { errno = 0; } while(0)
+#endif
+
+/*write error message*/
+#if defined(_WIN32) || defined(_WIN64)
+#define __PRINT_ERROR(X,Y) FPRINTF(stderr,_doserrno ? T("%s:\n%s\n\n>>> %s [Code: 0x%X] <<<\n\n") : T("%s:\n%s\n\n>>> %s <<<\n\n"), (X), (Y), STRERROR(errno), _doserrno)
+#else
+#define __PRINT_ERROR(X,Y) FPRINTF(stderr, T("%s:\n%s\n\n>>> %s <<<\n\n"), (X), (Y), STRERROR(errno))
+#endif
+
+/*error handler*/
+#define PRINT_ERROR(X) do \
+{ \
+               if (param->ignore_errors && multi_file) \
+               { \
+                       FPRINTF(stderr, T("Skipped file: %s\n"), file_name ? file_name : T("<STDIN>")); \
+               } \
+               else \
+               { \
+                       print_logo(); \
+                       __PRINT_ERROR(X, file_name ? file_name : T("<STDIN>")); \
+               } \
+} \
+while(0)
+
 #endif /*MHASH_CLI_UTILS_INCLUDED*/