Imported Upstream version 2.8.4
[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 <string.h>
28 #include <stdlib.h>
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #include "xgetcwd.h"
35 #include "xvasprintf.h"
36
37 #include "manconfig.h"
38 #include "pathsearch.h"
39
40 static int pathsearch (const char *name, const mode_t bits)
41 {
42         char *cwd = NULL;
43         char *path = getenv ("PATH");
44         char *pathtok;
45         const char *element;
46         struct stat st;
47         int ret = 0;
48
49         if (!path)
50                 /* Eh? Oh well. */
51                 return 0;
52
53         if (strchr (name, '/')) {
54                 /* Qualified name; look directly. */
55                 if (stat (name, &st) == -1)
56                         return 0;
57                 if (S_ISREG (st.st_mode) && (st.st_mode & bits))
58                         return 1;
59                 return 0;
60         }
61
62         pathtok = path = xstrdup (path);
63
64         /* Unqualified name; iterate over $PATH looking for it. */
65         for (element = strsep (&pathtok, ":"); element;
66              element = strsep (&pathtok, ":")) {
67                 char *filename;
68
69                 if (!*element) {
70                         if (!cwd)
71                                 cwd = xgetcwd ();
72                         element = cwd;
73                 }
74
75                 filename = xasprintf ("%s/%s", element, name);
76                 if (stat (filename, &st) == -1) {
77                         free (filename);
78                         continue;
79                 }
80
81                 free (filename);
82
83                 if (S_ISREG (st.st_mode) && (st.st_mode & bits)) {
84                         ret = 1;
85                         break;
86                 }
87         }
88
89         free (path);
90         free (cwd);
91         return ret;
92 }
93
94 int pathsearch_executable (const char *name)
95 {
96         return pathsearch (name, 0111);
97 }
98
99 int directory_on_path (const char *dir)
100 {
101         char *cwd = NULL;
102         char *path = getenv ("PATH");
103         char *pathtok;
104         const char *element;
105         int ret = 0;
106
107         if (!path)
108                 /* Eh? Oh well. */
109                 return 0;
110
111         pathtok = path = xstrdup (path);
112
113         for (element = strsep (&pathtok, ":"); element;
114              element = strsep (&pathtok, ":")) {
115                 if (!*element) {
116                         if (!cwd)
117                                 cwd = xgetcwd ();
118                         element = cwd;
119                 }
120
121                 if (STREQ (element, dir)) {
122                         ret = 1;
123                         break;
124                 }
125         }
126
127         free (path);
128         free (cwd);
129         return ret;
130 }