aboutsummaryrefslogtreecommitdiff
path: root/crypt.c
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2020-01-07 09:37:06 +1300
committerTom Ryder <tom@sanctum.geek.nz>2020-01-07 09:37:37 +1300
commit61200ea6fe6e6df53479148801276e18cf4e9cd7 (patch)
treefd20769cfb332dbd8ae1d23a7f9a74414a361e15 /crypt.c
parentRename to README.md (diff)
downloadcrypt-61200ea6fe6e6df53479148801276e18cf4e9cd7.tar.gz
crypt-61200ea6fe6e6df53479148801276e18cf4e9cd7.zip
Refactor usage function and output
Diffstat (limited to 'crypt.c')
-rw-r--r--crypt.c37
1 files changed, 18 insertions, 19 deletions
diff --git a/crypt.c b/crypt.c
index 54b90ce..22f82bd 100644
--- a/crypt.c
+++ b/crypt.c
@@ -8,38 +8,37 @@ int main (int argc, char **argv)
/* The hash we will produce, hopefully */
char *hash = NULL;
- /* Assume user doesn't want help */
- int help = 0;
+ /* Option character */
+ int opt;
/* Iterate through any options */
- int o;
- while ((o = getopt(argc, argv, OPTSTRING)) != -1) {
- switch (o) {
+ while ((opt = getopt(argc, argv, "h")) != -1) {
+ switch (opt) {
case 'h':
- help = 1;
+ usage(stdout, EXIT_SUCCESS);
break;
case '?':
- fprintf(stderr, "Unknown option\n");
+ usage(stderr, EXIT_FAILURE);
break;
default:
abort();
}
}
- /* If help was asked, give it */
- if (help) {
- fprintf(stdout, "%s\n", USAGE);
- exit(EXIT_SUCCESS);
- }
-
- /* If we don't have three arguments, bail */
- if (argc != 3) {
- fprintf(stderr, "%s\n", USAGE);
- exit(EXIT_FAILURE);
- }
+ /* If we don't have three arguments left now, bail */
+ if (argc != 3)
+ usage(stderr, EXIT_FAILURE);
/* All seems well, build the hash and print it */
hash = crypt(argv[1], argv[2]);
- fprintf(stdout, "%s\n", hash);
+ puts(hash);
exit(EXIT_SUCCESS);
}
+
+/*
+ * Show usage to given stream, and exit with given code
+ */
+void usage(FILE *stream, int status) {
+ fputs("USAGE: crypt [-h | KEY SALT]\n", stream);
+ exit(status);
+}