[✏️] norm: normed project.

This commit is contained in:
KeyZox committed 2024-04-07 11:03:38 +02:00
1 parent 4e0a049e1d
commit 81f16dc864
21 files changed
+236 -20

No files matched your search

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+22
View 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
+7 -2
View File
@@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 21:14:06 by adjoly #+# #+# */ /* 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 typedef struct s_pipex
{ {
t_pcmd *cmd; t_pcmd *cmd;
char **path;
char **env;
int infile; int infile;
int outfile; char *outfile;
} t_pipex; } t_pipex;
void ft_senderror(t_pipex *pipex, char *msg); 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 #endif
+3
View File
@@ -0,0 +1,3 @@
"src/main.c",$
"obj/src/main.o"$
"file": "src/main.c"$
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
+91
View 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);
}
+17 -10
View File
@@ -6,11 +6,10 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 10:03:04 by adjoly #+# #+# */ /* 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 "libft.h"
#include "parsing.h" #include "parsing.h"
#include "pipex.h" #include "pipex.h"
@@ -61,6 +60,8 @@ void ft_senderror(t_pipex *pipex, char *msg)
ft_putendl_fd(msg, STDERR_FILENO); ft_putendl_fd(msg, STDERR_FILENO);
if (!pipex) if (!pipex)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (pipex->path)
ft_freearr(pipex->path);
if (pipex->cmd) if (pipex->cmd)
free_pcmd(pipex->cmd); free_pcmd(pipex->cmd);
free(pipex); free(pipex);
@@ -71,18 +72,24 @@ int main(int ac, char **av, char **env)
{ {
t_pipex *pipex; t_pipex *pipex;
check_empty_args(av);
pipex = malloc(sizeof(t_pipex)); 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->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); pipex->cmd = parse_cmd(ac - 3, av);
while (*env) pipex->env = env;
{ getpath(pipex);
ft_putstr_fd(*env, STDOUT_FILENO); if (pipex->path == NULL)
env++; 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_pcmd(pipex->cmd);
free(pipex); free(pipex);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
+2 -2
View File
@@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 21:13:26 by adjoly #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -47,7 +47,7 @@ t_pcmd *parse_cmd(int ac, char **av)
int i; int i;
t_pcmd *cmd_arr; t_pcmd *cmd_arr;
t_pcmd *cmd_tmp; t_pcmd *cmd_tmp;
i = 0; i = 0;
cmd_arr = ft_calloc(ac + 1, sizeof(t_pcmd)); cmd_arr = ft_calloc(ac + 1, sizeof(t_pcmd));
cmd_tmp = cmd_arr; cmd_tmp = cmd_arr;
@@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_freetab.c :+: :+: :+: */ /* ft_freearr.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 22:34:09 by adjoly #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -17,7 +17,7 @@ void ft_freearr(char **arr)
char **tmp; char **tmp;
tmp = arr; tmp = arr;
while(tmp && *tmp) while (tmp && *tmp)
{ {
free(*tmp); free(*tmp);
tmp++; tmp++;
+30
View 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
View 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++;
}
}
+3 -3
View File
@@ -6,11 +6,10 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:36:42 by adjoly #+# #+# */ /* 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 <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
@@ -24,7 +23,8 @@ int main()
int fd[2]; int fd[2];
if (pipe(fd) == -1) if (pipe(fd) == -1)
return(1); return(1);
int pid = fork(); int pid;
pid = fork();
if (!pid) if (!pid)
{ {
dup2(fd[1], STDOUT_FILENO); dup2(fd[1], STDOUT_FILENO);