aboutsummaryrefslogtreecommitdiff
path: root/crypt.c
blob: f4d3a116de16f698a0a89c9b1d93ca81ac677b3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "crypt.h"

#define OPTSTRING "h"
#define USAGE "USAGE: crypt KEY SALT"

/**
 * crypt(1) - Simple shell script frontend to crypt(3), because I'm tired of
 * fighting with mkpasswd(1).
 * 
 * Author: Tom Ryder <tom@sanctum.geek.nz>
 * Copyright: 2015
 * License: MIT
 */
int main (int argc, char **argv)
{
    /* 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 */
    char *hash = crypt(argv[1], argv[2]);
    fprintf(stdout, "%s\n", hash);
    exit(EXIT_SUCCESS);
}