diff options
Diffstat (limited to 'src/builtin')
| -rw-r--r-- | src/builtin/cd.c | 23 | ||||
| -rw-r--r-- | src/builtin/echo.c | 2 | ||||
| -rw-r--r-- | src/builtin/exit.c | 14 | ||||
| -rw-r--r-- | src/builtin/export.c | 16 | ||||
| -rw-r--r-- | src/builtin/unset.c | 6 |
5 files changed, 25 insertions, 36 deletions
diff --git a/src/builtin/cd.c b/src/builtin/cd.c index d7115e5..de1eeb9 100644 --- a/src/builtin/cd.c +++ b/src/builtin/cd.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:20 by charles #+# #+# */ -/* Updated: 2020/07/19 19:05:25 by charles ### ########.fr */ +/* Updated: 2020/09/12 11:09:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,35 +19,26 @@ int builtin_cd(char **argv, t_env env) { - char buf[PATH_MAX]; + char buf[PATH_MAX + 1]; char *home; (void)env; if (argv[1] != NULL && argv[2] != NULL) - { - errorf("cd: too many arguments\n"); - return (1); - } + return (errorf_ret(1, "cd: too many arguments\n")); if (argv[1] != NULL && argv[1][0] == '\0') return (0); if (argv[1] == NULL) { if ((home = env_search(env, "HOME")) == NULL) - { - errorf("cd: HOME not set\n"); - return (1); - } + return (errorf_ret(1, "cd: HOME not set\n")); argv[1] = home; } errno = 0; if (chdir(argv[1]) == -1) - { - errorf("cd: %s: %s\n", argv[1], strerror(errno)); - return (1); - } - if (!(getcwd(buf, PATH_MAX))) + return (errorf_ret(1, "cd: %s: %s\n", argv[1], strerror(errno))); + if (getcwd(buf, PATH_MAX) == NULL) return (1); if (env_export(env, "PWD", buf) == NULL) - return (2); // FIXME malloc error recognition in builtins and cmd + return (EVAL_FATAL); return (0); } diff --git a/src/builtin/echo.c b/src/builtin/echo.c index 75a350c..9ad427a 100644 --- a/src/builtin/echo.c +++ b/src/builtin/echo.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:47 by charles #+# #+# */ -/* Updated: 2020/06/15 14:26:30 by charles ### ########.fr */ +/* Updated: 2020/09/12 15:24:34 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/builtin/exit.c b/src/builtin/exit.c index 640fc01..a24efad 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:16 by charles #+# #+# */ -/* Updated: 2020/08/20 17:31:18 by charles ### ########.fr */ +/* Updated: 2020/09/10 19:41:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ int builtin_exit(char **argv, t_env env) (void)env; if (argv[1] == NULL) - status = g_last_status_code; + status = g_last_status; else { errno = 0; @@ -32,15 +32,9 @@ int builtin_exit(char **argv, t_env env) while (ft_isblank(*after)) after++; if (*after != '\0' || errno == ERANGE) - { - errorf("exit: %s: numeric argument required\n", argv[1]); - return (2); - } + return (errorf_ret(2, "exit: %s: numeric argument required\n", argv[1])); if (argv[2] != NULL) - { - errorf("exit: too many arguments\n"); - return (1); - } + return (errorf_ret(1, "exit: too many arguments\n")); } exit(status % 256); return (0); diff --git a/src/builtin/export.c b/src/builtin/export.c index e19756d..d394af6 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/08/28 17:49:47 by charles ### ########.fr */ +/* Updated: 2020/09/12 11:06:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,16 +21,20 @@ static void st_put_declare_x(char *s) { char *equal_ptr; - // could use errorf on stdout if (s == NULL) return ; - ft_putstr("declare -x "); + ft_putstr("export "); if ((equal_ptr = ft_strchr(s, '=')) == NULL) equal_ptr = ft_strchr(s, '\0'); write(STDOUT_FILENO, s, equal_ptr - s); ft_putchar('='); ft_putchar('"'); - ft_putstr(equal_ptr + 1); + while (*++equal_ptr != '\0') + { + if (*equal_ptr == '"') + ft_putchar('\\'); + ft_putchar(*equal_ptr); + } ft_putchar('"'); ft_putchar('\n'); } @@ -38,7 +42,7 @@ static void st_put_declare_x(char *s) int builtin_export(char **argv, t_env env) { int status; - size_t i; + size_t i; char *equal_ptr; if (argv[1] == NULL) @@ -62,7 +66,7 @@ int builtin_export(char **argv, t_env env) continue; } if (env_export(env, argv[i], equal_ptr == NULL ? "" : equal_ptr + 1) == NULL) - return (127); // malloc error + return (EVAL_FATAL); } return (status); } diff --git a/src/builtin/unset.c b/src/builtin/unset.c index 367f063..30facd3 100644 --- a/src/builtin/unset.c +++ b/src/builtin/unset.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:51 by charles #+# #+# */ -/* Updated: 2020/07/19 18:47:36 by charles ### ########.fr */ +/* Updated: 2020/09/10 13:49:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,11 +31,11 @@ int builtin_unset(char **argv, t_env env) { errorf("unset: `%s': not a valid identifier\n", argv[i]); status = 1; - continue; // put invalid identifier + continue ; } found_index = env_search_index(env, argv[i]); if (found_index == -1) - continue; + continue ; ft_vecremove(env, found_index, free); } return (status); |
