2 * pathsearch.c: $PATH-searching functions.
4 * Copyright (C) 2004, 2007, 2008, 2009, 2011 Colin Watson.
6 * This file is part of man-db.
8 * man-db is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * man-db is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with man-db; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25 #endif /* HAVE_CONFIG_H */
30 #include <sys/types.h>
35 #include "xvasprintf.h"
37 #include "manconfig.h"
38 #include "pathsearch.h"
40 static int pathsearch (const char *name, const mode_t bits)
43 char *path = getenv ("PATH");
53 if (strchr (name, '/')) {
54 /* Qualified name; look directly. */
55 if (stat (name, &st) == -1)
57 if (S_ISREG (st.st_mode) && (st.st_mode & bits))
62 pathtok = path = xstrdup (path);
64 /* Unqualified name; iterate over $PATH looking for it. */
65 for (element = strsep (&pathtok, ":"); element;
66 element = strsep (&pathtok, ":")) {
75 filename = xasprintf ("%s/%s", element, name);
76 if (stat (filename, &st) == -1) {
83 if (S_ISREG (st.st_mode) && (st.st_mode & bits)) {
94 int pathsearch_executable (const char *name)
96 return pathsearch (name, 0111);
99 int directory_on_path (const char *dir)
102 char *path = getenv ("PATH");
111 pathtok = path = xstrdup (path);
113 for (element = strsep (&pathtok, ":"); element;
114 element = strsep (&pathtok, ":")) {
121 if (STREQ (element, dir)) {