Cygwin's execlp() does not work with an empty $PATH element

Lavrentiev, Anton (NIH/NLM/NCBI) [C] lavr@ncbi.nlm.nih.gov
Sun Jun 26 15:09:34 GMT 2022


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



More information about the Cygwin mailing list