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 */
31 #include <sys/types.h>
36 #include "xvasprintf.h"
38 #include "manconfig.h"
39 #include "pathsearch.h"
41 static bool pathsearch (const char *name, const mode_t bits)
44 char *path = getenv ("PATH");
54 if (strchr (name, '/')) {
55 /* Qualified name; look directly. */
56 if (stat (name, &st) == -1)
58 if (S_ISREG (st.st_mode) && (st.st_mode & bits))
63 pathtok = path = xstrdup (path);
65 /* Unqualified name; iterate over $PATH looking for it. */
66 for (element = strsep (&pathtok, ":"); element;
67 element = strsep (&pathtok, ":")) {
76 filename = xasprintf ("%s/%s", element, name);
77 if (stat (filename, &st) == -1) {
84 if (S_ISREG (st.st_mode) && (st.st_mode & bits)) {
95 bool pathsearch_executable (const char *name)
97 return pathsearch (name, 0111);
100 bool directory_on_path (const char *dir)
103 char *path = getenv ("PATH");
112 pathtok = path = xstrdup (path);
114 for (element = strsep (&pathtok, ":"); element;
115 element = strsep (&pathtok, ":")) {
122 if (STREQ (element, dir)) {