• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/timemachine/gettext-0.17/gettext-tools/gnulib-lib/
1/* Locating a program in PATH.
2   Copyright (C) 2001-2004, 2006-2007 Free Software Foundation, Inc.
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 3 of the License, or
8   (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18
19#include <config.h>
20
21/* Specification.  */
22#include "findprog.h"
23
24#include <stdbool.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28
29#include "xalloc.h"
30#include "filename.h"
31
32
33const char *
34find_in_path (const char *progname)
35{
36#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
37  /* Win32, Cygwin, OS/2, DOS */
38  /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are
39     too complicated.  Leave it to the OS.  */
40  return progname;
41#else
42  /* Unix */
43  char *path;
44  char *path_rest;
45  char *cp;
46
47  if (strchr (progname, '/') != NULL)
48    /* If progname contains a slash, it is either absolute or relative to
49       the current directory.  PATH is not used.  */
50    return progname;
51
52  path = getenv ("PATH");
53  if (path == NULL || *path == '\0')
54    /* If PATH is not set, the default search path is implementation
55       dependent.  */
56    return progname;
57
58  /* Make a copy, to prepare for destructive modifications.  */
59  path = xstrdup (path);
60  for (path_rest = path; ; path_rest = cp + 1)
61    {
62      const char *dir;
63      bool last;
64      char *progpathname;
65
66      /* Extract next directory in PATH.  */
67      dir = path_rest;
68      for (cp = path_rest; *cp != '\0' && *cp != ':'; cp++)
69	;
70      last = (*cp == '\0');
71      *cp = '\0';
72
73      /* Empty PATH components designate the current directory.  */
74      if (dir == cp)
75	dir = ".";
76
77      /* Concatenate dir and progname.  */
78      progpathname = concatenated_filename (dir, progname, NULL);
79
80      /* On systems which have the eaccess() system call, let's use it.
81	 On other systems, let's hope that this program is not installed
82	 setuid or setgid, so that it is ok to call access() despite its
83	 design flaw.  */
84      if (eaccess (progpathname, X_OK) == 0)
85	{
86	  /* Found!  */
87	  if (strcmp (progpathname, progname) == 0)
88	    {
89	      free (progpathname);
90
91	      /* Add the "./" prefix for real, that concatenated_filename()
92		 optimized away.  This avoids a second PATH search when the
93		 caller uses execlp/execvp.  */
94	      progpathname = XNMALLOC (2 + strlen (progname) + 1, char);
95	      progpathname[0] = '.';
96	      progpathname[1] = '/';
97	      memcpy (progpathname + 2, progname, strlen (progname) + 1);
98	    }
99
100	  free (path);
101	  return progpathname;
102	}
103
104      free (progpathname);
105
106      if (last)
107	break;
108    }
109
110  /* Not found in PATH.  An error will be signalled at the first call.  */
111  free (path);
112  return progname;
113#endif
114}
115