From 60930484fce5add335ff1828cd169c2822e6face Mon Sep 17 00:00:00 2001 From: joe Date: Sun, 24 Dec 2000 12:21:15 +0000 Subject: [PATCH] Clean up the source directory finding code, and in the process fix a problem that occurs when a program's source isn't found. --- usr.sbin/crunch/crunchgen/crunchgen.c | 40 ++++++++++++++++----------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/usr.sbin/crunch/crunchgen/crunchgen.c b/usr.sbin/crunch/crunchgen/crunchgen.c index 5fcdb3ac8dea..d1e5e0243190 100644 --- a/usr.sbin/crunch/crunchgen/crunchgen.c +++ b/usr.sbin/crunch/crunchgen/crunchgen.c @@ -515,7 +515,6 @@ void gen_outputs(void) void fillin_program(prog_t *p) { char path[MAXPATHLEN]; - char *srcparent; char line[MAXLINELEN]; FILE *f; @@ -524,27 +523,29 @@ void fillin_program(prog_t *p) if(!p->ident) p->ident = genident(p->name); + + /* look for the source directory if one wasn't specified by a special */ if(!p->srcdir) { - srcparent = dir_search(p->name); - if(srcparent) - snprintf(line, MAXLINELEN, "%s/%s", srcparent, p->name); - if(is_dir(line)) - if ((p->srcdir = strdup(line)) == NULL) - out_of_memory(); + p->srcdir = dir_search(p->name); } /* Determine the actual srcdir (maybe symlinked). */ - snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`", p->srcdir); - f = popen(line,"r"); - if (f) { + if (p->srcdir) { + snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`", p->srcdir); + f = popen(line,"r"); + if (!f) + errx(1, "Can't execute: %s\n", line); + path[0] = '\0'; fgets(path, sizeof path, f); - if (!pclose(f)) { - p->realsrcdir = strdup(path); - } + if (pclose(f)) + errx(1, "Can't execute: %s\n", line); + + if (!*path) + errx(1, "Can't perform pwd on: %s\n", p->srcdir); + + p->realsrcdir = strdup(path); } - if (!p->realsrcdir) - errx(1, "Can't execute: %s\n", line); /* Unless the option to make object files was specified the * the objects will be built in the source directory unless @@ -809,10 +810,17 @@ char *dir_search(char *progname) { char path[MAXPATHLEN]; strlst_t *dir; + char *srcdir; for(dir=srcdirs; dir != NULL; dir=dir->next) { snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname); - if(is_dir(path)) return dir->str; + if (!is_dir(path)) { + continue; + } + + if ((srcdir = strdup(path)) == NULL) + out_of_memory(); + return srcdir; } return NULL; }