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");
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.
The which command from GnuWin32 does just this; I suggest looking at its source code.
Which for Windows
Tim S.
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 $ */
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.
Any time