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
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #endif
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 /* Specification. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "findprog.h"
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <stdbool.h>
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <stdlib.h>
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <string.h>
6751
1b0092424a44 Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
30 #include <unistd.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 #include "xalloc.h"
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #include "pathname.h"
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 const char *
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 find_in_path (const char *progname)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 too complicated. Leave it to the OS. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #else
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 /* Unix */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 char *path;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 char *dir;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 char *cp;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 if (strchr (progname, '/') != NULL)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 /* If progname contains a slash, it is either absolute or relative to
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 the current directory. PATH is not used. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 path = getenv ("PATH");
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 if (path == NULL || *path == '\0')
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 /* If PATH is not set, the default search path is implementation
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 dependent. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 return progname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 /* Make a copy, to prepare for destructive modifications. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 path = xstrdup (path);
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 for (dir = path; ; dir = cp + 1)
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 {
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 bool last;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 char *progpathname;
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 /* Extract next directory in PATH. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 for (cp = dir; *cp != '\0' && *cp != ':'; cp++)
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. */
f292c9f75051 New module 'findprog'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 progpathname = xmalloc (2 + strlen (progname) + 1);
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 }