Print the system error message, if the input or output file could not be opened.

This commit is contained in:
LoRd_MuldeR 2022-02-27 19:28:38 +01:00
parent 65c17e6759
commit b95e578e33
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 44 additions and 21 deletions

View File

@ -20,6 +20,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <ctype.h> #include <ctype.h>
#include <assert.h> #include <assert.h>
#include <errno.h>
// ========================================================================== // ==========================================================================
// Constants // Constants
@ -37,14 +38,14 @@ static int open_files(FILE **const file_in, FILE **const file_out, const CHR *co
{ {
if (!(*file_in = FOPEN(input_path, T("rb")))) if (!(*file_in = FOPEN(input_path, T("rb"))))
{ {
FPRINTF(stderr, T("Error: Failed to open input file \"%") T(PRISTR) T("\" for reading!\n\n"), input_path); FPRINTF(stderr, T("Error: Failed to open input file \"%") T(PRISTR) T("\" for reading!\n\n%") T(PRISTR) T("\n\n"), input_path, STRERROR(errno));
*file_out = NULL; *file_out = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (!(*file_out = FOPEN(output_path, T("wb")))) if (!(*file_out = FOPEN(output_path, T("wb"))))
{ {
FPRINTF(stderr, T("Error: Failed to open output file \"%") T(PRISTR) T("\" for writing!\n\n"), output_path); FPRINTF(stderr, T("Error: Failed to open output file \"%") T(PRISTR) T("\" for writing!\n\n%") T(PRISTR) T("\n\n"), output_path, STRERROR(errno));
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@ -63,6 +63,7 @@
# define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__) # define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__)
# define REMOVE(X) _wremove((X)) # define REMOVE(X) _wremove((X))
# define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE) # define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE)
# define STRERROR(X) _wcserror((X))
# ifdef __USE_MINGW_ANSI_STDIO # ifdef __USE_MINGW_ANSI_STDIO
# define PRISTR "ls" # define PRISTR "ls"
# define PRIstr "hs" # define PRIstr "hs"
@ -87,6 +88,7 @@
# define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__) # define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__)
# define REMOVE(X) remove((X)) # define REMOVE(X) remove((X))
# define FOPEN(X,Y) fopen((X),(Y)) # define FOPEN(X,Y) fopen((X),(Y))
# define STRERROR(X) strerror((X))
# define PRISTR "s" # define PRISTR "s"
# define PRIstr "s" # define PRIstr "s"
# define PRIwcs "ls" # define PRIwcs "ls"

View File

@ -16,6 +16,7 @@
/* CRT */ /* CRT */
#include <ctype.h> #include <ctype.h>
#include <errno.h>
// ========================================================================== // ==========================================================================
// Constants // Constants
@ -37,51 +38,70 @@ static const char PASSWD_SYMBOLS[] =
// Read passphrase from file // Read passphrase from file
// ========================================================================== // ==========================================================================
#define PASSPHRASE_BUFFSIZE (SLUNKCRYPT_PWDLEN_MAX + 2U)
char *read_passphrase(const CHR *const file_name) char *read_passphrase(const CHR *const file_name)
{ {
size_t str_len = 0U;
CHR *passphrase_path = NULL;
char *buffer = NULL;
FILE *passphrase_file = NULL;
if ((!file_name) || (!file_name[0U])) if ((!file_name) || (!file_name[0U]))
{ {
FPUTS(T("Error: The passphrase input file name must not be empty!\n\n"), stderr); FPUTS(T("Error: The passphrase input file name must not be empty!\n\n"), stderr);
return NULL; goto finish;
} }
const size_t max_len = SLUNKCRYPT_PWDLEN_MAX + 2U; if (STRICMP(file_name, T("-")))
char *const buffer = (char*) malloc(max_len * sizeof(char)); {
passphrase_path = absolute_path(file_name);
if ((!passphrase_path) || (!passphrase_path[0U]))
{
FPUTS(T("Error: Passphrase input file path could not be resolved!\n\n"), stderr);
goto finish;
}
}
passphrase_file = passphrase_path ? FOPEN(passphrase_path, T("rb")): stdin;
if (!passphrase_file)
{
FPRINTF(stderr, T("Error: Failed to open passphrase file \"%") T(PRISTR) T("\" for reading!\n\n%") T(PRISTR) T("\n\n"), passphrase_path, STRERROR(errno));
goto finish;
}
buffer = (char*) calloc(PASSPHRASE_BUFFSIZE, sizeof(char));
if (!buffer) if (!buffer)
{ {
FPUTS(T("Error: Failed to allocate the passphrase buffer!\n\n"), stderr); FPUTS(T("Error: Failed to allocate the passphrase buffer!\n\n"), stderr);
return NULL; goto finish;
}
const int use_stdin = (!file_name) || (!STRICMP(file_name, T("-")));
FILE *const file_in = use_stdin ? stdin : FOPEN(file_name, T("rb"));
if (!file_in)
{
FPRINTF(stderr, T("Error: Failed to open input file \"%") T(PRISTR) T("\" for reading!\n\n"), file_name);
free(buffer);
return NULL;
} }
do do
{ {
if (!fgets(buffer, (int)max_len, file_in)) if (!fgets(buffer, (int)PASSPHRASE_BUFFSIZE, passphrase_file))
{ {
buffer[0U] = '\0'; buffer[0U] = '\0';
goto finish; goto finish;
} }
size_t length = strlen(buffer); str_len = strlen(buffer);
while ((length > 0U) && ((buffer[length - 1U] == '\r') || (buffer[length - 1U] == '\n'))) while ((str_len > 0U) && ((buffer[str_len - 1U] == '\r') || (buffer[str_len - 1U] == '\n')))
{ {
buffer[--length] = '\0'; buffer[--str_len] = '\0';
} }
} }
while (!buffer[0U]); while (!buffer[0U]);
finish: finish:
if ((!use_stdin) && file_in) if (passphrase_file && (passphrase_file != stdin))
{ {
fclose(file_in); fclose(passphrase_file);
}
if (passphrase_path)
{
free(passphrase_path);
} }
return buffer; return buffer;