title: C, runtime test if executable exists in PATH
body: <p>I am currently writing an application in C, targetting BSD and Linux systems with a hope to being generall portable. This program a runtime dependency, in this case mplayer.</p>
  
  <p>As it stands I am using <code>execlp()</code> to start mplayer. I am checking the error code of the execlp call and I am testing for <code>EACCESS</code>, so I know when I attempt to run mplayer if it exists or not.</p>
  
  <p>Because of the way my program works, mplayer is a required dependency but may not be used for some time after my program starts. As a user experience it is poor for the program to have been running for some time before failing due to mplayer being missing. So I would like to test for mplayer existing as my program starts up. Probably delivering an error message if mplayer is not available.</p>
  
  <p>Now I understand there is a race condition here so my current handling of an <code>EACCESS</code> error will have to stay. We could find a situation where a user starts my program running, then uninstalls mplayer. This is accepted.</p>
  
  <p>My initial thought was to call <code>execlp()</code> early on in execution and however this results in mplayer visibly starting. To be honest I'd prefer not to be starting mplayer, just testing if I "could" start it (eg. does a file exist called mplayer somewhere in my path and is it executable).</p>
  
  <p>A second thought was then to run those precise steps, looking through the path and testing if the matching file is executable. I've not yet coded this for two reasons. The first reason, to be sure execlp is finding the same thing I have found I would have to pass the discovered pathname to execlp, bypassing the builtin PATH searching mechanism. The other reason is simply I feel I'm missing an obvious trick.</p>
  
  <p>Is there a function I should be using to do the search for an executable? Or do I really need to just get on and code it the long way.</p>