Added wildcard explanation

This commit is contained in:
Dmitriy Shishkov 2020-07-15 19:52:57 +05:00
parent 3ab9e3a769
commit d6ed9d8de0
3 changed files with 24 additions and 1 deletions

View File

@ -13,6 +13,7 @@ Work is still in porgress, buf when you will see a "finished" topic assigned to
* Username, ip address and current path in prompt before each command input * Username, ip address and current path in prompt before each command input
* Show previous command return status in prompt and invert it with `!`, separated with space, specified before it * Show previous command return status in prompt and invert it with `!`, separated with space, specified before it
* Running multiple commands separated by `;`, `&&` or `||` * Running multiple commands separated by `;`, `&&` or `||`
* Expand `*` wildcards
# Builtin commands # Builtin commands
* `cd`: changes current working directory to the one specified by user. If no arguments provided, shows error. * `cd`: changes current working directory to the one specified by user. If no arguments provided, shows error.

View File

@ -7,6 +7,7 @@
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <glob.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/types.h> #include <sys/types.h>

View File

@ -297,7 +297,27 @@ int sh_exec(char **args)
if (strcmp(args[0], "exec") == 0) if (strcmp(args[0], "exec") == 0)
args = slice_array(args, 1, -1, 1); args = slice_array(args, 1, -1, 1);
if (execvp(args[0], args) < 0) int amount = get_null_term_arr_size(args) - 1, k = 0;
char **ex_args = malloc(0);
for (int i = 0; i < amount; i++)
{
if (strchr(args[i], '*') || strchr(args[i], '~'))
{
glob_t globbuf;
glob(args[i], GLOB_DOOFFS, NULL, &globbuf);
for (int j = 0; j < globbuf.gl_pathc; j++)
append_to_str_arr(&ex_args, &k, globbuf.gl_pathv[j]);
}
else
append_to_str_arr(&ex_args, &k, args[i]);
}
ex_args = realloc(ex_args, sizeof(char *) * ++k);
ex_args[k - 1] = NULL;
if (execvp(ex_args[0], ex_args) < 0)
{ {
perror("myshell3"); perror("myshell3");
} }
@ -394,6 +414,7 @@ cmds_p *new_cmd()
cmds_p *new = malloc(sizeof(cmds_p)); cmds_p *new = malloc(sizeof(cmds_p));
new->args = calloc(0, sizeof(char *)); new->args = calloc(0, sizeof(char *));
new->stat.s = 0; new->stat.s = 0;
new->sep_next = NO_SEP;
new->stat.invert = false; new->stat.invert = false;
new->next = NULL; new->next = NULL;