timevar: import from Bison.
[gnulib.git] / lib / findprog.c
blob7316418a9eedb5357fc8f5d5fd95ee9f4201ce45
1 /* Locating a program in PATH.
2 Copyright (C) 2001-2004, 2006-2018 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #include <config.h>
21 /* Specification. */
22 #include "findprog.h"
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 /* Avoid collision between findprog.c and findprog-lgpl.c. */
30 #if IN_FINDPROG_LGPL || ! GNULIB_FINDPROG_LGPL
32 #if !IN_FINDPROG_LGPL
33 # include "xalloc.h"
34 #endif
35 #include "concat-filename.h"
38 const char *
39 find_in_path (const char *progname)
41 #if defined _WIN32 || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
42 /* Native Windows, Cygwin, OS/2, DOS */
43 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are
44 too complicated. Leave it to the OS. */
45 return progname;
46 #else
47 /* Unix */
48 char *path;
49 char *path_rest;
50 char *cp;
52 if (strchr (progname, '/') != NULL)
53 /* If progname contains a slash, it is either absolute or relative to
54 the current directory. PATH is not used. */
55 return progname;
57 path = getenv ("PATH");
58 if (path == NULL || *path == '\0')
59 /* If PATH is not set, the default search path is implementation
60 dependent. */
61 return progname;
63 /* Make a copy, to prepare for destructive modifications. */
64 # if !IN_FINDPROG_LGPL
65 path = xstrdup (path);
66 # else
67 path = strdup (path);
68 if (path == NULL)
69 /* Out of memory. */
70 return progname;
71 # endif
72 for (path_rest = path; ; path_rest = cp + 1)
74 const char *dir;
75 bool last;
76 char *progpathname;
78 /* Extract next directory in PATH. */
79 dir = path_rest;
80 for (cp = path_rest; *cp != '\0' && *cp != ':'; cp++)
82 last = (*cp == '\0');
83 *cp = '\0';
85 /* Empty PATH components designate the current directory. */
86 if (dir == cp)
87 dir = ".";
89 /* Concatenate dir and progname. */
90 # if !IN_FINDPROG_LGPL
91 progpathname = xconcatenated_filename (dir, progname, NULL);
92 # else
93 progpathname = concatenated_filename (dir, progname, NULL);
94 if (progpathname == NULL)
96 /* Out of memory. */
97 free (path);
98 return progname;
100 # endif
102 /* On systems which have the eaccess() system call, let's use it.
103 On other systems, let's hope that this program is not installed
104 setuid or setgid, so that it is ok to call access() despite its
105 design flaw. */
106 if (eaccess (progpathname, X_OK) == 0)
108 /* Found! */
109 if (strcmp (progpathname, progname) == 0)
111 free (progpathname);
113 /* Add the "./" prefix for real, that xconcatenated_filename()
114 optimized away. This avoids a second PATH search when the
115 caller uses execlp/execvp. */
116 progpathname = XNMALLOC (2 + strlen (progname) + 1, char);
117 progpathname[0] = '.';
118 progpathname[1] = '/';
119 memcpy (progpathname + 2, progname, strlen (progname) + 1);
122 free (path);
123 return progpathname;
126 free (progpathname);
128 if (last)
129 break;
132 /* Not found in PATH. An error will be signalled at the first call. */
133 free (path);
134 return progname;
135 #endif
138 #endif