aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2020-01-07 10:36:35 +1300
committerTom Ryder <tom@sanctum.geek.nz>2020-01-07 10:36:35 +1300
commitce7bd22af9d55eada0cff15f719f338dd212f2e0 (patch)
tree0c63840969a53b41140a9cc4349a00b7b1dddb8c
parentMore idiomatic Makefile (diff)
parentAdd VERSION (diff)
downloadcrypt-ce7bd22af9d55eada0cff15f719f338dd212f2e0.tar.gz
crypt-ce7bd22af9d55eada0cff15f719f338dd212f2e0.zip
Merge branch 'release/v1.0.0'v1.0.0
* release/v1.0.0: Add VERSION More refactoring and rearranging, commenting Switch to tab indentation Refactor usage function and output Rename to README.md
-rw-r--r--README.md (renamed from README.markdown)0
-rw-r--r--VERSION1
-rw-r--r--crypt.c91
-rw-r--r--crypt.h9
4 files changed, 57 insertions, 44 deletions
diff --git a/README.markdown b/README.md
index cb45439..cb45439 100644
--- a/README.markdown
+++ b/README.md
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..3eefcb9
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+1.0.0
diff --git a/crypt.c b/crypt.c
index 54b90ce..5e13fbd 100644
--- a/crypt.c
+++ b/crypt.c
@@ -1,45 +1,56 @@
#include "crypt.h"
-/*
- * Main function
- */
int main (int argc, char **argv)
{
- /* The hash we will produce, hopefully */
- char *hash = NULL;
-
- /* Assume user doesn't want help */
- int help = 0;
-
- /* Iterate through any options */
- int o;
- while ((o = getopt(argc, argv, OPTSTRING)) != -1) {
- switch (o) {
- case 'h':
- help = 1;
- break;
- case '?':
- fprintf(stderr, "Unknown option\n");
- 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);
- }
-
- /* All seems well, build the hash and print it */
- hash = crypt(argv[1], argv[2]);
- fprintf(stdout, "%s\n", hash);
- exit(EXIT_SUCCESS);
+ char *hash, *key, *salt;
+ int opt;
+
+ while ((opt = getopt(argc, argv, "h")) != -1) {
+ switch (opt) {
+ case 'h': /* Help */
+ usage(stdout, EXIT_SUCCESS);
+ break;
+ case '?': /* Unknown option */
+ usage(stderr, EXIT_FAILURE);
+ break;
+ default: /* Shouldn't happen */
+ abort();
+ }
+ }
+
+ /*
+ * If we don't have three arguments left after processing the options,
+ * exit with usage information and error status
+ */
+ if (argc != 3)
+ usage(stderr, EXIT_FAILURE);
+
+ key = argv[1];
+ salt = argv[2];
+
+ /*
+ * Create the hash, but exit immediately with the system error string
+ * if it returns a null pointer (error condition)
+ */
+ if (!(hash = crypt(key, salt)))
+ error(strerror(errno));
+
+ 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);
+}
+
+/*
+ * Exit with error error message
+ */
+void error(char *message) {
+ fprintf(stderr, "%s\n", message);
+ exit(EXIT_FAILURE);
}
diff --git a/crypt.h b/crypt.h
index 2b8145d..6612e01 100644
--- a/crypt.h
+++ b/crypt.h
@@ -1,8 +1,9 @@
#define _XOPEN_SOURCE
-#include <ctype.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
+#include <string.h> /* strerror(3) */
+#include <unistd.h> /* crypt(3) */
-#define OPTSTRING "h"
-#define USAGE "USAGE: crypt KEY SALT"
+void usage(FILE *, int);
+void error(char *);