Imported Upstream version 2.6.5
[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         if (cwd)
91                 free (cwd);
92         return ret;
93 }
94
95 int pathsearch_executable (const char *name)
96 {
97         return pathsearch (name, 0111);
98 }
99
100 int directory_on_path (const char *dir)
101 {
102         char *cwd = NULL;
103         char *path = getenv ("PATH");
104         char *pathtok;
105         const char *element;
106         int ret = 0;
107
108         if (!path)
109                 /* Eh? Oh well. */
110                 return 0;
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 = 1;
124                         break;
125                 }
126         }
127
128         free (path);
129         if (cwd)
130                 free (cwd);
131         return ret;
132 }