/* Locating a program in PATH.
- Copyright (C) 2001-2002 Free Software Foundation, Inc.
+ Copyright (C) 2001-2003 Free Software Foundation, Inc.
Written by Bruno Haible <haible@clisp.cons.org>, 2001.
This program is free software; you can redistribute it and/or modify
/* Specification. */
#include "findprog.h"
+#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
path = xstrdup (path);
for (dir = path; ; dir = cp + 1)
{
- int last;
+ bool last;
char *progpathname;
/* Extract next directory in PATH. */
- for (cp = dir; *cp != '\0' && *cp != ':'; cp++);
+ for (cp = dir; *cp != '\0' && *cp != ':'; cp++)
+ ;
last = (*cp == '\0');
*cp = '\0';
/* Concatenate dir and progname. */
progpathname = concatenated_pathname (dir, progname, NULL);
- /* This program is usually not installed setuid or setgid, therefore
- it is ok to call access() despite its design flaw. */
- if (access (progpathname, X_OK) == 0)
+ /* On systems which have the eaccess() system call, let's use it.
+ On other systems, let's hope that this program is not installed
+ setuid or setgid, so that it is ok to call access() despite its
+ design flaw. */
+ if (eaccess (progpathname, X_OK) == 0)
{
/* Found! */
if (strcmp (progpathname, progname) == 0)
free (progpathname);
/* Add the "./" prefix for real, that concatenated_pathname()
- optimized away. */
+ optimized away. This avoids a second PATH search when the
+ caller uses execlp/execvp. */
progpathname = xmalloc (2 + strlen (progname) + 1);
progpathname[0] = '.';
progpathname[1] = '/';
/* Locating a program in PATH.
- Copyright (C) 2001-2002 Free Software Foundation, Inc.
+ Copyright (C) 2001-2003 Free Software Foundation, Inc.
Written by Bruno Haible <haible@clisp.cons.org>, 2001.
This program is free software; you can redistribute it and/or modify
Attempt to determine the pathname that would be called by execlp/execvp
of PROGNAME. If successful, return a pathname containing a slash
(either absolute or relative to the current directory). Otherwise,
- return PROGNAME unmodified. */
+ return PROGNAME unmodified.
+ Because of the latter case, callers should use execlp/execvp, not
+ execl/execv on the returned pathname.
+ The returned string is freshly malloc()ed if it is != PROGNAME. */
extern const char *find_in_path (const char *progname);