From: Carl Worth Date: Tue, 17 Apr 2007 05:33:06 +0000 (-0700) Subject: Initial commit of mnemon X-Git-Url: https://git.cworth.org/git?p=mnemon;a=commitdiff_plain;h=33c6037c59e76bf85487253953aa9d09ea208532 Initial commit of mnemon It doesn't really do anything yet---just reads in a file of challenge/response pairs and prints them out. --- 33c6037c59e76bf85487253953aa9d09ea208532 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77d9c45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +Makefile.dep +*~ +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dbb41d5 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +MY_CFLAGS=-Wall -Wextra -Wmissing-prototypes -Wno-unused-parameter -Wno-sign-compare + +PROGRAMS=mnemon +all: $(PROGRAMS) + +%: %.o + $(CC) $(CFLAGS) $(MY_CFLAGS) $(LDFLAGS) -o $@ $^ + +%.o: %.c + $(CC) $(CFLAGS) $(MY_CFLAGS) -c -o $@ $< + +Makefile.dep: *.c + $(CC) -M $(CPPFLAGS) $(MY_CFLAGS) $^ > $@ +-include Makefile.dep + +.PHONY: clean +clean: + rm -f $(PROGRAMS) *.o Makefile.dep diff --git a/mnemon.c b/mnemon.c new file mode 100644 index 0000000..8f735f4 --- /dev/null +++ b/mnemon.c @@ -0,0 +1,196 @@ +/* + * Copyright © 2006 Carl Worth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA." + */ + +/* for asprintf */ +#define _GNU_SOURCE +#include +#include +#include + +#include +#include +#include +#include + +typedef struct _mnemon { + int dummy; +} mnemon_t; + +typedef struct _ic { + int bin; + char *challenge; + char *response; +} ic_t; + +static void +xasprintf (char **strp, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start (ap, fmt); + ret = vasprintf (strp, fmt, ap); + va_end (ap); + + if (ret < 0) { + fprintf (stderr, "Error: out of memory\n"); + exit (1); + } +} + +static void +chomp (char *s) +{ + int len = strlen (s); + if (len == 0) + return; + if (s[len - 1] == '\n') + s[len - 1] = '\0'; +} + +static void +mnemon_load_from_file (mnemon_t *mnemon, + char *path) +{ + FILE *file; + char *line = NULL, *end; + size_t line_size = 0; + ssize_t bytes_read; + int line_count = 0; + + file = fopen (path, "r"); + if (file == NULL) { + fprintf (stderr, "Error: Failed to open %s: %s\n", + path, strerror (errno)); + exit (1); + } + + while (1) { + ic_t ic; + + /* Read bin number (ignoring blank separator lines) */ + do { + bytes_read = getline (&line, &line_size, file); + if (bytes_read == -1) + goto END_OF_FILE; + line_count++; + chomp (line); + } while (*line == '\0'); + + ic.bin = strtol (line, &end, 10); + if (*end != '\0') { + fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n", + line, path, line_count); + exit (1); + } + + /* Read challenge */ + bytes_read = getline (&line, &line_size, file); + if (bytes_read == -1) + break; + line_count++; + chomp (line); + ic.challenge = strdup (line); + + /* Read response */ + bytes_read = getline (&line, &line_size, file); + if (bytes_read == -1) + break; + line_count++; + chomp (line); + ic.response = strdup (line); + + /* XXX: Add ic to mnemon here */ + printf ("%d: %s => %s\n", ic.bin, ic.challenge, ic.response); + free (ic.challenge); + free (ic.response); + } + END_OF_FILE: + + free (line); + fclose (file); +} + +static void +mnemon_load_from_directory_recursive (mnemon_t *mnemon, + char *path) +{ + DIR *dir; + struct dirent *dirent; + char *child_path; + + dir = opendir (path); + if (dir == NULL) { + fprintf (stderr, "Error: Failed to open directory %s: %s\n", + path, strerror (errno)); + exit (1); + } + + while (1) { + dirent = readdir (dir); + if (dirent == NULL) + break; + + xasprintf (&child_path, "%s/%s", path, dirent->d_name); + if (dirent->d_type == DT_DIR) { + if (strcmp (dirent->d_name, ".") && + strcmp (dirent->d_name, "..")) + { + mnemon_load_from_directory_recursive (mnemon, child_path); + } + } else if (dirent->d_type == DT_REG) { + /* Ignore files matching *~, (yes, this shouldn't be + * hard-coded in such an ad-hoc way, but there you go. */ + if (child_path[strlen(child_path)-1] != '~') + mnemon_load_from_file (mnemon, child_path); + } else { + fprintf (stderr, "Warning: Ignoring file %s\n", child_path); + } + + free (child_path); + } + + closedir (dir); +} + +static void +mnemon_init (mnemon_t *mnemon) +{ + char *dot_mnemon; + char *home; + + home = getenv ("HOME"); + if (home == NULL) + home = ""; + + xasprintf (&dot_mnemon, "%s/.mnemon", getenv ("HOME")); + + mnemon_load_from_directory_recursive (mnemon, dot_mnemon); + + free (dot_mnemon); +} + +int +main (int argc, char *argv[]) +{ + mnemon_t mnemon; + + mnemon_init (&mnemon); + + return 0; +}