From 48399c3960524ebb74e6708ff935d38964dbfb09 Mon Sep 17 00:00:00 2001 From: Dmitry Demirkylych Date: Mon, 17 Jun 2024 11:22:19 +0100 Subject: [PATCH] fix:norminette --- inc/builtins.h | 4 +- inc/pipe.h | 14 +++++- src/builtins/cd.c | 4 +- src/builtins/exit.c | 4 +- src/builtins/export.c | 90 ++++++++++++++++++++++----------------- src/builtins/pwd.c | 35 +++++++++------ src/builtins/unset.c | 60 +++++++++++++++----------- src/env/env.c | 4 +- src/execute/execute.c | 26 ++++------- src/execute/utils.c | 6 +-- src/pipe/pipe.c | 39 +++++++++++------ src/test/main.c | 4 +- src/test/pipe/pipe_test.c | 40 +++++++++++------ src/test/test.h | 4 +- 14 files changed, 195 insertions(+), 139 deletions(-) diff --git a/inc/builtins.h b/inc/builtins.h index 853a4dfd..d1cccfad 100644 --- a/inc/builtins.h +++ b/inc/builtins.h @@ -6,7 +6,7 @@ /* By: dmdemirk envp, "OLDPWD", get_env(data->envp, "PWD")); - if(getcwd(cwd, sizeof(cwd)) != NULL) + if (getcwd(cwd, sizeof(cwd)) != NULL) set_env(&data->envp, "PWD", cwd); return (0); } diff --git a/src/builtins/exit.c b/src/builtins/exit.c index 554b51b3..4caedda7 100644 --- a/src/builtins/exit.c +++ b/src/builtins/exit.c @@ -6,7 +6,7 @@ /* By: dmdemirk /* - Functionalities: - - If no arguments are passed or "-p", print all environment variables - - If arguments are passed, set the environment variables -*/ - -int builtin_export(t_minishell_data *data) +Functionalities: +- If no arguments are passed or "-p", print all environment variables +- If arguments are passed, set the environment variables + */ +/* +int builtin_export(t_minishell_data *data) { - t_env *curr_node; - char *key; - int i; + t_env *curr_node; + char *key; + int i; - printf("\nbuiltin_export\n"); - i = 0; - if ((data->args[1] == NULL && data->args[2] == NULL) || ft_strncmp(data->args[1], "-p", 2) == 0) - { - curr_node = data->envp; - while (curr_node) - { - printf("declare -x %s=\"%s\"\n", curr_node->key, curr_node->value); - curr_node = curr_node->next; - } - } - else - { - while(data->args[++i]) - { - if (ft_strchr(data->args[i], '=')) - set_env(&data->envp, data->args[i], ft_strchr(data->args[i], '=') + 1); - else - { - key = ft_strcdup(data->args[i], '='); - if (get_env(data->envp, key)) - set_env(&data->envp, key, ""); - else - set_env(&data->envp, key, NULL); - free(key); - printf("declare -x %s=\"%s\"\n", key, get_env(data->envp, key)); - } - } - } - return (0); -} \ No newline at end of file + i = 0; + if ((data->args[1] == NULL && data->args[2] == NULL) \ + || ft_strncmp(data->args[1], "-p", 2) == 0) + { + curr_node = data->envp; + while (curr_node) + curr_node = curr_node->next; + } + else + { + while (data->args[++i]) + { + if (ft_strchr(data->args[i], '=')) + set_env(&data->envp, data->args[i], \ + ft_strchr(data->args[i], '=') + 1); + else + { + key = ft_strcdup(data->args[i], '='); + if (get_env(data->envp, key)) + set_env(&data->envp, key, ""); + else + set_env(&data->envp, key, NULL); + free(key); + } + } + } + return (0); + +} +*/ diff --git a/src/builtins/pwd.c b/src/builtins/pwd.c index 27dac94b..b84962e8 100644 --- a/src/builtins/pwd.c +++ b/src/builtins/pwd.c @@ -1,3 +1,14 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pwd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dmdemirk @@ -5,22 +16,22 @@ #include /* - Functionality: - - Print the current working directory -*/ +Functionality: +- Print the current working directory + */ -int builtin_pwd(t_minishell_data *data) +int builtin_pwd(t_minishell_data *data) { - char cwd[4096]; + char cwd[4096]; - (void)data; - if (getcwd(cwd, sizeof(cwd)) != NULL) - ft_putendl_fd(cwd, STDOUT_FILENO); - else - { + (void)data; + if (getcwd(cwd, sizeof(cwd)) != NULL) + ft_putendl_fd(cwd, STDOUT_FILENO); + else + { ft_putstr_fd("bash: cd: ", 2); perror("cwd"); return (1); } - return (0); -} \ No newline at end of file + return (0); +} diff --git a/src/builtins/unset.c b/src/builtins/unset.c index 5d5d2376..d332e729 100644 --- a/src/builtins/unset.c +++ b/src/builtins/unset.c @@ -1,33 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dmdemirk /* - todo - set the exit status to 1 if the variable is not a valid identifier - Functionality: - - Unset the environment variable - - Return 1 if the variable is not a valid identifier -*/ + todo + set the exit status to 1 if the variable is not a valid identifier +Functionality: +- Unset the environment variable +- Return 1 if the variable is not a valid identifier + */ -int builtin_unset(t_minishell_data *data) +int builtin_unset(t_minishell_data *data) { - int i; - int ret; + int i; + int ret; - i = 0; - ret = 0; - while (data->args[++i]) - { - if (unset_env(&data->envp, data->args[i]) == -1) - { - ft_putstr_fd("bash: unset: `", STDERR_FILENO); - ft_putstr_fd(data->args[i], STDERR_FILENO); - ft_putendl_fd("': not a valid identifier", STDERR_FILENO); - ret = 1; - } - } - data->exit_status = ret; - return (ret); -} \ No newline at end of file + i = 0; + ret = 0; + while (data->args[++i]) + { + if (unset_env(&data->envp, data->args[i]) == -1) + { + ft_putstr_fd("bash: unset: `", STDERR_FILENO); + ft_putstr_fd(data->args[i], STDERR_FILENO); + ft_putendl_fd("': not a valid identifier", STDERR_FILENO); + ret = 1; + } + } + data->exit_status = ret; + return (ret); +} diff --git a/src/env/env.c b/src/env/env.c index fb38febb..5663d8a7 100644 --- a/src/env/env.c +++ b/src/env/env.c @@ -6,7 +6,7 @@ /* By: dmdemirk args[0] == NULL) return (1); i = -1; - while (++i < sizeof(builtin_commands) / sizeof(char *)) + while (++i < sizeof(builtin_commands) / sizeof(char *)) if (ft_strcmp(data->args[0], builtin_commands[i]) == 0) return ((*builtin_functions[i])(data)); - printf("\nexternal_func\n"); return (new_process(data)); } @@ -57,13 +54,6 @@ int new_process(t_minishell_data *data) char *path; char **envp; - /* - todo - add absolute - / path handling - add relative - path handling - add home - ~ path handling - */ - envp = env_to_array(data->envp); path = ft_find_path(data->args[0], data->envp); pid = fork(); diff --git a/src/execute/utils.c b/src/execute/utils.c index 7e2ac2a6..30c09eca 100644 --- a/src/execute/utils.c +++ b/src/execute/utils.c @@ -6,7 +6,7 @@ /* By: dmdemirk #include "libft.h" #include "execute.h" #include +#include + +void ft_perror(void); +void builtin_pipe(t_minishell_data *data); void builtin_pipe(t_minishell_data *data) { @@ -12,18 +28,12 @@ void builtin_pipe(t_minishell_data *data) pipe(fd); pid = fork(); if (pid == -1) - { - perror ("Error"); - exit (EXIT_FAILURE); - } + ft_perror(); if (pid == 0) { close(fd[0]); if (dup2(fd[1], STDOUT_FILENO) == -1) - { - perror ("Error"); - exit (EXIT_FAILURE); - } + ft_perror(); close(fd[1]); execute(data); } @@ -31,11 +41,14 @@ void builtin_pipe(t_minishell_data *data) { close(fd[1]); if (dup2(fd[0], STDIN_FILENO) == -1) - { - perror ("Error"); - exit (EXIT_FAILURE); - } + ft_perror(); close(fd[0]); waitpid(pid, NULL, 0); } -} \ No newline at end of file +} + +void ft_perror(void) +{ + perror ("Error"); + exit (EXIT_FAILURE); +} diff --git a/src/test/main.c b/src/test/main.c index aca1232b..a9d2ee4a 100644 --- a/src/test/main.c +++ b/src/test/main.c @@ -6,7 +6,7 @@ /* By: dmdemirk #include #include "shell.h" #include "pipe.h" #include "libft.h" -void builtin_pipe_test(t_minishell_data *data); -void pipe_tests(t_minishell_data *data); +void builtin_pipe_test(t_minishell_data *data); +void pipe_tests(t_minishell_data *data); -void pipe_tests(t_minishell_data *data) +void pipe_tests(t_minishell_data *data) { - printf("\nPIPE TESTS\n"); - builtin_pipe_test(data); - printf("\033[0m"); - printf("\033[0;32m"); - printf("pipe_test -> OK\n"); - printf("\033[0m"); + printf("\nPIPE TESTS\n"); + builtin_pipe_test(data); + printf("\033[0m"); + printf("\033[0;32m"); + printf("pipe_test -> OK\n"); + printf("\033[0m"); } -void builtin_pipe_test(t_minishell_data *data) +void builtin_pipe_test(t_minishell_data *data) { - data->args = ft_split("ls -l | wc -l", ' '); - builtin_pipe(data); - assert(data->args != NULL); -} \ No newline at end of file + data->args = ft_split("ls -l | wc -l", ' '); + builtin_pipe(data); + assert(data->args != NULL); +} diff --git a/src/test/test.h b/src/test/test.h index b2a00d68..5dac1c91 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -6,7 +6,7 @@ /* By: dmdemirk