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
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.
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
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6751
diff changeset
20 #include <config.h>
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* Specification. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include "findprog.h"
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdbool.h>
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stdlib.h>
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <string.h>
6751
1b0092424a44 Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
28 #include <unistd.h>
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include "xalloc.h"
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include "pathname.h"
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 const char *
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 find_in_path (const char *progname)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 too complicated. Leave it to the OS. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #else
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 /* Unix */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 char *path;
7527
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
45 char *path_rest;
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 char *cp;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 if (strchr (progname, '/') != NULL)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 /* If progname contains a slash, it is either absolute or relative to
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 the current directory. PATH is not used. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 path = getenv ("PATH");
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 if (path == NULL || *path == '\0')
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 /* If PATH is not set, the default search path is implementation
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 dependent. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 /* Make a copy, to prepare for destructive modifications. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 path = xstrdup (path);
7527
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
61 for (path_rest = path; ; path_rest = cp + 1)
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 {
7527
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
63 const char *dir;
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 bool last;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 char *progpathname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 /* Extract next directory in PATH. */
7527
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
68 dir = path_rest;
c15258519e7c Avoid a gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
69 for (cp = path_rest; *cp != '\0' && *cp != ':'; cp++)
4294
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 ;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 last = (*cp == '\0');
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 *cp = '\0';
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 /* Empty PATH components designate the current directory. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 if (dir == cp)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 dir = ".";
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 /* Concatenate dir and progname. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 progpathname = concatenated_pathname (dir, progname, NULL);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 /* On systems which have the eaccess() system call, let's use it.
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 On other systems, let's hope that this program is not installed
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 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
84 design flaw. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 if (eaccess (progpathname, X_OK) == 0)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 {
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 /* Found! */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 if (strcmp (progpathname, progname) == 0)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 {
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 free (progpathname);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 /* Add the "./" prefix for real, that concatenated_pathname()
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 optimized away. This avoids a second PATH search when the
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 progpathname[0] = '.';
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 progpathname[1] = '/';
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 memcpy (progpathname + 2, progname, strlen (progname) + 1);
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 free (path);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 return progpathname;
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 free (progpathname);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 if (last)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 break;
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 /* 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
112 free (path);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 #endif
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 }