aboutsummaryrefslogtreecommitdiffstats
path: root/params.c
diff options
context:
space:
mode:
Diffstat (limited to 'params.c')
-rw-r--r--params.c86
1 files changed, 27 insertions, 59 deletions
diff --git a/params.c b/params.c
index 08272d6..d2a1be0 100644
--- a/params.c
+++ b/params.c
@@ -18,9 +18,11 @@
*/
#include <assert.h>
+#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <glob.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
@@ -28,7 +30,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
-#include <ctype.h>
#include "eprintf.h"
#include "params.h"
@@ -37,12 +38,10 @@
/* TODO: make this dynamic */
#define SZMAX_PFILE 1024
-#ifndef DATADIR
-#define DATADIR "hktool"
-#endif
+char *datadir;
/* readparams: read a file's param definition */
-static void readparams(struct flashparams *fp, const char *file)
+void readparams(struct flashparams *fp, const char *file)
{
char cont[SZMAX_PFILE], *pos;
size_t contsz;
@@ -124,81 +123,50 @@ static void printcname(const char *file)
putchar(c);
}
-/* getparams: Locate a parameter file and load it or show names of all files */
-bool getparams(struct flashparams *fp, const char *name)
+/* listparams: Print a list of the parameter files which could be found */
+void listparams(void)
{
- char pathbuf[PATH_MAX], *dirname, *fullpath;
+ char pathbuf[PATH_MAX], *fullpath;
struct dirent *de;
size_t pathsize;
- bool found;
DIR *dir;
- assert((fp == NULL && name == NULL) || (fp != NULL && name != NULL));
fullpath = pathbuf;
pathsize = sizeof pathbuf;
- dirname = getenv("HKTOOL_DATADIR");
- if (dirname == NULL || strlen(dirname) == 0)
- dirname = DATADIR;
-
- dir = opendir(dirname);
+ dir = opendir(datadir);
if (dir == NULL)
- eprintf("Could not open directory '%s':", dirname);
+ eprintf("Could not open directory '%s':", datadir);
- if (name == NULL)
- printf("Searching '%s' for Device Parameter Files:\n", dirname);
-
- found = false;
while (errno = 0, de = readdir(dir), de != NULL) {
- bool ismatch, doprint;
-
- doprint = name == NULL;
- if (doprint)
- ismatch = strcmp(de->d_name, name) == 0;
- else
- ismatch = false;
-
- if (doprint || ismatch) {
- int size;
-
- size = snprintf(NULL, 0, "%s/%s", dirname, de->d_name);
- assert(size > 0);
- if ((size_t)size > pathsize) {
- if (fullpath == pathbuf)
- fullpath = NULL;
- fullpath = erealloc(fullpath, size);
- pathsize = size;
- }
- size = snprintf(fullpath, pathsize, "%s/%s", dirname, de->d_name);
- assert(size > 0);
- assert((size_t)size < pathsize);
- }
+ int size;
- if (doprint) {
- printf("%s", de->d_name);
- printcname(fullpath);
- putchar('\n');
- continue;
- };
-
- if (!ismatch)
+ if (de->d_name[0] == '.')
continue;
+ size = snprintf(NULL, 0, "%s/%s", datadir, de->d_name);
+ assert(size > 0);
+ if ((size_t)size > pathsize) {
+ if (fullpath == pathbuf)
+ fullpath = NULL;
+ fullpath = erealloc(fullpath, size);
+ pathsize = size;
+ }
+ size = snprintf(fullpath, pathsize, "%s/%s", datadir, de->d_name);
+ assert(size > 0);
+ assert((size_t)size < pathsize);
- readparams(fp, fullpath);
- found = true;
- errno = 0;
- break;
+ printf("%s", de->d_name);
+ printcname(fullpath);
+ putchar('\n');
}
if (errno)
- weprintf("Could not read directory '%s':", dirname);
+ weprintf("Could not read directory '%s':", datadir);
if (fullpath != pathbuf)
free(fullpath);
if (closedir(dir) == -1)
- eprintf("Could not close directory '%s':", dirname);
-
- return found;
+ eprintf("Could not close directory '%s':", datadir);
}