diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/collect_redirs.c | 30 | ||||
| -rw-r--r-- | src/create_files.c | 61 | ||||
| -rw-r--r-- | src/error.c | 9 | ||||
| -rw-r--r-- | src/get_cmd_path.c | 6 | ||||
| -rw-r--r-- | src/new_node.c | 7 | ||||
| -rw-r--r-- | src/parse_cmd.c | 15 | ||||
| -rw-r--r-- | src/parser.c | 8 | ||||
| -rw-r--r-- | src/praise_the_norme.c | 30 | ||||
| -rw-r--r-- | src/repl.c | 6 |
9 files changed, 101 insertions, 71 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c index e5bc8f6..f274053 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:30:00 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:39:24 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,6 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, static void collect_and_check_redir(t_redirection *result, t_token **cur, t_minidata *data, t_token **tokens) { - t_token *next_token; char *str; if ((*cur)->content.redir_type != INPUT_LIMITER) @@ -63,28 +62,15 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, return ; } else if ((*cur)->content.redir_type == INPUT_FILE) - ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[0], - INPUT_FILE, format_string(str, data->env, 0), data->env))); + q4fc(data->create_files, set_redir(&result[0], INPUT_FILE, + format_string(str, data->env, 0), data->env)); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_OVERRIDE, format_string(str, data->env, 0), - data->env))); + q4fc(data->create_files, set_redir(&result[1], OUTPUT_OVERRIDE, + format_string(str, data->env, 0), data->env)); else if ((*cur)->content.redir_type == OUTPUT_APPEND) - ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_APPEND, format_string(str, data->env, 0), - data->env))); - next_token = (*cur)->next; - free_token_and_connect(*cur); - if (next_token) - { - if (next_token->previous == NULL) - *tokens = next_token->next; - // free_token_and_connect(*cur); - *cur = next_token->next; - free_token_and_connect(next_token); - } - else - *cur = NULL; + q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND, + format_string(str, data->env, 0), data->env)); + i_love_the_norme(cur, tokens); } static t_redirection *set_redir(t_redirection *redir, int type, char *spec, diff --git a/src/create_files.c b/src/create_files.c index 0550e57..eee2860 100644 --- a/src/create_files.c +++ b/src/create_files.c @@ -6,45 +6,56 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 15:54:00 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:30:40 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include <sys/fcntl.h> #include <unistd.h> +static int cant_write(char *filename); +static void create_file(char *filename, int mode); + int create_files(t_list *files) { t_redirection *file; - int fd; while (files) { - if (files->content == NULL) - { - files = files->next; - continue ; - } - file = (t_redirection *)files->content; - if (file->type == INPUT_FILE && (access(file->specifier, F_OK) == -1 || access(file->specifier, R_OK) == -1)) - return (EXIT_FAILURE); - if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) - break ; - if (file->type == OUTPUT_OVERRIDE) - { - fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); - close(fd); - } - else if (file->type == OUTPUT_APPEND) + if (files->content != NULL) { - fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); - close(fd); + file = (t_redirection *)files->content; + if (file->type == INPUT_FILE && (access(file->specifier, F_OK) == -1 + || access(file->specifier, R_OK) == -1)) + return (EXIT_FAILURE); + if (cant_write(file->specifier)) + break ; + if (file->type == OUTPUT_OVERRIDE) + create_file(file->specifier, O_TRUNC); + else if (file->type == OUTPUT_APPEND) + create_file(file->specifier, O_APPEND); + if (files->next == NULL) + break ; + if (((t_redirection *)files->next->content)->type == 0) + break ; } - if (files->next == NULL) - break ; - if (((t_redirection *) files->next->content)->type == 0) - break ; files = files->next; } - return (EXIT_SUCCESS); + return (EXIT_SUCCESS); +} + +static int cant_write(char *filename) +{ + return (access(filename, F_OK) != -1 && access(filename, W_OK) == -1); +} + +static void create_file(char *filename, int mode) +{ + close(open(filename, O_WRONLY | O_CREAT | mode, 0644)); +} + +void q4fc(t_list **queue, t_redirection *redir) +{ + ft_lstadd_back(queue, ft_lstnew(redir)); } diff --git a/src/error.c b/src/error.c index 628200d..2ca60b2 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/15 16:35:53 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 16:36:18 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:12:40 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,3 +21,10 @@ void *error(int err_code, char *err_text, int exit_code, int *ret_code) *ret_code = exit_code; return (NULL); } + +void command_not_found_error(char *cmd) +{ + ft_printf("%s:", cmd); + ft_putstr_fd(" command not found", 2); + ft_printf("\n"); +} diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 2980b0f..70043a7 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2025/01/20 13:18:45 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:12:33 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,9 +85,7 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code) path++; } *return_code = 127; - ft_printf("%s:", cmd); - ft_putstr_fd(" command not found", 2); - ft_printf("\n"); + command_not_found_error(cmd); return (NULL); } diff --git a/src/new_node.c b/src/new_node.c index bbac154..b2ab7ea 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */ -/* Updated: 2025/01/19 19:01:01 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:59:01 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,8 @@ t_node *new_pipe_node(t_node *left, t_node *right) return (node); } -t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files) +t_node *new_cmd_node(char **args, t_redirection redirs[2], + t_list *create_files) { t_node *node; @@ -51,7 +52,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files) node->content.cmd.redirs[1] = redirs[1]; node->content.cmd.create_files = create_files; free(redirs); - redirs = NULL;//1 + redirs = NULL; return (node); } return (NULL); diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 5502f74..b30d126 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:30:06 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 17:57:50 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,10 +35,10 @@ t_node *parse_cmd(t_token *tokens, t_env **env) static char **collect_args(t_token **tokens, t_env **env) { - t_token *cur; - t_token *next; // 2 - char **result; - int i; + t_token *cur; + char **result; + int i; + t_token *next; cur = *tokens; i = 0; @@ -51,13 +51,12 @@ static char **collect_args(t_token **tokens, t_env **env) i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { - next = cur->next; // 2 + next = cur->next; if (cur->previous) free_token(cur->previous); result[i] = format_string(cur->content.string, *env, ft_atoi("0")); i++; - // cur = cur->next; - cur = next; // 2 + cur = next; } result[i] = NULL; return (result); diff --git a/src/parser.c b/src/parser.c index aef8d70..75f1c64 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,14 +6,14 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */ -/* Updated: 2025/01/19 18:59:00 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:57:20 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ +#include "env.h" #include "libft.h" #include "minishell.h" #include "token.h" -#include "env.h" static t_token *find_token_by_type(t_token *tokens, int type); t_token *split_at_first(t_token **tokens, int type); @@ -40,7 +40,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env) if (left_side_tokens == NULL) { free_tokens(tokens); - tokens = NULL;//1 + tokens = NULL; return (NULL); } else if (tokens != NULL) @@ -71,7 +71,7 @@ t_token *split_at_first(t_token **tokens, int type) if (result == split) result = NULL; free_token(split); - split = NULL;//1 + split = NULL; return (result); } diff --git a/src/praise_the_norme.c b/src/praise_the_norme.c new file mode 100644 index 0000000..a22843b --- /dev/null +++ b/src/praise_the_norme.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* praise_the_norme.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/20 18:35:41 by dkaiser #+# #+# */ +/* Updated: 2025/01/20 18:39:31 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void i_love_the_norme(t_token **cur, t_token **tokens) +{ + t_token *next_token; + + next_token = (*cur)->next; + free_token_and_connect(*cur); + if (next_token) + { + if (next_token->previous == NULL) + *tokens = next_token->next; + *cur = next_token->next; + free_token_and_connect(next_token); + } + else + *cur = NULL; +} @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 12:45:00 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:58:43 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ void repl(const char *prompt, t_env **env, int *promptflag) while (1) { input = readline(prompt); - if (input == NULL) + if (input == NULL) { if (*promptflag > 1) (*promptflag)--; @@ -41,5 +41,3 @@ void repl(const char *prompt, t_env **env, int *promptflag) free(input); } } - -//echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission
\ No newline at end of file |
