Mercurial > hg > octave-kai > gnulib-hg
annotate lib/findprog.c @ 6836:e2c19cb657e7
Tweak for FreeBSD.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sat, 17 Jun 2006 20:02:16 +0000 (2006-06-17) |
parents | 1b0092424a44 |
children | 1c4ed7637c24 |
rev | line source |
---|---|
4294 | 1 /* Locating a program in PATH. |
6751
1b0092424a44
Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
2 Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc. |
4294 | 3 Written by Bruno Haible <haible@clisp.cons.org>, 2001. |
4 | |
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 2, or (at your option) | |
8 any later version. | |
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 | |
16 along with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5053
diff
changeset
|
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
4294 | 18 |
19 | |
20 #ifdef HAVE_CONFIG_H | |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
21 # include <config.h> |
4294 | 22 #endif |
23 | |
24 /* Specification. */ | |
25 #include "findprog.h" | |
26 | |
27 #include <stdbool.h> | |
28 #include <stdlib.h> | |
29 #include <string.h> | |
6751
1b0092424a44
Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
30 #include <unistd.h> |
4294 | 31 |
32 #include "xalloc.h" | |
33 #include "pathname.h" | |
34 | |
35 | |
36 const char * | |
37 find_in_path (const char *progname) | |
38 { | |
5053
eebff8c51a9b
Treat Cygwin like Windows regarding pathname syntax.
Bruno Haible <bruno@clisp.org>
parents:
4294
diff
changeset
|
39 #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
|
40 /* Win32, Cygwin, OS/2, DOS */ |
4294 | 41 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are |
42 too complicated. Leave it to the OS. */ | |
43 return progname; | |
44 #else | |
45 /* Unix */ | |
46 char *path; | |
47 char *dir; | |
48 char *cp; | |
49 | |
50 if (strchr (progname, '/') != NULL) | |
51 /* If progname contains a slash, it is either absolute or relative to | |
52 the current directory. PATH is not used. */ | |
53 return progname; | |
54 | |
55 path = getenv ("PATH"); | |
56 if (path == NULL || *path == '\0') | |
57 /* If PATH is not set, the default search path is implementation | |
58 dependent. */ | |
59 return progname; | |
60 | |
61 /* Make a copy, to prepare for destructive modifications. */ | |
62 path = xstrdup (path); | |
63 for (dir = path; ; dir = cp + 1) | |
64 { | |
65 bool last; | |
66 char *progpathname; | |
67 | |
68 /* Extract next directory in PATH. */ | |
69 for (cp = dir; *cp != '\0' && *cp != ':'; cp++) | |
70 ; | |
71 last = (*cp == '\0'); | |
72 *cp = '\0'; | |
73 | |
74 /* Empty PATH components designate the current directory. */ | |
75 if (dir == cp) | |
76 dir = "."; | |
77 | |
78 /* Concatenate dir and progname. */ | |
79 progpathname = concatenated_pathname (dir, progname, NULL); | |
80 | |
81 /* On systems which have the eaccess() system call, let's use it. | |
82 On other systems, let's hope that this program is not installed | |
83 setuid or setgid, so that it is ok to call access() despite its | |
84 design flaw. */ | |
85 if (eaccess (progpathname, X_OK) == 0) | |
86 { | |
87 /* Found! */ | |
88 if (strcmp (progpathname, progname) == 0) | |
89 { | |
90 free (progpathname); | |
91 | |
92 /* Add the "./" prefix for real, that concatenated_pathname() | |
93 optimized away. This avoids a second PATH search when the | |
94 caller uses execlp/execvp. */ | |
95 progpathname = xmalloc (2 + strlen (progname) + 1); | |
96 progpathname[0] = '.'; | |
97 progpathname[1] = '/'; | |
98 memcpy (progpathname + 2, progname, strlen (progname) + 1); | |
99 } | |
100 | |
101 free (path); | |
102 return progpathname; | |
103 } | |
104 | |
105 free (progpathname); | |
106 | |
107 if (last) | |
108 break; | |
109 } | |
110 | |
111 /* Not found in PATH. An error will be signalled at the first call. */ | |
112 free (path); | |
113 return progname; | |
114 #endif | |
115 } |