✏️」 norm(src/exec/*): normed everything...

This commit is contained in:
y-syo
2024-06-05 01:23:18 +02:00
parent 5642c267f0
commit cf0a230f28
4 changed files with 116 additions and 67 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
/* Updated: 2024/06/04 21:43:34 by mmoussou ### ########.fr */
/* Updated: 2024/06/04 23:07:27 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@
# include "env.h"
char **get_path(char *path);
int exec_split_cmd(t_list *list_cmd, t_env *env);
int exec_split_cmd(t_list *list_cmd, t_env *env);
/**
* @brief spawn a heredoc

View File

@ -6,13 +6,13 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
/* Updated: 2024/06/04 22:03:56 by mmoussou ### ########.fr */
/* Updated: 2024/06/05 01:08:00 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *get_cmd_path(char *cmd, t_env *env)
char *get_cmd_global_path(char *cmd, t_env *env)
{
int i;
char *path;
@ -36,6 +36,25 @@ char *get_cmd_path(char *cmd, t_env *env)
return (path);
}
char *get_cmd_local_path(char *cmd, t_env *env)
{
(void) cmd;
(void) env;
// TODO : get pwd, append cmd, done.
return (NULL);
}
int switch_cmd_path(t_cmd *cmd, t_env *env)
{
if (cmd->cmd[0] != '/')
cmd->cmd = get_cmd_global_path(cmd->cmd, env);
else if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
if (!(cmd->cmd))
return (-1);
return (0);
}
int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
{
int fork_pid;
@ -50,22 +69,18 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
status = dup2(STDOUT_FILENO, STDIN_FILENO);
if (status == -1)
return (-1);
if (cmd->cmd[0] != '/')
cmd->cmd = get_cmd_path(cmd->cmd, env_t);
if (!(cmd->cmd))
status = switch_cmd_path(cmd, env_t);
if (!status)
return (-1);
fork_pid = fork();
if (fork_pid == -1)
return (-1);
if (!fork_pid)
{
status = execve(cmd->cmd, cmd->argv, env);
if (!fork_pid)
exit(-1);
}
else
waitpid(fork_pid, NULL, 0);
if (status == -1)
return (-1);
return (0);
}
@ -80,9 +95,8 @@ int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t)
status = dup2(STDOUT_FILENO, cmd->outfile);
if (status == -1)
return (-1);
if (cmd->cmd[0] != '/')
cmd->cmd = get_cmd_path(cmd->cmd, env_t);
if (!(cmd->cmd))
status = switch_cmd_path(cmd, env_t);
if (!status)
return (-1);
fork_pid = fork();
if (fork_pid == -1)
@ -94,8 +108,6 @@ int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t)
}
else
waitpid(fork_pid, NULL, 0);
if (status == -1)
return (-1);
return (0);
}

View File

@ -6,19 +6,64 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 01:42:17 by mmoussou #+# #+# */
/* Updated: 2024/06/04 21:53:30 by mmoussou ### ########.fr */
/* Updated: 2024/06/05 01:05:24 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int add_element_to_list(t_list *list_entry, char *abs_path)
{
t_list *new_list_entry;
if (list_entry->content == NULL)
list_entry->content = abs_path;
else
{
new_list_entry = ft_lstnew(abs_path);
if (!new_list_entry)
{
free(abs_path);
return (-1);
}
ft_lstadd_back(&list_entry, new_list_entry);
}
return (0);
}
int add_path_to_list(char *path, struct dirent *dir_entry, t_list *list_entry)
{
char *abs_path;
struct stat entry;
int status;
abs_path = ft_calloc(sizeof(char), strlen(path)
+ strlen(dir_entry->d_name) + 2);
if (!abs_path)
return (-1);
ft_strlcat(abs_path, path, ft_strlen(path));
ft_strlcat(abs_path, "/", 1);
ft_strlcat(abs_path, dir_entry->d_name, ft_strlen(dir_entry->d_name));
stat(abs_path, &entry);
if (S_ISREG(entry.st_mode)
&& (entry.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
{
status = add_element_to_list(list_entry, abs_path);
if (status)
{
free(abs_path);
return (-1);
}
}
free(abs_path);
return (0);
}
int get_path_list(char *path, t_list *list_entry)
{
DIR *path_dir;
struct dirent *dir_entry;
char *abs_path;
struct stat entry;
t_list *new_list_entry;
int status;
path_dir = opendir(path);
if (!path_dir)
@ -26,40 +71,41 @@ int get_path_list(char *path, t_list *list_entry)
dir_entry = readdir(path_dir);
while (dir_entry)
{
if (!ft_strncmp(dir_entry->d_name, ".", ft_strlen(dir_entry->d_name))
|| !ft_strncmp(dir_entry->d_name, "..",
ft_strlen(dir_entry->d_name)))
if (!ft_strncmp(dir_entry->d_name, ".", 2)
|| !ft_strncmp(dir_entry->d_name, "..", 3))
{
dir_entry = readdir(path_dir);
continue ;
}
abs_path = ft_calloc(sizeof(char), strlen(path)
+ strlen(dir_entry->d_name) + 2);
if (!abs_path)
status = add_path_to_list(path, dir_entry, list_entry);
if (status)
closedir(path_dir);
if (status)
return (-1);
strcat(abs_path, path);
strcat(abs_path, "/");
strcat(abs_path, dir_entry->d_name);
stat(abs_path, &entry);
if (S_ISREG(entry.st_mode)
&& (entry.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
{
if (list_entry->content == NULL)
list_entry->content = abs_path;
else
{
new_list_entry = ft_lstnew(abs_path);
if (!new_list_entry)
return (-1);
ft_lstadd_back(&list_entry, new_list_entry);
}
}
dir_entry = readdir(path_dir);
}
closedir(path_dir);
return (0);
}
char **path_trans_list_to_char(t_list *list_entry)
{
char **path_list;
int i;
i = 0;
path_list = ft_calloc(sizeof(char *), ft_lstsize(list_entry) + 1);
if (!path_list)
return (path_list);
while (list_entry)
{
path_list[i] = list_entry->content;
list_entry = list_entry->next;
i++;
}
return (path_list);
}
char **get_path(char *path)
{
char **path_dir;
@ -79,14 +125,7 @@ char **get_path(char *path)
get_path_list(path_dir[i], list_entry);
i++;
}
i = 0;
path_list = ft_calloc(sizeof(char *), ft_lstsize(list_entry) + 1);
while (list_entry)
{
path_list[i] = list_entry->content;
list_entry = list_entry->next;
i++;
}
path_list = path_trans_list_to_char(list_entry);
ft_free("al", &path_dir, &list_entry);
return (path_list);
}

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 09:19:39 by mmoussou #+# #+# */
/* Updated: 2024/05/23 07:18:01 by mmoussou ### ########.fr */
/* Updated: 2024/06/05 01:21:06 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,7 @@
/*
# mode
0: ouvre un nouveau fichier
<fd>: close ce fd
1: réouvre le fichier pour la lecture
-1: pour signifier qu'il faut decrémenter la static
*/
int fd_manager(int mode)
@ -25,13 +25,10 @@ int fd_manager(int mode)
char *path;
int fd;
if (mode > 0)
close(mode);
if (mode)
{
if (mode < 0)
index--;
if (mode < 0)
return (0);
}
path = ft_calloc(sizeof(char), 24 + 3);
if (!path)
return (-1);
@ -41,9 +38,12 @@ int fd_manager(int mode)
if (!index_itoa)
return (-1);
index++;
strcat(path, "/tmp/.minishell-heredoc-");
strcat(path, index_itoa);
fd = open(path, O_RDONLY | O_WRONLY | O_TRUNC | O_CREAT, 0644);
ft_strlcat(path, "/tmp/.minishell-heredoc-", 24);
ft_strlcat(path, index_itoa, ft_strlen(index_itoa));
if (mode > 0)
fd = open(path, O_RDONLY);
else
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
return (fd);
}
@ -52,7 +52,6 @@ static int get_input(char *delimiter, int fd)
char *line;
int status;
printf("%s | ", delimiter);
line = readline("heredoc> ");
while (ft_strcmp(line, delimiter))
{
@ -73,9 +72,7 @@ static int get_input(char *delimiter, int fd)
status = write(fd, "\0", 1);
if (status == -1)
fd_manager(fd);
if (status == -1)
return (-1);
return (0);
return (-(status == -1));
}
int ft_heredoc(char *delimiter)
@ -92,7 +89,8 @@ int ft_heredoc(char *delimiter)
fork_pid = fork();
if (fork_pid == -1)
{
fd_manager(fd);
fd_manager(-1);
close(fd);
return (-1);
}
if (!fork_pid)
@ -102,5 +100,5 @@ int ft_heredoc(char *delimiter)
}
else
waitpid(fork_pid, NULL, 0);
return (fd);
return (fd_manager(1));
}