diff options
Diffstat (limited to 'crypt.c')
-rw-r--r-- | crypt.c | 46 |
1 files changed, 38 insertions, 8 deletions
@@ -1,17 +1,36 @@ +/** + * Copyright (C) 2015--2017, 2020--2021 Tom Ryder <tom@sanctum.geek.nz> + * + * This file is part of crypt. + * + * crypt is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * crypt is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * crypt. If not, see <https://www.gnu.org/licenses/>. + */ + #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 +63,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 +91,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); |