Skip to content

Commit

Permalink
After talkin' behind my back like a bitch would
Browse files Browse the repository at this point in the history
  • Loading branch information
akdovlet committed May 15, 2024
1 parent f39d94a commit ac886b0
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 45 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/01 13:57:12 by akdovlet #+# #+# #
# Updated: 2024/05/12 15:41:16 by akdovlet ### ########.fr #
# Updated: 2024/05/15 00:48:39 by akdovlet ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -17,6 +17,7 @@ LIBFT = $(LIBDIR)/libft/libft.a
SRC := check_file.c \
free.c \
main.c \
dr_here.c \
ak_pipe.c \
parsing.c \
px_split.c \
Expand Down
11 changes: 8 additions & 3 deletions include/pipex.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/20 23:07:37 by akdovlet #+# #+# */
/* Updated: 2024/05/13 17:47:22 by akdovlet ### ########.fr */
/* Updated: 2024/05/15 01:32:15 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -40,12 +40,17 @@ typedef struct s_data
int hermes;
int cmd_count;
int exit_code;
bool here_doc;
int here_doc;
} t_data;

/******************************check_file.c***********************************/
int check_file(char *file);



void dr_here(t_data *data);
int ft_strcmp(char *s1, char *s2);

/********************************free.c***************************************/
void ft_free(char **str);
void clear_all(t_data *data);
Expand All @@ -67,7 +72,7 @@ char **px_split(char const *s, char c);

/******************************seek_and_execute.c*****************************/
void seek_and_execute(t_data *data);
void cmd_exe(t_data *data, int i);
void cmd_exe(t_data *data);

/******************************setup.c****************************************/
bool setup(t_data *data, int ac, char **av, char **env);
Expand Down
29 changes: 12 additions & 17 deletions src/ak_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ void child(int fd[2], t_data *data, int i)
{
close(fd[0]);
if (dup2(data->hermes, STDIN_FILENO) == -1)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
return (perror("pipex1"), clear_exit(data, EXIT_FAILURE));
close(data->hermes);
if(dup2(fd[1], STDOUT_FILENO) == -1)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
return (perror("pipex2"), clear_exit(data, EXIT_FAILURE));
close(fd[1]);
cmd_exe(data, i);
cmd_exe(data);
}

void ak_pipe(t_data *data, int i)
Expand All @@ -31,12 +31,11 @@ void ak_pipe(t_data *data, int i)
if (data->first == -1 && i == 2)
return ;
if (pipe(fd) == -1)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
printf("i is: %d\n", i);
data->ids[i - 2] = fork();
if (data->ids[i - 2] < 0)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
if (!data->ids[i - 2])
return (perror("pipex3"), clear_exit(data, EXIT_FAILURE));
data->ids[i - (2 + data->here_doc)] = fork();
if (data->ids[i - (2 + data->here_doc)] < 0)
return (perror("pipex4"), clear_exit(data, EXIT_FAILURE));
if (!data->ids[i - (2 + data->here_doc)])
child(fd, data, i);
close(fd[1]);
close(data->hermes);
Expand All @@ -51,7 +50,7 @@ void child_out(t_data *data)
if (dup2(data->last, STDOUT_FILENO) == -1)
clear_exit(data, EXIT_FAILURE);
close(data->last);
cmd_exe(data, data->ac - 2);
cmd_exe(data);
}

