www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2022/06/27/07:18:02

X-Recipient: archive-cygwin AT delorie DOT com
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 50727386C58D
Authentication-Results: sourceware.org;
dmarc=fail (p=none dis=none) header.from=nifty.ne.jp
Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=nifty.ne.jp
DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-03.nifty.com 25RBGaVP012273
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.ne.jp;
s=dec2015msa; t=1656328597;
bh=EjgDt7xdM6v7duBqDV0JGeFwQifZ2/SGpElELYzBlV4=;
h=Date:From:To:Cc:Subject:In-Reply-To:References:From;
b=MpyoJ7hh8aRHWWzC8Mr5fbaMCr8XRz1YqLW8vbiddFbicUlK9fWlJ/UM7BifglQlq
0A+blmQWLjMRklWp0Zl2qTS/f6/pVYa1M3YYMZ+mZL0erfclQ4AqucAFZSeBUupm9a
OyfXGDrXGm+BnYtBNJFMRyRhJ/32QlKzEL1NrYBa3JEgmHTEs/UM1EWvhwZtNXBwls
3zlVk5Jd/gwc7yyZznNYhrtMDEBx6rkMaMeFdmRlsbvkhuLHWvnh7TjtcsYKJxX0Ar
0CyZpqKwq6/NUWJHOfn4+6DROp0lJqF7diBKSj9RUadnfkD8TgsBkjv90gKckvmom6
EiWuAQhjVxf7A==
X-Nifty-SrcIP: [119.150.44.95]
Date: Mon, 27 Jun 2022 20:16:36 +0900
From: Takashi Yano <takashi DOT yano AT nifty DOT ne DOT jp>
To: cygwin AT cygwin DOT com
Subject: Re: Cygwin's execlp() does not work with an empty $PATH element
Message-Id: <20220627201636.46f55975dc6485a7d3f5ee27@nifty.ne.jp>
In-Reply-To: <DM8PR09MB7095965E449CC03DEE772338A5B69@DM8PR09MB7095.namprd09.prod.outlook.com>
References: <DM8PR09MB7095965E449CC03DEE772338A5B69 AT DM8PR09MB7095 DOT namprd09 DOT prod DOT outlook DOT com>
X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
Mime-Version: 1.0
X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED,
DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A,
RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP,
T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on
server2.sourceware.org
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.29
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
Errors-To: cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com>

On Sun, 26 Jun 2022 15:09:34 +0000
"Lavrentiev, Anton \(NIH/NLM/NCBI\) \[C\] wrote:
> An empty PATH element (":xxx" or "xxx::xxx" or "xxx:") is to be considered as the current directory (from the very first days of Unix).
> 
> However, Cygwin does not seem to obey the rule.
> 
> Consider the following simple C program:
> 
> $ cat hello.c
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <unistd.h>
> 
> int main(int argc, const char* argv[])
> {
>     if (argc < 2) {
>         const char* prog = strrchr(argv[0], '/');
>         if (!prog++)
>            prog = argv[0];
>         execlp(prog, prog, "Hello", NULL);  // execute just by the program name
>         perror("exec");
>         return 1;
>     }
>     printf("%s\n", argv[1]);
>     return 0;
> }
> 
> Now compare the execution on Linux and Cygwin:
> 
> Linux:
> 
> $ gcc -Wall -o hello hello.c
> $ hello
> bash: hello: command not found
> $ ./hello
> exec: No such file or directory
> $ PATH=".:$PATH" ./hello
> Hello
> $ PATH=":$PATH" ./hello
> Hello
> $ PATH="${PATH}:" ./hello
> Hello
> 
> Cygwin:
> 
> $ gcc -Wall -o hello hello.c
> $ hello
> -bash: hello: command not found
> $ ./hello
> exec: No such file or directory
> $ PATH=".:$PATH" ./hello
> Hello
> $ PATH=":$PATH" ./hello
> exec: No such file or directory
> $ PATH="${PATH}:" ./hello
> exec: No such file or directory
> 
> As you can see, the execution failed when an empty PATH element was added on Cygwin
> (yet it was perfectly fine on Linux).

How about the following patch?

diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index e0f1247e1..8fe9d089f 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -118,8 +118,10 @@ find_exec (const char *name, path_conv& buf, const char *search,
      the name of an environment variable. */
   if (strchr (search, '/'))
     *stpncpy (tmp, search, NT_MAX_PATH - 1) = '\0';
-  else if (has_slash || isdrive (name) || !(path = getenv (search)) || !*path)
+  else if (has_slash || isdrive (name))
     goto errout;
+  else if (!(path = getenv (search)) || !*path)
+    strcpy (tmp, "."); /* Search the current directory when PATH is absent */
   else
     *stpncpy (tmp, path, NT_MAX_PATH - 1) = '\0';
 
@@ -134,7 +136,9 @@ find_exec (const char *name, path_conv& buf, const char *search,
 	 already tried that.  */
       if ((opt & FE_CWD) && (tmp_path[0] == '\0'
 			     || (tmp_path[0] == '.' && tmp_path[1] == '\0')))
-	continue;
+	goto next;
+      else if (tmp_path[0] == '\0') /* An empty path means the current dir. */
+	eotmp = stpcpy (tmp_path, ".");
 
       *eotmp++ = '/';
       stpcpy (eotmp, name);
@@ -146,7 +150,7 @@ find_exec (const char *name, path_conv& buf, const char *search,
       if ((suffix = perhaps_suffix (tmp_path, buf, err1, opt)) != NULL)
 	{
 	  if (buf.has_acls () && check_file_access (buf, X_OK, true))
-	    continue;
+	    goto next;
 	  /* Overwrite potential symlink target with original path.
 	     See comment preceeding this method. */
 	  buf.set_posix (tmp_path);
@@ -154,8 +158,13 @@ find_exec (const char *name, path_conv& buf, const char *search,
 	  goto out;
 	}
 
+next:
+      if (*path == '\0')
+	break;
+      if (*path == ':')
+	path++;
     }
-  while (*path && *++path);
+  while (true);
 
  errout:
   /* Couldn't find anything in the given path.

-- 
Takashi Yano <takashi DOT yano AT nifty DOT ne DOT jp>

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019