]> Savannah Git Hosting - gnulib.git/commitdiff
execlp: New module.
authorBruno Haible <bruno@clisp.org>
Sat, 26 Dec 2020 13:42:38 +0000 (14:42 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 26 Dec 2020 13:42:38 +0000 (14:42 +0100)
* lib/execlp.c: New file.
* m4/execlp.m4: New file.
* modules/execlp: New file.
* doc/posix-functions/execlp.texi: Mention more Windows problems and the
new module.

ChangeLog
doc/posix-functions/execlp.texi
lib/execlp.c [new file with mode: 0644]
m4/execlp.m4 [new file with mode: 0644]
modules/execlp [new file with mode: 0644]

index e7a8228ce5fc196aa018c70b7dc38a252941bb2f..0bda9d19abcc7d77ccd65f8128480a3532abbc81 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2020-12-26  Bruno Haible  <bruno@clisp.org>
 
+       execlp: New module.
+       * lib/execlp.c: New file.
+       * m4/execlp.m4: New file.
+       * modules/execlp: New file.
+       * doc/posix-functions/execlp.texi: Mention more Windows problems and the
+       new module.
+
        execle: Add tests.
        * tests/test-execle-main.c: New file.
        * tests/test-execle.sh: New file.
index 1314c7580a1f045748dc2df84ac5403b8193e72c..89351194df67342a038fa7063a1345ed4800ad05 100644 (file)
@@ -4,10 +4,22 @@
 
 POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9699919799/functions/execlp.html}
 
-Gnulib module: ---
+Gnulib module: execlp
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+On Windows platforms (excluding Cygwin), this function does not pass
+command-line arguments correctly if they contain space, tab, backslash,
+or double-quote characters.
+@item
+On Windows platforms (excluding Cygwin), this function spawns an asynchronous
+child process and then exits the current process immediately.  As a
+consequence, the parent of the current process 1. may incorrectly proceed
+as if its child had exited, and 2. will never see the child's exit status.
+@item
+On Windows platforms (excluding Cygwin), the return type of this function is
+@code{intptr_t}, not @code{int}.
 @end itemize
 
 Portability problems not fixed by Gnulib:
@@ -15,8 +27,4 @@ Portability problems not fixed by Gnulib:
 @item
 On some platforms, a script without executable permission is still run:
 Cygwin 1.5.x.
-@item
-On Windows platforms (excluding Cygwin), this function operates by spawning
-and then by exiting the current process, which means the current
-process's parent may incorrectly proceed as if its child had exited.
 @end itemize
diff --git a/lib/execlp.c b/lib/execlp.c
new file mode 100644 (file)
index 0000000..cefa3ec
--- /dev/null
@@ -0,0 +1,82 @@
+/* execlp() function: Execute a program, replacing the current process.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   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 3 of the License, 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, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2020.  */
+
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   may optimize away the arg0 == NULL test below.  */
+#define _GL_ARG_NONNULL(params)
+
+#include <config.h>
+
+/* Specification.  */
+#include <unistd.h>
+
+#include <errno.h>
+#include <stdarg.h>
+
+#include "malloca.h"
+
+int
+execlp (const char *program, const char *arg0, ...)
+{
+  va_list args;
+
+  /* The callee is not expecting a NULL argv[0].  */
+  if (arg0 == NULL)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+
+  /* Count the number of arguments (including arg0 and the trailing NULL).  */
+  size_t count = 1;
+  va_start (args, arg0);
+  for (;;)
+    {
+      count++;
+      if (va_arg (args, const char *) == NULL)
+        break;
+    }
+  va_end (args);
+
+  /* Allocate the argument vector.  */
+  const char **argv = (const char **) malloca (count * sizeof (const char *));
+  if (argv == NULL)
+    {
+      errno = ENOMEM;
+      return -1;
+    }
+
+  /* Copy the arguments into the argument vector.  */
+  {
+    size_t i = 0;
+    argv[i++] = arg0;
+    va_start (args, arg0);
+    for (; i < count;)
+      argv[i++] = va_arg (args, const char *);
+    va_end (args);
+  }
+
+  /* Invoke execvp.  */
+  execvp (program, argv);
+
+  /* If execvp returned, it must have failed.  */
+  int saved_errno = errno;
+  freea (argv);
+  errno = saved_errno;
+  return -1;
+}
diff --git a/m4/execlp.m4 b/m4/execlp.m4
new file mode 100644 (file)
index 0000000..09df0c6
--- /dev/null
@@ -0,0 +1,15 @@
+# execlp.m4 serial 1
+dnl Copyright (C) 2020 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_EXECLP],
+[
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_REQUIRE([AC_CANONICAL_HOST])
+
+  case "$host_os" in
+    mingw*) REPLACE_EXECLP=1 ;;
+  esac
+])
diff --git a/modules/execlp b/modules/execlp
new file mode 100644 (file)
index 0000000..b31191c
--- /dev/null
@@ -0,0 +1,29 @@
+Description:
+execlp() function: Execute a program, replacing the current process.
+
+Files:
+lib/execlp.c
+m4/execlp.m4
+
+Depends-on:
+unistd
+execvp          [test $REPLACE_EXECLP = 1]
+malloca         [test $REPLACE_EXECLP = 1]
+
+configure.ac:
+gl_FUNC_EXECLP
+if test $REPLACE_EXECLP = 1; then
+  AC_LIBOBJ([execlp])
+fi
+gl_UNISTD_MODULE_INDICATOR([execlp])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all