From a36b4ecdab4aeef22948b4508490793f6b44a07b Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 5 Jul 2023 00:02:49 +0200 Subject: [PATCH] fix: verified file permision --- src/builtin-exec/builtin.c | 2 ++ src/exec/exec.c | 22 ++++++++++------------ src/redirection/redirection.c | 35 +++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/builtin-exec/builtin.c b/src/builtin-exec/builtin.c index c877eea..d69ecee 100644 --- a/src/builtin-exec/builtin.c +++ b/src/builtin-exec/builtin.c @@ -57,6 +57,8 @@ int where(char** args, lst** env) int builtin_execute(cmd_t* input, data_t *data, int fd_in, int fd_out) { + if (input->executable == NULL) + return 1; if (strcmp(input->executable, "cd") == 0) { change_directory(input->args, data->env); diff --git a/src/exec/exec.c b/src/exec/exec.c index 9c0df53..1bd0019 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -14,13 +14,17 @@ int execute(lst** cmds, cmd_t* cmd, lst** env) { int pid; - if (cmd->executable != NULL) - pid = fork(); - else + if (cmd->input[0] == -2 || cmd->input[0] == -2) { - dprintf(2, "Executable inconnu\n"); - return 1; + pid = -1; + return 0; } + if (cmd->executable == NULL) + { + dprintf(2, "zzsh: command not found: %s\n", cmd->args[0]); + return 0; + } + pid = fork(); if (pid == -1) return 1; else if (pid == 0) @@ -37,9 +41,7 @@ int execute(lst** cmds, cmd_t* cmd, lst** env) return 1; } else - { cmd->pid = pid; - } return 0; } @@ -125,11 +127,7 @@ int cmds_list_exec(lst** cmds, data_t *data) add_fd(content->output, fds[1]); add_fd(((cmd_t*)current->next->content)->input, fds[0]); } - if (content->args[0] == NULL) - ; - else if (content->executable == NULL) - dprintf(2, "zzsh: command not found: %s\n", content->args[0]); - else if (builtin_execute(content, data, fds[0], fds[1]) == 1) + if (builtin_execute(content, data, fds[0], fds[1]) == 1) { if (execute(cmds, content, data->env)) { diff --git a/src/redirection/redirection.c b/src/redirection/redirection.c index b9d686f..0e104f0 100644 --- a/src/redirection/redirection.c +++ b/src/redirection/redirection.c @@ -2,6 +2,7 @@ #include #include #include +#include char* get_path(const char* start_path) { @@ -36,34 +37,40 @@ static size_t get_path_size(const char* str) return i; } -int redirection_fill(const char* str, size_t nb_symbols, cmd_t* command, const char *path) +void redirection_fill(const char* str, size_t nb_symbols, cmd_t* command, const char *path) { int fd; if (str[0] == '>') { + if (access(path, F_OK)) + dprintf(2, "zzsh: no such file or directory: %s\n", path); + else if (access(path, R_OK)) + dprintf(2, "zzsh: permission denied: %s\n", path); if (nb_symbols == 1) fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644); else fd = open(path, O_APPEND | O_CREAT | O_WRONLY, 0644); + if (command->input[0] > 2) + close(command->input[0]); if (fd == -1) - dprintf(2, "zzsh: \"%s\": file error\n", path); - if (command->output[0] > 2) - close(command->output[0]); - command->output[0] = fd; - return (fd == -1); + fd = -2; + command->input[0] = fd; } else { + if (access(path, F_OK)) + dprintf(2, "zzsh: no such file or directory: %s\n", path); + else if (access(path, R_OK)) + dprintf(2, "zzsh: permission denied\n"); if (nb_symbols == 1) fd = open(path, O_RDONLY); else fd = open(path, O_RDONLY); + if (command->output[0] > 2) + close(command->output[0]); if (fd == -1) - dprintf(2, "zzsh: \"%s\": file error\n", path); - if (command->input[0] > 2) - close(command->input[0]); - command->input[0] = fd; - return (fd == -1); + fd = -2; + command->output[0] = fd; } } @@ -102,11 +109,7 @@ int get_redirections(char *str, cmd_t* command) path = get_path(redirection + redirection_type); if (path == NULL) return 1; - if (redirection_fill(redirection, redirection_type, command, path)) - { - free(path); - return (1); - } + redirection_fill(redirection, redirection_type, command, path); str_shift(redirection, (get_path_size(redirection + redirection_type) + redirection_type) * -1); free(path); }