Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / findprog.c
blob469acfd2e4d182fa0708b89af0cfb4ca1010f48e
1 /* Locating a program in PATH.
2 Copyright (C) 2001-2004 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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.
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.
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,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 /* Specification. */
25 #include "findprog.h"
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #include "xalloc.h"
36 #include "pathname.h"
39 const char *
40 find_in_path (const char *progname)
42 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
43 /* Win32, Cygwin, OS/2, DOS */
44 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are
45 too complicated. Leave it to the OS. */
46 return progname;
47 #else
48 /* Unix */
49 char *path;
50 char *dir;
51 char *cp;
53 if (strchr (progname, '/') != NULL)
54 /* If progname contains a slash, it is either absolute or relative to
55 the current directory. PATH is not used. */
56 return progname;
58 path = getenv ("PATH");
59 if (path == NULL || *path == '\0')
60 /* If PATH is not set, the default search path is implementation
61 dependent. */
62 return progname;
64 /* Make a copy, to prepare for destructive modifications. */
65 path = xstrdup (path);
66 for (dir = path; ; dir = cp + 1)
68 bool last;
69 char *progpathname;
71 /* Extract next directory in PATH. */
72 for (cp = dir; *cp != '\0' && *cp != ':'; cp++)
74 last = (*cp == '\0');
75 *cp = '\0';
77 /* Empty PATH components designate the current directory. */
78 if (dir == cp)
79 dir = ".";
81 /* Concatenate dir and progname. */
82 progpathname = concatenated_pathname (dir, progname, NULL);
84 /* On systems which have the eaccess() system call, let's use it.
85 On other systems, let's hope that this program is not installed
86 setuid or setgid, so that it is ok to call access() despite its
87 design flaw. */
88 if (eaccess (progpathname, X_OK) == 0)
90 /* Found! */
91 if (strcmp (progpathname, progname) == 0)
93 free (progpathname);
95 /* Add the "./" prefix for real, that concatenated_pathname()
96 optimized away. This avoids a second PATH search when the
97 caller uses execlp/execvp. */
98 progpathname = xmalloc (2 + strlen (progname) + 1);
99 progpathname[0] = '.';
100 progpathname[1] = '/';
101 memcpy (progpathname + 2, progname, strlen (progname) + 1);
104 free (path);
105 return progpathname;
108 free (progpathname);
110 if (last)
111 break;
114 /* Not found in PATH. An error will be signalled at the first call. */
115 free (path);
116 return progname;
117 #endif