Skip to content

Commit

Permalink
Who's reading this shit
Browse files Browse the repository at this point in the history
  • Loading branch information
akdovlet committed May 14, 2024
1 parent 59c59ff commit f39d94a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 48 deletions.
6 changes: 4 additions & 2 deletions lib/libft/src/ft_multi_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/07 22:07:22 by akdovlet #+# #+# */
/* Updated: 2024/01/14 16:32:59 by akdovlet ### ########.fr */
/* Updated: 2024/05/14 18:54:46 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"
#include <stdio.h>

static char **ft_splitter(char *str, char *sep, int *tab, int word_count)
{
Expand Down Expand Up @@ -46,9 +47,10 @@ char **ft_multi_split(char *str, char *sep)
int word_count;
char **strs;

tab = NULL;
word_count = ak_countword(str, sep, &tab);
if (word_count == -1)
return (NULL);
return (free(tab), NULL);
strs = ft_splitter(str, sep, tab, word_count);
free(tab);
return (strs);
Expand Down
6 changes: 4 additions & 2 deletions lib/libft/src/ft_strchr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 20:55:38 by akdovlet #+# #+# */
/* Updated: 2024/01/03 21:00:57 by akdovlet ### ########.fr */
/* Updated: 2024/05/14 18:35:56 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,12 +17,14 @@ char *ft_strchr(const char *s, int c)
int i;
char *str;

if (!s)
return (NULL);
str = (char *)s;
i = 0;
while (str[i] != (char)c)
{
if (str[i] == '\0')
return (0);
return (NULL);
i++;
}
return (str + i);
Expand Down
24 changes: 12 additions & 12 deletions src/ak_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void ak_pipe(t_data *data, int i)
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));
Expand All @@ -45,10 +46,10 @@ void ak_pipe(t_data *data, int i)
void child_out(t_data *data)
{
if (dup2(data->hermes, STDIN_FILENO) == -1)
perror("pipex");
clear_exit(data, EXIT_FAILURE);
close(data->hermes);
if (dup2(data->last, STDOUT_FILENO) == -1)
perror("pipex");
clear_exit(data, EXIT_FAILURE);
close(data->last);
cmd_exe(data, data->ac - 2);
}
Expand All @@ -59,25 +60,24 @@ void ak_pipeout(t_data *data, int i)
int j;

j = 0;
status = 0;
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_all(data), exit(EXIT_FAILURE));
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)
{
clear_all(data);
return (perror("pipex"));
}
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
if (!data->ids[i - 2])
child_out(data);
close(data->first);
close(data->hermes);
while (j < data->ac - 3)
{
if (waitpid(data->ids[j],&status, 0) == -1)
perror("waitpid");
if (WIFEXITED(status))
data->exit_code = WEXITSTATUS(status);
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++;
}
}
8 changes: 7 additions & 1 deletion src/free.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/07 00:41:28 by akdovlet #+# #+# */
/* Updated: 2024/05/13 17:02:02 by akdovlet ### ########.fr */
/* Updated: 2024/05/14 19:47:09 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,6 +20,10 @@ void clear_all(t_data *data)
data->ids = NULL;
if (data->first > 0)
close(data->first);
data->first = 0;
if (data->hermes > 0)
close(data->hermes);
data->hermes = 0;
if (data->last > 0)
close(data->last);
}
Expand All @@ -29,6 +33,8 @@ void ft_free(char **str)
int i;

i = 0;
if (!str)
return ;
while (str[i])
{
free(str[i]);
Expand Down
28 changes: 1 addition & 27 deletions src/parsing.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/23 17:52:22 by akdovlet #+# #+# */
/* Updated: 2024/05/13 17:41:36 by akdovlet ### ########.fr */
/* Updated: 2024/05/14 18:15:23 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -29,10 +29,7 @@ char **parse_env(char **env)

i = 0;
if (!env[0])
{

return (px_split(HARDPATH, ':'));
}
while (env[i])
{
if (!ft_strncmp("PATH=", env[i], 5))
Expand All @@ -41,26 +38,3 @@ char **parse_env(char **env)
}
return (ft_split("./", '\0'));
}

void find_exec(char *cmd, t_data *data)
{
int i;
char *full_path;

i = 0;
full_path = NULL;
while (data->path[i])
{
full_path = ft_strjoin(data->path[i], cmd);
if (!full_path)
return (clear_exit(data, EXIT_FAILURE));
if (!access(full_path, X_OK))
{
execve(full_path, data->cmd, data->env);
free(full_path);
clear_exit(data, 127);
}
free(full_path);
i++;
}
}
40 changes: 36 additions & 4 deletions src/seek_and_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,45 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/06 23:52:25 by akdovlet #+# #+# */
/* Updated: 2024/05/13 17:47:47 by akdovlet ### ########.fr */
/* Updated: 2024/05/14 20:29:55 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "pipex.h"

void find_exec(char *cmd, t_data *data)
{
int i;
char *full_path;

i = 0;
full_path = NULL;
while (data->path[i])
{
full_path = ft_strjoin(data->path[i], cmd);
if (!full_path)
return (clear_exit(data, EXIT_FAILURE));
if (!access(full_path, X_OK))
{
execve(full_path, data->cmd, data->env);
free(full_path);
perror("pipex");
clear_exit(data, 127);
}
free(full_path);
i++;
}
ft_dprintf(STDERR_FILENO, "pipex: %s: command not found\n", data->cmd[0]);
clear_exit(data, 127);
}

void cmd_exe(t_data *data, int i)
{
if (!data->cmd[0])
{
ft_dprintf(STDERR_FILENO, "%s: command not found\n", data->cmd[0]);
clear_exit(data, 127);
}
if (ft_strchr(data->cmd[0], '/'))
{
if (access(data->cmd[0], X_OK) == -1)
Expand All @@ -24,8 +55,8 @@ void cmd_exe(t_data *data, int i)
else
{
execve(data->cmd[0], data->cmd, data->env);
perror("execve");
clear_exit(data, EXIT_FAILURE);
ft_dprintf(STDERR_FILENO, "%s: command not found\n", data->cmd[0]);
clear_exit(data, 127);
}
}
else
Expand All @@ -38,11 +69,12 @@ 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));
while (i < data->ac - 2)
{
data->cmd = ft_multi_split(data->av[i], " \t\n'");
data->cmd = ft_multi_split(data->av[i], " '");
if (!data->cmd)
return (clear_exit(data, EXIT_FAILURE));
ak_pipe(data, i);
Expand Down

0 comments on commit f39d94a

Please sign in to comment.