Thread: How can I search for an executable in the users $PATH

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310

    Question How can I search for an executable in the users $PATH

    Hi!

    I'm trying to find an executable in the users environment PATH, let's say I want to search for "foo.exe", how can I traverse getenv ("PATH");
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,745
    The which command from GnuWin32 does just this; I suggest looking at its source code.

    Which for Windows

    Tim S.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Joelito View Post
    Hi!

    I'm trying to find an executable in the users environment PATH, let's say I want to search for "foo.exe", how can I traverse getenv ("PATH");
    The easiest way, since (on Windows at least) the system will auto-search the path, is to try to open the file. If fopen() succeeds the file is there. The function can then close the file and return TRUE or 1. If fopen() fails your function should return FALSE or 0.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    You can try getting the path, splitting it up into its respective directories, appending the filename to each directory, and then test to see if the file exists? To split the path, you can use strtok, like so:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main( void )
    {
        char *path = getenv( "PATH" );
        char *dir;
    
        for ( dir = strtok( path, ":" ); dir; dir = strtok( NULL, ":" ) ) {
            puts( dir );
        }
    
        return 0;
    }
    
    /*
     Output:
    	$ gcc -ansi -pedantic -Wall main.c
    	$ ./a.out 
    	/usr/bin
    	/bin
    	/usr/sbin
    	/sbin
    	/usr/local/bin
    	/opt/X11/bin
    	/usr/texbin
    	/usr/X11/bin
    	/usr/X11R6/bin
    	$
    */

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Thanks kmess
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Any time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find out the path of the calling executable from a DLL
    By iShafayet in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2011, 04:41 PM
  2. Can a executable find the path to itself?
    By sulli_jj in forum C Programming
    Replies: 3
    Last Post: 06-23-2006, 12:27 PM
  3. Finding the path to your executable
    By jmd15 in forum Windows Programming
    Replies: 3
    Last Post: 07-19-2005, 09:32 AM
  4. Getting the executable-path from a window
    By johny145 in forum Windows Programming
    Replies: 3
    Last Post: 06-04-2005, 12:43 PM
  5. Print the path of the executable
    By Termos in forum C Programming
    Replies: 14
    Last Post: 12-04-2003, 11:29 AM