Imported Upstream version 2.8.6
[platform/upstream/man-db.git] / lib / pathsearch.c
1 /*
2  * pathsearch.c: $PATH-searching functions.
3  *
4  * Copyright (C) 2004, 2007, 2008, 2009, 2011 Colin Watson.
5  *
6  * This file is part of man-db.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif /* HAVE_CONFIG_H */
26
27 #include <stdbool.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "xgetcwd.h"
36 #include "xvasprintf.h"
37
38 #include "manconfig.h"
39 #include "pathsearch.h"
40
41 static bool pathsearch (const char *name, const mode_t bits)
42 {
43         char *cwd = NULL;
44         char *path = getenv ("PATH");
45         char *pathtok;
46         const char *element;
47         struct stat st;
48         bool ret = false;
49
50         if (!path)
51                 /* Eh? Oh well. */
52                 return false;
53
54         if (strchr (name, '/')) {
55                 /* Qualified name; look directly. */
56                 if (stat (name, &st) == -1)
57                         return false;
58                 if (S_ISREG (st.st_mode) && (st.st_mode & bits))
59                         return true;
60                 return false;
61         }
62
63         pathtok = path = xstrdup (path);
64
65         /* Unqualified name; iterate over $PATH looking for it. */
66         for (element = strsep (&pathtok, ":"); element;
67              element = strsep (&pathtok, ":")) {
68                 char *filename;
69
70                 if (!*element) {
71                         if (!cwd)
72                                 cwd = xgetcwd ();
73                         element = cwd;
74                 }
75
76                 filename = xasprintf ("%s/%s", element, name);
77                 if (stat (filename, &st) == -1) {
78                         free (filename);
79                         continue;
80                 }
81
82                 free (filename);
83
84                 if (S_ISREG (st.st_mode) && (st.st_mode & bits)) {
85                         ret = true;
86                         break;
87                 }
88         }
89
90         free (path);
91         free (cwd);
92         return ret;
93 }
94
95 bool pathsearch_executable (const char *name)
96 {
97         return pathsearch (name, 0111);
98 }
99
100 bool directory_on_path (const char *dir)
101 {
102         char *cwd = NULL;
103         char *path = getenv ("PATH");
104         char *pathtok;
105         const char *element;
106         bool ret = false;
107
108         if (!path)
109                 /* Eh? Oh well. */
110                 return false;
111
112         pathtok = path = xstrdup (path);
113
114         for (element = strsep (&pathtok, ":"); element;
115              element = strsep (&pathtok, ":")) {
116                 if (!*element) {
117                         if (!cwd)
118                                 cwd = xgetcwd ();
119                         element = cwd;
120                 }
121
122                 if (STREQ (element, dir)) {
123                         ret = true;
124                         break;
125                 }
126         }
127
128         free (path);
129         free (cwd);
130         return ret;
131 }