Created mexecvpe to support both environment variables and $PATH command completion

This commit is contained in:
Dmitriy Shishkov 2020-07-18 17:30:20 +05:00
parent e6c2e4a479
commit c1dc450ef1
6 changed files with 42 additions and 7 deletions

View File

@ -17,6 +17,7 @@ Work is still in porgress, buf when you will see a "finished" topic assigned to
* Commands I/O redirection with `|` pipes and `>`, `<`, `>>`, `<>` file input/output
* Save history into file and access recent command in another instance of shell
* `$ENVIRONMENT_VARIABLES` expansion
* Own `execvpe` implimentation as `mexecvpe` with commands getting from `$PATH`
# Builtin commands
* `cd`: changes current working directory to the one specified by user. If no arguments provided, shows error.

View File

@ -33,7 +33,7 @@ struct hist_sub
struct history
{
char **content;
int length;
ssize_t length;
int pos;
FILE *file;
@ -57,7 +57,7 @@ typedef enum
typedef struct pipes
{
char **args;
int args_am;
ssize_t args_am;
int pipefd[2];
struct pipes *next;
} cmd_pipe;
@ -65,7 +65,7 @@ typedef struct pipes
typedef struct commands
{
cmd_pipe *pipe;
int pipes_am;
ssize_t pipes_am;
struct status stat;
struct commands *next;
@ -98,6 +98,7 @@ int expand_wildcatrd(char ***arr, char *input);
int execute_with_pipes(cmds_p *command);
int execute(cmd_pipe *command);
int launch(cmd_pipe *command);
int mexecvpe(char *file, char **argv, char **envp);
int sh_cd(char **args);
int sh_exit(char **args);

View File

@ -22,7 +22,7 @@ char *trim_string(char *str, bool leave_trailing_space);
void free_str_arr(char **arr, int sz);
char **slice_array(char **arr, int beg, int end, bool asc);
int get_null_term_arr_size(char **arr);
int append_to_str_arr(char ***arr, int *sz, char *str);
int append_to_str_arr(char ***arr, ssize_t *sz, char *str);
char *get_curr_dir_name();
int get_num_of_lines(FILE *file);
bool str_is_in_arr(char **arr, size_t sz, char *str);

View File

@ -157,7 +157,7 @@ int sh_exec(char **args)
if (strcmp(args[0], "exec") == 0)
args = slice_array(args, 1, -1, 1);
if (execvp(args[0], args) < 0)
if (mexecvpe(args[0], args, NULL) < 0)
{
perror("mshell");
}
@ -175,4 +175,37 @@ void redirect_fd(int old, int new)
else
close(old);
}
}
int mexecvpe(char *file, char **argv, char **envp)
{
if (file == NULL || file[0] == '\0')
return -1;
if (envp == NULL)
execvp(file, argv);
if (strchr(file, '/') != NULL)
execve(file, argv, envp);
char *path = getenv("PATH");
size_t file_len = strlen(file) + 1, path_len = strlen(path) + 1;
char buff[file_len + path_len + 1], *subp;
for (char *p = path; ; p = subp)
{
subp = strchr(p, ':');
memcpy(buff, p, subp - p);
buff[subp - p] = '/';
memcpy(buff + (subp - p) + (p < subp), file, file_len);
execve(buff, argv, envp);
if (*subp++ == '\0')
break;
}
}

View File

@ -303,7 +303,7 @@ cmds_p *process_line(char *line)
*/
int expand_wildcatrd(char ***arr, char *input)
{
int size = 0;
ssize_t size = 0;
if (strchr(input, '*') || strchr(input, '~') || strchr(input, '?'))
{

View File

@ -162,7 +162,7 @@ int get_null_term_arr_size(char **arr)
* @param str
* @return int
*/
int append_to_str_arr(char ***arr, int *sz, char *str)
int append_to_str_arr(char ***arr, ssize_t *sz, char *str)
{
(*sz)++;