diff --git a/.minishellrc b/.minishellrc new file mode 100644 index 0000000..e69de29 diff --git a/.minishellrc.swp b/.minishellrc.swp new file mode 100644 index 0000000..890d048 Binary files /dev/null and b/.minishellrc.swp differ diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..1a16943 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,21 @@ +[ + { + "directory": "/nfs/homes/adjoly/Documents/ft_minipowershell", + "arguments": [ + "cc", + "-I", + "include/", + "-I", + "libft/", + "-Werror", + "-Wall", + "-Wextra", + "-g", + "src/main.c", + "-c", + "-o", + "obj/src/main.o" + ], + "file": "src/main.c" + } +] diff --git a/libft/io/get_next_line/get_next_line.h b/include/parsing.h similarity index 59% rename from libft/io/get_next_line/get_next_line.h rename to include/parsing.h index 8580459..0768c87 100644 --- a/libft/io/get_next_line/get_next_line.h +++ b/include/parsing.h @@ -1,28 +1,22 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_next_line.h :+: :+: :+: */ +/* parsing.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */ -/* Updated: 2024/02/04 15:24:21 by adjoly ### ########.fr */ +/* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */ +/* Updated: 2024/04/25 14:30:50 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef GET_NEXT_LINE_H -# define GET_NEXT_LINE_H +#ifndef PARSING_H +# define PARSING_H -# include -# include - -# ifndef BUFFER_SIZE -# define BUFFER_SIZE 1 -# endif - -char *get_next_line(int fd); -char *ft_strjoin_gnl(char *s1, char *s2); -void *ft_calloc(size_t nmemb, size_t size); -size_t ft_strlen(const char *s); +typedef struct s_cmd +{ + char *cmd; + char **argv; +} t_cmd; #endif diff --git a/libft/Makefile b/libft/Makefile index d1a3541..193e6ae 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/01 11:03:22 by adjoly #+# #+# # -# Updated: 2024/03/18 14:33:40 by adjoly ### ########.fr # +# Updated: 2024/04/27 17:17:00 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -37,6 +37,7 @@ SRCS = is/ft_isalnum.c \ mem/ft_memcpy.c \ mem/ft_memmove.c \ mem/ft_memset.c \ + mem/ft_freetab.c \ print/ft_putchar.c \ print/ft_putchar_fd.c \ print/ft_putendl_fd.c \ @@ -70,16 +71,16 @@ SRCS = is/ft_isalnum.c \ print/printf/ft_putnbrbase.c \ print/printf/ft_putnbr.c \ print/printf/ft_putstr.c \ + io/get_next_line/get_next_line.c \ + io/get_next_line/get_next_line_utils.c \ OBJS = $(addprefix $(OBJSDIR),$(SRCS:.c=.o)) FLAGS = -Werror -Wall -Wextra -g -LIB = io/get_next_line/get_next_line.a $(NAME): $(OBJS) - @make -s -C io/get_next_line/ - @ar -rcs $(NAME) $(OBJS) $(LIB) + @ar -rcs $(NAME) $(OBJS) @echo "[✔] Libft compiled" $(OBJSDIR)%.o: %.c @@ -89,11 +90,9 @@ $(OBJSDIR)%.o: %.c all: $(NAME) clean: - @make -s -C io/get_next_line/ clean @rm -f $(OBJS) fclean: clean - @make -s -C io/get_next_line/ fclean @rm -f $(NAME) @echo "[X] Libft cleaned" diff --git a/libft/io/get_next_line/get_next_line.c b/libft/io/get_next_line/get_next_line.c index e554492..6eb5cda 100644 --- a/libft/io/get_next_line/get_next_line.c +++ b/libft/io/get_next_line/get_next_line.c @@ -6,11 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:23:07 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:25:28 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "get_next_line.h" +#include "../../libft.h" char check_line(char *res, char *buf) { diff --git a/libft/io/get_next_line/get_next_line_utils.c b/libft/io/get_next_line/get_next_line_utils.c index 9847cd8..25995df 100644 --- a/libft/io/get_next_line/get_next_line_utils.c +++ b/libft/io/get_next_line/get_next_line_utils.c @@ -6,11 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */ -/* Updated: 2024/03/16 21:45:27 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:25:44 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "get_next_line.h" +#include "../../libft.h" char *ft_strjoin_gnl(char *s1, char *s2) { diff --git a/libft/io/get_next_line/objget_next_line.o b/libft/io/get_next_line/objget_next_line.o deleted file mode 100644 index 734b398..0000000 Binary files a/libft/io/get_next_line/objget_next_line.o and /dev/null differ diff --git a/libft/io/get_next_line/objget_next_line_utils.o b/libft/io/get_next_line/objget_next_line_utils.o deleted file mode 100644 index c860114..0000000 Binary files a/libft/io/get_next_line/objget_next_line_utils.o and /dev/null differ diff --git a/libft/libft.h b/libft/libft.h index 196fe66..b0213c0 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */ -/* Updated: 2024/03/16 21:45:08 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:27:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,13 +16,15 @@ # include # include # include - -# include "print/printf/ft_printf.h" -# include "io/get_next_line/get_next_line.h" +# include # define INT_MAX 2147483647 # define INT_MIN -2147483648 +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 5 +# endif + typedef struct s_list { void *content; @@ -36,6 +38,7 @@ typedef enum s_boolean TRUE, } t_boolean; +char *ft_strjoinvaarg(char *src, ...); long long ft_atoll(const char *nptr); int ft_atoi(const char *nptr); void *ft_calloc(size_t nmemb, size_t size); @@ -84,6 +87,10 @@ void ft_lstclear(t_list **lst, void (*del)(void *)); void ft_lstiter(t_list *lst, void (*f)(void *)); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +char *get_next_line(int fd); +char *ft_strjoin_gnl(char *s1, char *s2); +void *ft_calloc(size_t nmemb, size_t size); + void ft_putchar(char c); void ft_putstr(char *s); void ft_putnbrbase_fd(int n, char *base, int fd); @@ -95,10 +102,12 @@ int ft_printconversion(char conversion, va_list args); int ft_putnbrulong(unsigned int n); int ft_putaddr(void *ptr); int ft_putstr_p(char *s); -int ft_putnbrbase_p(unsigned long int n, char *base); +int ft_putnbrbasep(unsigned long int n, char *base); int ft_putnbrbase_pf(unsigned int n, char *base); int ft_putchar_p(char c); int ft_putnbr_p(int n); size_t ft_strlen(const char *s); +void ft_freearr(void **arr); + #endif diff --git a/libft/print/printf/ft_printf.h b/libft/mem/ft_freetab.c similarity index 50% rename from libft/print/printf/ft_printf.h rename to libft/mem/ft_freetab.c index 4884572..3175d73 100644 --- a/libft/print/printf/ft_printf.h +++ b/libft/mem/ft_freetab.c @@ -1,33 +1,26 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_printf.h :+: :+: :+: */ +/* ft_freetab.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/17 16:50:36 by adjoly #+# #+# */ -/* Updated: 2024/03/16 21:47:28 by adjoly ### ########.fr */ +/* Created: 2024/04/27 15:33:20 by adjoly #+# #+# */ +/* Updated: 2024/04/27 17:20:24 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef FT_PRINTF_H -# define FT_PRINTF_H +#include -# include -# include -# include +void ft_freearr(void **arr) +{ + void **tmp; -int ft_printf(const char *format, ...); -int ft_printconversion(char conversion, va_list args); -int ft_putnbrulong(unsigned int n); -int ft_putaddr(void *ptr); - -int ft_putstr_p(char *s); - -int ft_putnbrbase_p(unsigned long int n, char *base); -int ft_pputnbrbase(unsigned int n, char *base); -int ft_putchar_p(char c); -int ft_putnbr_p(int n); -size_t ft_strlen(const char *s); - -#endif + tmp = arr; + while (*tmp) + { + free(*tmp); + tmp++; + } + free(arr); +} diff --git a/libft/obj/str/ft_strjoinvaarg.o b/libft/obj/str/ft_strjoinvaarg.o new file mode 100644 index 0000000..9fd129f Binary files /dev/null and b/libft/obj/str/ft_strjoinvaarg.o differ diff --git a/libft/print/printf/ft_printf.c b/libft/print/printf/ft_printf.c index c4dc19f..4792193 100644 --- a/libft/print/printf/ft_printf.c +++ b/libft/print/printf/ft_printf.c @@ -6,11 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */ -/* Updated: 2024/03/16 21:46:36 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:27:13 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_printf.h" +#include "../../libft.h" int ft_putaddr(void *ptr) { @@ -19,7 +19,7 @@ int ft_putaddr(void *ptr) if (ptr == NULL) return (write(1, "(nil)", 5)); write(1, "0x", 2); - r = ft_putnbrbase_p((long unsigned int)ptr, "0123456789abcdef"); + r = ft_putnbrbasep((long unsigned int)ptr, "0123456789abcdef"); return (2 + r); } @@ -56,9 +56,9 @@ int ft_printconversion(char conversion, va_list args) else if (conversion == 'p') count = ft_putaddr(va_arg(args, void *)); else if (conversion == 'x') - count = ft_pputnbrbase(va_arg(args, unsigned long), "0123456789abcdef"); + count = ft_putnbrbasep(va_arg(args, unsigned long), "0123456789abcdef"); else if (conversion == 'X') - count = ft_pputnbrbase(va_arg(args, unsigned long), "0123456789ABCDEF"); + count = ft_putnbrbasep(va_arg(args, unsigned long), "0123456789ABCDEF"); return (count); } diff --git a/libft/print/printf/ft_putchar.c b/libft/print/printf/ft_putchar.c index e33bb8b..558d5e0 100644 --- a/libft/print/printf/ft_putchar.c +++ b/libft/print/printf/ft_putchar.c @@ -6,11 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */ -/* Updated: 2024/02/04 15:17:42 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:22:30 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_printf.h" +#include int ft_putchar_p(char c) { diff --git a/libft/print/printf/ft_putnbr.c b/libft/print/printf/ft_putnbr.c index 4ef829e..7759a4b 100644 --- a/libft/print/printf/ft_putnbr.c +++ b/libft/print/printf/ft_putnbr.c @@ -6,11 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */ -/* Updated: 2024/02/04 15:20:26 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:24:27 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_printf.h" +#include "../../libft.h" int ft_putnbr_p(int n) { diff --git a/libft/print/printf/ft_putnbrbase.c b/libft/print/printf/ft_putnbrbase.c index ce603cf..678c08f 100644 --- a/libft/print/printf/ft_putnbrbase.c +++ b/libft/print/printf/ft_putnbrbase.c @@ -6,11 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */ -/* Updated: 2024/02/04 15:21:56 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:29:04 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_printf.h" +#include "../../libft.h" int ft_putnbrbase_pf(unsigned int n, char *base) { @@ -29,7 +29,7 @@ int ft_putnbrbase_pf(unsigned int n, char *base) return (len); } -int ft_putnbrbase_p(unsigned long int n, char *base) +int ft_putnbrbasep(unsigned long int n, char *base) { unsigned long int base_len; int len; @@ -40,7 +40,7 @@ int ft_putnbrbase_p(unsigned long int n, char *base) len += write(1, &base[n % base_len], 1); else { - len += ft_putnbrbase_p(n / base_len, base); + len += ft_putnbrbasep(n / base_len, base); len += write(1, &base[n % base_len], 1); } return (len); diff --git a/libft/print/printf/ft_putstr.c b/libft/print/printf/ft_putstr.c index ef55877..13a5ad0 100644 --- a/libft/print/printf/ft_putstr.c +++ b/libft/print/printf/ft_putstr.c @@ -6,11 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */ -/* Updated: 2024/03/12 15:05:16 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:24:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_printf.h" +#include "../../libft.h" int ft_putstr_p(char *s) { diff --git a/libft/str/ft_split.c b/libft/str/ft_split.c index 3927f1f..1e5e3bf 100644 --- a/libft/str/ft_split.c +++ b/libft/str/ft_split.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/12 09:14:19 by adjoly #+# #+# */ -/* Updated: 2024/02/04 14:43:50 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:21:29 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ static int ft_countletter(char const *s, char sep) return (i); } -static void *ft_freearr(char **arr) +static void *ft_freearr_s(char **arr) { int i; @@ -67,7 +67,7 @@ char **ft_split_to_result(char **tab, char const *s, char c) k = 0; tab[j] = ft_calloc(ft_countletter(&s[i], c) + 1, sizeof(char)); if (!tab[j]) - return (ft_freearr(tab)); + return (ft_freearr_s(tab)); while (s[i] && s[i] != c) tab[j][k++] = s [i++]; tab[j][k] = '\0'; diff --git a/src/main.c b/src/main.c index 2cb6020..543fca1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,23 +6,47 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */ -/* Updated: 2024/04/25 11:30:17 by adjoly ### ########.fr */ +/* Updated: 2024/04/27 17:34:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include #include "libft.h" t_boolean is_str(char *src, char *dst) { - while (*src++ == *dst++) - ; + while (*src && *dst && *src == *dst) + { + src++; + dst++; + } if (*src) return (FALSE); return (TRUE); } +void get_prompt(void) +{ + char *pwd; + char *home; + char *user; + char *host; + + pwd = getenv("PWD"); + home = getenv("HOME"); + host = getenv("HOST"); + user = getenv("USER"); + + if (!ft_strncmp(pwd, home, ft_strlen(home))) + { + ft_printf("%s@%s:~%s> ", user, host, pwd + ft_strlen(home)); + return ; + } + ft_printf("%s@%s:%s> ", user, host, pwd); +} + int main(int ac, char **av, char **env) { char *test; @@ -33,16 +57,14 @@ int main(int ac, char **av, char **env) (void)env; while (1) { - test = readline("test>"); + get_prompt(); + test = readline(NULL); lll = ft_split(test, ' '); - if (is_str(*lll, "exit") == TRUE) - return (0); - if (is_str(*lll, "uwu") == TRUE) - { - printf("go burn in hell\n"); - return (0); - } - printf("%s\n", test); + if (!*lll) + continue; + if (is_str(test, "exit")) + break; } + ft_freearr((void **)lll); return (0); }