I'm writing a C program that functions like a shell. What it does is when user gives it a path, such as /bin
, it needs to be able to check if a given executable program is on that path and if so, execute that program. From another question, I know I can check if a file exists using stat() function:
int file_exist (char *filename){
struct stat buffer;
return (stat (filename, &buffer) == 0);
}
and call it such as like this:
if (file_exist ("myfile.txt")) {
printf ("It exists\n");
}
But I have a path variable:
char* path = "/bin";
How do I search for an executable program is on this path, with the purpose of trying to execute it later on?