├── Makefile ├── inc ├── pipex.h └── utils.h ├── src ├── check.c ├── init.c ├── main.c └── pipex.c └── utils ├── Makefile ├── ft_dprintf.c ├── ft_memcpy.c ├── ft_split.c ├── ft_strchr.c ├── ft_strjoin.c ├── ft_strlen.c ├── ft_strncmp.c ├── ft_strnstr.c ├── ft_strrchr.c ├── ft_substr.c └── get_next_line.c /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: hahadiou +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/02/03 15:11:30 by hahadiou #+# #+# # 9 | # Updated: 2023/03/15 18:12:34 by hahadiou ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | CC = cc 14 | FLAGS = -Wall -Wextra -Werror -IINC 15 | 16 | NAME = pipex 17 | 18 | INC = inc 19 | UTILS_PATH = utils 20 | SRC_PATH = src 21 | OBJ_PATH = obj 22 | 23 | SRCS = pipex.c \ 24 | init.c \ 25 | main.c \ 26 | check.c 27 | 28 | SRC = $(addprefix $(SRC_PATH)/,$(SRCS)) 29 | OBJ = $(addprefix $(OBJ_PATH)/,$(SRCS:.c=.o)) 30 | 31 | NOC = \033[0m 32 | RED = \033[1;31m 33 | GREEN = \033[1;32m 34 | YELLOW = \033[1;33m 35 | BLUE = \033[1;34m 36 | WHITE = \033[1;37m 37 | 38 | all: $(NAME) 39 | 40 | bonus: all 41 | 42 | $(NAME): $(OBJ) 43 | @echo "$(YELLOW)Compiling Utils...$(NOC)" 44 | @make -sC $(UTILS_PATH) 45 | @echo "$(YELLOW)Compiling Pipex...$(NOC)" 46 | @$(CC) $(FLAGS) -L $(UTILS_PATH) -o $@ $^ -lft 47 | @echo "$(GREEN)$@$(NOC)" 48 | 49 | $(OBJ_PATH)/%.o: $(SRC_PATH)/%.c $(INC)/$(NAME).h $(INC)/$(UTILS_PATH).h 50 | @mkdir -p obj 51 | @$(CC) $(FLAGS) -I$(INC) -c -o $@ $< 52 | 53 | clean: 54 | @echo "$(RED)Deleting OBJS ✔️ $(NOC)" 55 | @make clean -sC $(UTILS_PATH) 56 | @rm -rf $(OBJ_PATH) 57 | 58 | fclean: clean 59 | @echo "$(RED)Deleting Binary ✔️$(NOC)" 60 | @make fclean -sC $(UTILS_PATH) 61 | @rm -f $(NAME) 62 | 63 | re: fclean all 64 | 65 | .PHONY: all bonus clean fclean re 66 | -------------------------------------------------------------------------------- /inc/pipex.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pipex.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/03 22:45:35 by hahadiou #+# #+# */ 9 | /* Updated: 2023/03/15 18:12:42 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef PIPEX_H 14 | # define PIPEX_H 15 | 16 | # include "utils.h" 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | 24 | typedef struct s_executer t_executer; 25 | typedef struct s_parser t_parser; 26 | typedef struct s_pipex t_pipex; 27 | 28 | struct s_parser 29 | { 30 | int in; 31 | int out; 32 | char *path; 33 | char **cmd_path; 34 | char **cmd_args; 35 | char *cmd; 36 | int heredoc; 37 | }; 38 | 39 | struct s_executer 40 | { 41 | pid_t pid; 42 | int prev_pipe; 43 | int pfds[2]; 44 | int cmd_idx; 45 | }; 46 | 47 | struct s_pipex 48 | { 49 | t_parser parse; 50 | t_executer execute; 51 | }; 52 | 53 | void init(t_pipex *p, int ac, char **av, char **e); 54 | void init_args(t_pipex *p, int ac, char **av, bool o); 55 | char *get_cmd(char **paths, char *cmd); 56 | int check_cmd(char *cmd); 57 | void is_valid_cmd(t_pipex *pipex, bool child); 58 | void start(t_pipex *pipex, int ac, char **av, char **e); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /inc/utils.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/03 17:33:01 by hahadiou #+# #+# */ 9 | /* Updated: 2023/03/14 23:26:11 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef UTILS_H 14 | # define UTILS_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | 22 | # ifdef BUFFER_SIZE 23 | # if BUFFER_SIZE < 0 24 | # undef BUFFER_SIZE 25 | # define BUFFER_SIZE 0 26 | # endif 27 | # endif 28 | 29 | # ifndef BUFFER_SIZE 30 | # define BUFFER_SIZE 10 31 | # endif 32 | 33 | typedef struct s_buffer t_line; 34 | typedef struct s_buffer t_read; 35 | typedef struct s_split t_split; 36 | 37 | struct s_buffer 38 | { 39 | char *buf; 40 | size_t pos; 41 | size_t size; 42 | }; 43 | 44 | struct s_split 45 | { 46 | char k; 47 | int i; 48 | int j; 49 | int wordlen; 50 | char next_stop_char; 51 | char *next_stop; 52 | char **str; 53 | char *start; 54 | }; 55 | 56 | char *get_next_line(int fd); 57 | void ft_dprintf(int fd, char *s, ...); 58 | char *ft_strjoin(char const *s1, char const *s2); 59 | char **ft_split(char *s, char c); 60 | char *ft_substr(char const *s, unsigned int start, 61 | size_t len); 62 | char *ft_strchr(const char *s, int c); 63 | char *ft_strrchr(const char *s, int c); 64 | size_t ft_strlen(const char *s); 65 | void *ft_memcpy(void *dst, const void *src, size_t n); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /src/check.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/29 21:36:06 by hahadiou #+# #+# */ 9 | /* Updated: 2023/03/29 21:36:07 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "pipex.h" 14 | 15 | static void ft_free(t_pipex *pipex) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | close(pipex->execute.pfds[0]); 21 | close(pipex->execute.pfds[1]); 22 | while (pipex->parse.cmd_path[i]) 23 | { 24 | free(pipex->parse.cmd_path[i]); 25 | i++; 26 | } 27 | free(pipex->parse.cmd_path); 28 | } 29 | 30 | int check_cmd(char *cmd) 31 | { 32 | if (access(cmd, F_OK) < 0) 33 | { 34 | ft_dprintf(2, "No Such file: %s\n", cmd); 35 | exit(126); 36 | } 37 | if (access(cmd, X_OK) < 0) 38 | { 39 | ft_dprintf(2, "Permissions: %s\n", cmd); 40 | exit(126); 41 | } 42 | return (1); 43 | } 44 | 45 | void is_valid_cmd(t_pipex *pipex, bool child) 46 | { 47 | if (child) 48 | { 49 | if (!(pipex->parse.cmd)) 50 | { 51 | ft_free(pipex); 52 | ft_dprintf(2, "pipex: %s: command not found\n", 53 | pipex->parse.cmd_args[0]); 54 | exit(0); 55 | } 56 | } 57 | else 58 | { 59 | if (!(pipex->parse.cmd)) 60 | { 61 | ft_free(pipex); 62 | ft_dprintf(2, "pipex: %s: command not found\n", 63 | pipex->parse.cmd_args[0]); 64 | exit(127); 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /src/init.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/29 21:35:59 by hahadiou #+# #+# */ 9 | /* Updated: 2023/03/29 21:36:00 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "pipex.h" 14 | 15 | void init_args(t_pipex *pipex, int ac, char **av, bool out) 16 | { 17 | if (out) 18 | { 19 | pipex->parse.in = open(av[pipex->execute.cmd_idx - 1], O_RDONLY); 20 | if (access(av[pipex->execute.cmd_idx - 1], F_OK) < 0) 21 | { 22 | ft_dprintf(2, "pipex: %s: No such file or directory\n", 23 | av[pipex->execute.cmd_idx - 1]); 24 | exit(0); 25 | } 26 | if (access(av[pipex->execute.cmd_idx - 1], R_OK) < 0) 27 | { 28 | ft_dprintf(2, "pipex: %s: permission denied\n", 29 | av[pipex->execute.cmd_idx - 1]); 30 | exit(1); 31 | } 32 | dup2(pipex->parse.in, 0); 33 | } 34 | pipex->parse.out = open(av[ac - 1], O_WRONLY | O_TRUNC | O_CREAT, 0644); 35 | if (access(av[ac - 1], W_OK) < 0) 36 | { 37 | ft_dprintf(2, "pipex: %s: permission denied\n", av[4]); 38 | exit(1); 39 | } 40 | dup2(pipex->parse.out, 1); 41 | } 42 | 43 | static char *find_path(char **env) 44 | { 45 | char *path; 46 | 47 | path = strdup("/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."); 48 | if (*env == NULL || strncmp("PATH", *env, 4)) 49 | return (path); 50 | while (strncmp("PATH", *env, 4)) 51 | env++; 52 | return (*env + 5); 53 | } 54 | 55 | static void init_paths(t_pipex *pipex, char **envp) 56 | { 57 | pipex->parse.path = find_path(envp); 58 | pipex->parse.cmd_path = ft_split(pipex->parse.path, ':'); 59 | } 60 | 61 | void init(t_pipex *pipex, int ac, char **av, char **envp) 62 | { 63 | init_args(pipex, ac, av, false); 64 | init_paths(pipex, envp); 65 | pipex->execute.cmd_idx = 1; 66 | pipex->execute.prev_pipe = 0; 67 | } 68 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/29 21:35:50 by hahadiou #+# #+# */ 9 | /* Updated: 2023/03/29 21:35:51 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "pipex.h" 14 | 15 | char *get_cmd(char **paths, char *cmd) 16 | { 17 | char *tmp; 18 | char *command; 19 | 20 | if (cmd[0] == '.') 21 | if (check_cmd(cmd) == 1) 22 | return (cmd); 23 | if (cmd[0] == '/') 24 | { 25 | cmd = ft_strchr(cmd, '/'); 26 | if (strrchr(cmd, '/') == NULL) 27 | return (0); 28 | } 29 | while (*paths) 30 | { 31 | tmp = ft_strjoin(*paths, "/"); 32 | command = ft_strjoin(tmp, cmd); 33 | free(tmp); 34 | if (access(command, F_OK) == 0) 35 | return (command); 36 | free(command); 37 | paths++; 38 | } 39 | return (NULL); 40 | } 41 | 42 | static void init_cmd(t_pipex *pipex, char **av, int cmd_idx) 43 | { 44 | pipex->parse.cmd_args = ft_split(av[cmd_idx], ' '); 45 | pipex->parse.cmd = get_cmd(pipex->parse.cmd_path, pipex->parse.cmd_args[0]); 46 | } 47 | 48 | static void execute_cmd(t_pipex *pipex, int ac, char **av, char **envp) 49 | { 50 | is_valid_cmd(pipex, true); 51 | if (pipex->execute.cmd_idx == 2) 52 | init_args(pipex, ac, av, true); 53 | if (pipex->execute.prev_pipe != 0) 54 | { 55 | dup2(pipex->execute.prev_pipe, 0); 56 | close(pipex->execute.prev_pipe); 57 | } 58 | dup2(pipex->execute.pfds[1], 1); 59 | close(pipex->execute.pfds[1]); 60 | execve(pipex->parse.cmd, pipex->parse.cmd_args, envp); 61 | ft_dprintf(2, "Execve Failed.\n"); 62 | exit(1); 63 | } 64 | 65 | void start(t_pipex *pipex, int ac, char **av, char **envp) 66 | { 67 | int i; 68 | 69 | i = -1; 70 | while (++i < ac - 4) 71 | { 72 | pipe(pipex->execute.pfds); 73 | init_cmd(pipex, av, ++pipex->execute.cmd_idx); 74 | pipex->execute.pid = fork(); 75 | if (!(pipex->execute.pid)) 76 | execute_cmd(pipex, ac, av, envp); 77 | close(pipex->execute.prev_pipe); 78 | close(pipex->execute.pfds[1]); 79 | pipex->execute.prev_pipe = pipex->execute.pfds[0]; 80 | } 81 | wait(NULL); 82 | if (pipex->execute.prev_pipe != 0) 83 | { 84 | dup2(pipex->execute.prev_pipe, 0); 85 | close(pipex->execute.prev_pipe); 86 | } 87 | init_cmd(pipex, av, ac - 2); 88 | is_valid_cmd(pipex, false); 89 | execve(pipex->parse.cmd, pipex->parse.cmd_args, envp); 90 | ft_dprintf(2, "Execve Failed.\n"); 91 | exit(1); 92 | } 93 | -------------------------------------------------------------------------------- /src/pipex.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pipex.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/12/10 00:48:26 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 10:30:21 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "pipex.h" 14 | 15 | int main(int ac, char **av, char **envp) 16 | { 17 | t_pipex pipex[1]; 18 | 19 | if (ac < 4) 20 | { 21 | ft_dprintf(2, "Enter A Valid Number of Arguments.\n"); 22 | return (1); 23 | } 24 | init(pipex, ac, av, envp); 25 | start(pipex, ac, av, envp); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /utils/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: hahadiou +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/03/03 17:29:07 by hahadiou #+# #+# # 9 | # Updated: 2023/03/14 22:35:47 by hahadiou ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | SRCS = ft_split.c \ 14 | ft_dprintf.c \ 15 | ft_strjoin.c \ 16 | ft_substr.c \ 17 | ft_strchr.c \ 18 | ft_strrchr.c \ 19 | ft_strlen.c \ 20 | ft_memcpy.c 21 | 22 | OBJS = ${addprefix obj/, ${SRCS:.c=.o}} 23 | OBJ_PATH = obj 24 | 25 | NAME = libft.a 26 | 27 | CC = cc 28 | 29 | CFLAGS = -Wall -Wextra -Werror -I../inc/ 30 | 31 | RM = rm -f 32 | 33 | all: ${NAME} 34 | 35 | obj/%.o: %.c 36 | @mkdir -p $(OBJ_PATH) 37 | @${CC} ${CFLAGS} -c $< -o $@ 38 | 39 | $(NAME): $(OBJS) 40 | @ar -rc $(NAME) $(OBJS) 41 | 42 | clean: 43 | @rm -rf $(OBJ_PATH) 44 | 45 | fclean: clean 46 | @rm -f $(NAME) 47 | 48 | re: fclean all 49 | 50 | .PHONY: all clean fclean re -------------------------------------------------------------------------------- /utils/ft_dprintf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_dprintf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/12/17 07:46:26 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:11:58 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | static void ft_putchar_fd(int fd, char c) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | 20 | static void ft_putstr(int fd, char *s) 21 | { 22 | int i; 23 | 24 | i = -1; 25 | if (s == NULL) 26 | ft_putstr(fd, "(null)"); 27 | while (s[++i]) 28 | write(fd, &s[i], 1); 29 | } 30 | 31 | void ft_dprintf(int fd, char *s, ...) 32 | { 33 | va_list ap; 34 | int i; 35 | 36 | i = -1; 37 | va_start(ap, s); 38 | while (s[++i]) 39 | { 40 | if (s[i] == '%') 41 | { 42 | if (s[++i] == 's') 43 | ft_putstr(fd, va_arg(ap, char *)); 44 | } 45 | else 46 | ft_putchar_fd(fd, s[i]); 47 | } 48 | va_end(ap); 49 | } 50 | -------------------------------------------------------------------------------- /utils/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 15:05:36 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:04 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | size_t i; 18 | char *s; 19 | char *d; 20 | 21 | s = (char *)src; 22 | d = (char *)dst; 23 | if (!s && !d) 24 | return (NULL); 25 | i = 0; 26 | while (i < n) 27 | { 28 | d[i] = s[i]; 29 | i++; 30 | } 31 | return (dst); 32 | } 33 | -------------------------------------------------------------------------------- /utils/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 19:59:22 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:07 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | int count(char *s, char c) 16 | { 17 | t_split sp; 18 | 19 | sp.j = 0; 20 | sp.i = 0; 21 | while (s[sp.i] && s[sp.i] == c) 22 | sp.i++; 23 | while (s[sp.i]) 24 | { 25 | if (s[sp.i] == 39 || s[sp.i] == 34) 26 | sp.k = s[sp.i]; 27 | else 28 | sp.k = c; 29 | sp.next_stop = ft_strchr(&s[sp.i + 1], sp.k); 30 | sp.i += sp.next_stop - &s[sp.i]; 31 | if (sp.k != c) 32 | sp.i += 2; 33 | sp.j++; 34 | while (s[sp.i] && s[sp.i] == c) 35 | sp.i++; 36 | } 37 | return (sp.j); 38 | } 39 | 40 | char **ft_fill(t_split sp, char *s, char c) 41 | { 42 | sp.j = 0; 43 | while (s[sp.i]) 44 | { 45 | sp.wordlen = 0; 46 | sp.start = &s[sp.i]; 47 | if (s[sp.i] == 39 || s[sp.i] == 34) 48 | { 49 | sp.start = &s[sp.i + 1]; 50 | sp.next_stop_char = s[sp.i]; 51 | sp.wordlen = -1; 52 | } 53 | else 54 | sp.next_stop_char = c; 55 | sp.next_stop = ft_strchr(&s[sp.i + 1], sp.next_stop_char); 56 | sp.wordlen += sp.next_stop - &s[sp.i]; 57 | sp.i += sp.wordlen; 58 | if (sp.next_stop_char != c) 59 | sp.i += 2; 60 | sp.str[sp.j++] = ft_substr(sp.start, 0, sp.wordlen); 61 | while (s[sp.i] && s[sp.i] == c) 62 | sp.i++; 63 | } 64 | sp.str[sp.j] = NULL; 65 | return (sp.str); 66 | } 67 | 68 | char **ft_split(char *s, char c) 69 | { 70 | t_split sp; 71 | int x; 72 | 73 | x = 0; 74 | sp.str = (char **)malloc((count(s, c) + 1) * sizeof(char *)); 75 | if (!sp.str) 76 | return (NULL); 77 | sp.i = 0; 78 | while (s[sp.i] && s[sp.i] == c) 79 | sp.i++; 80 | sp.str = ft_fill(sp, s, c); 81 | return (sp.str); 82 | } 83 | -------------------------------------------------------------------------------- /utils/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 15:06:41 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:09 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | { 22 | if (s[i] == c) 23 | return ((char *)&s[i]); 24 | i++; 25 | } 26 | return ((char *)&s[i]); 27 | } 28 | -------------------------------------------------------------------------------- /utils/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 15:41:39 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:11 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | char *new; 18 | int len1; 19 | int len2; 20 | int i; 21 | int j; 22 | 23 | if (!s1 || !s2) 24 | return (NULL); 25 | i = -1; 26 | len1 = strlen(s1); 27 | len2 = strlen(s2); 28 | new = malloc(len1 + len2 + 1); 29 | if (!new) 30 | return (NULL); 31 | while (++i < len1) 32 | new[i] = s1[i]; 33 | j = -1; 34 | while (++j < len2) 35 | { 36 | new[i] = s2[j]; 37 | i++; 38 | } 39 | new[i] = '\0'; 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /utils/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 15:42:03 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:13 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | i++; 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /utils/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 15:42:09 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:15 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | unsigned char *p; 19 | unsigned char *b; 20 | 21 | i = 0; 22 | p = (unsigned char *)s1; 23 | b = (unsigned char *)s2; 24 | while ((p[i] || b[i]) && (i < n)) 25 | { 26 | if (p[i] < b[i]) 27 | return (-1); 28 | if (p[i] > b[i]) 29 | return (1); 30 | i++; 31 | } 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /utils/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 15:42:18 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:17 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/libft.h" 14 | 15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 | { 17 | size_t i; 18 | size_t j; 19 | 20 | i = 0; 21 | if (*needle == 0) 22 | return ((char *)haystack); 23 | while (i < len && haystack[i]) 24 | { 25 | j = 0; 26 | while (i + j < len && haystack[i + j] == needle[j] && needle[j]) 27 | j++; 28 | if (needle[j] == '\0') 29 | return ((char *)haystack + i); 30 | i++; 31 | } 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /utils/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 15:55:23 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:20 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = ft_strlen(s); 20 | while (i && s[i] != c) 21 | i--; 22 | if (s[i] == (char)c) 23 | return ((char *)s + i); 24 | return (NULL); 25 | } 26 | -------------------------------------------------------------------------------- /utils/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/09/29 18:13:53 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:22 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | char *ft_substr(char const *s, unsigned int start, size_t len) 16 | { 17 | size_t i; 18 | char *str; 19 | 20 | i = -1; 21 | if (!s) 22 | return (0); 23 | if (start >= strlen(s)) 24 | { 25 | return (calloc(1, 1)); 26 | } 27 | str = malloc(len + 1); 28 | if (!str) 29 | return (0); 30 | while (++i < len) 31 | str[i] = s[start + i]; 32 | str[i] = '\0'; 33 | return (str); 34 | } 35 | -------------------------------------------------------------------------------- /utils/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/01/11 10:13:19 by hahadiou #+# #+# */ 9 | /* Updated: 2023/01/11 11:12:26 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/libft.h" 14 | 15 | static void free_buffer(t_read *prb) 16 | { 17 | if (prb->buf != NULL) 18 | { 19 | free(prb->buf); 20 | prb->buf = NULL; 21 | } 22 | } 23 | 24 | static char *ft_realloc(t_line *line) 25 | { 26 | char *new_buf; 27 | size_t new_size; 28 | 29 | new_size = line->size + 1; 30 | new_buf = malloc((new_size + 1) * sizeof(char)); 31 | if (line->buf) 32 | { 33 | if (new_buf) 34 | ft_memcpy(new_buf, line->buf, line->size); 35 | free(line->buf); 36 | } 37 | line->buf = new_buf; 38 | line->size = new_size; 39 | return (new_buf); 40 | } 41 | 42 | static int read_file(int fd, t_read *reserve) 43 | { 44 | if (!reserve->buf) 45 | reserve->buf = malloc(BUFFER_SIZE); 46 | if (reserve->pos >= reserve->size) 47 | { 48 | reserve->size = read(fd, reserve->buf, BUFFER_SIZE); 49 | if (reserve->size < 1) 50 | return (0); 51 | reserve->pos = 0; 52 | } 53 | return (reserve->buf[reserve->pos++]); 54 | } 55 | 56 | static int put_line(t_line *line, char c) 57 | { 58 | if (line->pos >= line->size) 59 | { 60 | if (ft_realloc(line) == NULL) 61 | return (0); 62 | } 63 | line->buf[line->pos++] = c; 64 | if (c == '\n') 65 | return (0); 66 | return (1); 67 | } 68 | 69 | char *get_next_line(int fd) 70 | { 71 | int c; 72 | t_line line; 73 | static t_read reserve; 74 | 75 | if (fd < 0 || read(fd, line.buf, 0) < 0) 76 | return (0); 77 | line.buf = 0; 78 | line.pos = 0; 79 | line.size = 0; 80 | while (1) 81 | { 82 | c = read_file(fd, &reserve); 83 | if (c == 0) 84 | break ; 85 | if (put_line(&line, c) == 0) 86 | break ; 87 | } 88 | if (line.pos == 0) 89 | free_buffer(&reserve); 90 | else 91 | line.buf[line.pos] = 0; 92 | return (line.buf); 93 | } 94 | --------------------------------------------------------------------------------