added debug output, fixed error handling

This commit is contained in:
TLINDEN
2015-08-02 15:22:12 +02:00
parent 4db1d83456
commit 60bedf0c59
6 changed files with 69 additions and 35 deletions

View File

@@ -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 <count> Number of words (default: 4)\n"
"-f --dictfile <dictfile> Dictionary file to use (default:\n"
" /usr/share/dict/american-english)\n"
"-l --minlen <count> Minimum word len (default: 5)\n"
"-m --maxlen <count> 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<count; i++) {
tosses = toss(5);
tosses = toss(5, i);
one = tosses[0] * 10000;
two = tosses[1] * 1000;

View File

@@ -32,12 +32,16 @@
#include "dictfile.h"
#include "tossing.h"
#include "debug.h"
#define STRINGIZE(x) #x
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
#define DICTFILE "/usr/share/dict/american-english"
#define VERSION "1.0"
#define RLEN 1024
int humantoss;
int verbose;
int WMIN;
int WMAX;

View File

@@ -51,6 +51,19 @@ int *incr_dicedigit(int *digits) {
return digits;
}
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;
}
char **fetch_dict(char *dictfile) {
/*
read in the dictionary file. we generate an array of max
@@ -70,6 +83,7 @@ char **fetch_dict(char *dictfile) {
if((DICT = fopen(dictfile, "rb")) == NULL) {
perror("Could not open dictfile");
exit(1);
}
words = malloc(66666 * sizeof(char *));
@@ -115,7 +129,8 @@ char **fetch_dict(char *dictfile) {
words[pos] = malloc(linelen);
strncpy( words[pos], line, linelen);
//fprintf(stdout, "%d: %s\n", pos, line);
if(verbose > 1)
debug("add to wordlist at index %d: %s", pos, line);
digits = incr_dicedigit(digits);
pos = get_dicenum(digits);

View File

@@ -30,11 +30,13 @@
#include <ctype.h>
#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

View File

@@ -21,7 +21,7 @@
#include "tossing.h"
unsigned char *toss(int count) {
unsigned char *toss(int count, int dicenum) {
/*
toss <count> 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<count; i++) { /* only digits allowed */
if(isdigit(line[i]) == 0)
}
for(i=0; i<count; i++) {
if(isdigit(line[i]) == 0) {
fprintf(stderr, "error: please enter only digits!\n");
goto RETRY;
}
digit[0] = line[i];
onedice = atoi(digit);
if (onedice < 1 || onedice > 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;
}

View File

@@ -29,14 +29,15 @@
#include <string.h>
#include <ctype.h>
#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