aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-16 19:16:44 +0100
committerDominik Kaiser2025-01-16 19:16:44 +0100
commit3392f2b811269f174620832d663b09ef4f4e43f3 (patch)
treef19d804b37896ea1aca7c3abe4d639e86154d0fe /src
parent1032eefe7247cc5c240feedd2f49d8d69a326d79 (diff)
downloadminishell-3392f2b811269f174620832d663b09ef4f4e43f3.tar.gz
minishell-3392f2b811269f174620832d663b09ef4f4e43f3.zip
Create files
Diffstat (limited to 'src')
-rw-r--r--src/collect_redirs.c42
-rw-r--r--src/create_files.c45
-rw-r--r--src/execute_cmd.c3
-rw-r--r--src/interpreter.c8
-rw-r--r--src/new_node.c5
-rw-r--r--src/parse_cmd.c8
-rw-r--r--src/tokenizer.c3
7 files changed, 90 insertions, 24 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
index 4decda7..171dc06 100644
--- a/src/collect_redirs.c
+++ b/src/collect_redirs.c
@@ -6,20 +6,21 @@
/* 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;
@@ -33,7 +34,7 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env)
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
@@ -44,17 +45,30 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env)
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;
@@ -69,9 +83,11 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur,
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)
diff --git a/src/create_files.c b/src/create_files.c
new file mode 100644
index 0000000..faee27f
--- /dev/null
+++ b/src/create_files.c
@@ -0,0 +1,45 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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;
+ }
+}
diff --git a/src/execute_cmd.c b/src/execute_cmd.c
index 83addd2..8f5b541 100644
--- a/src/execute_cmd.c
+++ b/src/execute_cmd.c
@@ -6,7 +6,7 @@
/* 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 */
/* */
/* ************************************************************************** */
@@ -51,6 +51,7 @@ int execute_cmd(t_cmd *cmd, t_env **env)
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]);
diff --git a/src/interpreter.c b/src/interpreter.c
index 0a6d781..c7fe67c 100644
--- a/src/interpreter.c
+++ b/src/interpreter.c
@@ -6,7 +6,7 @@
/* 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 */
/* */
/* ************************************************************************** */
@@ -30,7 +30,7 @@ int handle_redirections(t_redirection *redirs)
}
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");
@@ -49,7 +49,7 @@ int handle_redirections(t_redirection *redirs)
}
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");
@@ -60,7 +60,7 @@ int handle_redirections(t_redirection *redirs)
}
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");
diff --git a/src/new_node.c b/src/new_node.c
index c58d291..83d9159 100644
--- a/src/new_node.c
+++ b/src/new_node.c
@@ -6,7 +6,7 @@
/* 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 */
/* */
/* ************************************************************************** */
@@ -37,7 +37,7 @@ 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_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files)
{
t_node *node;
@@ -49,6 +49,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2])
{
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);
}
diff --git a/src/parse_cmd.c b/src/parse_cmd.c
index 9ca741b..92dfd12 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/15 17:22:58 by dkaiser ### ########.fr */
+/* Updated: 2025/01/16 19:06:03 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,8 +18,10 @@ t_node *parse_cmd(t_token *tokens, t_env **env)
{
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);
@@ -28,7 +30,7 @@ t_node *parse_cmd(t_token *tokens, t_env **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)
diff --git a/src/tokenizer.c b/src/tokenizer.c
index 6d16f1d..eb5d8fe 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -6,7 +6,7 @@
/* 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 */
/* */
/* ************************************************************************** */
@@ -101,6 +101,7 @@ void handle_special_chars(char *s, int *i, int *start, t_token **token_list)
(*i)++;
if (s[*i] == '>' && s[*i + 1] == '>')
(*i)++;
+ print_token(*token_list);
*start = *i + 1;
}