fix: verified file permision

This commit is contained in:
starnakin 2023-07-05 00:02:49 +02:00
parent 449bc574b1
commit a36b4ecdab
3 changed files with 31 additions and 28 deletions

View File

@ -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) 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) if (strcmp(input->executable, "cd") == 0)
{ {
change_directory(input->args, data->env); change_directory(input->args, data->env);

View File

@ -14,13 +14,17 @@
int execute(lst** cmds, cmd_t* cmd, lst** env) int execute(lst** cmds, cmd_t* cmd, lst** env)
{ {
int pid; int pid;
if (cmd->executable != NULL) if (cmd->input[0] == -2 || cmd->input[0] == -2)
pid = fork();
else
{ {
dprintf(2, "Executable inconnu\n"); pid = -1;
return 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) if (pid == -1)
return 1; return 1;
else if (pid == 0) else if (pid == 0)
@ -37,9 +41,7 @@ int execute(lst** cmds, cmd_t* cmd, lst** env)
return 1; return 1;
} }
else else
{
cmd->pid = pid; cmd->pid = pid;
}
return 0; return 0;
} }
@ -125,11 +127,7 @@ int cmds_list_exec(lst** cmds, data_t *data)
add_fd(content->output, fds[1]); add_fd(content->output, fds[1]);
add_fd(((cmd_t*)current->next->content)->input, fds[0]); add_fd(((cmd_t*)current->next->content)->input, fds[0]);
} }
if (content->args[0] == NULL) if (builtin_execute(content, data, fds[0], fds[1]) == 1)
;
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 (execute(cmds, content, data->env)) if (execute(cmds, content, data->env))
{ {

View File

@ -2,6 +2,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h>
char* get_path(const char* start_path) char* get_path(const char* start_path)
{ {
@ -36,34 +37,40 @@ static size_t get_path_size(const char* str)
return i; 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; int fd;
if (str[0] == '>') 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) if (nb_symbols == 1)
fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644); fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
else else
fd = open(path, O_APPEND | O_CREAT | O_WRONLY, 0644); fd = open(path, O_APPEND | O_CREAT | O_WRONLY, 0644);
if (command->input[0] > 2)
close(command->input[0]);
if (fd == -1) if (fd == -1)
dprintf(2, "zzsh: \"%s\": file error\n", path); fd = -2;
if (command->output[0] > 2) command->input[0] = fd;
close(command->output[0]);
command->output[0] = fd;
return (fd == -1);
} }
else 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) if (nb_symbols == 1)
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
else else
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (command->output[0] > 2)
close(command->output[0]);
if (fd == -1) if (fd == -1)
dprintf(2, "zzsh: \"%s\": file error\n", path); fd = -2;
if (command->input[0] > 2) command->output[0] = fd;
close(command->input[0]);
command->input[0] = fd;
return (fd == -1);
} }
} }
@ -102,11 +109,7 @@ int get_redirections(char *str, cmd_t* command)
path = get_path(redirection + redirection_type); path = get_path(redirection + redirection_type);
if (path == NULL) if (path == NULL)
return 1; return 1;
if (redirection_fill(redirection, redirection_type, command, path)) redirection_fill(redirection, redirection_type, command, path);
{
free(path);
return (1);
}
str_shift(redirection, (get_path_size(redirection + redirection_type) + redirection_type) * -1); str_shift(redirection, (get_path_size(redirection + redirection_type) + redirection_type) * -1);
free(path); free(path);
} }