public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Cygwin's execlp() does not work with an empty $PATH element
@ 2022-06-26 15:09 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  2022-06-26 16:26 ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  2022-06-27 11:16 ` Takashi Yano
  0 siblings, 2 replies; 4+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2022-06-26 15:09 UTC (permalink / raw)
  To: 'cygwin@cygwin.com'

Hi all,

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).

Anton Lavrentiev
Contractor NIH/NLM/NCBI


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-27 12:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-26 15:09 Cygwin's execlp() does not work with an empty $PATH element Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2022-06-26 16:26 ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2022-06-27 11:16 ` Takashi Yano
2022-06-27 12:15   ` Takashi Yano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).