1 /* vi: set sw=4 ts=4: */
5 * Copyright 2006 Rob landley <rob@landley.net>
12 // Find an exectuable file either at a path with a slash in it (absolute or
13 // relative to current directory), or in $PATH. Returns absolute path to file,
14 // or NULL if not found.
16 static int which_in_path(char *filename)
18 struct string_list *list;
20 // If they gave us a path, don't worry about $PATH or -a
22 if (index(filename, '/')) {
23 // Confirm it has the executable bit set, and it's not a directory.
24 if (!access(filename, X_OK)) {
27 if (!stat(filename, &st) && S_ISREG(st.st_mode)) {
35 // Search $PATH for matches.
36 list = find_in_path(getenv("PATH"), filename);
41 if (!access(list->str, X_OK)) {
43 // If we should stop at one match, do so
44 if (toys.optflags & OPT_a) {
45 llist_free(list, NULL);
49 free(llist_pop(&list));
59 if (!*toys.optargs) rc++;
62 for (i=0; toys.optargs[i]; i++) rc |= which_in_path(toys.optargs[i]);
64 // if (CFG_TOYS_FREE) free(argv);