diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..507d0e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.dSYM +.gitignore diff --git a/Bug_report_Pipex.jpeg b/Bug_report_Pipex.jpeg deleted file mode 100644 index 736e336..0000000 Binary files a/Bug_report_Pipex.jpeg and /dev/null differ diff --git a/Makefile b/Makefile index decb687..a73dbeb 100644 --- a/Makefile +++ b/Makefile @@ -5,96 +5,61 @@ # +:+ +:+ +:+ # # By: mabbas +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # -# Created: 2022/12/18 02:39:41 by mabbas #+# #+# # -# Updated: 2022/12/20 02:31:26 by mabbas ### ########.fr # +# Created: 2022/12/23 01:03:33 by mabbas #+# #+# # +# Updated: 2022/12/23 03:05:17 by mabbas ### ########.fr # # # # **************************************************************************** # -NAME = pipex -NAME_B = pipex_bonus -#define compiler and flags for Debugger -CC = gcc -CFLAGS = -Wall -Werror -Wextra -g -INC = -I ./includes/ - -LIBFT = ./libs/libft/ - -SRCS = src/pipex.c \ - src/pipex_utils.c \ - libs/gnl/get_next_line.c \ - libs/gnl/get_next_line_utils.c -SRCS_B = src/pipex_bonus.c \ - src/pipex_utils.c \ - src/pipex_utils_bonus.c \ - libs/gnl/get_next_line.c \ - libs/gnl/get_next_line_utils.c -OBJS = ${SRCS:.c=.o} -OBJS_B = ${SRCS_B:.c=.o} +PROG = pipex +PROG_B = pipex_bonus +SRCS = srcs/pipex.c srcs/utils.c +OBJS = ${SRCS:.c=.o} +MAIN = srcs/pipex.c -SUBM_STATE := $(shell find libs/libft -type f && libs/gnl -type f) +SRCS_B = srcs/pipex_bonus.c srcs/utils.c srcs/utils_bonus.c +OBJS_B = ${SRCS_B:.c=.o} +MAIN_B = srcs/pipex_bonus.c -ifeq ($(SUBM_STATE),) -SUBM_FLAG = submodule -else -SUBM_FLAG = -endif +HEADER = -Iincludes -NC := \033[m -B_RED := \033[1;31m -RED := \033[0;31m -B_GREEN := \033[1;32m -GREEN := \033[0;33m -B_BLUE := \033[1;34m -BLUE := \033[0;34m -PURPLE := \033[0;35m -WHCOLOR := \033[0;40m - - -UNAME = $(shell uname -s) +CC = gcc +CFLAGS = -Wall -Wextra -Werror -g -ifeq ($(UNAME),Linux) - VALGRIND = valgrind -q --leak-check=full --track-origin=yes -else - detected_OS := $(shell sh -c 'uname 2>/dev/null || echo You are Mac stupid!_!') -endif +.c.o: %.o : %.c + @gcc ${CFLAGS} ${HEADER} -c $< -o $(<:.c=.o) -all: libft $(NAME) +all: ${PROG} -%.o : %.c - @$(CC) $(CFLAGS) -c $< -o $@ $(INC) +${PROG}: ${OBJS} + @echo "\033[33m----Compiling libft---" + @make re -C ./lib/libft + @$(CC) ${OBJS} -Llib/libft -lft -o ${PROG} + @echo "\033[32mPipex Compiled! (\033[31m\033[32m_\033[31m\033[32m)\n" -submodule: - git submodule init - git submodule update -libft: - @echo "____!!!$(BLUE)----- Compiling Libft------$(NC)" - @$(MAKE) -C $(LIBFT) - @$(MAKE) -C $(LIBFT) bonus - @echo "Compiling Libft: \033[1;32mOK\033[m" - +bonus: ${PROG_B} -$(NAME): $(OBJS) - @$(CC) $(CFLAGS) $(OBJS) $(LIBFT)libft.a -o $(NAME) - @echo "Pipex: \033[1;32mCOMPILED⛓️\033[m" +${PROG_B}: ${OBJS_B} + @echo "\033[33m----Compiling lib----" + @make re -C ./lib/libft + @$(CC) ${OBJS_B} -Llib/libft -lft -o pipex + @echo "\033[32mPipex Bonus Compiled!(\033[31m\033[32m_\033[31m\033[32m)\n" clean: - @echo "$(PURPLE)🚿 Sweeping in progress $(B_RED)🚿" - @rm -f $(OBJS) - @$(MAKE) -C $(LIBFT) clean - @sleep 0.5 - @echo "$(B_GREEN) Swept off 🧹🧹🧹$(NC)" + @make clean -C ./lib/libft + @rm -f ${OBJS} ${OBJS_B} -fclean: clean - @rm -f $(NAME) - @echo "$(GREEN) I am gone forever🚿🚿🚿 $(B_RED)🚿🚿 $(NC)" +fclean: clean + @make fclean -C ./lib/libft + @rm -f $(NAME) + @rm -f ${PROG} + @echo "\n\033[31mDeleting EVERYTHING! ⌐(ಠ۾ಠ)¬\n" -re: fclean all +re: fclean all -.phony: all libft clean fclean -.silent: all libft clean fclean +re_bonus: fclean bonus - \ No newline at end of file +.PHONY: all clean fclean re re_bonus bonus \ No newline at end of file diff --git a/README.md b/README.md index b50af43..7a4d335 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ # Pipex +Looking underneath the infamous ( | ) command and learning to execute multiple commands in one line. + + + ## Learning Goal: @@ -13,3 +17,28 @@ [5.]☑ File descriptors and when and what happens with stdin, stdout, stderror, [6.]☑ usage of perror. + +## Functions/Ideas yet to be discovered more: + +[7.] unlink , access, strerror + +[8.] How to shorten your Flags(O_CREAT) for open/close function by using void pointer. + + + +## One basic applications of the operator is here: + +image + +## **heredoc** applications: + +image + + + + + +## Sources Used to learn : + +1. https://csnotes.medium.com/pipex-tutorial-42-project-4469f5dd5901 +2. https://www.youtube.com/watch?v=6xbLgZpOBi8 diff --git a/b.txt b/b.txt new file mode 100755 index 0000000..d546a97 --- /dev/null +++ b/b.txt @@ -0,0 +1 @@ +askdjsa diff --git a/includes/pipex.h b/includes/pipex.h index 8504f19..5807d0f 100644 --- a/includes/pipex.h +++ b/includes/pipex.h @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: mabbas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2022/12/18 02:33:33 by mabbas #+# #+# */ -/* Updated: 2022/12/21 04:18:17 by mabbas ### ########.fr */ +/* Created: 2021/08/02 09:58:35 by mabbas #+# #+# */ +/* Updated: 2022/12/23 02:31:56 by mabbas ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,29 +15,19 @@ # include # include +# include # include # include -# include # include -# include -# include "../libs/libft/libft.h" -# include "../libs/gnl/get_next_line.h" +# include "../lib/libft/libft.h" -typedef struct s_pipe -{ - int fd[2]; - pid_t pid1; - pid_t reader; - int in_file; - int out_file; -} t_pipe; - -/** Pipe opetator initialization and file handling **/ +/* Mandatory functions */ void error(void); -char *path_find(char *cmd, char **envp); -void exec(char *argv, char **envp); - -int file_open(char *argv, int i); -void display(void); +char *find_path(char *cmd, char **envp); +void execute(char *argv, char **envp); +int get_next_line(char **line); +/* Bonus functions */ +int open_file(char *argv, int i); +void usage(void); #endif diff --git a/lib/libft/Makefile b/lib/libft/Makefile new file mode 100644 index 0000000..e4388ac --- /dev/null +++ b/lib/libft/Makefile @@ -0,0 +1,50 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mabbas +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/12/23 00:46:49 by mabbas #+# #+# # +# Updated: 2022/12/23 00:46:49 by mabbas ### ########.fr # +# # +# **************************************************************************** # + + +NAME = libft.a +SRCS = ft_isalnum.c ft_isdigit.c ft_memccpy.c ft_memcpy.c ft_putchar_fd.c\ + ft_putstr_fd.c ft_strdup.c ft_strlcpy.c ft_strncmp.c ft_strtrim.c\ + ft_toupper.c ft_bzero.c ft_isalpha.c ft_isprint.c ft_memchr.c\ + ft_memmove.c ft_putendl_fd.c ft_split.c ft_strjoin.c ft_strlen.c\ + ft_strnstr.c ft_substr.c ft_atoi.c ft_calloc.c ft_isascii.c ft_itoa.c\ + ft_memcmp.c ft_memset.c ft_putnbr_fd.c ft_strchr.c ft_strlcat.c\ + ft_strmapi.c ft_strrchr.c ft_tolower.c ft_atof.c +OBJS = ${SRCS:.c=.o} +SRCS_B = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c\ + ft_lstadd_back.c ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c +OBJS_B = ${SRCS_B:.c=.o} +HEADER = ./ + +CC = gcc +CFLAGS = -Wall -Wextra -Werror + +.c.o: %.o : %.c + @${CC} ${CFLAGS} -I ${HEADER} -c $< -o $(<:.c=.o) + +$(NAME): ${OBJS} + @ar rc ${NAME} ${OBJS} + +all: ${NAME} + +bonus: ${OBJS} ${OBJS_B} + @ar rc ${NAME} ${OBJS} ${OBJS_B} + +clean: + @rm -f ${OBJS} ${OBJS_B} + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re bonus \ No newline at end of file diff --git a/lib/libft/ft_atof.c b/lib/libft/ft_atof.c new file mode 100644 index 0000000..728290e --- /dev/null +++ b/lib/libft/ft_atof.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/07/12 15:38:52 by gcollet #+# #+# */ +/* Updated: 2021/07/28 16:25:22 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +double ft_atof(const char *str) +{ + double res; + double res2; + char *c; + int len; + + c = (char *)str; + res = (double)ft_atoi(c); + while (*c && *c != '.') + c++; + if (*c == '.') + c++; + res2 = (double)ft_atoi(c); + len = ft_strlen(c); + while (len--) + res2 /= 10; + if (res >= 0) + return (res + res2); + else + return (res + -res2); +} diff --git a/lib/libft/ft_atoi.c b/lib/libft/ft_atoi.c new file mode 100644 index 0000000..79f86ca --- /dev/null +++ b/lib/libft/ft_atoi.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/08 11:53:08 by gcollet #+# #+# */ +/* Updated: 2021/05/12 17:33:21 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction atoi() convertit le début de la chaîne pointée par nptr en +entier de type int . */ +/* VALEUR RENVOYÉE : Le résultat de la conversion. */ + +#include "libft.h" + +int ft_atoi(const char *nptr) +{ + long num; + int sign; + int i; + + i = 0; + sign = 1; + num = 0; + while ((nptr[i] >= '\t' && nptr[i] <= '\r') || nptr[i] == ' ') + nptr++; + if (nptr[i] == '-') + sign = sign * -1; + if (nptr[i] == '+' || nptr[i] == '-') + nptr++; + while (nptr[i] >= '0' && nptr[i] <= '9') + { + num = (num * 10) + nptr[i] - '0'; + nptr++; + if (num * sign > 2147483647) + return (-1); + if (num * sign < -2147483648) + return (0); + } + return (num * sign); +} diff --git a/lib/libft/ft_bzero.c b/lib/libft/ft_bzero.c new file mode 100644 index 0000000..1ec00d6 --- /dev/null +++ b/lib/libft/ft_bzero.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/05 15:00:08 by gcollet #+# #+# */ +/* Updated: 2021/05/08 12:41:53 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction bzero() met à 0 (octets contenant « \0 ») les n premiers octets +du bloc pointé par s. */ +/* VALEUR RENVOYÉE : Aucune. */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + char *str; + + str = (char *)s; + while (n) + { + *str = 0; + str++; + n--; + } +} diff --git a/lib/libft/ft_calloc.c b/lib/libft/ft_calloc.c new file mode 100644 index 0000000..6d7f9b0 --- /dev/null +++ b/lib/libft/ft_calloc.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/08 12:17:37 by gcollet #+# #+# */ +/* Updated: 2021/05/12 15:28:51 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* calloc() alloue la mémoire necessaire pour un tableau de nmemb éléments de +taille size octets, et renvoie un pointeur vers la mémoire allouée. Cette zone +est remplie avec des zéros. Si nmemb ou size vaut 0, calloc() renvoie soit +NULL, soit un pointeur unique qui pourra être passé ultérieurement à free() +avec succès. */ + +#include "libft.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + char *ptr; + + if (nmemb == 0 || size == 0) + { + nmemb = 1; + size = 1; + } + ptr = malloc (nmemb * size); + if (ptr) + ft_bzero(ptr, nmemb * size); + return (ptr); +} diff --git a/lib/libft/ft_isalnum.c b/lib/libft/ft_isalnum.c new file mode 100644 index 0000000..245a76e --- /dev/null +++ b/lib/libft/ft_isalnum.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/06 16:21:34 by gcollet #+# #+# */ +/* Updated: 2021/05/06 16:42:35 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Vérifie si l'on a un caractère alphanumérique. */ +/* Les valeurs renvoyées sont non nulles si le caractère c entre dans la +catégorie testée, et zéro sinon */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') + || (c >= '0' && c <= '9')) + { + return (1); + } + return (0); +} diff --git a/lib/libft/ft_isalpha.c b/lib/libft/ft_isalpha.c new file mode 100644 index 0000000..97de5e2 --- /dev/null +++ b/lib/libft/ft_isalpha.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/06 15:08:39 by gcollet #+# #+# */ +/* Updated: 2021/05/06 15:45:10 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Vérifie si l'on a un caractère alphabétique. */ +/* Les valeurs renvoyées sont non nulles si le caractère c entre dans la +catégorie testée, et zéro sinon */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return (1); + return (0); +} diff --git a/lib/libft/ft_isascii.c b/lib/libft/ft_isascii.c new file mode 100644 index 0000000..b049c10 --- /dev/null +++ b/lib/libft/ft_isascii.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 10:04:18 by gcollet #+# #+# */ +/* Updated: 2021/05/07 10:25:01 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Vérifie si c est un unsigned char sur 7 bits, entrant dans le jeu de +caractères ASCII. */ +/* Les valeurs renvoyées sont non nulles si le caractère c entre dans la +catégorie testée, et zéro sinon */ + +#include "libft.h" + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/lib/libft/ft_isdigit.c b/lib/libft/ft_isdigit.c new file mode 100644 index 0000000..e3714df --- /dev/null +++ b/lib/libft/ft_isdigit.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/06 16:04:28 by gcollet #+# #+# */ +/* Updated: 2021/05/12 10:09:29 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Vérifie si l'on a un chiffre (0 à 9). */ +/* Les valeurs renvoyées sont non nulles si le caractère c entre dans la +catégorie testée, et zéro sinon */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/lib/libft/ft_isprint.c b/lib/libft/ft_isprint.c new file mode 100644 index 0000000..520492a --- /dev/null +++ b/lib/libft/ft_isprint.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 10:27:30 by gcollet #+# #+# */ +/* Updated: 2021/05/07 10:33:03 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Vérifie s'il s'agit d'un caractère imprimable, y compris l'espace. */ +/* Les valeurs renvoyées sont non nulles si le caractère c entre dans la +catégorie testée, et zéro sinon */ + +#include "libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/lib/libft/ft_itoa.c b/lib/libft/ft_itoa.c new file mode 100644 index 0000000..f41e9fc --- /dev/null +++ b/lib/libft/ft_itoa.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/10 17:26:08 by gcollet #+# #+# */ +/* Updated: 2021/05/13 15:57:51 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Alloue (avec malloc) et retourne une chaine de caractères représentant +l’integer reçu en argument .Les nombres négatifs doivent être gérés. */ +/* La chaine de caractères représentant l’integer. NULL si l’allocation +échoue. */ + +#include "libft.h" + +static char *ft_swap(char *tab, int size) +{ + int temp; + int i; + int j; + + i = 0; + j = size - 1; + if (tab[i] == '-') + i++; + while (i < j) + { + temp = tab[i]; + tab[i] = tab[j]; + tab[j] = temp; + i++; + j--; + } + return (tab); +} + +static char *ft_is_zero(char *tab) +{ + int i; + + i = 0; + tab[i++] = '0'; + tab[i] = '\0'; + return (tab); +} + +static int ft_len(int c) +{ + int len; + + len = 1; + if (c >= 0) + c *= -1; + else + len++; + while (c <= -10) + { + c /= 10; + len++; + } + return (len); +} + +char *ft_itoa(int n) +{ + char *tab; + int i; + long l_n; + int len; + + i = 0; + len = ft_len(n); + l_n = n; + tab = (char *)malloc(sizeof(char) * (len + 1)); + if (!tab) + return (0); + if (l_n == 0) + return (ft_is_zero(tab)); + if (l_n < 0) + { + l_n = -l_n; + tab[i++] = '-'; + } + while (l_n != 0) + { + tab[i++] = (l_n % 10) + '0'; + l_n /= 10; + } + tab [i] = '\0'; + return (ft_swap (tab, ft_strlen(tab))); +} diff --git a/includes/mode.h b/lib/libft/ft_lstadd_back.c similarity index 55% rename from includes/mode.h rename to lib/libft/ft_lstadd_back.c index 965dcc3..2c70388 100644 --- a/includes/mode.h +++ b/lib/libft/ft_lstadd_back.c @@ -1,28 +1,28 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* mode.h :+: :+: :+: */ +/* ft_lstadd_back.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mabbas +#+ +:+ +#+ */ +/* By: gcollet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2022/12/21 04:15:27 by mabbas #+# #+# */ -/* Updated: 2022/12/21 04:17:29 by mabbas ### ########.fr */ +/* Created: 2021/05/20 11:10:13 by gcollet #+# #+# */ +/* Updated: 2021/05/20 11:48:50 by gcollet ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef MODE_H -# define MODE_H +/* Ajoute l’élément new à la fin de la liste. */ -# include +#include "libft.h" -typedef struct s_mode +void ft_lstadd_back(t_list **alst, t_list *new) { - int mode; - char *name; -} t_filemode; + t_list *temp; -t_filemode g_file_mode[] = {{O_WRONLY | O_CREAT | O_APPEND, "append"}, \ - {O_WRONLY | O_CREAT | O_TRUNC, "truncate"}, \ - {O_RDONLY, "read"}}; - -#endif \ No newline at end of file + if (*alst == NULL) + *alst = new; + else + { + temp = ft_lstlast(*(alst)); + temp->next = new; + } +} diff --git a/lib/libft/ft_lstadd_front.c b/lib/libft/ft_lstadd_front.c new file mode 100644 index 0000000..261e2e4 --- /dev/null +++ b/lib/libft/ft_lstadd_front.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 09:29:13 by gcollet #+# #+# */ +/* Updated: 2021/05/20 10:49:20 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Ajoute l’élément ’new’ au début de la liste. */ + +#include "libft.h" + +void ft_lstadd_front(t_list **alst, t_list *new) +{ + new->next = *alst; + *alst = new; +} diff --git a/lib/libft/ft_lstclear.c b/lib/libft/ft_lstclear.c new file mode 100644 index 0000000..9ff9406 --- /dev/null +++ b/lib/libft/ft_lstclear.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 13:17:48 by gcollet #+# #+# */ +/* Updated: 2021/05/20 15:04:09 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Supprime et libère la mémoire de l’élément passé en paramètre, et de tous +les élements qui suivent, à l’aide de del et de free(3) Enfin, le pointeur +initial doit être mis à NULL. */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void*)) +{ + t_list *temp; + + if (lst == NULL) + return ; + while (*lst) + { + temp = (*lst)->next; + ft_lstdelone(*lst, del); + (*lst) = temp; + } +} diff --git a/lib/libft/ft_lstdelone.c b/lib/libft/ft_lstdelone.c new file mode 100644 index 0000000..5bc96cb --- /dev/null +++ b/lib/libft/ft_lstdelone.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 11:52:19 by gcollet #+# #+# */ +/* Updated: 2021/05/20 13:45:11 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Libère la mémoire de l’élément passé en argument en utilisant la fonction +del puis avec free(3). La mémoire de next ne doit pas être free. */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void*)) +{ + if (lst == NULL) + return ; + if (lst->content) + { + del(lst->content); + free(lst); + } +} diff --git a/lib/libft/ft_lstiter.c b/lib/libft/ft_lstiter.c new file mode 100644 index 0000000..d619292 --- /dev/null +++ b/lib/libft/ft_lstiter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 13:40:10 by gcollet #+# #+# */ +/* Updated: 2021/05/20 15:04:34 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Itère sur la list lst et applique la fonction f au contenu chaque élément. */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + while (lst) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/lib/libft/ft_lstlast.c b/lib/libft/ft_lstlast.c new file mode 100644 index 0000000..dc9202b --- /dev/null +++ b/lib/libft/ft_lstlast.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 10:48:37 by gcollet #+# #+# */ +/* Updated: 2021/05/20 11:07:54 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Renvoie le dernier élément de la liste. */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst) + { + while (lst->next) + lst = lst->next; + } + return (lst); +} diff --git a/lib/libft/ft_lstmap.c b/lib/libft/ft_lstmap.c new file mode 100644 index 0000000..2466f7c --- /dev/null +++ b/lib/libft/ft_lstmap.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 13:50:09 by gcollet #+# #+# */ +/* Updated: 2021/05/20 15:16:58 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Itère sur la liste lst et applique la fonction f au contenu de chaque +élément. Crée une nouvelle liste résultant des applications successives de f. +La fonction del est la pour detruire le contenu d un element si necessaire */ + +#include "libft.h" + +static t_list *ft_newlst(t_list *new_lst, t_list *lst, void *(*f)(void *) + , void (*del)(void *)) +{ + t_list *add; + + add = ft_lstnew(f(lst->content)); + if (add == NULL) + { + ft_lstclear(&new_lst, del); + return (NULL); + } + ft_lstadd_back(&new_lst, add); + return (new_lst); +} + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *new_lst; + + if (lst == NULL) + return (NULL); + new_lst = ft_lstnew(f(lst->content)); + if (new_lst == NULL) + return (NULL); + lst = lst->next; + while (lst) + { + ft_newlst(new_lst, lst, f, del); + lst = lst->next; + } + return (new_lst); +} diff --git a/lib/libft/ft_lstnew.c b/lib/libft/ft_lstnew.c new file mode 100644 index 0000000..a257ede --- /dev/null +++ b/lib/libft/ft_lstnew.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 08:21:11 by gcollet #+# #+# */ +/* Updated: 2021/05/20 11:42:33 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Alloue (avec malloc) et renvoie un nouvel élément. La variable content est +initialisée à l’aide de la valeur du paramètre content. La variable ’next’ est +initialisée à NULL. */ +/* Valeur de retour: Le nouvel element */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *list; + + list = malloc(sizeof(*list)); + if (list == NULL) + return (NULL); + list->content = content; + list->next = NULL; + return (list); +} diff --git a/lib/libft/ft_lstsize.c b/lib/libft/ft_lstsize.c new file mode 100644 index 0000000..75ae237 --- /dev/null +++ b/lib/libft/ft_lstsize.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 10:10:24 by gcollet #+# #+# */ +/* Updated: 2021/05/20 10:50:27 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Compte le nombre d’éléments de la liste. */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + i = 0; + while (lst) + { + lst = lst->next; + i++; + } + return (i); +} diff --git a/lib/libft/ft_memccpy.c b/lib/libft/ft_memccpy.c new file mode 100644 index 0000000..7c95847 --- /dev/null +++ b/lib/libft/ft_memccpy.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/06 10:03:50 by gcollet #+# #+# */ +/* Updated: 2021/05/11 13:52:27 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction memccpy() copie au plus n octets de la zone mémoire src vers +la zone mémoire dest, s'arrêtant dès qu'elle rencontre le caractère c. */ +/* La fonction memccpy() renvoie un pointeur sur le caractère immédiatement +après c dans la zone dest, ou NULL si c n'a pas été trouvé dans les n premiers +caractères de src. */ + +#include "libft.h" + +void *ft_memccpy(void *dest, const void *src, int c, size_t n) +{ + size_t i; + unsigned char *str_dest; + unsigned char *str_src; + + str_dest = (unsigned char *)dest; + str_src = (unsigned char *)src; + i = 0; + while (i < n) + { + str_dest[i] = str_src[i]; + if (str_dest[i] == (unsigned char)c) + return (dest + i + 1); + i++; + } + return (NULL); +} diff --git a/lib/libft/ft_memchr.c b/lib/libft/ft_memchr.c new file mode 100644 index 0000000..e40c94c --- /dev/null +++ b/lib/libft/ft_memchr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/06 11:23:30 by gcollet #+# #+# */ +/* Updated: 2021/05/11 13:18:41 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction memchr() examine les n premiers octets de la zone mémoire +pointée par s à la recherche du caractère c. Le premier octet correspondant à c +(interprété comme un unsigned char) arrête l'opération. */ +/* La fonction memchr() renvoie un pointeur sur l'octet correspondant, ou NULL +si le caractère n'est pas présent dans la zone de mémoire concernée. */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + unsigned char *str_s; + + str_s = (unsigned char *)s; + i = 0; + while (i < n) + { + if (str_s[i] == (unsigned char)c) + return (str_s + i); + i++; + } + return (NULL); +} diff --git a/lib/libft/ft_memcmp.c b/lib/libft/ft_memcmp.c new file mode 100644 index 0000000..d17a009 --- /dev/null +++ b/lib/libft/ft_memcmp.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/06 11:49:42 by gcollet #+# #+# */ +/* Updated: 2021/05/12 16:49:27 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction memcmp() compare les n premiers octets des zones mémoire s1 et +s2. Elle renvoie un entier inférieur, égal, ou supérieur à zéro, si s1 est +respectivement inférieure, égale ou supérieur à s2. */ +/* La fonction memcmp() renvoie un entier négatif, nul ou positif si les n +premiers octets de s1 sont respectivement inférieurs, égaux ou supérieurs aux n +premiers octets de s2. */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned int i; + unsigned char *str_s1; + unsigned char *str_s2; + + i = 0; + str_s1 = (unsigned char *)s1; + str_s2 = (unsigned char *)s2; + if (n == 0) + return (0); + while (str_s1[i] == str_s2[i] && i < n - 1) + { + i++; + } + return (str_s1[i] - str_s2[i]); +} diff --git a/lib/libft/ft_memcpy.c b/lib/libft/ft_memcpy.c new file mode 100644 index 0000000..84f9c3e --- /dev/null +++ b/lib/libft/ft_memcpy.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/05 11:18:02 by gcollet #+# #+# */ +/* Updated: 2021/05/13 13:17:42 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction memcpy() copie n octets depuis la zone mémoire src vers la zone +mémoire dest. Les deux zones ne doivent pas se chevaucher. Si c'est le cas, +utilisez plutôt memmove(). */ +/* La fonction memcpy() renvoie un pointeur sur dest. */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + + i = 0; + if (!dest && !src) + return (NULL); + while (i < n) + { + ((unsigned char *)dest)[i] = ((unsigned char *)src)[i]; + i++; + } + return (dest); +} diff --git a/lib/libft/ft_memmove.c b/lib/libft/ft_memmove.c new file mode 100644 index 0000000..8920416 --- /dev/null +++ b/lib/libft/ft_memmove.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/06 10:37:42 by gcollet #+# #+# */ +/* Updated: 2022/02/06 10:00:40 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction memmove() copie n octets depuis la zone mémoire src vers la zone +mémoire dest. Les deux zones peuvent se chevaucher : la copie se passe comme +si les octets de src étaient d'abord copiés dans une zone temporaire qui ne +chevauche ni src ni dest, et les octets sont ensuite copiés de la zone +temporaire vers dest. */ +/* La fonction memmove() renvoie un pointeur sur dest. */ + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + char *str_dest; + char *str_src; + + str_dest = (char *)dest; + str_src = (char *)src; + if (str_dest > str_src) + { + while (n--) + str_dest[n] = str_src[n]; + } + else if (str_dest < str_src) + ft_memcpy(dest, src, n); + return (dest); +} diff --git a/lib/libft/ft_memset.c b/lib/libft/ft_memset.c new file mode 100644 index 0000000..51dcd73 --- /dev/null +++ b/lib/libft/ft_memset.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/05 10:19:52 by gcollet #+# #+# */ +/* Updated: 2021/07/28 14:09:52 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction memset() remplit les n premiers octets de la zone mémoire +pointée par s avec l'octet c. */ +/* La fonction memset() renvoie un pointeur sur la zone mémoire s. */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + char *str; + + str = s; + while (n) + { + *str = (unsigned char)c; + str++; + n--; + } + return (s); +} diff --git a/lib/libft/ft_putchar_fd.c b/lib/libft/ft_putchar_fd.c new file mode 100644 index 0000000..70ce152 --- /dev/null +++ b/lib/libft/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/11 11:01:32 by gcollet #+# #+# */ +/* Updated: 2021/05/11 12:00:30 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Écrit le caractère ’c’ sur le file descriptor donné. */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write (fd, &c, 1); +} diff --git a/lib/libft/ft_putendl_fd.c b/lib/libft/ft_putendl_fd.c new file mode 100644 index 0000000..e1a3a2a --- /dev/null +++ b/lib/libft/ft_putendl_fd.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/11 11:44:55 by gcollet #+# #+# */ +/* Updated: 2021/05/13 16:00:14 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Écrit La chaine de caractères ’s’ sur le file descriptor donné, suivie +d’un retour à la ligne. */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + if (!s) + return ; + while (*s) + { + ft_putchar_fd(*s, fd); + s++; + } + ft_putchar_fd('\n', fd); +} diff --git a/lib/libft/ft_putnbr_fd.c b/lib/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..6ab0db8 --- /dev/null +++ b/lib/libft/ft_putnbr_fd.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/11 11:51:46 by gcollet #+# #+# */ +/* Updated: 2021/05/11 11:59:00 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Écrit l’integer ’n’ sur le file descriptor donné. */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + long i; + + i = n; + if (i < 0) + { + ft_putchar_fd('-', fd); + i = i * -1; + } + if (i > 9) + { + ft_putnbr_fd(i / 10, fd); + ft_putnbr_fd(i % 10, fd); + } + else + { + ft_putchar_fd(i + 48, fd); + } +} diff --git a/lib/libft/ft_putstr_fd.c b/lib/libft/ft_putstr_fd.c new file mode 100644 index 0000000..95742f7 --- /dev/null +++ b/lib/libft/ft_putstr_fd.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/11 11:17:23 by gcollet #+# #+# */ +/* Updated: 2021/05/13 15:59:39 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Écrit la chaine de caractères ’s’ sur le file descriptor donné. */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + if (!s) + return ; + while (*s) + { + ft_putchar_fd(*s, fd); + s++; + } +} diff --git a/lib/libft/ft_split.c b/lib/libft/ft_split.c new file mode 100644 index 0000000..e8fcb5b --- /dev/null +++ b/lib/libft/ft_split.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/10 14:59:30 by gcollet #+# #+# */ +/* Updated: 2021/05/13 15:11:03 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Alloue (avec malloc) et retourne un tableau de chaines de caracteres obtenu +en séparant ’s’ à l’aide du caractère ’c’, utilisé comme délimiteur. Le tableau +doit être terminé par NULL. */ +/* Le tableau de nouvelles chaines de caractères, résultant du découpage. +NULL si l’allocation échoue. */ + +#include "libft.h" + +static char **ft_malloc_error(char **tab) +{ + size_t i; + + i = 0; + while (tab[i]) + { + free(tab[i]); + i++; + } + free(tab); + return (NULL); +} + +static size_t ft_nb_words(char const *s, char c) +{ + size_t i; + size_t nb_words; + + if (!s[0]) + return (0); + i = 0; + nb_words = 0; + while (s[i] && s[i] == c) + i++; + while (s[i]) + { + if (s[i] == c) + { + nb_words++; + while (s[i] && s[i] == c) + i++; + continue ; + } + i++; + } + if (s[i - 1] != c) + nb_words++; + return (nb_words); +} + +static void ft_get_next_word(char **next_word, size_t *next_word_len, char c) +{ + size_t i; + + *next_word += *next_word_len; + *next_word_len = 0; + i = 0; + while (**next_word && **next_word == c) + (*next_word)++; + while ((*next_word)[i]) + { + if ((*next_word)[i] == c) + return ; + (*next_word_len)++; + i++; + } +} + +char **ft_split(char const *s, char c) +{ + char **tab; + char *next_word; + size_t next_word_len; + size_t i; + + if (!s) + return (NULL); + tab = (char **)malloc(sizeof(char *) * (ft_nb_words(s, c) + 1)); + if (!tab) + return (NULL); + i = 0; + next_word = (char *)s; + next_word_len = 0; + while (i < ft_nb_words(s, c)) + { + ft_get_next_word(&next_word, &next_word_len, c); + tab[i] = (char *)malloc(sizeof(char) * (next_word_len + 1)); + if (!tab[i]) + return (ft_malloc_error(tab)); + ft_strlcpy(tab[i], next_word, next_word_len + 1); + i++; + } + tab[i] = NULL; + return (tab); +} diff --git a/lib/libft/ft_strchr.c b/lib/libft/ft_strchr.c new file mode 100644 index 0000000..c5a6730 --- /dev/null +++ b/lib/libft/ft_strchr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 11:06:54 by gcollet #+# #+# */ +/* Updated: 2021/05/11 14:23:26 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction strchr() renvoie un pointeur sur la première occurrence du +caractère c dans la chaîne s. */ +/* La fonction strchr() renvoie un pointeur sur le caractère correspondant, +ou NULL si le caractère n'a pas été trouvé. */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + char *str; + + str = (char *)s; + while (*str != c) + { + if (*str == '\0') + return (NULL); + str++; + } + return (str); +} diff --git a/lib/libft/ft_strdup.c b/lib/libft/ft_strdup.c new file mode 100644 index 0000000..c1972a4 --- /dev/null +++ b/lib/libft/ft_strdup.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/08 12:51:40 by gcollet #+# #+# */ +/* Updated: 2021/05/13 16:40:52 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction strdup() renvoie un pointeur sur une nouvelle chaîne de +caractères qui est dupliquée depuis s. La mémoire occupée par cette nouvelle +chaîne est obtenue en appelant malloc(), et peut (doit) donc être libérée avec +free().*/ +/* La fonction strdup() renvoie un pointeur sur la chaîne dupliquée, ou NULL +s'il n'y avait pas assez de mémoire. */ + +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *tab; + int i; + + i = 0; + tab = (char *)malloc(sizeof(*tab) * (ft_strlen(s) + 1)); + if (!tab) + return (NULL); + while (s[i]) + { + tab[i] = s[i]; + i++; + } + tab[i] = '\0'; + return (tab); +} diff --git a/lib/libft/ft_strjoin.c b/lib/libft/ft_strjoin.c new file mode 100644 index 0000000..dc416e0 --- /dev/null +++ b/lib/libft/ft_strjoin.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/10 11:42:29 by gcollet #+# #+# */ +/* Updated: 2021/05/13 13:20:08 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Alloue (avec malloc) et retourne une nouvelle chaine, résultat de la +concaténation de s1 et s2. */ +/* La nouvelle chaine de caractères. NULL si l’allocation échoue. */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *tab; + int i; + int j; + + i = 0; + j = 0; + if (!s1 || !s2) + return (NULL); + tab = (char *)malloc(sizeof(*tab) * (ft_strlen(s1) + ft_strlen(s2)) + 1); + if (tab == 0) + return (NULL); + while (s1[i]) + { + tab[i] = s1[i]; + i++; + } + while (s2[j]) + { + tab[i] = s2[j]; + j++; + i++; + } + tab[i] = '\0'; + return (tab); +} diff --git a/lib/libft/ft_strlcat.c b/lib/libft/ft_strlcat.c new file mode 100644 index 0000000..bdbe4c7 --- /dev/null +++ b/lib/libft/ft_strlcat.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 14:49:15 by gcollet #+# #+# */ +/* Updated: 2021/05/17 10:15:29 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* The strlcat() function appends the NUL-terminated string src to the end of +dst. It will append at most size - strlen(dst) - 1 bytes, NUL-terminating the +result. */ +/* The strlcat() functions return the total length of the string they tried +to create that means the initial length of dst plus the length of src. */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t n; + size_t m; + size_t p; + + m = 0; + n = ft_strlen(dst); + p = ft_strlen(src); + if (size == 0) + return (p); + while ((src[m] != '\0') && ((n + m) < (size - 1))) + { + dst[n + m] = src[m]; + m++; + } + dst[n + m] = '\0'; + if (size > n) + return (n + p); + return (size + p); +} diff --git a/lib/libft/ft_strlcpy.c b/lib/libft/ft_strlcpy.c new file mode 100644 index 0000000..0d57e17 --- /dev/null +++ b/lib/libft/ft_strlcpy.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 13:31:38 by gcollet #+# #+# */ +/* Updated: 2021/05/11 14:40:33 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* The strlcpy() function copies up to size characters from the +NUL-terminated string src to dst, NUL-terminating the result. */ +/* The strlcpy() functions return the total length of the string +they tried to create. For strlcpy() that means the length of src. */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t i; + + i = 0; + if (!dst || !src) + return (0); + if (size > 0) + { + while (src[i] && --size) + { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; + } + while (src[i]) + i++; + return (i); +} diff --git a/lib/libft/ft_strlen.c b/lib/libft/ft_strlen.c new file mode 100644 index 0000000..dfdf22c --- /dev/null +++ b/lib/libft/ft_strlen.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/06 14:39:40 by gcollet #+# #+# */ +/* Updated: 2021/05/06 15:00:37 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction strlen() calcule la longueur de la chaîne de caractères s, sans +compter l'octet nul « \0 » final. */ +/* La fonction strlen() renvoie le nombre de caractères dans la chaîne s. */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t i; + + i = 0; + while (s[i]) + i++; + return (i); +} diff --git a/lib/libft/ft_strmapi.c b/lib/libft/ft_strmapi.c new file mode 100644 index 0000000..b92af1b --- /dev/null +++ b/lib/libft/ft_strmapi.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/11 10:20:19 by gcollet #+# #+# */ +/* Updated: 2021/05/13 13:34:37 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* Applique la fonction ’f’ à chaque caractère de la chaine de caractères +passée en argument pour créer une nouvelle chaine de caractères (avec malloc) +résultant des applications successives de ’f’. */ +/* La chaine de caractères résultant des application ssuccessives de f. +Retourne NULL si l’allocation échoue. */ + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + int i; + char *tab; + + i = 0; + if (!s || !f) + return (NULL); + tab = (char *)malloc(sizeof(char) * ft_strlen(s) + 1); + if (tab == 0) + return (NULL); + while (s[i]) + { + tab[i] = (*f)(i, s[i]); + i++; + } + tab[i] = '\0'; + return (tab); +} diff --git a/lib/libft/ft_strncmp.c b/lib/libft/ft_strncmp.c new file mode 100644 index 0000000..5b7d741 --- /dev/null +++ b/lib/libft/ft_strncmp.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mabbas +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 11:51:46 by gcollet #+# #+# */ +/* Updated: 2022/12/23 01:56:56 by mabbas ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction strcmp() compare les deux chaînes s1 et s2. Elle renvoie un +entier négatif, nul, ou positif, si s1 est respectivement inférieure, égale +ou supérieure à s2. */ +/* La fonction strncmp() renvoie un entier inférieur, égal ou supérieur à zéro +si s1 (ou ses n premiers octets) est respectivement inférieure, égale ou +supérieure à s2. */ + +#include "libft.h" + +int ft_strncmp(const char *str1, const char *str2, size_t n) +{ + while (n-- > 0 && (*str1 || *str2)) + { + if (*str1 != *str2) + return ((*(unsigned char *) str1) - (*(unsigned char *) str2)); + str1++; + str2++; + } + return (0); +} diff --git a/lib/libft/ft_strnstr.c b/lib/libft/ft_strnstr.c new file mode 100644 index 0000000..a0e1214 --- /dev/null +++ b/lib/libft/ft_strnstr.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/08 11:17:09 by gcollet #+# #+# */ +/* Updated: 2021/05/11 13:33:00 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* The strnstr() function locates the first occurrence of the null-terminated +string little in the string big, where not more than len character are +searched. */ +/* If little is an empty string, big is returned; if little occurs nowhere in +big, NULL is returned; otherwise a pointer to the first character of the first +occurrence of little is returned. */ + +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t j; + + i = 0; + if (little[i] == '\0') + return ((char *)big); + while (big[i] && i < len) + { + j = 0; + while (big[i + j] == little[j] && i + j < len) + { + if (little[j + 1] == '\0') + return ((char *)big + i); + j++; + } + i++; + } + return (0); +} diff --git a/lib/libft/ft_strrchr.c b/lib/libft/ft_strrchr.c new file mode 100644 index 0000000..11f2c94 --- /dev/null +++ b/lib/libft/ft_strrchr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 11:38:19 by gcollet #+# #+# */ +/* Updated: 2021/05/11 14:49:22 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* La fonction strrchr() renvoie un pointeur sur la dernière occurrence du +caractère c dans la chaîne s. */ +/* La fonction strrchr() renvoie un pointeur sur le caractère correspondant, +ou NULL si le caractère n'a pas été trouvé. */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + + i = 0; + while (s[i]) + i++; + if (c == '\0') + return ((char *)s + i); + while (i--) + { + if (s[i] == c) + return ((char *)(s + i)); + } + return (0); +} diff --git a/lib/libft/ft_strtrim.c b/lib/libft/ft_strtrim.c new file mode 100644 index 0000000..c2e7551 --- /dev/null +++ b/lib/libft/ft_strtrim.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/10 12:46:47 by gcollet #+# #+# */ +/* Updated: 2021/05/13 13:30:05 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Alloue (avec malloc) et retourne une copie de la chaine ’s1’, sans les +caractères spécifiés dans ’set’ au début et à la fin de la chaine de +caractères. */ +/* La chaine de caractères trimmée. NULL si l’allocation échoue. */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t len; + char *tab; + + if (!s1 || !set) + return (NULL); + while (*s1 && ft_strchr(set, *s1)) + s1++; + len = ft_strlen(s1); + while (len && ft_strchr(set, s1[len])) + len--; + tab = ft_substr((char *)s1, 0, len + 1); + return (tab); +} diff --git a/lib/libft/ft_substr.c b/lib/libft/ft_substr.c new file mode 100644 index 0000000..45f68fe --- /dev/null +++ b/lib/libft/ft_substr.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/10 10:51:58 by gcollet #+# #+# */ +/* Updated: 2021/05/13 16:02:24 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* Alloue (avec malloc()) et retourne une chaine de caractères issue de la +chaine ’s’. Cette nouvelle chaine commence à l’index ’start’ et a pour taille +maximale ’len’ */ +/* La nouvelle chaine de caractere. NULL si l’allocation échoue. */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *tab; + size_t i; + size_t j; + + i = 0; + j = 0; + if (!s) + return (NULL); + tab = (char *)malloc(sizeof(*s) * (len + 1)); + if (!tab) + return (NULL); + while (s[i]) + { + if (i >= start && j < len) + { + tab[j] = s[i]; + j++; + } + i++; + } + tab[j] = '\0'; + return (tab); +} diff --git a/lib/libft/ft_tolower.c b/lib/libft/ft_tolower.c new file mode 100644 index 0000000..024383f --- /dev/null +++ b/lib/libft/ft_tolower.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 11:01:50 by gcollet #+# #+# */ +/* Updated: 2021/05/07 11:05:35 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* tolower() convertit la lettre c en minuscule si c'est possible. */ +/* La valeur renvoyée est celle de la lettre convertie, ou bien c si la +conversion n'était pas possible. */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + c += 32; + return (c); +} diff --git a/lib/libft/ft_toupper.c b/lib/libft/ft_toupper.c new file mode 100644 index 0000000..c98b9f7 --- /dev/null +++ b/lib/libft/ft_toupper.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gcollet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/07 10:50:12 by gcollet #+# #+# */ +/* Updated: 2021/05/07 11:03:34 by gcollet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* toupper() convertit la lettre c en majuscule si c'est possible. */ +/* La valeur renvoyée est celle de la lettre convertie, ou bien c si la à +conversion n'était pas possible. */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + c -= 32; + return (c); +} diff --git a/lib/libft/libft.h b/lib/libft/libft.h new file mode 100644 index 0000000..0c29bed --- /dev/null +++ b/lib/libft/libft.h @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mabbas +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/23 01:04:25 by mabbas #+# #+# */ +/* Updated: 2022/12/23 01:04:28 by mabbas ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_atoi(const char *nptr); +double ft_atof(const char *str); +void ft_bzero(void *s, size_t n); +void *ft_calloc(size_t nmemb, size_t size); +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_isprint(int c); +char *ft_itoa(int n); +void *ft_memccpy(void *dest, const void *src, int c, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memset(void *s, int c, size_t n); +void ft_putchar_fd(char c, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_putstr_fd(char *s, int fd); +char **ft_split(char const *s, char c); +char *ft_strchr(const char *s, int c); +char *ft_strdup(const char *s); +char *ft_strjoin(char const *s1, char const *s2); +size_t ft_strlcat(char *dst, const char *src, size_t size); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlen(const char *s); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strrchr(const char *s, int c); +char *ft_strtrim(char const *s1, char const *set); +char *ft_substr(char const *s, unsigned int start, size_t len); +int ft_tolower(int c); +int ft_toupper(int c); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **alst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **alst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void*)); +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 *)); + +#endif \ No newline at end of file diff --git a/libs/gnl b/libs/gnl deleted file mode 160000 index 6486e2d..0000000 --- a/libs/gnl +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6486e2df2c80976baffd125dd023d79c84a82a09 diff --git a/libs/libft b/libs/libft deleted file mode 160000 index 494a5e2..0000000 --- a/libs/libft +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 494a5e2c9eae73c2c707a4d85f108f4054dfb502 diff --git a/pipex b/pipex deleted file mode 100755 index 308dfe3..0000000 Binary files a/pipex and /dev/null differ diff --git a/src/pipex.c b/src/pipex.c deleted file mode 100644 index 90a8731..0000000 --- a/src/pipex.c +++ /dev/null @@ -1,86 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* pipex.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mabbas +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/12/18 02:31:23 by mabbas #+# #+# */ -/* Updated: 2022/12/21 04:03:55 by mabbas ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../includes/pipex.h" - -/** Child process inside a fork that runs , takes the input file, puts the - * output inside a pipe and closes with the exec function */ - -void process_child(char **argv, char **envp, int *fd) -{ - t_pipe *piped; - - piped = NULL; - piped->in_file = open(argv[1], O_RDONLY, 0777); - if (piped->in_file == -1) - error(); - dup2(fd[1], STDOUT_FILENO); - dup2(piped->in_file, STDIN_FILENO); - close(fd[0]); - exec(argv[2], envp); -} - -/** Parent process takes data from the pipe and changes the output - * for the output.It closes with the exec function. - * If it is not done then it can lead to resource leaks /the fd remaining open - * even after cmd is executed **/ - -void process_parent(char **argv, char **envp, int *fd) -{ - t_pipe *pipex; - int j; - - j = 1; - pipex = NULL; - pipex->out_file = open(argv[4], O_WRONLY | O_CREAT | O_TRUNC, 0777); - if (pipex->out_file == -1) - error(); - dup2(fd[0], STDIN_FILENO); - dup2(pipex->out_file, STDOUT_FILENO); - close(fd[1]); - exec(argv[3], envp); -} - -/** This two fnc runs the child and parent process or displays an - * an error message if wrong arguments are passed **/ -void msg(void) -{ - ft_putendl_fd("\033[32mArguments are wrong Bro!!\n\e[0m", 2); - ft_putendl_fd("Ex: ./pipex \n", 2); -} - -int main(int argc, char **argv, char **envp) -{ - t_pipe *pipex; - - pipex = NULL; - if (argc > 5) - { - ft_putendl_fd("Argument threshold exceeded!\n", 2); - exit(EXIT_FAILURE); - } - else if (argc == 5) - { - if (pipe(pipex->fd) == -1) - error(); - pipex->pid1 = fork(); - if (pipex->pid1 == -1) - error(); - if (pipex->pid1 == 0) - process_child(argv, envp, pipex->fd); - waitpid(pipex->pid1, NULL, 0); - process_parent(argv, envp, pipex->fd); - } - else - msg(); - return (0); -} diff --git a/src/pipex.o b/src/pipex.o deleted file mode 100644 index 5819a6e..0000000 Binary files a/src/pipex.o and /dev/null differ diff --git a/src/pipex_bonus.c b/src/pipex_bonus.c deleted file mode 100644 index 496d3d6..0000000 --- a/src/pipex_bonus.c +++ /dev/null @@ -1,102 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* pipex_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mabbas +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/12/19 03:03:44 by mabbas #+# #+# */ -/* Updated: 2022/12/19 04:21:39 by mabbas ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../includes/pipex.h" - -/** Child process that is created by fork and pipe, outputs are put - * inside a pipe and then closed with the exec fnc. Main process will - * change the stdin for the pipe file descriptor. */ -void process_child(char *argv, char **envp) -{ - t_pipe pipex; - - if (pipe(pipex.fd) == -1) - error(); - pipex.pid1 = fork(); - if (pipex.pid1 == -1) - error(); - if (pipex.pid1 == 0) - { - close(pipex.fd[0]); - dup2(pipex.fd[1], STDOUT_FILENO); - exec(argv, envp); - } - else - { - close(pipex.fd[1]); - dup2(pipex.fd[0], STDIN_FILENO); - waitpid(pipex.pid1, NULL, 0); - } -} - -/** Function which makes a child process and reads from the stdin - * with get_next_line until reaching the delimiter word and puts - * the output inside a pipe. - * like suppose cmd << delimiter. THis helps to avoid escape - * special characters in the text. - * Here I used fd[0] to read from the stdin. **/ - -void heredoc(char *limiter, int argc) -{ - char *line; - t_pipe pipex; - - if (argc < 6) - use(); - if (pipe(pipex.fd) == -1) - error(); - pipex.reader = fork(); - if (pipex.reader == 0) - { - close(pipex.fd[0]); - while (get_next_line(pipex.fd[0]) != NULL) - { - if (ft_strncmp(line, limiter, ft_strlen(limiter)) == 0) - exit(EXIT_SUCCESS); - write(pipex.fd[1], line, ft_strlen(line)); - } - } - else - { - close(pipex.fd[1]); - dup2(pipex.fd[0], STDIN_FILENO); - wait (NULL); - } -} - -int main(int argc, char **argv, char **envp) -{ - int i; - t_pipe *pipex; - - if (argc >= 5) - { - if (ft_strncmp(argv[1], "here_doc", 8) == 0) - { - i = 3; - pipex->out_file = file_open(argv[argc - 1], 0); - heredoc(argv[2], argc); - } - else - { - i = 2; - pipex->out_file = file_open(argv[argc - 1], 1); - pipex->out_file = file_open(argv[1], 2); - dup2(pipex->in_file, STDIN_FILENO); - } - while (i < argc -2) - process_child(argv[i++], envp); - dup2(pipex->out_file, STDOUT_FILENO); - exec(argv[argc - 2], envp); - } - display(); -} diff --git a/src/pipex_utils.c b/src/pipex_utils.c deleted file mode 100644 index dee9256..0000000 --- a/src/pipex_utils.c +++ /dev/null @@ -1,81 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* pipex_utils.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mabbas +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/12/18 14:13:34 by mabbas #+# #+# */ -/* Updated: 2022/12/21 03:17:52 by mabbas ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../includes/pipex.h" - -void free_ptr(char **ptr) -{ - int i; - - i = 0; - while (ptr[i++]) - free(ptr[i]); - free(ptr); -} - -/**Function to look into the path line inside the environment variable - * Each command path is splitted and tested and then returns the correct value. - * access function is used to check accessibility of a file -**/ -char *path_find(char *cmd, char **envp) -{ - char **path_var; - char *path; - int i; - char *partial_path; - - i = 0; - while (ft_strnstr(envp[i], "PATH", 4) == 0) - i++; - path_var = ft_split(envp[i] + 5, ':'); - i = 0; - while (path_var[i]) - { - partial_path = ft_strjoin(path_var[i], "/"); - path = ft_strjoin(*path_var, cmd); - free(partial_path); - if (access(path, 0) == 0) - return (path); - free (path); - i++; - } - free_ptr(path_var); - return (0); -} - -void error(void) -{ - perror("\033[32mError"); - exit(EXIT_FAILURE); -} - -/**Function to take the cmd and send to find_path before executing. - * Here using the execve which transforms the calling process - * to a new process.If execve return to calling process then - * error will occur and return value is -1. **/ -void exec(char *argv, char **envp) -{ - char **cmd; - int i; - char *path; - - i = 0; - cmd = ft_split(argv, ' '); - path = path_find(cmd[0], envp); - if (!path) - { - free_ptr(cmd); - error(); - } - if (execve(path, cmd, envp) == -1) - error(); -} diff --git a/src/pipex_utils.o b/src/pipex_utils.o deleted file mode 100644 index 6af4f40..0000000 Binary files a/src/pipex_utils.o and /dev/null differ diff --git a/src/pipex_utils_bonus.c b/src/pipex_utils_bonus.c deleted file mode 100644 index fed3b80..0000000 --- a/src/pipex_utils_bonus.c +++ /dev/null @@ -1,48 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* pipex_utils_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mabbas +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/12/19 03:36:07 by mabbas #+# #+# */ -/* Updated: 2022/12/21 04:17:57 by mabbas ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../includes/pipex.h" -#include "..includes/mode.h" - -void display(void) -{ - ft_putendl_fd("\033[32mArguments are wrong Bro!!\n\e[0m", 2); - ft_putendl_fd("Ex: ./pipex <...> \n", 1); - ft_putendl_fd("Ex: ./pipex \"here_doc\" \ - <...> \n", 1); - exit(EXIT_SUCCESS); -} - -/** Fnc to open the files with right flag - * Here I had an array f structs containing all the necessary info - * for each file opening mode. Also added descriptinve name for the - * mode("append"). I used the global var g_file_mode array to replace - * if else statements. When the array of struct matches **/ -int file_open(char *argv, int i) -{ - int file; - int j; - - file = -1; - j = 0; - while (j++ < 3) - { - if (g_file_mode[j].mode == i) - { - file = open(argv, g_file_mode[j].mode, 0777); - break ; - } - } - if (file == -1) - error(); - return (file); -} diff --git a/srcs/pipex.c b/srcs/pipex.c new file mode 100644 index 0000000..4cca45f --- /dev/null +++ b/srcs/pipex.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mabbas +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/23 01:03:59 by mabbas #+# #+# */ +/* Updated: 2022/12/23 22:12:14 by mabbas ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/pipex.h" + +/* Child process that run inside a fork, take the filein, put the output inside + a pipe and then close with the exec function */ +void child_process(char **argv, char **envp, int *fd) +{ + int filein; + + filein = open(argv[1], O_RDONLY, 0777); + if (filein == -1) + error(); + dup2(fd[1], STDOUT_FILENO); + dup2(filein, STDIN_FILENO); + close(fd[0]); + execute(argv[2], envp); +} + +/* Parent process that take the data from the pipe, change the output for the + fileout and also close with the exec function*/ +void parent_process(char **argv, char **envp, int *fd) +{ + int fileout; + + fileout = open(argv[4], O_WRONLY | O_CREAT | O_TRUNC, 0777); + if (fileout == -1) + error(); + dup2(fd[0], STDIN_FILENO); + dup2(fileout, STDOUT_FILENO); + close(fd[1]); + execute(argv[3], envp); +} + +/* Main function that run the child and parent process or display an error + message if arguments are wrong */ +int main(int argc, char **argv, char **envp) +{ + int fd[2]; + pid_t pid1; + + if (argc == 5) + { + if (pipe(fd) == -1) + error(); + pid1 = fork(); + if (pid1 == -1) + error(); + if (pid1 == 0) + child_process(argv, envp, fd); + waitpid(pid1, NULL, 0); + parent_process(argv, envp, fd); + } + else + { + ft_putstr_fd("\033[31mError: Bad arguments\n\e[0m", 2); + ft_putstr_fd("Ex: ./pipex \n", 1); + } + return (0); +} diff --git a/srcs/pipex_bonus.c b/srcs/pipex_bonus.c new file mode 100644 index 0000000..f79cb11 --- /dev/null +++ b/srcs/pipex_bonus.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mabbas +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/23 00:47:16 by mabbas #+# #+# */ +/* Updated: 2022/12/23 02:57:18 by mabbas ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/pipex.h" + +/* Child process that create a fork and a pipe, put the output inside a pipe + and then close with the exec function. The main process will change his stdin + for the pipe file descriptor. */ +void child_process(char *argv, char **envp) +{ + pid_t pid; + int fd[2]; + + if (pipe(fd) == -1) + error(); + pid = fork(); + if (pid == -1) + error(); + if (pid == 0) + { + close(fd[0]); + dup2(fd[1], STDOUT_FILENO); + execute(argv, envp); + } + else + { + close(fd[1]); + dup2(fd[0], STDIN_FILENO); + waitpid(pid, NULL, 0); + } +} + +static void inputs(int fd, char *line, char *limiter) +{ + if (ft_strncmp(line, limiter, ft_strlen(limiter)) == 0) + exit(EXIT_SUCCESS); + write(fd, line, ft_strlen(line)); + free(line); +} + +/* Function who make a child process that will read from the stdin with + get_next_line until it find the limiter word and then put the output inside a + pipe. The main process will change his stdin for the pipe file descriptor. */ +void here_doc(char *limiter, int argc) +{ + pid_t reader; + int fd[2]; + char *line; + + if (argc < 6) + usage(); + if (pipe(fd) == -1) + error(); + reader = fork(); + if (reader == 0) + { + close(fd[0]); + while (get_next_line(&line)) + inputs(fd[1], line, limiter); + } + else + { + close(fd[1]); + dup2(fd[0], STDIN_FILENO); + wait(NULL); + } +} + +/* Main function that run the childs process with the right file descriptor + or display an error message if arguments are wrong. It will run here_doc + function if the "here_doc" string is find in argv[1] */ +int main(int argc, char **argv, char **envp) +{ + int i; + int filein; + int fileout; + + if (argc >= 5) + { + if (ft_strncmp(argv[1], "here_doc", 8) == 0) + { + i = 3; + fileout = open_file(argv[argc - 1], 0); + here_doc(argv[2], argc); + } + else + { + i = 2; + fileout = open_file(argv[argc - 1], 1); + filein = open_file(argv[1], 2); + dup2(filein, STDIN_FILENO); + } + while (i < argc - 2) + child_process(argv[i++], envp); + dup2(fileout, STDOUT_FILENO); + execute(argv[argc - 2], envp); + } + usage(); +} diff --git a/srcs/utils.c b/srcs/utils.c new file mode 100644 index 0000000..1818a6d --- /dev/null +++ b/srcs/utils.c @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mabbas +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/23 02:45:19 by mabbas #+# #+# */ +/* Updated: 2022/12/23 02:49:33 by mabbas ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/pipex.h" + +/* Function that will look for the path line inside the environment, will + split and test each command path and then return the right one. */ +char *find_path(char *cmd, char **envp) +{ + char **paths; + char *path; + int i; + char *part_path; + + i = 0; + while (ft_strnstr(envp[i], "PATH", 4) == 0) + i++; + paths = ft_split(envp[i] + 5, ':'); + i = 0; + while (paths[i]) + { + part_path = ft_strjoin(paths[i], "/"); + path = ft_strjoin(part_path, cmd); + free(part_path); + if (access(path, F_OK) == 0) + return (path); + free(path); + i++; + } + i = -1; + while (paths[++i]) + free(paths[i]); + free(paths); + return (0); +} + +/* A simple error displaying function. */ +void error(void) +{ + perror("\033[31mError"); + exit(EXIT_FAILURE); +} + +/* Function that take the command and send it to find_path + before executing it. */ +void execute(char *argv, char **envp) +{ + char **cmd; + int i; + char *path; + + i = -1; + cmd = ft_split(argv, ' '); + path = find_path(cmd[0], envp); + if (!path) + { + while (cmd[++i]) + free(cmd[i]); + free(cmd); + error(); + } + if (execve(path, cmd, envp) == -1) + error(); +} + +// /* Function that will read input from the terminal and return line. */ +// int get_next_line(char **line) +// { +// char *buffer; +// int readed; +// int i; +// char c; + +// i = 0; +// readed = 0; +// buffer = (char *)malloc(10000); +// if (!buffer) +// return (-1); +// readed = read(0, &c, 1); +// while (readed && c != '\n' && c != '\0') +// { +// if (c != '\n' && c != '\0') +// buffer[i] = c; +// i++; +// readed = read(0, &c, 1); +// } +// buffer[i] = '\n'; +// buffer[++i] = '\0'; +// *line = buffer; +// free(buffer); +// return (readed); +// } diff --git a/srcs/utils_bonus.c b/srcs/utils_bonus.c new file mode 100644 index 0000000..e22a43e --- /dev/null +++ b/srcs/utils_bonus.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mabbas +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/23 01:03:50 by mabbas #+# #+# */ +/* Updated: 2022/12/23 02:39:09 by mabbas ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/pipex.h" + +/* Function to display error message when arguments are wrong */ +void usage(void) +{ + ft_putstr_fd("\033[31mError: Bad argument\n\e[0m", 2); + ft_putstr_fd("Ex: ./pipex <...> \n", 1); + ft_putstr_fd("./pipex \"here_doc\" <...> \n", 1); + exit(EXIT_SUCCESS); +} + +/* Function to open the files with the right flags */ +int open_file(char *argv, int i) +{ + int file; + + file = 0; + if (i == 0) + file = open(argv, O_WRONLY | O_CREAT | O_APPEND, 0777); + else if (i == 1) + file = open(argv, O_WRONLY | O_CREAT | O_TRUNC, 0777); + else if (i == 2) + file = open(argv, O_RDONLY, 0777); + if (file == -1) + error(); + return (file); +} + +/* Function that will read input from the terminal and return line. */ +int get_next_line(char **line) +{ + char *buffer; + int readed; + int i; + char c; + + i = 0; + readed = 0; + buffer = (char *)malloc(10000); + if (!buffer) + return (-1); + readed = read(0, &c, 1); + while (readed && c != '\n' && c != '\0') + { + if (c != '\n' && c != '\0') + buffer[i] = c; + i++; + readed = read(0, &c, 1); + } + buffer[i] = '\n'; + buffer[++i] = '\0'; + *line = ft_strdup(buffer); + free(buffer); + return (readed); +}