Mercurial > hg > octave-kai > gnulib-hg
annotate lib/findprog.c @ 9309:bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 07 Oct 2007 19:14:58 +0200 |
parents | 1e16373508e1 |
children | f0a681493fa6 |
rev | line source |
---|---|
4294 | 1 /* Locating a program in PATH. |
8251
1e16373508e1
Rename module 'pathname' to 'filename'.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
2 Copyright (C) 2001-2004, 2006-2007 Free Software Foundation, Inc. |
4294 | 3 Written by Bruno Haible <haible@clisp.cons.org>, 2001. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8251
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
4294 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8251
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8251
diff
changeset
|
8 (at your option) any later version. |
4294 | 9 |
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. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8251
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4294 | 17 |
18 | |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
19 #include <config.h> |
4294 | 20 |
21 /* Specification. */ | |
22 #include "findprog.h" | |
23 | |
24 #include <stdbool.h> | |
25 #include <stdlib.h> | |
26 #include <string.h> | |
6751
1b0092424a44
Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
27 #include <unistd.h> |
4294 | 28 |
29 #include "xalloc.h" | |
8251
1e16373508e1
Rename module 'pathname' to 'filename'.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
30 #include "filename.h" |
4294 | 31 |
32 | |
33 const char * | |
34 find_in_path (const char *progname) | |
35 { | |
5053
eebff8c51a9b
Treat Cygwin like Windows regarding pathname syntax.
Bruno Haible <bruno@clisp.org>
parents:
4294
diff
changeset
|
36 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__ |
eebff8c51a9b
Treat Cygwin like Windows regarding pathname syntax.
Bruno Haible <bruno@clisp.org>
parents:
4294
diff
changeset
|
37 /* Win32, Cygwin, OS/2, DOS */ |
4294 | 38 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are |
39 too complicated. Leave it to the OS. */ | |
40 return progname; | |
41 #else | |
42 /* Unix */ | |
43 char *path; | |
7527 | 44 char *path_rest; |
4294 | 45 char *cp; |
46 | |
47 if (strchr (progname, '/') != NULL) | |
48 /* If progname contains a slash, it is either absolute or relative to | |
49 the current directory. PATH is not used. */ | |
50 return progname; | |
51 | |
52 path = getenv ("PATH"); | |
53 if (path == NULL || *path == '\0') | |
54 /* If PATH is not set, the default search path is implementation | |
55 dependent. */ | |
56 return progname; | |
57 | |
58 /* Make a copy, to prepare for destructive modifications. */ | |
59 path = xstrdup (path); | |
7527 | 60 for (path_rest = path; ; path_rest = cp + 1) |
4294 | 61 { |
7527 | 62 const char *dir; |
4294 | 63 bool last; |
64 char *progpathname; | |
65 | |
66 /* Extract next directory in PATH. */ | |
7527 | 67 dir = path_rest; |
68 for (cp = path_rest; *cp != '\0' && *cp != ':'; cp++) | |
4294 | 69 ; |
70 last = (*cp == '\0'); | |
71 *cp = '\0'; | |
72 | |
73 /* Empty PATH components designate the current directory. */ | |
74 if (dir == cp) | |
75 dir = "."; | |
76 | |
77 /* Concatenate dir and progname. */ | |
8251
1e16373508e1
Rename module 'pathname' to 'filename'.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
78 progpathname = concatenated_filename (dir, progname, NULL); |
4294 | 79 |
80 /* On systems which have the eaccess() system call, let's use it. | |
81 On other systems, let's hope that this program is not installed | |
82 setuid or setgid, so that it is ok to call access() despite its | |
83 design flaw. */ | |
84 if (eaccess (progpathname, X_OK) == 0) | |
85 { | |
86 /* Found! */ | |
87 if (strcmp (progpathname, progname) == 0) | |
88 { | |
89 free (progpathname); | |
90 | |
8251
1e16373508e1
Rename module 'pathname' to 'filename'.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
91 /* Add the "./" prefix for real, that concatenated_filename() |
4294 | 92 optimized away. This avoids a second PATH search when the |
93 caller uses execlp/execvp. */ | |
7603
23f14c284219
Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents:
7586
diff
changeset
|
94 progpathname = XNMALLOC (2 + strlen (progname) + 1, char); |
4294 | 95 progpathname[0] = '.'; |
96 progpathname[1] = '/'; | |
97 memcpy (progpathname + 2, progname, strlen (progname) + 1); | |
98 } | |
99 | |
100 free (path); | |
101 return progpathname; | |
102 } | |
103 | |
104 free (progpathname); | |
105 | |
106 if (last) | |
107 break; | |
108 } | |
109 | |
110 /* Not found in PATH. An error will be signalled at the first call. */ | |
111 free (path); | |
112 return progname; | |
113 #endif | |
114 } |