From 7f2e3ab1c11e9dc38370604c5be1f8f2dc678859 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 7 Jan 2020 09:19:25 +1300 Subject: Rename to README.md --- README.markdown | 20 -------------------- README.md | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 20 deletions(-) delete mode 100644 README.markdown create mode 100644 README.md diff --git a/README.markdown b/README.markdown deleted file mode 100644 index cb45439..0000000 --- a/README.markdown +++ /dev/null @@ -1,20 +0,0 @@ -`crypt(1)` -========== - -A convenience wrapper for UNIX `crypt(3)`. Includes a manual page and `-h` -output and nothing else. - - $ make - $ sudo make install - -Or put it somewhere else: - - $ make install PREFIX="$HOME"/.local - -License -------- - -Copyright (c) [Tom Ryder][1]. Distributed under an [MIT License][2]. - -[1]: https://sanctum.geek.nz/ -[2]: https://www.opensource.org/licenses/MIT diff --git a/README.md b/README.md new file mode 100644 index 0000000..cb45439 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +`crypt(1)` +========== + +A convenience wrapper for UNIX `crypt(3)`. Includes a manual page and `-h` +output and nothing else. + + $ make + $ sudo make install + +Or put it somewhere else: + + $ make install PREFIX="$HOME"/.local + +License +------- + +Copyright (c) [Tom Ryder][1]. Distributed under an [MIT License][2]. + +[1]: https://sanctum.geek.nz/ +[2]: https://www.opensource.org/licenses/MIT -- cgit v1.2.3 From 61200ea6fe6e6df53479148801276e18cf4e9cd7 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 7 Jan 2020 09:37:06 +1300 Subject: Refactor usage function and output --- crypt.c | 37 ++++++++++++++++++------------------- crypt.h | 3 +-- 2 files changed, 19 insertions(+), 21 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); +} diff --git a/crypt.h b/crypt.h index 2b8145d..6b2b7ca 100644 --- a/crypt.h +++ b/crypt.h @@ -4,5 +4,4 @@ #include #include -#define OPTSTRING "h" -#define USAGE "USAGE: crypt KEY SALT" +void usage(FILE *, int); -- cgit v1.2.3 From db2175f28f504e462c154b6a188dc2f37fbfe9de Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 7 Jan 2020 09:48:35 +1300 Subject: Switch to tab indentation --- crypt.c | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/crypt.c b/crypt.c index 22f82bd..29c4794 100644 --- a/crypt.c +++ b/crypt.c @@ -5,40 +5,40 @@ */ int main (int argc, char **argv) { - /* The hash we will produce, hopefully */ - char *hash = NULL; + /* The hash we will produce, hopefully */ + char *hash = NULL; - /* Option character */ - int opt; + /* Option character */ + int opt; - /* Iterate through any options */ - while ((opt = getopt(argc, argv, "h")) != -1) { - switch (opt) { - case 'h': - usage(stdout, EXIT_SUCCESS); - break; - case '?': - usage(stderr, EXIT_FAILURE); - break; - default: - abort(); - } - } + /* Iterate through any options */ + while ((opt = getopt(argc, argv, "h")) != -1) { + switch (opt) { + case 'h': + usage(stdout, EXIT_SUCCESS); + break; + case '?': + usage(stderr, EXIT_FAILURE); + break; + default: + abort(); + } + } - /* If we don't have three arguments left now, bail */ - if (argc != 3) - usage(stderr, 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]); - puts(hash); - exit(EXIT_SUCCESS); + /* All seems well, build the hash and print it */ + hash = crypt(argv[1], argv[2]); + 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); + fputs("USAGE: crypt [-h | KEY SALT]\n", stream); + exit(status); } -- cgit v1.2.3 From d779c0b056defcf7eb4d75ab510e168ee03b5d95 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 7 Jan 2020 10:21:27 +1300 Subject: More refactoring and rearranging, commenting --- crypt.c | 40 ++++++++++++++++++++++++++-------------- crypt.h | 6 ++++-- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/crypt.c b/crypt.c index 29c4794..5e13fbd 100644 --- a/crypt.c +++ b/crypt.c @@ -1,36 +1,40 @@ #include "crypt.h" -/* - * Main function - */ int main (int argc, char **argv) { - /* The hash we will produce, hopefully */ - char *hash = NULL; - - /* Option character */ + char *hash, *key, *salt; int opt; - /* Iterate through any options */ while ((opt = getopt(argc, argv, "h")) != -1) { switch (opt) { - case 'h': + case 'h': /* Help */ usage(stdout, EXIT_SUCCESS); break; - case '?': + case '?': /* Unknown option */ usage(stderr, EXIT_FAILURE); break; - default: + default: /* Shouldn't happen */ abort(); } } - /* If we don't have three arguments left now, bail */ + /* + * 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); - /* All seems well, build the hash and print it */ - hash = crypt(argv[1], argv[2]); + 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); } @@ -42,3 +46,11 @@ 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 6b2b7ca..6612e01 100644 --- a/crypt.h +++ b/crypt.h @@ -1,7 +1,9 @@ #define _XOPEN_SOURCE -#include +#include #include #include -#include +#include /* strerror(3) */ +#include /* crypt(3) */ void usage(FILE *, int); +void error(char *); -- cgit v1.2.3 From 7a7f0b1c7e1fc65274ea324a0d80e18f688357c7 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 7 Jan 2020 10:36:14 +1300 Subject: Add VERSION --- VERSION | 1 + 1 file changed, 1 insertion(+) create mode 100644 VERSION diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..3eefcb9 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.0.0 -- cgit v1.2.3