2 * Copyright © 2006 Carl Worth
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
25 #include <sys/types.h>
30 typedef struct _mnemon {
41 xasprintf (char **strp, const char *fmt, ...)
47 ret = vasprintf (strp, fmt, ap);
51 fprintf (stderr, "Error: out of memory\n");
62 if (s[len - 1] == '\n')
67 mnemon_load_from_file (mnemon_t *mnemon,
71 char *line = NULL, *end;
76 file = fopen (path, "r");
78 fprintf (stderr, "Error: Failed to open %s: %s\n",
79 path, strerror (errno));
86 /* Read bin number (ignoring blank separator lines) */
88 bytes_read = getline (&line, &line_size, file);
93 } while (*line == '\0');
95 ic.bin = strtol (line, &end, 10);
97 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
98 line, path, line_count);
103 bytes_read = getline (&line, &line_size, file);
104 if (bytes_read == -1)
108 ic.challenge = strdup (line);
111 bytes_read = getline (&line, &line_size, file);
112 if (bytes_read == -1)
116 ic.response = strdup (line);
118 /* XXX: Add ic to mnemon here */
119 printf ("%d: %s => %s\n", ic.bin, ic.challenge, ic.response);
130 mnemon_load_from_directory_recursive (mnemon_t *mnemon,
134 struct dirent *dirent;
137 dir = opendir (path);
139 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
140 path, strerror (errno));
145 dirent = readdir (dir);
149 xasprintf (&child_path, "%s/%s", path, dirent->d_name);
150 if (dirent->d_type == DT_DIR) {
151 if (strcmp (dirent->d_name, ".") &&
152 strcmp (dirent->d_name, ".."))
154 mnemon_load_from_directory_recursive (mnemon, child_path);
156 } else if (dirent->d_type == DT_REG) {
157 /* Ignore files matching *~, (yes, this shouldn't be
158 * hard-coded in such an ad-hoc way, but there you go. */
159 if (child_path[strlen(child_path)-1] != '~')
160 mnemon_load_from_file (mnemon, child_path);
162 fprintf (stderr, "Warning: Ignoring file %s\n", child_path);
172 mnemon_init (mnemon_t *mnemon)
177 home = getenv ("HOME");
181 xasprintf (&dot_mnemon, "%s/.mnemon", getenv ("HOME"));
183 mnemon_load_from_directory_recursive (mnemon, dot_mnemon);
189 main (int argc, char *argv[])
193 mnemon_init (&mnemon);