diff --git a/Makefile b/Makefile index 2d856d9..9f304da 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ $(LIBFT): $(OBJDIR)/%.o: $(SRCDIR)/%.c @mkdir -p $(dir $@) - @$(CC) $(WFLAGS) -MMD -MP -I$(INCDIR) -c $< -g3 -ggdb -o $@ $(LINK) + @$(CC) $(WFLAGS) -MMD -MP -I$(INCDIR) -c $< -o $@ $(LINK) $(NAME): $(LIBFT) $(OBJS) @$(CC) $(WFLAGS) $(OBJS) $(LIBFT) -o $(NAME) $(LINK) diff --git a/includes/builtins.h b/includes/builtins.h index d8ab256..e2e940f 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -8,11 +8,15 @@ # include # include # include +# include // void echo(char *msg, int flag); -void builtin_echo(char *arg); +void builtin_echo(char *arg, char **envp); void builtin_exit(char *arg, bool depth); void builtin_pwd(char *arg); +void builtin_env(char *str, char **envp); +void builtin_unset(char *str, char **envp); - +//UTILS +int count_char(char *str); #endif diff --git a/lib/libft/srcs/format/ft_atoi.c b/lib/libft/srcs/format/ft_atoi.c index cc37998..168f9cf 100644 --- a/lib/libft/srcs/format/ft_atoi.c +++ b/lib/libft/srcs/format/ft_atoi.c @@ -52,7 +52,7 @@ int ft_atoi(const char *str) int current_digit; if (!str) - return (-1); + return (0); res = 0; i = 0; while (ft_isspace(str[i])) diff --git a/lib/libft/srcs/str/ft_strnstr.c b/lib/libft/srcs/str/ft_strnstr.c index aaa3f2c..2d2153c 100644 --- a/lib/libft/srcs/str/ft_strnstr.c +++ b/lib/libft/srcs/str/ft_strnstr.c @@ -15,23 +15,47 @@ char *ft_strnstr(const char *haystack, const char *needle, size_t len) { size_t i; - size_t j; + int j; i = 0; - if (!haystack || !needle) - return (NULL); - if (needle[0] == 0) + if (!haystack || !needle || *needle == 0) return ((char *)haystack); while (haystack[i] && i < len) { - j = 0; - while (haystack[i + j] == needle[j] && i + j < len) + if (haystack[i] == needle[0]) { - j++; - if (needle[j] == 0) - return ((char *)(haystack + i)); + j = 0; + while (haystack[i + j] == needle[j] && i + j < len) + { + j++; + if (needle[j] == 0) + return ((char *)(haystack + i)); + } } i++; } - return (NULL); + return (0); } +// char *ft_strnstr(const char *haystack, const char *needle, size_t len) +// { +// size_t i; +// size_t j; +// +// i = 0; +// if (!haystack || !needle) +// return (NULL); +// if (needle[0] == 0) +// return ((char *)haystack); +// while (haystack[i] && i < len) +// { +// j = 0; +// while (haystack[i + j] == needle[j] && i + j < len) +// { +// j++; +// if (needle[j] == 0) +// return ((char *)(haystack + i)); +// } +// i++; +// } +// return (NULL); +// } diff --git a/srcs/builtins/cd.c b/srcs/builtins/cd.c index 1478060..0fbbd8f 100644 --- a/srcs/builtins/cd.c +++ b/srcs/builtins/cd.c @@ -1,27 +1,6 @@ #include "../../includes/builtins.h" -// static char *find_home(char **envp) -// { -// int i; -// char *str; -// -// i = 0; -// if (!envp) -// return (NULL); -// while (ft_strnstr(envp[i], "HOME=", 5) == 0) -// i++; -// str = envp[i + 5]; -// return (str); -// } -// -// int cd(char *path, char **envp) -// { -// char *str; -// -// if (path == NULL) -// { -// //cd $HOME -// str = find_home(envp); -// chdir(str); -// } -// } +void builtin_cd(char *str) +{ + (void)str; +} diff --git a/srcs/builtins/echo.c b/srcs/builtins/echo.c index 42c7a95..1f7ae86 100644 --- a/srcs/builtins/echo.c +++ b/srcs/builtins/echo.c @@ -24,14 +24,52 @@ int is_silent(char *str) return (0); } -void builtin_echo(char *arg) +static int extractenv(char *str, char **envp) +{ + int i; + char *var; + char *tmp; + + i = 0; + (void)envp; + while (str[i] && str[i] != ' ') + i++; + if (i >= 1) + tmp = ft_substr(str, 1, i - 1); + var = getenv(tmp); + free(tmp); + if (var) + ft_printf("%s", var); + return (i); +} + +static void echo_print(char *str, char **envp) +{ + int i; + + i = 0; + while (str[i]) + { + if (str[i] == '$') + { + if (!str[i + 1] || str[i + 1] == ' ') + ft_put_c(str[i++]); + else + i += extractenv(&str[i], envp); + } + else + ft_put_c(str[i++]); + } +} + +void builtin_echo(char *arg, char **envp) { int i; i = 4; while (arg[i] && is_silentchar(arg[i])) i++; - printf("%s", &arg[i]); + echo_print(&arg[i], envp); if (!is_silent(arg)) printf("\n"); } diff --git a/srcs/builtins/env.c b/srcs/builtins/env.c index 6d3f2ef..e74dea6 100644 --- a/srcs/builtins/env.c +++ b/srcs/builtins/env.c @@ -1 +1,15 @@ #include "../../includes/builtins.h" + +void builtin_env(char *str, char **envp) +{ + int i; + + i = 0; + (void)str; + while (envp[i]) + { + if (envp[i][0]) + printf("%s\n", envp[i]); + i++; + } +} diff --git a/srcs/builtins/exit.c b/srcs/builtins/exit.c index 761108a..c9a88a5 100644 --- a/srcs/builtins/exit.c +++ b/srcs/builtins/exit.c @@ -5,6 +5,8 @@ int ft_isnumeric(char *str) int i; i = 0; + if (!str) + return (1); while (str[i]) { if (ft_isdigit(str[i])) @@ -30,8 +32,23 @@ void bash_exit_error(char *arg) exit(2); } -void builtin_exit(char *arg, bool depth) +static char *get_arg(char *str) { + int i; + + i = 4; + if (!str[i]) + return (NULL); + while (str[i] && str[i] == ' ') + i++; + return (&str[i]); +} + +void builtin_exit(char *str, bool depth) +{ + char *arg; + + arg = get_arg(str); if (depth == true) { if (!ft_isnumeric(arg)) diff --git a/srcs/builtins/pwd.c b/srcs/builtins/pwd.c index e6104fa..a901561 100644 --- a/srcs/builtins/pwd.c +++ b/srcs/builtins/pwd.c @@ -1,20 +1,36 @@ #include "../../includes/builtins.h" +int count_char(char *str) +{ + int i; + int count; + + i = 0; + count = 0; + while (str[i]) + { + if (str[i] != ' ') + count++; + i++; + } + return (count); +} + void builtin_pwd(char *arg) { char *cwd; - if (ft_strlen(arg) > 3) + if (count_char(arg) > 3) ft_putendl_fd("pwd: too many arguments", 2); else { - cwd = getcwd(NULL, 4096); + cwd = getcwd(NULL, 0); if (cwd != NULL) { printf("%s\n", cwd); free(cwd); } if (!cwd) - perror("pwd"); + perror("pwd"); } } diff --git a/srcs/builtins/unset.c b/srcs/builtins/unset.c index 6d3f2ef..8d51dd4 100644 --- a/srcs/builtins/unset.c +++ b/srcs/builtins/unset.c @@ -1 +1,22 @@ #include "../../includes/builtins.h" + +void builtin_unset(char *str, char **envp) +{ + int i; + char *var; + + if (count_char(str) == 5) + { + ft_putendl_fd("unset: not enough arguments", 2); + // exit(1); + } + i = 0; + while(str[5 + i] && str[5 + i] == ' ') + i++; + var = &str[5 + i]; + i = 0; + while (ft_strnstr(envp[i], var, ft_strlen(var)) == NULL) + i++; + if (envp[i]) + envp[i][0] = '\0'; +} diff --git a/srcs/main.c b/srcs/main.c index d991c46..1ad2e67 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -5,7 +5,7 @@ char *powerline(void) char *pwd; char *tilt; - pwd = getenv("PWD"); + pwd = getcwd(NULL, 0); if (ft_strncmp(pwd, "/home/", 6) == 0) { pwd = pwd + 6; @@ -22,20 +22,41 @@ char *powerline(void) return (readline("")); } +char **ft_setnewenv(void) +{ + char **envp; + + envp = malloc(sizeof(char *) * 2); + if (!envp) + return (NULL); + envp[0] = ft_strjoin("PWD=", getcwd(NULL, 0)); + envp[1] = ft_strdup("SHLVL=1"); + if (!envp[0] || !envp[1]) + return (ft_free(&envp[0]), ft_free(&envp[1]), NULL); + return (envp); +} + int main(int ac, char **av, char **envp) { char *input; (void)ac; (void)av; - (void)envp; + if (!envp[0]) + envp = ft_setnewenv(); while (1) { input = powerline(); if (ft_strncmp(input, "exit", 4) == 0) - builtin_exit(&input[5], true); + builtin_exit(input, true); if (ft_strncmp(input, "pwd", 3) == 0) builtin_pwd(input); + if (ft_strncmp(input, "echo", 4) == 0) + builtin_echo(input, envp); + if (ft_strncmp(input, "env", 3) == 0) + builtin_env(input, envp); + if (ft_strncmp(input, "unset", 5) == 0) + builtin_unset(input, envp); free(input); } return (0);