Mercurial > hg > octave-kai > gnulib-hg
annotate lib/findprog.c @ 7586:4a8b5467d8b2
Make it compile in C++ mode.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Tue, 31 Oct 2006 19:18:54 +0000 |
parents | c15258519e7c |
children | 23f14c284219 |
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 | |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6751
diff
changeset
|
20 #include <config.h> |
4294 | 21 |
22 /* Specification. */ | |
23 #include "findprog.h" | |
24 | |
25 #include <stdbool.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
6751
1b0092424a44
Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
28 #include <unistd.h> |
4294 | 29 |
30 #include "xalloc.h" | |
31 #include "pathname.h" | |
32 | |
33 | |
34 const char * | |
35 find_in_path (const char *progname) | |
36 { | |
5053
eebff8c51a9b
Treat Cygwin like Windows regarding pathname syntax.
Bruno Haible <bruno@clisp.org>
parents:
4294
diff
changeset
|
37 #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
|
38 /* Win32, Cygwin, OS/2, DOS */ |
4294 | 39 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are |
40 too complicated. Leave it to the OS. */ | |
41 return progname; | |
42 #else | |
43 /* Unix */ | |
44 char *path; | |
7527 | 45 char *path_rest; |
4294 | 46 char *cp; |
47 | |
48 if (strchr (progname, '/') != NULL) | |
49 /* If progname contains a slash, it is either absolute or relative to | |
50 the current directory. PATH is not used. */ | |
51 return progname; | |
52 | |
53 path = getenv ("PATH"); | |
54 if (path == NULL || *path == '\0') | |
55 /* If PATH is not set, the default search path is implementation | |
56 dependent. */ | |
57 return progname; | |
58 | |
59 /* Make a copy, to prepare for destructive modifications. */ | |
60 path = xstrdup (path); | |
7527 | 61 for (path_rest = path; ; path_rest = cp + 1) |
4294 | 62 { |
7527 | 63 const char *dir; |
4294 | 64 bool last; |
65 char *progpathname; | |
66 | |
67 /* Extract next directory in PATH. */ | |
7527 | 68 dir = path_rest; |
69 for (cp = path_rest; *cp != '\0' && *cp != ':'; cp++) | |
4294 | 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. */ | |
7586
4a8b5467d8b2
Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents:
7527
diff
changeset
|
95 progpathname = (char *) xmalloc (2 + strlen (progname) + 1); |
4294 | 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 } |