[✏️] norm: normed project.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
22
include/exec.h
Normal file
22
include/exec.h
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exec.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/06 14:15:47 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/06 14:18:27 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef EXEC_H
|
||||
# define EXEC_H
|
||||
|
||||
# include <sys/wait.h>
|
||||
|
||||
#include "pipex.h"
|
||||
|
||||
void exec_pipe(t_pipex *pipex);
|
||||
|
||||
#endif
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/22 21:14:06 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/03 14:24:09 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/06 17:26:47 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,10 +23,15 @@
|
||||
typedef struct s_pipex
|
||||
{
|
||||
t_pcmd *cmd;
|
||||
char **path;
|
||||
char **env;
|
||||
int infile;
|
||||
int outfile;
|
||||
char *outfile;
|
||||
} t_pipex;
|
||||
|
||||
void ft_senderror(t_pipex *pipex, char *msg);
|
||||
void getpath(t_pipex *pipex);
|
||||
void get_arrcmd_path(t_pipex *pipex);
|
||||
void exec_pipe(t_pipex *pipex);
|
||||
|
||||
#endif
|
||||
|
BIN
obj/src/exec/exec.o
Normal file
BIN
obj/src/exec/exec.o
Normal file
Binary file not shown.
BIN
obj/src/main.o
BIN
obj/src/main.o
Binary file not shown.
BIN
obj/src/test.o
BIN
obj/src/test.o
Binary file not shown.
BIN
obj/src/utils/getpath.o
Normal file
BIN
obj/src/utils/getpath.o
Normal file
Binary file not shown.
BIN
obj/src/utils/set_path.o
Normal file
BIN
obj/src/utils/set_path.o
Normal file
Binary file not shown.
91
src/exec/exec.c
Normal file
91
src/exec/exec.c
Normal file
@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exec.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/06 14:07:27 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/07 11:01:39 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "exec.h"
|
||||
#include "pipex.h"
|
||||
#include <sys/wait.h>
|
||||
|
||||
//void ft_printtamerelaputedemaelysdearraydemerde(char **arr)
|
||||
//{
|
||||
// char **ll = arr;
|
||||
//
|
||||
// while (*ll)
|
||||
// {
|
||||
// ft_putendl_fd(*ll, STDOUT_FILENO);
|
||||
// ll++;
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void ft_printstdindemerdemaelystagueule_ps_flocon_est_mims(int fd)
|
||||
//{
|
||||
//// char *s = malloc(1);
|
||||
// ft_putnbr_fd(fd, 2);
|
||||
// /*while (read(fd, s, 1))
|
||||
// {
|
||||
// ft_putchar_fd(*s, 2);
|
||||
// }*/
|
||||
//}
|
||||
//
|
||||
|
||||
pid_t ft_exec_fst_pipe(t_pipex *pipex, int fd[2])
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
pid = fork();
|
||||
if (!pid)
|
||||
{
|
||||
dup2(pipex->infile, STDIN_FILENO);
|
||||
dup2(fd[1], STDOUT_FILENO);
|
||||
close(pipex->infile);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
execve(pipex->cmd[0].cmd, pipex->cmd[0].option, pipex->env);
|
||||
}
|
||||
return (pid);
|
||||
}
|
||||
|
||||
pid_t ft_exec_scnd_pipe(t_pipex *pipex, int fd[2])
|
||||
{
|
||||
pid_t pid;
|
||||
int outfile;
|
||||
|
||||
pid = fork();
|
||||
if (!pid)
|
||||
{
|
||||
outfile = open(pipex->outfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
||||
dup2(fd[0], STDIN_FILENO);
|
||||
dup2(outfile, STDOUT_FILENO);
|
||||
close(outfile);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
execve(pipex->cmd[1].cmd, pipex->cmd[1].option, pipex->env);
|
||||
}
|
||||
return (pid);
|
||||
}
|
||||
|
||||
void exec_pipe(t_pipex *pipex)
|
||||
{
|
||||
pid_t pid;
|
||||
pid_t pid2;
|
||||
int fd[2];
|
||||
|
||||
if (pipe(fd) == -1)
|
||||
ft_senderror(pipex, "Error in opening the pipe");
|
||||
pid = ft_exec_fst_pipe(pipex, fd);
|
||||
pid2 = ft_exec_scnd_pipe(pipex, fd);
|
||||
close(fd[1]);
|
||||
close(pipex->infile);
|
||||
close(fd[1]);
|
||||
close(fd[0]);
|
||||
waitpid(pid, NULL, 0);
|
||||
waitpid(pid2, NULL, 0);
|
||||
}
|
27
src/main.c
27
src/main.c
@ -6,11 +6,10 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/21 10:03:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/03 16:30:07 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/07 11:03:19 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
//#include "libft.h"
|
||||
#include "libft.h"
|
||||
#include "parsing.h"
|
||||
#include "pipex.h"
|
||||
@ -61,6 +60,8 @@ void ft_senderror(t_pipex *pipex, char *msg)
|
||||
ft_putendl_fd(msg, STDERR_FILENO);
|
||||
if (!pipex)
|
||||
exit(EXIT_FAILURE);
|
||||
if (pipex->path)
|
||||
ft_freearr(pipex->path);
|
||||
if (pipex->cmd)
|
||||
free_pcmd(pipex->cmd);
|
||||
free(pipex);
|
||||
@ -71,18 +72,24 @@ int main(int ac, char **av, char **env)
|
||||
{
|
||||
t_pipex *pipex;
|
||||
|
||||
check_empty_args(av);
|
||||
pipex = malloc(sizeof(t_pipex));
|
||||
if (ac != 5)
|
||||
ft_senderror(pipex, "Error : Invalid number of args");
|
||||
check_empty_args(av);
|
||||
pipex->infile = open(av[1], O_RDONLY);
|
||||
pipex->outfile = open(av[ac - 1], O_RDONLY);
|
||||
dup2(pipex->infile, STDIN_FILENO);
|
||||
pipex->outfile = av[ac - 1];
|
||||
pipex->cmd = parse_cmd(ac - 3, av);
|
||||
while (*env)
|
||||
{
|
||||
ft_putstr_fd(*env, STDOUT_FILENO);
|
||||
env++;
|
||||
}
|
||||
pipex->env = env;
|
||||
getpath(pipex);
|
||||
if (pipex->path == NULL)
|
||||
ft_senderror(pipex, "Error : Can't find path");
|
||||
ft_strlcpy(*(pipex->path), *(pipex->path) + 5, \
|
||||
ft_strlen(*(pipex->path) + 5) + 1);
|
||||
get_arrcmd_path(pipex);
|
||||
exec_pipe(pipex);
|
||||
ft_freearr(pipex->path);
|
||||
free_pcmd(pipex->cmd);
|
||||
free(pipex);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/22 21:13:26 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/03 14:20:56 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/07 11:03:02 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_freetab.c :+: :+: :+: */
|
||||
/* ft_freearr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/28 22:34:09 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/30 11:58:39 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/07 11:02:43 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
30
src/utils/getpath.c
Normal file
30
src/utils/getpath.c
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* getpath.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/04 15:32:56 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/07 11:01:17 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "pipex.h"
|
||||
|
||||
void getpath(t_pipex *pipex)
|
||||
{
|
||||
char **env;
|
||||
|
||||
env = pipex->env;
|
||||
while (*env)
|
||||
{
|
||||
if (!ft_strncmp(*env, "PATH=", 5))
|
||||
{
|
||||
pipex->path = ft_split(*env, ':');
|
||||
return ;
|
||||
}
|
||||
env++;
|
||||
}
|
||||
pipex->path = NULL;
|
||||
}
|
58
src/utils/set_path.c
Normal file
58
src/utils/set_path.c
Normal file
@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_path.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/06 13:31:15 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/06 17:30:03 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "pipex.h"
|
||||
|
||||
char *join_cmd(char *path, char *cmd)
|
||||
{
|
||||
char *str;
|
||||
char *ret;
|
||||
|
||||
str = ft_strjoin(path, "/");
|
||||
ret = ft_strjoin(str, cmd);
|
||||
free(str);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
char *get_cmd_path(t_pipex *pipex, char *cmd)
|
||||
{
|
||||
char **path;
|
||||
char *tmp;
|
||||
|
||||
path = pipex->path;
|
||||
while (*path)
|
||||
{
|
||||
tmp = join_cmd(*path, cmd);
|
||||
if (access(tmp, X_OK) == 0)
|
||||
{
|
||||
free(cmd);
|
||||
cmd = ft_strdup(tmp);
|
||||
free (tmp);
|
||||
return (cmd);
|
||||
}
|
||||
free(tmp);
|
||||
path++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void get_arrcmd_path(t_pipex *pipex)
|
||||
{
|
||||
t_pcmd *cmd;
|
||||
|
||||
cmd = pipex->cmd;
|
||||
while ((*cmd).option)
|
||||
{
|
||||
(*cmd).cmd = get_cmd_path(pipex, (*cmd).cmd);
|
||||
cmd++;
|
||||
}
|
||||
}
|
6
test.c
6
test.c
@ -6,11 +6,10 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/03 15:36:42 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/03 16:27:03 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/04 15:20:24 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
@ -24,7 +23,8 @@ int main()
|
||||
int fd[2];
|
||||
if (pipe(fd) == -1)
|
||||
return(1);
|
||||
int pid = fork();
|
||||
int pid;
|
||||
pid = fork();
|
||||
if (!pid)
|
||||
{
|
||||
dup2(fd[1], STDOUT_FILENO);
|
||||
|
Reference in New Issue
Block a user