CFLAGS=-Wall -O2
-all: audiobooker bytor errno_speak show_default_sockopts simple_time vimstart hexconv
+all: audiobooker bytor errno_speak show_default_sockopts random_word simple_time vimstart hexconv
audiobooker:
go build audiobooker.go
hexconv: hexconv.o
+random_word: random_word.o
+
clean:
rm -rf bytor errno_speak show_default_sockopts simple_time vimstart hexconv *.o
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
+#include <unistd.h>
-#define DICT "/usr/share/dict/linux.words"
+#define DEFAULT_DICT_PATH "/usr/share/dict/linux.words"
#define STARTING_SZ 8192
#define MAX_ARRAY_SZ 1073741824
return dict->words[choice];
}
-int main(void)
+static void print_usage(char *program_name)
+{
+ printf("%s: prints a random word from a linebreak-delimited file.\n",
+ program_name);
+ printf("options:\n");
+ printf("\t-d: the dictionary file to use. Default: %s.\n",
+ DEFAULT_DICT_PATH);
+ printf("\t-h: this help message.\n");
+}
+
+static void parse_args(char **argv, int argc, char **dict_path)
+{
+ char c;
+ *dict_path = DEFAULT_DICT_PATH;
+ opterr = 0;
+ while ((c = getopt(argc, argv, "d:h")) != -1) {
+ switch (c) {
+ case 'd':
+ *dict_path = optarg;
+ break;
+ case 'h':
+ print_usage(argv[0]);
+ exit(0);
+ break;
+ default:
+ fprintf (stderr, "Argument parsing error.\n");
+ exit(1);
+ }
+ }
+}
+
+int main(int argc, char **argv)
{
FILE *fp;
+ char *dict_path;
const char *word;
struct dict *dict;
struct timeval tv;
+ parse_args(argv, argc, &dict_path);
gettimeofday(&tv, NULL);
srandom(tv.tv_usec * tv.tv_sec);
- fp = fopen(DICT, "r");
+ fp = fopen(dict_path, "r");
if (! fp) {
int err = errno;
fprintf(stderr, "failed to open %s: %s (%d).\n",
- DICT, strerror(err), err);
+ dict_path, strerror(err), err);
return 1;
}
dict = read_dict(fp);