From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from conssluserg-03.nifty.com (conssluserg-03.nifty.com [210.131.2.82]) by sourceware.org (Postfix) with ESMTPS id 50727386C58D for ; Mon, 27 Jun 2022 11:17:19 +0000 (GMT) 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 Received: from HP-Z230 (ak044095.dynamic.ppp.asahi-net.or.jp [119.150.44.95]) (authenticated) by conssluserg-03.nifty.com with ESMTP id 25RBGaVP012273; Mon, 27 Jun 2022 20:16:37 +0900 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 To: cygwin@cygwin.com Subject: Re: Cygwin's execlp() does not work with an empty $PATH element Message-Id: <20220627201636.46f55975dc6485a7d3f5ee27@nifty.ne.jp> In-Reply-To: References: X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Jun 2022 11:17:23 -0000 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 > #include > #include > #include > > 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