changeset 4294:f292c9f75051

New module 'findprog'.
author Bruno Haible <bruno@clisp.org>
date Thu, 10 Apr 2003 20:22:51 +0000
parents b0c93dea1259
children f366a5d5b79e
files ChangeLog MODULES.html.sh lib/ChangeLog lib/findprog.c lib/findprog.h m4/ChangeLog m4/eaccess.m4 m4/findprog.m4 modules/findprog
diffstat 9 files changed, 217 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sat Apr 05 08:16:43 2003 +0000
+++ b/ChangeLog	Thu Apr 10 20:22:51 2003 +0000
@@ -1,3 +1,8 @@
+2003-04-10  Bruno Haible  <bruno@clisp.org>
+
+	* modules/findprog: New file.
+	* MODULES.html.sh (func_all_modules): Add it.
+
 2003-04-04  Bruno Haible  <bruno@clisp.org>
 
 	* modules/linebreak: New file.
--- a/MODULES.html.sh	Sat Apr 05 08:16:43 2003 +0000
+++ b/MODULES.html.sh	Thu Apr 10 20:22:51 2003 +0000
@@ -1837,13 +1837,13 @@
   func_wrap H3
   func_echo "$element"
 
-  #func_begin_table
-  #func_module findprog
+  func_begin_table
+  func_module findprog
   #func_module wait-process
   #func_module execute
   #func_module pipe
   #func_module sh-quote
-  #func_end_table
+  func_end_table
 
   element="Java"
   element=`printf "%s" "$element" | sed -e "$sed_lt" -e "$sed_gt"`
--- a/lib/ChangeLog	Sat Apr 05 08:16:43 2003 +0000
+++ b/lib/ChangeLog	Thu Apr 10 20:22:51 2003 +0000
@@ -1,3 +1,8 @@
+2003-04-10  Bruno Haible  <bruno@clisp.org>
+
+	* findprog.h: New file, from GNU gettext.
+	* findprog.c: New file, from GNU gettext.
+
 2003-04-05  Jim Meyering  <jim@meyering.net>
 
 	Merge changes from Coreutils.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/findprog.c	Thu Apr 10 20:22:51 2003 +0000
@@ -0,0 +1,118 @@
+/* Locating a program in PATH.
+   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
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+/* Specification.  */
+#include "findprog.h"
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include "xalloc.h"
+#include "pathname.h"
+
+
+const char *
+find_in_path (const char *progname)
+{
+#if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
+  /* Win32, OS/2, DOS */
+  /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are
+     too complicated.  Leave it to the OS.  */
+  return progname;
+#else
+  /* Unix */
+  char *path;
+  char *dir;
+  char *cp;
+
+  if (strchr (progname, '/') != NULL)
+    /* If progname contains a slash, it is either absolute or relative to
+       the current directory.  PATH is not used.  */
+    return progname;
+
+  path = getenv ("PATH");
+  if (path == NULL || *path == '\0')
+    /* If PATH is not set, the default search path is implementation
+       dependent.  */
+    return progname;
+
+  /* Make a copy, to prepare for destructive modifications.  */
+  path = xstrdup (path);
+  for (dir = path; ; dir = cp + 1)
+    {
+      bool last;
+      char *progpathname;
+
+      /* Extract next directory in PATH.  */
+      for (cp = dir; *cp != '\0' && *cp != ':'; cp++)
+	;
+      last = (*cp == '\0');
+      *cp = '\0';
+
+      /* Empty PATH components designate the current directory.  */
+      if (dir == cp)
+	dir = ".";
+
+      /* Concatenate dir and progname.  */
+      progpathname = concatenated_pathname (dir, progname, NULL);
+
+      /* 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.  This avoids a second PATH search when the
+		 caller uses execlp/execvp.  */
+	      progpathname = xmalloc (2 + strlen (progname) + 1);
+	      progpathname[0] = '.';
+	      progpathname[1] = '/';
+	      memcpy (progpathname + 2, progname, strlen (progname) + 1);
+	    }
+
+	  free (path);
+	  return progpathname;
+	}
+
+      free (progpathname);
+
+      if (last)
+	break;
+    }
+
+  /* Not found in PATH.  An error will be signalled at the first call.  */
+  free (path);
+  return progname;
+#endif
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/findprog.h	Thu Apr 10 20:22:51 2003 +0000
@@ -0,0 +1,27 @@
+/* Locating a program in PATH.
+   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
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+/* Look up a program in the PATH.
+   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.
+   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);
--- a/m4/ChangeLog	Sat Apr 05 08:16:43 2003 +0000
+++ b/m4/ChangeLog	Thu Apr 10 20:22:51 2003 +0000
@@ -1,3 +1,8 @@
+2003-04-10  Bruno Haible  <bruno@clisp.org>
+
+	* findprog.m4: New file.
+	* eaccess.m4: New file.
+
 2003-04-04  Bruno Haible  <bruno@clisp.org>
 
 	* linebreak.m4: New file.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m4/eaccess.m4	Thu Apr 10 20:22:51 2003 +0000
@@ -0,0 +1,14 @@
+# eaccess.m4 serial 1
+dnl Copyright (C) 2003 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License.  As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_FUNC_EACCESS],
+[
+  AC_CHECK_FUNC(eaccess, ,
+    [AC_DEFINE(eaccess, access,
+       [Define as 'access' if you don't have the eaccess() function.])])
+])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m4/findprog.m4	Thu Apr 10 20:22:51 2003 +0000
@@ -0,0 +1,14 @@
+# findprog.m4 serial 1
+dnl Copyright (C) 2003 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License.  As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_FINDPROG],
+[
+  dnl Prerequisites of lib/findprog.c.
+  AC_CHECK_HEADERS_ONCE(unistd.h)
+  AC_REQUIRE([gl_FUNC_EACCESS])
+])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/findprog	Thu Apr 10 20:22:51 2003 +0000
@@ -0,0 +1,26 @@
+Description:
+Locating a program in PATH.
+
+Files:
+lib/findprog.h
+lib/findprog.c
+m4/findprog.m4
+m4/eaccess.m4
+
+Depends-on:
+stdbool
+xalloc
+pathname
+
+configure.ac:
+gl_FINDPROG
+
+Makefile.am:
+lib_SOURCES += findprog.h findprog.c
+
+Include:
+"findprog.h"
+
+Maintainer:
+Bruno Haible
+