From 70c76a4e6d73551a1a78ed61e7cd8f7287137a99 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 9 Oct 2020 13:37:02 +0200 Subject: Norming bultin and path --- src/builtin/cd.c | 8 ++------ src/builtin/export.c | 23 ++++++++++++++--------- src/path.c | 53 +++++++++++++++++++++++++++++----------------------- 3 files changed, 46 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/builtin/cd.c b/src/builtin/cd.c index 5fee38d..a350cf4 100644 --- a/src/builtin/cd.c +++ b/src/builtin/cd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:20 by charles #+# #+# */ -/* Updated: 2020/10/09 08:41:17 by cacharle ### ########.fr */ +/* Updated: 2020/10/09 12:44:55 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,11 +35,7 @@ int builtin_cd(char **argv, t_env env) if (oldwd == NULL) oldwd = ""; if (argv[1] != NULL && argv[1][0] == '\0') - { - if (env_export(env, "OLDPWD", oldwd) == NULL) - return (EVAL_FATAL); - return (0); - } + return (env_export(env, "OLDPWD", oldwd) == NULL ? EVAL_FATAL : 0); if (argv[1] == NULL) { if ((home = env_search(env, "HOME", NULL)) == NULL) diff --git a/src/builtin/export.c b/src/builtin/export.c index e388700..900833a 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/10/08 16:59:28 by cacharle ### ########.fr */ +/* Updated: 2020/10/09 12:50:37 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,16 @@ static void st_put_declare_x(char *s) ft_putchar('\n'); } +/* +** \brief Garbage function for norm complience +*/ + +int st_declare_x_iter(t_env env) +{ + ft_veciter(env, (void (*)(void*))st_put_declare_x); + return (0); +} + /* ** \brief Export variables to the environment ** \param argv arguments @@ -62,16 +72,12 @@ int builtin_export(char **argv, t_env env) char *equal_ptr; if (argv[1] == NULL) - { - ft_veciter(env, (void (*)(void*))st_put_declare_x); - return (0); - } + return (st_declare_x_iter(env)); status = 0; i = 0; while (argv[++i] != NULL) { - equal_ptr = ft_strchr(argv[i], '='); - if (equal_ptr != NULL) + if ((equal_ptr = ft_strchr(argv[i], '=')) != NULL) *equal_ptr = '\0'; if (*argv[i] == '\0' || env_key_len(argv[i], false) != ft_strlen(argv[i])) @@ -80,9 +86,8 @@ int builtin_export(char **argv, t_env env) *equal_ptr = '='; errorf("export: `%s': not a valid identifier\n", argv[i]); status = 1; - continue; } - if (equal_ptr != NULL + else if (equal_ptr != NULL && env_export(env, argv[i], equal_ptr + 1) == NULL) return (EVAL_FATAL); } diff --git a/src/path.c b/src/path.c index 40bf018..3819555 100644 --- a/src/path.c +++ b/src/path.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */ -/* Updated: 2020/10/09 09:29:51 by cacharle ### ########.fr */ +/* Updated: 2020/10/09 13:36:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,40 +53,47 @@ static bool st_dir_search( return (false); } -int path_search( - t_env env, char *exec_name, char exec_path[PATH_MAX + 1], bool print) +static int st_path_search_iter( + char *exec_name, char exec_path[PATH_MAX + 1], char **current_dir) { - char *current_dir; char *collon; char cwd[PATH_MAX + 1]; - if (ft_strchr(exec_name, '/') != NULL) + while ((collon = ft_strchr(*current_dir, ':')) != NULL) { - ft_strcpy(exec_path, exec_name); - return (st_path_check(exec_path, false)); + *collon = '\0'; + if (**current_dir == '\0') + *current_dir = getcwd(cwd, PATH_MAX + 1); + if (st_dir_search(*current_dir, exec_name, exec_path)) + { + *collon = ':'; + return (st_path_check(exec_path, false)); + } + *collon = ':'; + *current_dir = collon + 1; } - if ((current_dir = env_search(env, "PATH", NULL)) == NULL) + return (-1); +} + +int path_search( + t_env env, char *exec_name, char exec_path[PATH_MAX + 1], bool print) +{ + char *current_dir; + char cwd[PATH_MAX + 1]; + int status; + + if (ft_strchr(exec_name, '/') != NULL || + (current_dir = env_search(env, "PATH", NULL)) == NULL) return (st_path_check(ft_strcpy(exec_path, exec_name), false)); if (*current_dir == '\0') { - ft_strcpy(exec_path, exec_name); if (print) - return (st_path_check(exec_name, false)); + return (st_path_check(ft_strcpy(exec_path, exec_name), false)); return (0); } - while ((collon = ft_strchr(current_dir, ':')) != NULL) - { - *collon = '\0'; - if (*current_dir == '\0') - current_dir = getcwd(cwd, PATH_MAX + 1); - if (st_dir_search(current_dir, exec_name, exec_path)) - { - *collon = ':'; - return (st_path_check(exec_path, true)); - } - *collon = ':'; - current_dir = collon + 1; - } + status = st_path_search_iter(exec_name, exec_path, ¤t_dir); + if (status != -1) + return (status); if (*current_dir == '\0') current_dir = getcwd(cwd, PATH_MAX + 1); if (st_dir_search(current_dir, exec_name, exec_path)) -- cgit