Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).

This commit is contained in:
Rob Landley 2008-05-17 17:52:51 -05:00
parent 1a221d9b4f
commit 59f490cb4e
2 changed files with 9 additions and 13 deletions

View File

@ -364,7 +364,7 @@ void xmkpath(char *path, int mode)
struct string_list *find_in_path(char *path, char *filename) struct string_list *find_in_path(char *path, char *filename)
{ {
struct string_list *rlist = NULL; struct string_list *rlist = NULL, **prlist=&rlist;
char *cwd = xgetcwd(); char *cwd = xgetcwd();
for (;;) { for (;;) {
@ -386,8 +386,9 @@ struct string_list *find_in_path(char *path, char *filename)
// Confirm it's not a directory. // Confirm it's not a directory.
if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) {
rnext->next = rlist; *prlist = rnext;
rlist = rnext; rnext->next = NULL;
prlist = &(rnext->next);
} else free(rnext); } else free(rnext);
if (!next) break; if (!next) break;

View File

@ -6,7 +6,7 @@
* *
* Not in SUSv3. * Not in SUSv3.
USE_WHICH(NEWTOY(which, "a", TOYFLAG_USR|TOYFLAG_BIN)) USE_WHICH(NEWTOY(which, "<1a", TOYFLAG_USR|TOYFLAG_BIN))
config WHICH config WHICH
bool "which" bool "which"
@ -20,8 +20,6 @@ config WHICH
*/ */
#include "toys.h" #include "toys.h"
#define OPT_a 1
// Find an exectuable file either at a path with a slash in it (absolute or // Find an exectuable file either at a path with a slash in it (absolute or
// relative to current directory), or in $PATH. Returns absolute path to file, // relative to current directory), or in $PATH. Returns absolute path to file,
// or NULL if not found. // or NULL if not found.
@ -54,7 +52,7 @@ static int which_in_path(char *filename)
if (!access(list->str, X_OK)) { if (!access(list->str, X_OK)) {
puts(list->str); puts(list->str);
// If we should stop at one match, do so // If we should stop at one match, do so
if (toys.optflags & OPT_a) { if (!toys.optflags) {
llist_free(list, NULL); llist_free(list, NULL);
break; break;
} }
@ -67,10 +65,7 @@ static int which_in_path(char *filename)
void which_main(void) void which_main(void)
{ {
if (!*toys.optargs) toys.exitval++; int i;
else { for (i=0; toys.optargs[i]; i++)
int i; toys.exitval |= which_in_path(toys.optargs[i]);
for (i=0; toys.optargs[i]; i++)
toys.exitval |= which_in_path(toys.optargs[i]);
}
} }