#define STAT64_T struct _stat64
#define MODE_T unsigned short
#define FSTAT64(X,Y) _fstat64((X),(Y))
+#define SYSERRNO _doserrno
+#define SET_SYSERRNO(X) _set_doserrno((X))
#define _T(X) L##X
#define T(X) _T(X)
#define R_OK 4
#define STAT64_T struct stat64
#define MODE_T mode_t
#define FSTAT64(X,Y) fstat64((X),(Y))
+#define SYSERRNO (0L)
+#define SET_SYSERRNO(X) ((void)0)
#define T(X) X
#endif
/* ---------------------------------------------------------------------------------------------- */
-/* MHash-384 - Example application (plain C) */
+/* MHash-384 - Example application (C++) */
/* Copyright(c) 2016-2018 LoRd_MuldeR <mulder2@gmx.de> */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */
uint_fast16_t update_iter;
/*clear error indicators first*/
- CLEAR_ERRORS();
+ clear_errors();
/*check if file is accessible*/
if (file_name && ACCESS(file_name, R_OK))
{
- PRINT_ERROR(T("Specified input file is inaccessible"));
+ print_error(T("Specified input file is inaccessible"), file_name, param, multi_file);
return 0;
}
/*open source file*/
if (!(source = file_name ? FOPEN(file_name, T("rb")) : stdin))
{
- PRINT_ERROR(T("Failed to open specified input file"));
+ print_error(T("Failed to open specified input file"), file_name, param, multi_file);
return 0;
}
/*determine file properties*/
if(!get_file_info(source, &file_size, &file_type))
{
- PRINT_ERROR(T("Failed to determine file properties"));
+ print_error(T("Failed to determine file properties"), file_name, param, multi_file);
return 0;
}
/*is a directory?*/
if(file_type == S_IFDIR)
{
- errno = EISDIR;
- PRINT_ERROR(T("Unsupported file type encountered"));
+ errno = EISDIR; /*set errno!*/
+ print_error(T("Unsupported input type encountered"), file_name, param, multi_file);
return 0;
}
/*process file contents*/
while (!(ferror(source) || feof(source)))
{
+ clear_errors();
count = (uint_fast32_t)fread(buffer, sizeof(uint8_t), BUFF_SIZE, source);
if (count > 0)
{
/*check file error status*/
if ((!g_interrupted) && ferror(source))
{
- errno = EIO; /*fread() doesn't set errno!*/
- PRINT_ERROR(T("Error encountered while reading from file"));
+ errno = EIO; /*set errno!*/
+ print_error(T("Error encountered while reading from file"), file_name, param, multi_file);
fclose(source);
return 0;
}
uint_fast16_t update_iter;
/*clear error indicators first*/
- CLEAR_ERRORS();
+ clear_errors();
/*check if file is accessible*/
if (file_name && ACCESS(file_name, R_OK))
{
- PRINT_ERROR(T("Specified input file is inaccessible"));
+ print_error(T("Specified input file is inaccessible"), file_name, param, multi_file);
return 0;
}
/*open source file*/
if (!(source = file_name ? FOPEN(file_name, T("rb")) : stdin))
{
- PRINT_ERROR(T("Failed to open specified input file"));
+ print_error(T("Failed to open specified input file"), file_name, param, multi_file);
return 0;
}
/*determine file properties*/
if(!get_file_info(source, &file_size, &file_type))
{
- PRINT_ERROR(T("Failed to determine file properties"));
+ print_error(T("Failed to determine file properties"), file_name, param, multi_file);
return 0;
}
/*is a directory?*/
if(file_type == S_IFDIR)
{
- errno = EISDIR;
- PRINT_ERROR(T("Unsupported file type encountered"));
+ errno = EISDIR; /*set errno!*/
+ print_error(T("Unsupported input type encountered"), file_name, param, multi_file);
return 0;
}
/*process file contents*/
while (!(ferror(source) || feof(source)))
{
+ clear_errors();
count = (uint_fast32_t)fread(buffer, sizeof(uint8_t), BUFF_SIZE, source);
if (count > 0)
{
/*check file error status*/
if ((!g_interrupted) && ferror(source))
{
- errno = EIO; /*fread() doesn't set errno!*/
- PRINT_ERROR(T("Error encountered while reading from file"));
+ errno = EIO; /*set errno!*/
+ print_error(T("Error encountered while reading from file"), file_name, param, multi_file);
fclose(source);
return 0;
}
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
+/*clear all errors*/
+static void clear_errors()
+{
+ errno = 0;
+ SET_SYSERRNO(0);
+}
/*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)
+static void print_error(const CHAR *const message, const CHAR *const file_name, const param_t *const param, const int multi_file)
+{
+ const CHAR *const source_name = file_name ? file_name : T("<STDIN>");
+ if (param->ignore_errors && multi_file)
+ {
+ FPRINTF(stderr, T("Skipped file: %s\n"), source_name); /*skip error message*/
+ }
+ else
+ {
+ const errno_t error = errno;
+ const unsigned long syserrno = SYSERRNO;
+ print_logo();
+ if (error && syserrno)
+ {
+ FPRINTF(stderr, T("%s:\n%s\n\n>>> %s [Code: 0x%X] <<<\n\n"), message, source_name, STRERROR(error), syserrno);
+ }
+ else
+ {
+ FPRINTF(stderr, T("%s:\n%s\n\n>>> %s <<<\n\n"), message, source_name, STRERROR(error));
+ }
+ }
+}
#endif /*MHASH_CLI_UTILS_INCLUDED*/