diff --git a/Makefile b/Makefile index 8e6f6c3..2a9327a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ GNL = get_next_line/get_next_line.c get_next_line/get_next_line_utils.c -SRCS = pipex.c ${GNL} +SRCS = mandatory/pipex.c mandatory/pipex_utils.c mandatory/pipex_parsing.c ${GNL} SRCS_BONUS = OBJS = ${SRCS:.c=.o} OBJS_BONUS = ${SRCS_BONUS:.c=.o} diff --git a/bonus/pipex.c b/bonus/pipex.c new file mode 100644 index 0000000..b7ea8a7 --- /dev/null +++ b/bonus/pipex.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */ +/* Updated: 2023/01/19 16:57:40 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../pipex.h" + +int free_all(t_data *data) +{ + int i; + + if (data->fd1 > 0 && data->cmd1) + close(data->fd1); + close(data->fd2); + i = 0; + while (data->flg1[i]) + free(data->flg1[i++]); + free(data->flg1); + i = 0; + while (data->flg2[i]) + free(data->flg2[i++]); + free(data->flg2); + free(data->cmd1); + free(data->cmd2); + return (1); +} + +void first_cmd(int fd[2], t_data data) +{ + dup2(data.fd1, 0); + dup2(fd[1], 1); + close(fd[0]); + close(fd[1]); + execve(data.cmd1, data.flg1, data.env); + free_all(&data); + exit(1); +} + +void second_cmd(int fd[2], t_data data) +{ + dup2(fd[0], 0); + dup2(data.fd2, 1); + close(fd[0]); + close(fd[1]); + execve(data.cmd2, data.flg2, data.env); + free_all(&data); + exit(1); +} + +int pipex(t_data data) +{ + int fd[2]; + int cmd1; + int cmd2; + + if (pipe(fd) < -1) + return (1); + if (data.fd1 > 0 && data.cmd1) + { + cmd1 = fork(); + if (cmd1 < 0) + return (1); + if (cmd1 == 0) + first_cmd(fd, data); + } + cmd2 = fork(); + if (cmd2 < 0) + return (1); + if (cmd2 == 0) + second_cmd(fd, data); + close(fd[0]); + close(fd[1]); + if (data.fd1 > 0 && data.cmd1) + waitpid(cmd1, NULL, 0); + waitpid(cmd2, NULL, 0); + return (0); +} + +int main(int argc, char *argv[], char **env) +{ + t_data data; + + if (argc < 5) + return (1); + if (parsing(argv, &data)) + return (2); + data.cmd1 = find_command(env, data.cmd1); + if (data.cmd1 == NULL) + print_error(1, argv[2]); + data.cmd2 = find_command(env, data.cmd2); + if (data.cmd2 == NULL) + { + print_error(1, argv[3]); + return (free_all(&data)); + } + data.env = env; + if (pipex(data)) + return (free_all(&data)); + free_all(&data); + return (0); +} diff --git a/bonus/pipex_parsing.c b/bonus/pipex_parsing.c new file mode 100644 index 0000000..f621169 --- /dev/null +++ b/bonus/pipex_parsing.c @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex_parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */ +/* Updated: 2023/01/19 17:05:16 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../pipex.h" + +int parsing(char *argv[], t_data *data) +{ + data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); + if (data->fd2 < 0) + return (1); + data->fd1 = open(argv[1], O_RDONLY); + if (data->fd1 < 0) + print_error(0, argv[1]); + data->cmd1 = get_command(argv[2]); + data->cmd2 = get_command(argv[3]); + data->flg1 = ft_split(argv[2], ' '); + data->flg2 = ft_split(argv[3], ' '); + data->fl1 = argv[1]; + data->fl2 = argv[4]; + return (0); +} + +char *test_acces(char **s, char **cmd, char **path, int *y) +{ + *s = ft_strjoin_free(*s, "/", 1); + if (*s == NULL) + return (NULL); + *path = ft_strjoin_free(*s, *cmd, 1); + if (*path == NULL) + return (NULL); + if (access(*path, X_OK) == 0) + { + free(*cmd); + return (*path); + } + free(*path); + *path = NULL; + *y = 0; + return (NULL); +} + +char *cmd_exist(char **env, char **cmd, int y, char **s) +{ + char *path; + int i; + + i = 5; + while ((*env)[i]) + { + if ((*env)[i] != ':') + (*s)[y++] = (*env)[i]; + else + { + test_acces(s, cmd, &path, &y); + if (path) + return (path); + *s = ft_calloc(ft_strlen(*env) + 1, 1); + if (*s == NULL) + return (NULL); + } + i++; + } + free(*s); + free(*cmd); + return (NULL); +} + +char *find_command(char **env, char *cmd) +{ + char *s; + int y; + + while (*env != NULL) + { + if (ft_strnstr(*env, "PATH=/", ft_strlen(*env)) + && ft_strchr(*env, ':') > 0) + break ; + env++; + } + y = 0; + s = ft_calloc(ft_strlen(*env) + 1, 1); + if (s == NULL) + return (NULL); + return (cmd_exist(env, &cmd, y, &s)); +} diff --git a/bonus/pipex_utils.c b/bonus/pipex_utils.c new file mode 100644 index 0000000..b95258f --- /dev/null +++ b/bonus/pipex_utils.c @@ -0,0 +1,117 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */ +/* Updated: 2023/01/19 17:05:24 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../pipex.h" + +int get_next(char *cmd, char c) +{ + int i; + + i = 0; + while (cmd[i] != c) + { + if (!cmd[i]) + return (-1); + i++; + } + return (i); +} + +int print_error(int type, char *error) +{ + if (type == 0) + { + write(2, "zsh: no such file or directory: ", 32); + write(2, error, ft_strlen(error)); + } + if (type == 1) + { + write(2, "zsh: command not found: ", 24); + if (get_next(error, ' ') == -1) + write(2, error, ft_strlen(error)); + else + write(2, error, ft_strlen(error) + - (ft_strlen(error) - get_next(error, ' '))); + } + write(2, "\n", 1); + return (1); +} + +char **get_flags(char *cmd) +{ + int i; + int y; + char *new_str; + char **flags; + + i = get_next(cmd, ' ') + 1; + if (i == 0) + { + flags = ft_calloc(2, sizeof(char *)); + return (flags); + } + new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1); + if (new_str == NULL) + return (NULL); + y = 0; + while (cmd[i]) + new_str[y++] = cmd[i++]; + new_str[y] = '\0'; + flags = ft_split(new_str, ' '); + i = 0; + free(new_str); + return (flags); +} + +char *get_command(char *cmd) +{ + int i; + char *new_str; + + i = get_next(cmd, ' '); + if (i != -1) + new_str = ft_calloc(get_next(cmd, ' ') + 1, 1); + else + new_str = ft_calloc(ft_strlen(cmd) + 1, 1); + if (new_str == NULL) + return (NULL); + i = -1; + while (cmd[++i] != ' ' && cmd[i]) + new_str[i] = cmd[i]; + new_str[i] = '\0'; + return (new_str); +} + +char **add_cmd(char **flg, char *cmd) +{ + char *tmp; + int i; + int check; + + tmp = NULL; + i = 0; + check = 0; + while (check == 0) + { + if (flg[i] == NULL) + check = 1; + if (tmp != NULL) + flg[i] = tmp; + tmp = flg[i]; + i++; + } + flg[0] = ft_strdup(cmd); + if (flg[0] == NULL) + return (NULL); + flg[i] = NULL; + return (flg); +} diff --git a/input b/input index b33c560..9dcb0ca 100755 --- a/input +++ b/input @@ -1 +1,22 @@ -test test +hello that's why we are here s, your name +im not kidding +what's your name +please can we talk +commandes are not wrking, if you want send me in private +how can i reach you dud, s +pelase give me a wrod that end with a +oh do you mean likw palama +what !!! +palama +sorry id don't know what this mean +me too i don't know, +so way you say it +not your bussnis +hahahahahah +why you ?? +i wnat it to end with s +okay +what bout lives +emmm thats nice +and eyes +emmm thats batter diff --git a/libft/ft_split.c b/libft/ft_split.c index 7b305d7..5947f62 100644 --- a/libft/ft_split.c +++ b/libft/ft_split.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/28 09:52:05 by erey-bet #+# #+# */ -/* Updated: 2022/10/12 16:14:08 by erey-bet ### ########.fr */ +/* Updated: 2023/01/17 15:40:25 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -106,7 +106,7 @@ char **ft_split(char const *s, char c) iyx[2] = 0; if (s == NULL) return (NULL); - strs = malloc(sizeof(char *) * (get_len_all(s, c) + 1)); + strs = malloc(sizeof(char *) * (get_len_all(s, c) + 2)); if (strs == NULL) return (NULL); if (ft_split_bis(s, c, strs, iyx) == NULL) diff --git a/libft/ft_strjoin_free.c b/libft/ft_strjoin_free.c index a55f47c..2dc9a56 100644 --- a/libft/ft_strjoin_free.c +++ b/libft/ft_strjoin_free.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/27 14:47:27 by erey-bet #+# #+# */ -/* Updated: 2022/12/13 13:24:11 by erey-bet ### ########.fr */ +/* Updated: 2023/01/17 16:25:07 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c index 1609615..f12b894 100644 --- a/libft/ft_strlen.c +++ b/libft/ft_strlen.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/15 11:46:17 by erey-bet #+# #+# */ -/* Updated: 2022/09/27 08:52:38 by erey-bet ### ########.fr */ +/* Updated: 2023/01/18 18:04:47 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ size_t ft_strlen(const char *str) { int i; - + if (str == NULL) return (0); i = 0; diff --git a/mandatory/pipex.c b/mandatory/pipex.c new file mode 100644 index 0000000..c3624a1 --- /dev/null +++ b/mandatory/pipex.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */ +/* Updated: 2023/01/19 17:30:04 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../pipex.h" + +int free_all(t_data *data) +{ + int i; + + if (data->fd1 > 0 && data->cmd1) + close(data->fd1); + close(data->fd2); + i = 0; + while (data->flg1[i]) + free(data->flg1[i++]); + free(data->flg1); + i = 0; + while (data->flg2[i]) + free(data->flg2[i++]); + free(data->flg2); + free(data->cmd1); + free(data->cmd2); + return (1); +} + +void first_cmd(int fd[2], t_data data) +{ + dup2(data.fd1, 0); + dup2(fd[1], 1); + close(fd[0]); + close(fd[1]); + execve(data.cmd1, data.flg1, data.env); + free_all(&data); + exit(1); +} + +void second_cmd(int fd[2], t_data data) +{ + dup2(fd[0], 0); + dup2(data.fd2, 1); + close(fd[0]); + close(fd[1]); + execve(data.cmd2, data.flg2, data.env); + free_all(&data); + exit(1); +} + +int pipex(t_data data) +{ + int fd[2]; + int cmd1; + int cmd2; + + if (pipe(fd) < -1) + return (1); + if (data.fd1 > 0 && data.cmd1) + { + cmd1 = fork(); + if (cmd1 < 0) + return (1); + if (cmd1 == 0) + first_cmd(fd, data); + } + cmd2 = fork(); + if (cmd2 < 0) + return (1); + if (cmd2 == 0) + second_cmd(fd, data); + close(fd[0]); + close(fd[1]); + if (data.fd1 > 0 && data.cmd1) + waitpid(cmd1, NULL, 0); + waitpid(cmd2, NULL, 0); + return (0); +} + +int main(int argc, char *argv[], char **env) +{ + t_data data; + + if (argc != 5) + return (1); + if (parsing(argv, &data)) + return (2); + data.cmd1 = find_command(env, data.cmd1); + if (data.cmd1 == NULL) + print_error(1, argv[2]); + data.cmd2 = find_command(env, data.cmd2); + if (data.cmd2 == NULL) + { + print_error(1, argv[3]); + return (free_all(&data)); + } + data.env = env; + if (pipex(data)) + return (free_all(&data)); + free_all(&data); + return (0); +} diff --git a/mandatory/pipex_parsing.c b/mandatory/pipex_parsing.c new file mode 100644 index 0000000..f621169 --- /dev/null +++ b/mandatory/pipex_parsing.c @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex_parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */ +/* Updated: 2023/01/19 17:05:16 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../pipex.h" + +int parsing(char *argv[], t_data *data) +{ + data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); + if (data->fd2 < 0) + return (1); + data->fd1 = open(argv[1], O_RDONLY); + if (data->fd1 < 0) + print_error(0, argv[1]); + data->cmd1 = get_command(argv[2]); + data->cmd2 = get_command(argv[3]); + data->flg1 = ft_split(argv[2], ' '); + data->flg2 = ft_split(argv[3], ' '); + data->fl1 = argv[1]; + data->fl2 = argv[4]; + return (0); +} + +char *test_acces(char **s, char **cmd, char **path, int *y) +{ + *s = ft_strjoin_free(*s, "/", 1); + if (*s == NULL) + return (NULL); + *path = ft_strjoin_free(*s, *cmd, 1); + if (*path == NULL) + return (NULL); + if (access(*path, X_OK) == 0) + { + free(*cmd); + return (*path); + } + free(*path); + *path = NULL; + *y = 0; + return (NULL); +} + +char *cmd_exist(char **env, char **cmd, int y, char **s) +{ + char *path; + int i; + + i = 5; + while ((*env)[i]) + { + if ((*env)[i] != ':') + (*s)[y++] = (*env)[i]; + else + { + test_acces(s, cmd, &path, &y); + if (path) + return (path); + *s = ft_calloc(ft_strlen(*env) + 1, 1); + if (*s == NULL) + return (NULL); + } + i++; + } + free(*s); + free(*cmd); + return (NULL); +} + +char *find_command(char **env, char *cmd) +{ + char *s; + int y; + + while (*env != NULL) + { + if (ft_strnstr(*env, "PATH=/", ft_strlen(*env)) + && ft_strchr(*env, ':') > 0) + break ; + env++; + } + y = 0; + s = ft_calloc(ft_strlen(*env) + 1, 1); + if (s == NULL) + return (NULL); + return (cmd_exist(env, &cmd, y, &s)); +} diff --git a/mandatory/pipex_utils.c b/mandatory/pipex_utils.c new file mode 100644 index 0000000..b95258f --- /dev/null +++ b/mandatory/pipex_utils.c @@ -0,0 +1,117 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */ +/* Updated: 2023/01/19 17:05:24 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../pipex.h" + +int get_next(char *cmd, char c) +{ + int i; + + i = 0; + while (cmd[i] != c) + { + if (!cmd[i]) + return (-1); + i++; + } + return (i); +} + +int print_error(int type, char *error) +{ + if (type == 0) + { + write(2, "zsh: no such file or directory: ", 32); + write(2, error, ft_strlen(error)); + } + if (type == 1) + { + write(2, "zsh: command not found: ", 24); + if (get_next(error, ' ') == -1) + write(2, error, ft_strlen(error)); + else + write(2, error, ft_strlen(error) + - (ft_strlen(error) - get_next(error, ' '))); + } + write(2, "\n", 1); + return (1); +} + +char **get_flags(char *cmd) +{ + int i; + int y; + char *new_str; + char **flags; + + i = get_next(cmd, ' ') + 1; + if (i == 0) + { + flags = ft_calloc(2, sizeof(char *)); + return (flags); + } + new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1); + if (new_str == NULL) + return (NULL); + y = 0; + while (cmd[i]) + new_str[y++] = cmd[i++]; + new_str[y] = '\0'; + flags = ft_split(new_str, ' '); + i = 0; + free(new_str); + return (flags); +} + +char *get_command(char *cmd) +{ + int i; + char *new_str; + + i = get_next(cmd, ' '); + if (i != -1) + new_str = ft_calloc(get_next(cmd, ' ') + 1, 1); + else + new_str = ft_calloc(ft_strlen(cmd) + 1, 1); + if (new_str == NULL) + return (NULL); + i = -1; + while (cmd[++i] != ' ' && cmd[i]) + new_str[i] = cmd[i]; + new_str[i] = '\0'; + return (new_str); +} + +char **add_cmd(char **flg, char *cmd) +{ + char *tmp; + int i; + int check; + + tmp = NULL; + i = 0; + check = 0; + while (check == 0) + { + if (flg[i] == NULL) + check = 1; + if (tmp != NULL) + flg[i] = tmp; + tmp = flg[i]; + i++; + } + flg[0] = ft_strdup(cmd); + if (flg[0] == NULL) + return (NULL); + flg[i] = NULL; + return (flg); +} diff --git a/output b/output index 0cfbf08..b047e51 100755 --- a/output +++ b/output @@ -1 +1,4 @@ -2 +hello that's why we are here s, your name +what's your name +commandes are not wrking, if you want send me in private +emmm thats nice diff --git a/pipex.c b/pipex.c deleted file mode 100644 index 356c0bb..0000000 --- a/pipex.c +++ /dev/null @@ -1,217 +0,0 @@ -#include "pipex.h" - -char *read_file(int fd) -{ - char *str; - int len; - int old_len; - - str = get_next_line(fd); - len = ft_strlen(str); - old_len = 0; - while (len != old_len) - { - old_len = len; - str = ft_strjoin_free(str, get_next_line(fd), 2); - len = ft_strlen(str); - } - str[len] = '\0'; - return (str); -} - -int get_next(char *cmd, char c) -{ - int i; - - i = 0; - while (cmd[i] != c) - i++; - return (i); -} - -char **get_flags(char *cmd) -{ - size_t i; - int y; - char *new_str; - char **flags; - - i = get_next(cmd, ' ') + 1; - new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1); - y = 0; - while (cmd[i]) - new_str[y++] = cmd[i++]; - new_str[y] = '\0'; - flags = ft_split(new_str, ' '); - i = 0; - free(new_str); - return (flags); -} - -char *get_command(char *cmd) -{ - int i; - char *new_str; - - new_str = ft_calloc(get_next(cmd, ' ') + 1, 1); - i = -1; - while (cmd[++i] != ' ') - new_str[i] = cmd[i]; - new_str[i] = '\0'; - return (new_str); -} - -char **add_cmd(char **flg, char *cmd) -{ - char *tmp; - int i; - int check; - - tmp = NULL; - i = 0; - check = 0; - while (check == 0) - { - if (flg[i] == NULL) - check = 1; - if (tmp != NULL) - flg[i] = tmp; - tmp = flg[i]; - i++; - } - flg[0] = cmd; - return (flg); -} - -int parsing(char *argv[], t_data *data) -{ - int fd1; - int fd2; - - fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); - if (fd2 < 0) - return (-1); - fd1 = open(argv[1], O_RDONLY); - if (fd1 < 0) - { - close(fd2); - return (-1); - } - data->cmd1 = get_command(argv[2]); - data->cmd2 = get_command(argv[3]); - data->flg1 = get_flags(argv[2]); - data->flg1 = add_cmd(data->flg1, data->cmd1); - data->flg2 = get_flags(argv[3]); - data->flg2 = add_cmd(data->flg2, data->cmd2); - data->fl1 = argv[1]; - data->fl2 = argv[4]; - //data->ct_fl1 = read_file(fd1); - data->fd1 = fd1; - data->fd2 = fd2; - return (0); -} - -char *find_command(char **env, char *cmd) -{ - char *path; - char *s; - int i; - int y; - - while (*env != NULL) - { - if (ft_strnstr(*env, "PATH=/", ft_strlen(*env)) - && ft_strchr(*env, ':') > 0) - break; - env++; - } - i = 5; - y = 0; - s = ft_calloc(ft_strlen(*env) + 1, 1); - while ((*env)[i]) - { - if ((*env)[i] != ':') - s[y++] = (*env)[i]; - else - { - s = ft_strjoin(s, "/"); - path = ft_strjoin(s, cmd); - if (access(path, X_OK) == 0) - { - free(s); - return (path); - } - free(s); - free(path); - s = ft_calloc(ft_strlen(*env) + 1, 1); - y = 0; - } - i++; - } - return (NULL); -} - -void first_cmd(int fd[2], t_data data) -{ - dup2(data.fd1, 0); - dup2(fd[1], 1); - close(fd[0]); - close(fd[1]); - execve(data.cmd1, data.flg1, data.env); -} - -void second_cmd(int fd[2], t_data data) -{ - dup2(fd[0], 0); - dup2(data.fd2, 1); - close(fd[0]); - close(fd[1]); - execve(data.cmd2, data.flg2, data.env); -} - -int pipex(t_data data) -{ - int fd[2]; - int cmd1; - int cmd2; - - if (pipe(fd) < -1) - return (1); - cmd1 = fork(); - if (cmd1 == 0) - first_cmd(fd, data); - cmd2 = fork(); - if (cmd2 == 0) - second_cmd(fd, data); - close(fd[0]); - close(fd[1]); - waitpid(cmd1, NULL, 0); - waitpid(cmd2, NULL, 0); - return (0); -} - -int main(int argc, char *argv[], char **env) -{ - t_data data; - - //(void)argc; - //(void)argv; - //char **test = ft_split(argv[2], ' '); - //execve(find_command(env, argv[1]), test, env); - if (argc < 5) - return (1); - if (parsing(argv, &data)) - return (2); - data.cmd1 = find_command(env, data.cmd1); - if (data.cmd1 == NULL) - return (3); - data.cmd2 = find_command(env, data.cmd2); - if (data.cmd2 == NULL) - return (4); - data.env = env; - if (pipex(data)) - return (5); - close(data.fd1); - close(data.fd2); - return (0); -} diff --git a/pipex.h b/pipex.h index 94442e0..95e369e 100644 --- a/pipex.h +++ b/pipex.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */ -/* Updated: 2023/01/16 18:52:06 by erey-bet ### ########.fr */ +/* Updated: 2023/01/19 17:03:23 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,9 +29,19 @@ typedef struct s_data char *fl1; char *fl2; char *ct_fl1; - int fd1; - int fd2; + int fd1; + int fd2; char **env; } t_data; +int get_next(char *cmd, char c); +int print_error(int type, char *error); +char **get_flags(char *cmd); +char *get_command(char *cmd); +char **add_cmd(char **flg, char *cmd); +int parsing(char *argv[], t_data *data); +char *test_acces(char **s, char **cmd, char **path, int *y); +char *cmd_exist(char **env, char **cmd, int y, char **s); +char *find_command(char **env, char *cmd); + #endif diff --git a/pipex_parsing.c b/pipex_parsing.c new file mode 100644 index 0000000..dc1271f --- /dev/null +++ b/pipex_parsing.c @@ -0,0 +1,83 @@ + +#include "../pipex.h" + +int parsing(char *argv[], t_data *data) +{ + data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); + if (data->fd2 < 0) + return (1); + data->fd1 = open(argv[1], O_RDONLY); + if (data->fd1 < 0) + print_error(0, argv[1]); + data->cmd1 = get_command(argv[2]); + data->cmd2 = get_command(argv[3]); + data->flg1 = ft_split(argv[2], ' '); + data->flg2 = ft_split(argv[3], ' '); + data->fl1 = argv[1]; + data->fl2 = argv[4]; + return (0); +} + +char *test_acces(char **s, char **cmd, char **path, int *y) +{ + *s = ft_strjoin_free(*s, "/", 1); + if (*s == NULL) + return (NULL); + *path = ft_strjoin_free(*s, *cmd, 1); + if (*path == NULL) + return (NULL); + if (access(*path, X_OK) == 0) + { + free(*cmd); + return (*path); + } + free(*path); + *path = NULL; + *y = 0; + return (NULL); +} + +char *cmd_exist(char **env, char **cmd, int y, char **s) +{ + char *path; + int i; + + i = 5; + while ((*env)[i]) + { + if ((*env)[i] != ':') + (*s)[y++] = (*env)[i]; + else + { + test_acces(s, cmd, &path, &y); + if (path) + return (path); + *s = ft_calloc(ft_strlen(*env) + 1, 1); + if (*s == NULL) + return (NULL); + } + i++; + } + free(*s); + free(*cmd); + return (NULL); +} + +char *find_command(char **env, char *cmd) +{ + char *s; + int y; + + while (*env != NULL) + { + if (ft_strnstr(*env, "PATH=/", ft_strlen(*env)) + && ft_strchr(*env, ':') > 0) + break ; + env++; + } + y = 0; + s = ft_calloc(ft_strlen(*env) + 1, 1); + if (s == NULL) + return (NULL); + return (cmd_exist(env, &cmd, y, &s)); +} diff --git a/pipex_utils.c b/pipex_utils.c new file mode 100644 index 0000000..86a82c1 --- /dev/null +++ b/pipex_utils.c @@ -0,0 +1,106 @@ + +#include "../pipex.h" + +int get_next(char *cmd, char c) +{ + int i; + + i = 0; + while (cmd[i] != c) + { + if (!cmd[i]) + return (-1); + i++; + } + return (i); +} + +int print_error(int type, char *error) +{ + if (type == 0) + { + write(2, "zsh: no such file or directory: ", 32); + write(2, error, ft_strlen(error)); + } + if (type == 1) + { + write(2, "zsh: command not found: ", 24); + if (get_next(error, ' ') == -1) + write(2, error, ft_strlen(error)); + else + write(2, error, ft_strlen(error) + - (ft_strlen(error) - get_next(error, ' '))); + } + write(2, "\n", 1); + return (1); +} + +char **get_flags(char *cmd) +{ + int i; + int y; + char *new_str; + char **flags; + + i = get_next(cmd, ' ') + 1; + if (i == 0) + { + flags = ft_calloc(2, sizeof(char *)); + return (flags); + } + new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1); + if (new_str == NULL) + return (NULL); + y = 0; + while (cmd[i]) + new_str[y++] = cmd[i++]; + new_str[y] = '\0'; + flags = ft_split(new_str, ' '); + i = 0; + free(new_str); + return (flags); +} + +char *get_command(char *cmd) +{ + int i; + char *new_str; + + i = get_next(cmd, ' '); + if (i != -1) + new_str = ft_calloc(get_next(cmd, ' ') + 1, 1); + else + new_str = ft_calloc(ft_strlen(cmd) + 1, 1); + if (new_str == NULL) + return (NULL); + i = -1; + while (cmd[++i] != ' ' && cmd[i]) + new_str[i] = cmd[i]; + new_str[i] = '\0'; + return (new_str); +} + +char **add_cmd(char **flg, char *cmd) +{ + char *tmp; + int i; + int check; + + tmp = NULL; + i = 0; + check = 0; + while (check == 0) + { + if (flg[i] == NULL) + check = 1; + if (tmp != NULL) + flg[i] = tmp; + tmp = flg[i]; + i++; + } + flg[0] = ft_strdup(cmd); + if (flg[0] == NULL) + return (NULL); + flg[i] = NULL; + return (flg); +}