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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6751
diff changeset
19 #include <config.h>
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include "findprog.h"
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stdbool.h>
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdlib.h>
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <string.h>
6751
1b0092424a44 Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
27 #include <unistd.h>
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 const char *
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 find_in_path (const char *progname)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 too complicated. Leave it to the OS. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #else
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 /* Unix */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 char *path;
7527
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
44 char *path_rest;
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 char *cp;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 if (strchr (progname, '/') != NULL)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 /* If progname contains a slash, it is either absolute or relative to
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 the current directory. PATH is not used. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 path = getenv ("PATH");
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 if (path == NULL || *path == '\0')
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 /* If PATH is not set, the default search path is implementation
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 dependent. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 /* Make a copy, to prepare for destructive modifications. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 path = xstrdup (path);
7527
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
60 for (path_rest = path; ; path_rest = cp + 1)
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 {
7527
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
62 const char *dir;
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 bool last;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 char *progpathname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 /* Extract next directory in PATH. */
7527
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
67 dir = path_rest;
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
68 for (cp = path_rest; *cp != '\0' && *cp != ':'; cp++)
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 ;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 last = (*cp == '\0');
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 *cp = '\0';
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 /* Empty PATH components designate the current directory. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 if (dir == cp)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 dir = ".";
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 /* On systems which have the eaccess() system call, let's use it.
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 On other systems, let's hope that this program is not installed
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 setuid or setgid, so that it is ok to call access() despite its
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 design flaw. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 if (eaccess (progpathname, X_OK) == 0)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 {
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 /* Found! */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 if (strcmp (progpathname, progname) == 0)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 {
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 free (progpathname);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 optimized away. This avoids a second PATH search when the
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 progpathname[0] = '.';
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 progpathname[1] = '/';
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 memcpy (progpathname + 2, progname, strlen (progname) + 1);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 }
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 free (path);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 return progpathname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 }
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 free (progpathname);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 if (last)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 break;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 }
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 /* Not found in PATH. An error will be signalled at the first call. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 free (path);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 #endif
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 }