diff options
-rw-r--r-- | VERSION | 2 | ||||
-rw-r--r-- | crypt.c | 28 |
2 files changed, 21 insertions, 9 deletions
@@ -1 +1 @@ -2.0.0 +2.1.0 @@ -1,17 +1,18 @@ #define _XOPEN_SOURCE +#include <assert.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* strerror(3) */ #include <unistd.h> /* crypt(3) */ -void error(char *); +void error(const char *); void usage(FILE *, int); -int main (int argc, char **argv) +int main(int argc, char **argv) { - char *hash, *key, *salt; - int opt; + const char *hash, *key, *salt; + int opt, printed; while ((opt = getopt(argc, argv, "h")) != -1) { switch (opt) { @@ -44,15 +45,26 @@ int main (int argc, char **argv) if (!(hash = crypt(key, salt))) { error(strerror(errno)); } + assert(strlen(hash) > 0); + + /* + * Print the hash, and ensure we printed all of it + */ + if ((printed = printf("%s\n", hash)) < 0) { + error(strerror(errno)); + } + assert(printed > 0); + if ((unsigned) printed < strlen(hash) + 1) { /* +1 for newline */ + error("Incomplete print"); + } - puts(hash); exit(EXIT_SUCCESS); } /* - * Exit with error error message + * Exit with error message and status */ -void error(char *message) +void error(const char *message) { fprintf(stderr, "%s\n", message); exit(EXIT_FAILURE); @@ -61,7 +73,7 @@ void error(char *message) /* * Show usage to given stream, and exit with given code */ -void usage(FILE *stream, int status) +void usage(FILE *stream, const int status) { fputs("USAGE: crypt [-h | KEY SALT]\n", stream); exit(status); |