From: dfffffff Date: Sat, 3 Sep 2016 12:43:55 +0000 (-0400) Subject: refactored which utility X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=63493f4585ca28368dc9722fb73a41093b55290e;p=FUZIX.git refactored which utility --- diff --git a/Applications/util/which.c b/Applications/util/which.c index 167cdab0..3b0bfd1a 100644 --- a/Applications/util/which.c +++ b/Applications/util/which.c @@ -1,6 +1,7 @@ #include #include #include +#include int main(int argc, char *argv[]) { @@ -14,35 +15,39 @@ int main(int argc, char *argv[]) fprintf(stderr, "Usage: which cmd [cmd, ..]\n"); return 1; } - if ((envpath = getenv("PATH")) == 0) { - envpath = "."; + if ((envpath = getenv("PATH")) == NULL) { + envpath = "/bin"; } - argv[argc] = 0; + argv[argc] = NULL; + found = 0; + argc--; for (argv++; *argv; argv++) { - strcpy(patbuf, envpath); + snprintf(patbuf, sizeof(patbuf), "%s", envpath); cp = path = patbuf; - quit = found = 0; + quit = 0; while (!quit) { - cp = index(path, ':'); + cp = strchr(path, ':'); if (cp == NULL) { quit++; } else { - *cp = '\0'; + *cp = 0; } - snprintf(buf, 512, "%s/%s", (*path ? path : "."), *argv); + snprintf(buf, sizeof(buf), "%s/%s", (*path ? path : "."), *argv); path = ++cp; - if (access(buf, 1) == 0) { + if (access(buf, X_OK) == 0) { printf("%s\n", buf); found++; + break; } } - if (!found) { - printf("No %s in %s\n", *argv, envpath); - } } - return 0; + /* + * 0: if all the commands have been found and are executable + * 1: if at least one command is not found or not executable + */ + return found != argc; }