Finding Pathname for a file from Enviornment Variable in Linux using c++
So i am trying to create a Command Line Interpretor in Ubuntu using C/C++
It is part of our lab task for college The basic feature of the CLI is to
take input from the user , parse it to find the command and its arguments
, then look up the pathname for the command from the Environment Variable
I have been able to parse the String , get the command and its arguments ,
I have also been able to read to the Path Environment Variables to get the
directories of all the paths. Now i must look through these directories to
find where the file (command) lies , and then return the complete path so
it can be sent to execve for execution in the child process i have to
create a lookup function which takes the arguments array (the 0th index
position contains the command name ) and the array of directories Here is
an outline of the function that has been provided to us
// Search the directories identified by the dir argument to see
// if the argv[0] (the filename) appears there. Allocate a new
// string, place the full path name in it, then return the string.
//
char* lookupPath(char **argv, char **dir){
char* result;
char pName[MAX_PATH_LEN];
// check if is already an absolute path name
if( *argv[0] == '/' ) .....
// look in PATH directories, use access() to see if the
// file is in the dir
for( i = 0 ; i < MAX_PATHS ; i++ ) .....
// File name not found in any path variable
fprintf(stderr, "%s: command not found\n", argv[0]);
return NULL;
}
here argv is the array of arguments (0th index contains the command and
the following index contains arguments) and dir[] contains an array of all
the directories
Now how em i suppose to traverse trough the directories to find the full
path for the give command ?
No comments:
Post a Comment