static char* read_passphrase(const CHR* const file_name)
{
- const size_t buff_size = 1024U;
- char *const buffer = (char*) malloc(buff_size * sizeof(char));
+ static const size_t buff_size = 512U;
+ char *buffer = (char*) malloc(buff_size * sizeof(char));
if (!buffer)
{
return NULL;
}
- FILE *const file = FOPEN(file_name, T("rb"));
+
+ const int use_stdin = (STRICMP(file_name, T("-")) == 0);
+ FILE *const file = use_stdin ? stdin : FOPEN(file_name, T("rb"));
if (!file)
{
+ free(buffer);
return NULL;
}
+
do
{
if (!fgets(buffer, (int)buff_size, file))
{
- fclose(file);
free(buffer);
- return NULL;
+ buffer = NULL;
+ goto finish;
}
size_t length = strlen(buffer);
while ((length > 0U) && ((buffer[length - 1U] == '\r') || (buffer[length - 1U] == '\n')))
}
}
while (!buffer[0U]);
- fclose(file);
+
+finish:
+
+ if ((!use_stdin) && file)
+ {
+ fclose(file);
+ }
+
return buffer;
}
FPUTS(T("This software has been released under the CC0 1.0 Universal license:\n"), stderr);
FPUTS(T("https://creativecommons.org/publicdomain/zero/1.0/legalcode\n"), stderr);
FPUTS(T("====================================================================\n\n"), stderr);
- FPUTS(T("Usage:\n"), stderr);
+ FPUTS(T("Synopsis:\n"), stderr);
FPRINTF(stderr, T(" %") T(PRISTR) T(" --encrypt [[@][:]<passphrase>] <input.txt> <output.enc>\n"), program);
FPRINTF(stderr, T(" %") T(PRISTR) T(" --decrypt [[@][:]<passphrase>] <input.enc> <output.txt>\n\n"), program);
- FPUTS(T("Notes:\n"), stderr);
+ FPUTS(T("Remarks:\n"), stderr);
FPUTS(T("- If <passphrase> is prefixed with a '@' character, then it specifies the file\n"), stderr);
FPUTS(T(" to read the passphrase from; only the first line in that file is used!\n"), stderr);
FPUTS(T("- If <passphrase> is prefixed with a ':' character, then the leading character\n"), stderr);
FPUTS(T(" is skipped and the remainder of the argument is used as passphrase.\n"), stderr);
FPUTS(T("- If the argument <passphrase> is *not* present, then the environment variable\n"), stderr);
- FPRINTF(stderr, T(" \"%") T(PRISTR) T("\" must be set and it specifies the passphrase to be used.\n\n"), ENVV_PASSWD_NAME);
+ FPRINTF(stderr, T(" \"%") T(PRISTR) T("\" must be set; it specifies the passphrase to be used.\n"), ENVV_PASSWD_NAME);
+ FPUTS(T("- Specify \"@-\" in order to read the passphrase from the standard input stream!\n\n"), stderr);
return 0;
}
if ((!STRICMP(argv[1U], T("-t"))) || (!STRICMP(argv[1U], T("--self-test"))))