From bfb001a02503f9a49762b441edc6b23e4b2df7a0 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 23 May 2017 15:41:50 +0000 Subject: wip2 --- params.c | 86 ++++++++++++++++++++-------------------------------------------- 1 file changed, 27 insertions(+), 59 deletions(-) (limited to 'params.c') diff --git a/params.c b/params.c index 08272d6..d2a1be0 100644 --- a/params.c +++ b/params.c @@ -18,9 +18,11 @@ */ #include +#include #include #include #include +#include #include #include #include @@ -28,7 +30,6 @@ #include #include #include -#include #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); } -- cgit v1.2.3-54-g00ecf