void ak_pipeout(t_data *data, int i)
Expand All @@ -64,20 +63,16 @@ void ak_pipeout(t_data *data, int i)
data->last = open(data->av[data->ac - 1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (data->last < 0)
return (perror(data->av[data->ac -1]), clear_exit(data, EXIT_FAILURE));
data->ids[i - 2] = fork();
printf("last i is: %d\n", i);
if (data->ids[i - 2] < 0)
data->ids[i - (2 + data->here_doc)] = fork();
if (data->ids[i - (2 + data->here_doc)] < 0)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
if (!data->ids[i - 2])
if (!data->ids[i - (2 + data->here_doc)])
child_out(data);
close(data->hermes);
while (j < data->ac - 3)
{
printf("ids value is: %d\n", data->ids[j]);
waitpid(data->ids[j],&status, 0);
printf("status is: %d\n", status);
data->exit_code = WEXITSTATUS(status);
printf("exit_code is: %d\n", data->exit_code);
j++;
}
}
69 changes: 69 additions & 0 deletions src/dr_here.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dr_here.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 00:17:44 by akdovlet #+# #+# */
/* Updated: 2024/05/15 02:06:07 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "pipex.h"

int ft_strcmp(char *s1, char *s2)
{
while (*s1 && *s2)
{
if (*s1 != *s2)
return (*s1 - *s2);
s1++;
s2++;
}
return (*s1 - *s2);
}

void dr_dre(t_data *data, int *fd)
{
char *line;

close(fd[0]);
while (1)
{
write(STDOUT_FILENO, "> ", 2);
line = get_next_line(STDIN_FILENO);
if (!line)
clear_exit(data, EXIT_FAILURE);
if (!ft_strncmp(line, data->av[2], ft_strlen(data->av[2])))
{
free(line);
break ;
}
write(fd[1], line, ft_strlen(line));
free(line);
}
exit(EXIT_SUCCESS);
}

void dr_here(t_data *data)
{
int fd[2];
pid_t pid;
int status;

if (pipe(fd) == -1)
clear_exit(data, EXIT_FAILURE);
pid = fork();
if (pid == -1)
clear_exit(data, EXIT_FAILURE);
if (!pid)
dr_dre(data, fd);
else
{
close(fd[1]);
data->hermes = fd[0];
waitpid(pid, &status, 0);
data->exit_code = WEXITSTATUS(status);
}
}
11 changes: 1 addition & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/20 23:07:20 by akdovlet #+# #+# */
/* Updated: 2024/05/13 17:36:23 by akdovlet ### ########.fr */
/* Updated: 2024/05/14 23:14:01 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -42,12 +42,3 @@ int main(int ac, char **av, char **env)
clear_all(&data);
return (data.exit_code);
}

// int main()
// {
// char **strs;

// strs = ft_split("./", '\0');
// printf("%s\n", strs[0]);
// printf("%s\n", strs[1]);
// }
12 changes: 7 additions & 5 deletions src/seek_and_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/06 23:52:25 by akdovlet #+# #+# */
/* Updated: 2024/05/14 20:29:55 by akdovlet ### ########.fr */
/* Updated: 2024/05/15 01:49:59 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,7 +38,7 @@ void find_exec(char *cmd, t_data *data)
clear_exit(data, 127);
}

void cmd_exe(t_data *data, int i)
void cmd_exe(t_data *data)
{
if (!data->cmd[0])
{
Expand Down Expand Up @@ -69,9 +69,13 @@ void seek_and_execute(t_data *data)

i = 2;
data->ids = malloc(sizeof(pid_t) * (data->ac - 3));
printf("size is: %d\n", data->ac - 3);
if (!data->ids)
return (clear_exit(data, EXIT_FAILURE));
if (data->here_doc)
{
i = 3;
dr_here(data);
}
while (i < data->ac - 2)
{
data->cmd = ft_multi_split(data->av[i], " '");
Expand All @@ -85,6 +89,4 @@ void seek_and_execute(t_data *data)
if (!data->cmd)
return (clear_exit(data, EXIT_FAILURE));
ak_pipeout(data, i);
free(data->ids);
data->ids = NULL;
}
24 changes: 15 additions & 9 deletions src/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,35 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/06 23:41:38 by akdovlet #+# #+# */
/* Updated: 2024/05/13 17:03:28 by akdovlet ### ########.fr */
/* Updated: 2024/05/15 01:06:15 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "pipex.h"

bool setup(t_data *data, int ac, char **av, char **env)
void infile_setup(t_data *data, char **av)
{
data->path = parse_env(env);
if (!data->path)
{
perror("pipex");
return (false);
}
data->here_doc = 0;
data->first = check_file(av[1]);
if (data->first == -1)
{
data->hermes = open("/dev/null", O_RDONLY);
if (data->hermes < 0)
return (perror(av[1]), ft_free(data->path), false);
return (perror(av[1]), ft_free(data->path), exit(EXIT_FAILURE));
}
else
data->hermes = data->first;
}

bool setup(t_data *data, int ac, char **av, char **env)
{
data->path = parse_env(env);
if (!data->path)
return (perror("pipex"), false);
if (!ft_strcmp("here_doc", av[1]))
data->here_doc = 1;
else
infile_setup(data, av);
data->ids = NULL;
data->cmd = NULL;
data->exit_code = 0;
Expand Down

0 comments on commit ac886b0

Please sign in to comment.