parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \
get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \
builtins_part_one.c builtins_part_two.c env_tools.c error.c \
- read_heredoc.c
+ read_heredoc.c create_files.c
OBJ_DIR := _obj
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:48:27 by dkaiser #+# #+# */
-/* Updated: 2024/08/11 12:20:56 by dkaiser ### ########.fr */
+/* Updated: 2025/01/16 18:26:30 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
{
char **args;
struct s_redirection redirs[2];
+ t_list *create_files;
} t_cmd;
union u_node_content
t_node *new_node(int type);
t_node *new_pipe_node(t_node *left, t_node *right);
-t_node *new_cmd_node(char **args, t_redirection redirs[2]);
+t_node *new_cmd_node(char **args, t_redirection redirs[2],
+ t_list *create_files);
t_node *new_string_node(char *string);
void free_node(t_node *node);
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2025/01/15 18:24:09 by dkaiser ### ########.fr */
+/* Updated: 2025/01/16 18:38:44 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
t_list *parse(t_token *tokens, t_env **env);
t_node *parse_cmd(t_token *tokens, t_env **env);
-t_redirection *collect_redirs(t_token **tokens, t_env *env);
+t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **cf);
void print_ast(t_node *ast);
int eval(t_node *node, t_env **env);
void *error(int err_code, char *err_text, int exit_code,
int *ret_code);
char *read_heredoc(char *delimiter);
+void create_files(t_list *files);
#endif
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/15 18:54:42 by dkaiser ### ########.fr */
+/* Updated: 2025/01/16 18:19:36 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
+#include <stdlib.h>
-static void collect_and_check_redir(t_redirection *result, t_token **cur,
- t_env *env);
-static void set_redir(t_redirection *redir, int type, char *spec,
- t_env *env);
-static int set_heredoc_data(t_token *cur, t_redirection *result,
- t_env *env);
+static void collect_and_check_redir(t_redirection *result,
+ t_token **cur, t_env *env, t_list **create_files);
+static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
+ t_env *env);
+static int set_heredoc_data(t_token *cur, t_redirection *result,
+ t_env *env);
-t_redirection *collect_redirs(t_token **tokens, t_env *env)
+t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_files)
{
t_redirection *result;
t_token *cur;
while (cur != NULL && cur->next != NULL)
{
if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
- collect_and_check_redir(result, &cur, env);
+ collect_and_check_redir(result, &cur, env, create_files);
else if (cur->type == REDIR_TOKEN)
return (free(result), NULL);
else
return (result);
}
-static void set_redir(t_redirection *redir, int type, char *spec, t_env *env)
+static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
+ t_env *env)
{
+ t_redirection *result;
+
redir->type = type;
if (spec != NULL)
redir->specifier = format_string(spec, env);
else
redir->specifier = spec;
+ if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE)
+ {
+ result = malloc(sizeof(t_redirection));
+ if (!result)
+ return (NULL);
+ result->type = type;
+ result->specifier = spec;
+ return (result);
+ }
+ return (NULL);
}
static void collect_and_check_redir(t_redirection *result, t_token **cur,
- t_env *env)
+ t_env *env, t_list **create_files)
{
t_token *next_token;
char *str;
else if ((*cur)->content.redir_type == INPUT_FILE)
set_redir(&result[0], INPUT_FILE, str, env);
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
- set_redir(&result[1], OUTPUT_OVERRIDE, str, env);
+ ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1],
+ OUTPUT_OVERRIDE, str, env)));
else if ((*cur)->content.redir_type == OUTPUT_APPEND)
- set_redir(&result[1], OUTPUT_APPEND, str, env);
+ ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1],
+ OUTPUT_APPEND, str, env)));
next_token = (*cur)->next;
free_token_and_connect(*cur);
if (next_token)
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* create_files.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */
+/* Updated: 2025/01/16 19:16:33 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include <unistd.h>
+
+void create_files(t_list *files)
+{
+ t_redirection *file;
+ int fd;
+
+ while (files)
+ {
+ dbg("Test");
+ if (files->content == NULL)
+ continue;
+ file = (t_redirection *)files->content;
+ 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)
+ {
+ fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
+ close(fd);
+ }
+ /* if (files->next == NULL) */
+ /* break; */
+ /* if (((t_redirection *) files->next->content)->type == 0) */
+ /* break; */
+ files = files->next;
+ }
+}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */
-/* Updated: 2025/01/15 15:52:08 by dkaiser ### ########.fr */
+/* Updated: 2025/01/16 18:38:00 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
original_std[1] = dup(STDOUT_FILENO);
original_std[0] = dup(STDIN_FILENO);
+ create_files(cmd->create_files);
if (handle_redirections(cmd->redirs) == -1)
{
establish_pipeline(original_std[0], original_std[1]);
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */
-/* Updated: 2025/01/15 18:10:25 by dkaiser ### ########.fr */
+/* Updated: 2025/01/16 18:44:39 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
}
else if (redirs[0].type == INPUT_LIMITER)
{
- fd = open("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ fd = open("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644);
if (fd < 0)
{
perror("open");
}
if (redirs[1].type == OUTPUT_OVERRIDE)
{
- fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ fd = open(redirs[1].specifier, O_WRONLY | O_TRUNC, 0644);
if (fd < 0)
{
perror("open");
}
else if (redirs[1].type == OUTPUT_APPEND)
{
- fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
+ fd = open(redirs[1].specifier, O_WRONLY | O_APPEND, 0644);
if (fd < 0)
{
perror("open");
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */
-/* Updated: 2024/09/17 18:46:35 by dkaiser ### ########.fr */
+/* Updated: 2025/01/16 18:25:54 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
return (node);
}
-t_node *new_cmd_node(char **args, t_redirection redirs[2])
+t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files)
{
t_node *node;
{
node->content.cmd.redirs[0] = redirs[0];
node->content.cmd.redirs[1] = redirs[1];
+ node->content.cmd.create_files = create_files;
free(redirs);
return (node);
}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */
-/* Updated: 2025/01/15 17:22:58 by dkaiser ### ########.fr */
+/* Updated: 2025/01/16 19:06:03 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
{
char **args;
t_redirection *redirs;
+ t_list *create_files;
- redirs = collect_redirs(&tokens, *env);
+ create_files = NULL;
+ redirs = collect_redirs(&tokens, *env, &create_files);
if (redirs == NULL)
return (NULL);
args = collect_args(&tokens, env);
free(redirs);
return (NULL);
}
- return (new_cmd_node(args, redirs));
+ return (new_cmd_node(args, redirs, create_files));
}
static char **collect_args(t_token **tokens, t_env **env)
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */
-/* Updated: 2025/01/14 15:58:41 by chuhlig ### ########.fr */
+/* Updated: 2025/01/16 18:57:38 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
(*i)++;
if (s[*i] == '>' && s[*i + 1] == '>')
(*i)++;
+ print_token(*token_list);
*start = *i + 1;
}