Programmatically check if an executable exists without running it or with `which`

We can start the foo process using System.Process.proc as follows:

 createProcess $ proc "foo" [] 

However, we do not know in advance whether foo can be found. We could run it and catch the exception, but this is bad. We might as well first turn to which , but that also sucks. In the process library, we find RawCommand , which today says

The FilePath argument names the executable file and is interpreted in accordance with the standard platform policy for searching executable files. In particular:

  • Unix systems use the semantics of execvp (3), where if the executable file name does not contain a slash (/), then the PATH environment variable searches for the executable file.

  • Windows systems use the Win32 CreateProcess semantics. In short: if the file name does not contain a path, then the directory containing the parent searches for the executable file, followed by the current directory, then some standard locations, and finally the current PATH. The .exe extension is added if the file name does not already have the extension. See the documentation for the Windows SearchPath API for more details.

This is exactly what I would like to do, but there does not seem to be any open functionality that does this. I just skipped it, is there a more canonical / better way, or do I need to break through the internal process ? The solution must be portable (Windows / Linux).

+7
haskell
source share
1 answer

It looks like you want the findExecutable function from here .

 Prelude System.Directory> findExecutable "gcc" Just "/usr/bin/gcc" 
+2
source share

All Articles