diff --git a/dicepwgen.c b/dicepwgen.c index 48fc260..0f47961 100644 --- a/dicepwgen.c +++ b/dicepwgen.c @@ -24,14 +24,15 @@ int usage() { fprintf(stderr, "Generate a random diceware passphrase\n" - "Usage: dice [-tcfvh]\n" + "Usage: dice [-tcfvhd]\n" "Options: \n" - "-t --humantoss Asks interactively for tossed dices\n" + "-t --humantoss Asks interactively for rolled dices\n" "-c --wordcount Number of words (default: 4)\n" "-f --dictfile Dictionary file to use (default:\n" " /usr/share/dict/american-english)\n" "-l --minlen Minimum word len (default: 5)\n" "-m --maxlen Maximum word len (default: 10)\n" + "-d --debug Enable debug output\n" "-v --version Print program version\n" "-h -? --help Print this help screen\n" ); @@ -41,12 +42,12 @@ int usage() { int main (int argc, char **argv) { int count = 4; char *dictfile = NULL; - unsigned char *tosses = NULL; int opt; WMIN = 6; WMAX = 10; humantoss = 0; + verbose = 0; static struct option longopts[] = { { "wordcount", required_argument, NULL, 'c' }, @@ -56,9 +57,10 @@ int main (int argc, char **argv) { { "dictfile", required_argument, NULL, 'f' }, { "version", no_argument, NULL, 'v' }, { "help", no_argument, NULL, 'h' }, + { "debug", no_argument, NULL, 'd' }, }; - while ((opt = getopt_long(argc, argv, "l:m:tf:c:vh?", longopts, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "l:m:tf:c:vh?d", longopts, NULL)) != -1) { switch (opt) { case 'v': fprintf(stderr, "This is %s version %s\n", argv[0], VERSION); @@ -80,6 +82,9 @@ int main (int argc, char **argv) { case 't': humantoss = 1; break; + case 'd': + verbose++; + break; case 'f': dictfile = malloc(strlen(optarg)); strncpy(dictfile, optarg, strlen(optarg)); @@ -90,10 +95,17 @@ int main (int argc, char **argv) { } } - if(dictfile == NULL) { - dictfile = DICTFILE; + dictfile = STRINGIZE_VALUE_OF(DICTFILE); } + + debug(" using dictfile: %s", dictfile); + debug("minimum word length: %d", WMIN); + debug("maximum word length: %d", WMAX); + if(humantoss) + debug("user rolls dices"); + else + debug("program rolls dices"); getwords(dictfile, count); @@ -115,7 +127,7 @@ void getwords(char *dictfile, int count) { tossed = malloc(count * sizeof(int)); for(i=0; i 1) + debug("add to wordlist at index %d: %s", pos, line); digits = incr_dicedigit(digits); pos = get_dicenum(digits); diff --git a/dictfile.h b/dictfile.h index f564ffe..e48aa12 100644 --- a/dictfile.h +++ b/dictfile.h @@ -30,11 +30,13 @@ #include #include "tossing.h" +#include "debug.h" extern int WMIN; extern int WMAX; int *incr_dicedigit(int *digits); +int get_dicenum(int *digits); char **fetch_dict(char *dictfile); #endif diff --git a/tossing.c b/tossing.c index 7eda714..e34b356 100644 --- a/tossing.c +++ b/tossing.c @@ -21,7 +21,7 @@ #include "tossing.h" -unsigned char *toss(int count) { +unsigned char *toss(int count, int dicenum) { /* toss dices. if the global humandice is enabled (option -t), then ask the human to toss dices and enter the numbers interactively. @@ -43,23 +43,28 @@ unsigned char *toss(int count) { digit[1] = '\0'; RETRY: - fprintf(stderr, "enter 5 digits, each between 1-6\n"); + fprintf(stdout, "dice %d - enter 5 digits, each between 1-6: ", dicenum+1); linelen = getline(&line, &len, stdin); tosses = malloc((size_t)count); - if(linelen < 6) /* 5 digits max allowed */ + if(linelen < 6) { + fprintf(stderr, "error: please enter 5 digits!\n"); goto RETRY; - - for(i=0; i 6) /* no dice digit */ + if (onedice < 1 || onedice > 6) { + fprintf(stderr, "error: please enter only digits between 1 and 6!\n"); goto RETRY; - + } tosses[i] = onedice; } free(line); @@ -67,11 +72,15 @@ unsigned char *toss(int count) { else { rand = malloc(RLEN); - if((RAND = fopen("/dev/urandom", "rb")) == NULL) + if((RAND = fopen("/dev/urandom", "rb")) == NULL) { perror("Could not open /dev/urandom"); + exit(1); + } - if(fread(rand, RLEN, 1, RAND) != 1) + if(fread(rand, RLEN, 1, RAND) != 1) { perror("Could not read from /dev/urandom"); + exit(1); + } fclose(RAND); @@ -90,7 +99,10 @@ unsigned char *toss(int count) { } free(rand); } - + + debug("random rolled dices: %d %d %d %d %d", + tosses[0], tosses[1], tosses[2], tosses[3], tosses[4]); + return tosses; } @@ -110,15 +122,3 @@ int rand_lim(int limit) { } - -int get_dicenum(int *digits) { - /* - get the actual number of an array of dice digits - */ - int i = 0; - int pos = 0; - for(i=0; i<5; i++) - pos += digits[i]; - - return pos; -} diff --git a/tossing.h b/tossing.h index d1a2156..eca0351 100644 --- a/tossing.h +++ b/tossing.h @@ -29,14 +29,15 @@ #include #include +#include "debug.h" + #define RLEN 1024 extern int humantoss; extern int WMIN; extern int WMAX; -unsigned char *toss(int count); +unsigned char *toss(int count, int dicenum); int rand_lim(int limit); -int get_dicenum(int *digits); #endif