-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* child_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/13 17:37:04 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 21:56:55 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex_bonus.h" | ||
|
||
static char *get_cmd(char **paths, char *cmd) | ||
{ | ||
char *tmp; | ||
char *command; | ||
|
||
while (*paths) | ||
{ | ||
tmp = ft_strjoin(*paths, "/"); | ||
command = ft_strjoin(tmp, cmd); | ||
free(tmp); | ||
if (access(command, 0) == 0) | ||
return (command); | ||
free(command); | ||
paths++; | ||
} | ||
return (NULL); | ||
} | ||
|
||
static void sub_dup2(int zero, int first) | ||
{ | ||
dup2(zero, 0); | ||
dup2(first, 1); | ||
} | ||
|
||
void child(t_ppxb p, char **argv, char **envp) | ||
{ | ||
p.pid = fork(); | ||
if (!p.pid) | ||
{ | ||
if (p.idx == 0) | ||
sub_dup2(p.infile, p.pipe[1]); | ||
else if (p.idx == p.cmd_nmbs - 1) | ||
sub_dup2(p.pipe[2 * p.idx - 2], p.outfile); | ||
else | ||
sub_dup2(p.pipe[2 * p.idx - 2], p.pipe[2 * p.idx + 1]); | ||
close_pipes(&p); | ||
p.cmd_args = ft_split(argv[2 + p.here_doc + p.idx], ' '); | ||
p.cmd = get_cmd(p.cmd_paths, p.cmd_args[0]); | ||
if (!p.cmd) | ||
{ | ||
msg_pipe(p.cmd_args[0]); | ||
child_free(&p); | ||
exit(1); | ||
} | ||
execve(p.cmd, p.cmd_args, envp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* error_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/08 17:43:45 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 20:59:21 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex_bonus.h" | ||
|
||
int msg(char *err) | ||
{ | ||
write(2, err, ft_strlen(err)); | ||
return (1); | ||
} | ||
|
||
void msg_pipe(char *arg) | ||
{ | ||
write(2, ERR_CMD, ft_strlen(ERR_CMD)); | ||
write(2, arg, ft_strlen(arg)); | ||
write(2, "\n", 1); | ||
} | ||
|
||
void msg_error(char *err) | ||
{ | ||
perror(err); | ||
exit (1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* files_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/13 16:14:14 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 16:15:17 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex_bonus.h" | ||
|
||
char *find_path(char **envp) | ||
{ | ||
while (ft_strncmp("PATH", *envp, 4)) | ||
envp++; | ||
return (*envp + 5); | ||
} | ||
|
||
void get_infile(char **argv, t_ppxb *pipex) | ||
{ | ||
if (!ft_strncmp("here_doc", argv[1], 9)) | ||
here_doc(argv[2], pipex); | ||
else | ||
{ | ||
pipex->infile = open(argv[1], O_RDONLY); | ||
if (pipex->infile < 0) | ||
msg_error(ERR_INFILE); | ||
} | ||
} | ||
|
||
void get_outfile(char *argv, t_ppxb *pipex) | ||
{ | ||
if (pipex->here_doc) | ||
pipex->outfile = open(argv, O_WRONLY | O_CREAT | O_APPEND, 0000644); | ||
else | ||
pipex->outfile = open(argv, O_CREAT | O_RDWR | O_TRUNC, 0000644); | ||
if (pipex->outfile < 0) | ||
msg_error(ERR_OUTFILE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* free_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/09 14:56:20 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 20:58:39 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex_bonus.h" | ||
|
||
void parent_free(t_ppxb *pipex) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
close(pipex->infile); | ||
close(pipex->outfile); | ||
if (pipex->here_doc) | ||
unlink(".heredoc_tmp"); | ||
while (pipex->cmd_paths[i]) | ||
{ | ||
free(pipex->cmd_paths[i]); | ||
i++; | ||
} | ||
free(pipex->cmd_paths); | ||
free(pipex->pipe); | ||
} | ||
|
||
void child_free(t_ppxb *pipex) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
while (pipex->cmd_args[i]) | ||
{ | ||
free(pipex->cmd_args[i]); | ||
i++; | ||
} | ||
free(pipex->cmd_args); | ||
free(pipex->cmd); | ||
} | ||
|
||
void pipe_free(t_ppxb *pipex) | ||
{ | ||
close(pipex->infile); | ||
close(pipex->outfile); | ||
if (pipex->here_doc) | ||
unlink(".heredoc_tmp"); | ||
free(pipex->pipe); | ||
msg(ERR_ENVP); | ||
exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* here_doc_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/13 16:03:19 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 16:13:17 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex_bonus.h" | ||
|
||
int args_in(char *arg, t_ppxb *pipex) | ||
{ | ||
if (arg && !ft_strncmp("here_doc", arg, 9)) | ||
{ | ||
pipex->here_doc = 1; | ||
return (6); | ||
} | ||
else | ||
{ | ||
pipex->here_doc = 0; | ||
return (5); | ||
} | ||
} | ||
|
||
void here_doc(char *argv, t_ppxb *pipex) | ||
{ | ||
int file; | ||
char *buf; | ||
|
||
file = open(".heredoc_tmp", O_CREAT | O_WRONLY | O_TRUNC, 0000644); | ||
if (file < 0) | ||
msg_error(ERR_HEREDOC); | ||
while (1) | ||
{ | ||
write(1, "heredoc> ", 9); | ||
if (get_next_line(0, &buf) < 0) | ||
exit(1); | ||
if (!ft_strncmp(argv, buf, ft_strlen(argv) + 1)) | ||
break ; | ||
write(file, buf, ft_strlen(buf)); | ||
write(file, "\n", 1); | ||
free(buf); | ||
} | ||
free(buf); | ||
close(file); | ||
pipex->infile = open(".heredoc_tmp", O_RDONLY); | ||
if (pipex->infile < 0) | ||
{ | ||
unlink(".heredoc_tmp"); | ||
msg_error(ERR_HEREDOC); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* pipex_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/13 16:02:17 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 21:02:42 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex_bonus.h" | ||
|
||
static void creat_pipes(t_ppxb *pipex) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
while (i < pipex->cmd_nmbs - 1) | ||
{ | ||
if (pipe(pipex->pipe + 2 * i) < 0) | ||
parent_free(pipex); | ||
i++; | ||
} | ||
} | ||
|
||
void close_pipes(t_ppxb *pipex) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
while (i < (pipex->pipe_nmbs)) | ||
{ | ||
close(pipex->pipe[i]); | ||
i++; | ||
} | ||
} | ||
|
||
int main(int argc, char *argv[], char *envp[]) | ||
{ | ||
t_ppxb pipex; | ||
|
||
if (argc < args_in(argv[1], &pipex)) | ||
return (msg(ERR_INPUT)); | ||
get_infile(argv, &pipex); | ||
get_outfile(argv[argc - 1], &pipex); | ||
pipex.cmd_nmbs = argc - 3 - pipex.here_doc; | ||
pipex.pipe_nmbs = 2 * (pipex.cmd_nmbs - 1); | ||
pipex.pipe = (int *)malloc(sizeof(int) * pipex.pipe_nmbs); | ||
if (!pipex.pipe) | ||
msg_error(ERR_PIPE); | ||
pipex.env_path = find_path(envp); | ||
pipex.cmd_paths = ft_split(pipex.env_path, ':'); | ||
if (!pipex.cmd_paths) | ||
pipe_free(&pipex); | ||
creat_pipes(&pipex); | ||
pipex.idx = -1; | ||
while (++(pipex.idx) < pipex.cmd_nmbs) | ||
child(pipex, argv, envp); | ||
close_pipes(&pipex); | ||
waitpid(-1, NULL, 0); | ||
parent_free(&pipex); | ||
return (0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/06 16:23:03 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/06 16:23:05 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "get_next_line.h" | ||
|
||
int some_error(char *str) | ||
{ | ||
if (str) | ||
free(str); | ||
return (-1); | ||
} | ||
|
||
int get_next_line(int fd, char **line) | ||
{ | ||
char *buf; | ||
int rd; | ||
static char *rem; | ||
|
||
if (fd < 0 || BUFFER_SIZE < 1 || !line) | ||
return (-1); | ||
buf = (char *)malloc(BUFFER_SIZE + 1); | ||
if (!buf) | ||
return (some_error(rem)); | ||
rd = 1; | ||
while (!find_nl(rem) && rd > 0) | ||
{ | ||
rd = read(fd, buf, BUFFER_SIZE); | ||
if (rd < 0) | ||
return (some_error(buf)); | ||
buf[rd] = '\0'; | ||
rem = str_join(rem, buf); | ||
} | ||
free(buf); | ||
*line = get_line(rem); | ||
rem = trim_rem(rem); | ||
if (rd == 0 && !rem) | ||
return (0); | ||
return (1); | ||
} |
Oops, something went wrong.