unix path searching C function [closed]
Use PATH = getenv("PATH")
to get the path string from the environment, then use successive calls to strtok(PATH,":")
then strtok(NULL,":")
to parse out the paths from the PATH
string into an array of char **path
, which you will need to allocate with malloc()
. Place path[x]
+ '/'
+ argv[0]
into a buffer, and use access(buffer, X_OK)
to see if you can execute your file at that path location, if so, perform your execv(buffer,argv)
.
If you can't use execvp, you could get the PATH variable from char** environ
from <unistd.h> or char* getenv(const char* name)
from <stdlib.h> then use int access(const char* filename, int mode)
to see if the file exists and is executable.I'll leave the implementation up to you as it's a school project.