Skip to content

Commit

Permalink
Merge pull request #52 from refactor/#30/ft_export
Browse files Browse the repository at this point in the history
Refactor : #30/export - 함수 인자 변경
  • Loading branch information
mingxoxo authored Jan 14, 2023
2 parents 41be4b0 + ad2b843 commit 9bea86e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
69 changes: 50 additions & 19 deletions builtin/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,75 @@
/* By: wonyang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 16:46:45 by wonyang #+# #+# */
/* Updated: 2023/01/13 21:49:45 by wonyang ### ########seoul.kr */
/* Updated: 2023/01/14 18:28:52 by wonyang ### ########seoul.kr */
/* */
/* ************************************************************************** */

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include "return.h"
#include "ds_envp.h"
#include "libft.h"

t_error print_envp(t_envp *envp);
int print_envp(t_envp *envp);

static int count_argument(char **argv)
static bool is_valid_key(char *key)
{
int i;
if (ft_strcmp(key, "") == 0
|| ft_strchr(key, '\"')
|| ft_strchr(key, '\'')
|| ft_strchr(key, ' '))
{
ft_putstr_fd("bash: export: `", STDERR_FILENO);
ft_putstr_fd(key, STDERR_FILENO);
ft_putendl_fd("': not a valid identifier", STDERR_FILENO);
return (false);
}
return (true);
}

i = 0;
while (argv[i])
i++;
return (i);
static t_error check_argument(char *arg, t_envp *envp)
{
char *key;

if (ft_strchr(arg, '=') == 0)
{
if (is_valid_key(arg) == false)
return (FAIL);
else if (search_key_enode(envp, arg))
return (SCS);
else
return (set_env(envp, arg, NULL));
}
if (get_key(arg, &key)
|| is_valid_key(key) == false)
{
free(key);
return (FAIL);
}
free(key);
return (set_export(envp, arg));
}

int ft_export(char **argv, t_envp *envp)
{
int argc;
t_error errno;

errno = 0;
if (!argv || !(*argv))
{
printf("ft_export argument error!\n");
return (1);
}
argc = count_argument(argv);
if (argc == 1)
errno = print_envp(envp);
else if (argc == 2)
errno = set_env(envp, argv[1], NULL);
else
errno = set_env(envp, argv[1], argv[2]);
if (errno)
return (1);
return (0);
argv++;
if (!(*argv))
return (print_envp(envp));
while (*argv)
{
if (check_argument(*argv, envp) != SCS)
errno = 1;
argv++;
}
return (errno);
}
8 changes: 4 additions & 4 deletions builtin/print_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: wonyang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 20:04:00 by wonyang #+# #+# */
/* Updated: 2023/01/13 22:12:25 by wonyang ### ########seoul.kr */
/* Updated: 2023/01/14 17:06:19 by wonyang ### ########seoul.kr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -94,20 +94,20 @@ static char **make_arr(t_envp *envp)
return (key_arr);
}

t_error print_envp(t_envp *envp)
int print_envp(t_envp *envp)
{
int i;
char **key_arr;

key_arr = make_arr(envp);
if (!key_arr)
return (ERROR);
return (1);
i = 0;
while (key_arr[i])
{
print_line(envp, key_arr[i]);
i++;
}
free(key_arr);
return (SCS);
return (0);
}
6 changes: 3 additions & 3 deletions ds_envp/cast_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* cast_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jeongmin <jeongmin@student.42.fr> +#+ +:+ +#+ */
/* By: wonyang <wonyang@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 16:21:17 by jeongmin #+# #+# */
/* Updated: 2023/01/08 22:52:31 by jeongmin ### ########.fr */
/* Updated: 2023/01/14 17:11:37 by wonyang ### ########seoul.kr */
/* */
/* ************************************************************************** */

Expand All @@ -26,7 +26,7 @@ static int search_sep(char *line)
return (-1);
}

static t_error get_key(char *line, char **key)
t_error get_key(char *line, char **key)
{
int sep;

Expand Down
6 changes: 3 additions & 3 deletions execute/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: wonyang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 18:36:41 by wonyang #+# #+# */
/* Updated: 2023/01/14 12:58:24 by wonyang ### ########seoul.kr */
/* Updated: 2023/01/14 18:36:25 by wonyang ### ########seoul.kr */
/* */
/* ************************************************************************** */

Expand All @@ -23,10 +23,10 @@ extern t_global g_var;

bool is_builtin_cmd(t_tnode *node)
{
t_token *token;
t_token *token;
const char cmd[7][7] = {"echo", "pwd", "cd", "env", "export", \
"unset", "exit"};
int i;
int i;

if (!node || !node->content)
{
Expand Down
5 changes: 3 additions & 2 deletions includes/ds_envp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ds_envp.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jeongmin <jeongmin@student.42.fr> +#+ +:+ +#+ */
/* By: wonyang <wonyang@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/07 17:17:28 by jeongmin #+# #+# */
/* Updated: 2023/01/09 15:57:49 by jeongmin ### ########.fr */
/* Updated: 2023/01/14 17:11:41 by wonyang ### ########seoul.kr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -52,6 +52,7 @@ t_enode *search_key_enode(t_envp *envp, char *key);
char *search_key_value(t_envp *envp, char *key);

// cast_*.c
t_error get_key(char *line, char **key);
char **cast_envp_arr(t_envp *envp);
t_error cast_envp_line(t_envp *envp, char *line);
t_error cast_envp_list(t_envp *envp, char **arr);
Expand Down
3 changes: 2 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: wonyang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 22:30:32 by wonyang #+# #+# */
/* Updated: 2023/01/14 13:00:36 by wonyang ### ########seoul.kr */
/* Updated: 2023/01/14 18:34:14 by wonyang ### ########seoul.kr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -93,6 +93,7 @@ int main(int argc, char **argv, char **env)
ft_lstclear(&lst, del_t_paren);
clear_node(node, del_t_token);
free(str);
// system("leaks minishell | grep leaks");
}
clear_envp(&(g_var.envp));
return (g_var.status);
Expand Down

0 comments on commit 9bea86e

Please sign in to comment.