Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically check if executable exists without running it or using `which`

Tags:

haskell

We can run a process foo with help of System.Process.proc as follows:

createProcess $ proc "foo" []

However we do not know ahead of time whether foo can be found. We could run it and catch an exception but that's poor. We could also first call out to which but that also sucks. In the process library we find RawCommand which as of today says

The FilePath argument names the executable, and is interpreted according to the platform's standard policy for searching for executables. Specifically:

  • on Unix systems the execvp(3) semantics is used, where if the executable filename does not contain a slash (/) then the PATH environment variable is searched for the executable.

  • on Windows systems the Win32 CreateProcess semantics is used. Briefly: if the filename does not contain a path, then the directory containing the parent executable is searched, followed by the current directory, then some standard locations, and finally the current PATH. An .exe extension is added if the filename does not already have an extension. For full details see the documentation for the Windows SearchPath API.

This is precisely what I would like to do but there doesn't seem to be any exposed functionality that does it. Am I just missing it, is there a more canonical/better way or do I have to dig through process internals? The solution should be portable (Windows/Linux).

like image 802
Mateusz Kowalczyk Avatar asked Oct 30 '17 17:10

Mateusz Kowalczyk


1 Answers

Looks like you want findExecutable function from here.

Prelude System.Directory> findExecutable "gcc"
Just "/usr/bin/gcc"
like image 89
Yuras Avatar answered Sep 24 '22 02:09

Yuras