New module 'findprog'.
authorBruno Haible <bruno@clisp.org>
Thu, 10 Apr 2003 20:22:51 +0000 (20:22 +0000)
committerBruno Haible <bruno@clisp.org>
Thu, 10 Apr 2003 20:22:51 +0000 (20:22 +0000)
ChangeLog
MODULES.html.sh
lib/ChangeLog
lib/findprog.c [new file with mode: 0644]
lib/findprog.h [new file with mode: 0644]
m4/ChangeLog
m4/eaccess.m4 [new file with mode: 0644]
m4/findprog.m4 [new file with mode: 0644]
modules/findprog [new file with mode: 0644]

index 677dc4f7443ad16d2ef982b9192b0492d894219b..1ec9ed3183f3488407aa56b4a692f9b71f11136a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
index 69595190a0e792fbd9b36670745967c1e44e6856..c082bc60d40593d120f2e946099715364e4450f1 100755 (executable)
@@ -1837,13 +1837,13 @@ func_all_modules ()
   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"`
index c498888172e79227a807a338fe60905f4cc7a617..14d8d95905876c262ded8014c0d7631cc92cf00e 100644 (file)
@@ -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.
diff --git a/lib/findprog.c b/lib/findprog.c
new file mode 100644 (file)
index 0000000..48c3edf
--- /dev/null
@@ -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
+}
diff --git a/lib/findprog.h b/lib/findprog.h
new file mode 100644 (file)
index 0000000..81a95b6
--- /dev/null
@@ -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);
index 3bdc1a69c1bd77e29757b982c86713bc6723226c..4f4e4b03f02716168e62a9e43a27120ba4feb324 100644 (file)
@@ -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.
diff --git a/m4/eaccess.m4 b/m4/eaccess.m4
new file mode 100644 (file)
index 0000000..4c4111f
--- /dev/null
@@ -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.])])
+])
diff --git a/m4/findprog.m4 b/m4/findprog.m4
new file mode 100644 (file)
index 0000000..e2bc75c
--- /dev/null
@@ -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])
+])
diff --git a/modules/findprog b/modules/findprog
new file mode 100644 (file)
index 0000000..b91d00b
--- /dev/null
@@ -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
+