/*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;
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))
{
/*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
{
FPUTC(T('\n'), stdout);
}
+ /*flush*/
+ fflush(stdout);
+
/*check for interruption*/
if (g_interrupted)
{
FORCE_EXIT(SIGINT);
}
- /*flush*/
- fflush(stdout);
-
/*clean up*/
if (source != stdin)
{
/*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;
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))
{
/*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
{
FPUTC(T('\n'), stdout);
}
+ /*flush*/
+ fflush(stdout);
+
/*check for interruption*/
if (g_interrupted)
{
FORCE_EXIT(SIGINT);
}
- /*flush*/
- fflush(stdout);
-
/*clean up*/
if (source != stdin)
{
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*/