diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2020-02-01 01:42:50 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2020-02-01 01:42:50 +1300 |
commit | d29da9b76bc3de08e29a7deb713077741ff17a0d (patch) | |
tree | 5c284027031a91f9308a9eccbdfff0c6ca3ae017 | |
parent | Merge branch 'release/v2.0.0' (diff) | |
parent | Bump VERSION (diff) | |
download | crypt-d29da9b76bc3de08e29a7deb713077741ff17a0d.tar.gz crypt-d29da9b76bc3de08e29a7deb713077741ff17a0d.zip |
Merge branch 'release/v2.1.0'v2.1.0
* release/v2.1.0:
Check zero print
Cast variable for comparison
Use a manual check in lieu of an assert
Remove stray space after function name
Correct an error message
Check print success more comprehensively
Add const decorators
-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); |