Question: Language C: Implement the following given the base main function we have, see descriptons below: The main function is incomplete and handles the case where
Language C:
Implement the following given the base main function we have, see descriptons below:
The main function is incomplete and handles the case where no argument is passed in, but must use these base functions, please descriptively comment your solution--for the sake of my own understanding.

The which command searches for a command by name and reports where its matching executable file was found. Read its man page (man which) and try it out, e.g. which ls or which make or which vim. The response from which is the full path to the matching executable file or no output if not found.
The default search path includes directories such as /usr/local/bin/ and /usr/bin/which house the executable files for the standard unix commands.
The environment variable of particular interest for which is PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/sbin:/sbin:/usr/games. The value for PATH is a sequence of directories separated by colons; these are the directories searched when looking for an executable. When looking for a command, which searches the directories in the order they are listed in the search path and stops at the first directory that contains a matching executable. In order to match, the file's name must be an exact match and the file must be readable and executable by the user.
The my_which is similar in operation to the standard which with these differences:
my_which MYPATH is used for the search path, if there is no MYPATH, it defaults to PATH.
my_which called with no arguments prints the list of directories searched.
my_which treats each command-line argument prefixed with a + as a wildcard match. (standard which has no option for wildcard match)
my_which does not support any command-line flags. (standard which -a prints all exact matches)
When invoked with no arguments, mywhich prints the directories in the search path, one directory per line. This use case is a testing aid to verify that you are accessing the correct environment variable and can properly tokenize it.
myth> ./mywhich Directories in search path: /usr/bin /usr/local/bin /usr/pubsw/bin /bin
When invoked with one or more arguments, mywhich searches the directories in the search path for an exact match for each argument. The sample output below shows invoking mywhich to find three executables. Two of them were found, but no executable named submit was found in any directory in the user's MYPATH and thus nothing was printed for it.
myth> ./mywhich xemacs submit cp /usr/bin/xemacs /bin/cp
Any argument prefixed with + is handled as a wildcard search, instead of an exact match. A wildcard search prints all executables that contain that pattern from all directories in the search path. Let's say you vaguely remember there is a "fun" unix command, so use a wildcard search to find it:
myth> ./mywhich +fun /usr/bin/funzip /usr/bin/pdfunite
For testing purposes, you should test having run mywhich with different directories in the search path. Rather than muck with your actual PATH (which can create total chaos), we recommend that you change MYPATH, which only affects mywhich and nothing else. Use env to set the value of MYPATHwhen running mywhich like this:
myth> env MYPATH=/tmp:tools ./mywhich submit tools/submit
Requirements for mywhich
The mywhich program is invoked with zero or more arguments. Any argument prefixed with + is handled as a wildcard search. All other arguments are searched for using exact match. An argument is a non-empty string of one or more characters, i..e. "" or just a single + is invalid.
Assume correct usage in all cases, that the user's MYPATH and PATH are of valid format. Also no usage of unsupported -a flag, no empty arguments, and no malformed values for MYPATH..
The user's MYPATH (or PATH if no MYPATH var) defines the search path. For an exact match, the search stops at the first directory containing a readable, executable file matching the command name. For a wildcard search, it searches all directories and prints all matching executable
Output: For command name(s), the full path should be printed to the first matching excutable, nothing if no path/executable found. Matches should be listed in order that they were provided via command line. Wildcard results will print the full path for every matching executable in any of the directories. Directories need to be searched in the order of the search path. Matching files may be printed in any order.
We are restricted from: using the envp argument to mainf, from using getenv/env/which commands
const char "get_env (const char *envp[], const char "key) char temp[strlen(key) 2]; strcpy(temp, key); strncat (temp, , 2); int i 0; while(envp[1] |-NULL){ envpl], index(envp[i], if(!strncmp(temp, strlen(temp))){ '-'); char *eqPtr = eqPtr++; return eqPtr; return NULL; int main(int argc, char *argv[], const char *envp[]) const char *searchpath = getenv if (searchpath == NULL) searchpath = getenv (envp, "MYPATH"; envp, "PATH"); if (argc 1) { char dir [PATH MAX] const char *remaining = searchpath; printf("Directories in search path: "); while (scan_token(&remaining, ": ", dir, sizeof(dir))) printf("%s ", dir); return 0 const char "get_env (const char *envp[], const char "key) char temp[strlen(key) 2]; strcpy(temp, key); strncat (temp, , 2); int i 0; while(envp[1] |-NULL){ envpl], index(envp[i], if(!strncmp(temp, strlen(temp))){ '-'); char *eqPtr = eqPtr++; return eqPtr; return NULL; int main(int argc, char *argv[], const char *envp[]) const char *searchpath = getenv if (searchpath == NULL) searchpath = getenv (envp, "MYPATH"; envp, "PATH"); if (argc 1) { char dir [PATH MAX] const char *remaining = searchpath; printf("Directories in search path: "); while (scan_token(&remaining, ": ", dir, sizeof(dir))) printf("%s ", dir); return 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
