POSIX function to search PATH for an executable?

c, path, posix, unix

Solution

Is there a POSIX function that searches PATH for an executable according to the POSIX spec's description of the PATH environment variable and returns the absolute path to the executable?

No.

If not, is there a simple, safe, standard, and reliable way to search PATH?

Yes and no. Yes, there is a standard for the format of `PATH`, from which the correctness/reliability of implementations follow.

No, there is no standard function that does this. Copying code is your best bet.

If `PATH` is unset or is set to null, the path search is implementation-defined.

That means you can't always portably replicate what `execvp` does, but searching `/bin:/usr/bin` is a pretty safe bet. Alternatively, just raise an error in this case.

(I admit that it would have been nice if POSIX had had this function, but it just isn't there.)

Problem

Is there a POSIX function that searches `PATH` for an executable according to the POSIX spec's description of the `PATH` environment variable and returns the absolute path to the executable? If not, is there a simple, safe, standard, and reliable way to search `PATH`? Edit: glibc's `execvpe()` function does its own `PATH` search, so I'm guessing there isn't a specific `PATH` search function defined by the standard. Edit 2: I don't want to copy someone else's code or implement the `PATH` search myself for a few reasons: - DRY - More code I have to test and maintain - Possible licensing issues - POSIX says, "If `PATH` is unset or is set to null, the path search is implementation-defined." I would like the behavior in these cases to be consistent with whatever the system does, but I can't do this if there's not a standard function I can call.

Original source