mirror of
https://github.com/Comp211-SP24/project-Rushilwiz.git
synced 2025-04-09 23:10:18 -04:00
should be finished
This commit is contained in:
parent
984a058338
commit
d0796e6f10
63
shell.c
63
shell.c
|
@ -88,42 +88,6 @@ void parse(char* line, command_t* p_cmd) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines if the executable in p_cmd (i.e., p_cmd->argv[0]) can be found
|
|
||||||
* in one of the directories in $PATH. Returns true if so, else false.
|
|
||||||
* If the executable is found in $PATH, then p_cmd->argv[0] is mutated to be
|
|
||||||
* the absolute path of the executable.
|
|
||||||
*
|
|
||||||
* To see the format of $PATH, run `echo $PATH` in your terminal, which should
|
|
||||||
* output something like
|
|
||||||
* /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/learncli/bin
|
|
||||||
*
|
|
||||||
* For example, suppose the executable is "ls". This function needs to find its
|
|
||||||
* absolute path in the file system. For the $PATH above,
|
|
||||||
* it appends "/ls" to the first directory, resulting in
|
|
||||||
* /usr/local/sbin/ls. However, this file does not exist, and same for the next
|
|
||||||
* two directories. However, /usr/bin/ls exists, so p_cmd->argv[0] is set to
|
|
||||||
* "/usr/bin/ls", and the function returns true.
|
|
||||||
*
|
|
||||||
* If the executable is "doesnotexist", then this function would not find that
|
|
||||||
* file after checking all directories in $PATH, so it would return false.
|
|
||||||
*
|
|
||||||
* Note: This behaves similarly to the `which` command, e.g., `which ls`.
|
|
||||||
*
|
|
||||||
* To access $PATH within C, use getenv.
|
|
||||||
* For example, char* path = getenv("PATH");
|
|
||||||
*
|
|
||||||
* To parse PATH, we recommend strtok, as described in the docstring for the
|
|
||||||
* parse function.
|
|
||||||
*
|
|
||||||
* To check if a file exists, use access with amode F_OK
|
|
||||||
* (https://pubs.opengroup.org/onlinepubs/009695299/functions/access.html).
|
|
||||||
*
|
|
||||||
* @param p_cmd
|
|
||||||
* @return true (if command in PATH) | false (if command not in PATH). Remember
|
|
||||||
* to mutate p_cmd->argv[0] to be the full path of the executable if it is in
|
|
||||||
* PATH
|
|
||||||
*/
|
|
||||||
bool find_full_path(command_t* p_cmd) {
|
bool find_full_path(command_t* p_cmd) {
|
||||||
char* path_tokens = strtok(getenv("PATH"), ":");
|
char* path_tokens = strtok(getenv("PATH"), ":");
|
||||||
|
|
||||||
|
@ -147,8 +111,31 @@ bool find_full_path(command_t* p_cmd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int execute(command_t* p_cmd) {
|
int execute(command_t* p_cmd) {
|
||||||
// TODO:
|
if (is_builtin(p_cmd)) {
|
||||||
return 0;
|
return do_builtin(p_cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!find_full_path(p_cmd)) {
|
||||||
|
printf("Command %s not found!\n", p_cmd->argv[0]);
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// start fork
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
if (pid < 0) {
|
||||||
|
return ERROR;
|
||||||
|
} else if (pid == 0) {
|
||||||
|
// child process
|
||||||
|
execv(p_cmd->argv[0], p_cmd->argv);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
} else {
|
||||||
|
// parent process
|
||||||
|
int status;
|
||||||
|
waitpid(pid, &status, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_builtin(command_t* p_cmd) {
|
bool is_builtin(command_t* p_cmd) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user