mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 11:26:51 +01:00
「✏️」 norm(src/exec/*): normed everything...
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
|
/* 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"
|
#include "minishell.h"
|
||||||
|
|
||||||
char *get_cmd_path(char *cmd, t_env *env)
|
char *get_cmd_global_path(char *cmd, t_env *env)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char *path;
|
char *path;
|
||||||
@ -36,6 +36,25 @@ char *get_cmd_path(char *cmd, t_env *env)
|
|||||||
return (path);
|
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 exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
|
||||||
{
|
{
|
||||||
int fork_pid;
|
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);
|
status = dup2(STDOUT_FILENO, STDIN_FILENO);
|
||||||
if (status == -1)
|
if (status == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
if (cmd->cmd[0] != '/')
|
status = switch_cmd_path(cmd, env_t);
|
||||||
cmd->cmd = get_cmd_path(cmd->cmd, env_t);
|
if (!status)
|
||||||
if (!(cmd->cmd))
|
|
||||||
return (-1);
|
return (-1);
|
||||||
fork_pid = fork();
|
fork_pid = fork();
|
||||||
if (fork_pid == -1)
|
if (fork_pid == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
if (!fork_pid)
|
if (!fork_pid)
|
||||||
{
|
|
||||||
status = execve(cmd->cmd, cmd->argv, env);
|
status = execve(cmd->cmd, cmd->argv, env);
|
||||||
|
if (!fork_pid)
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
waitpid(fork_pid, NULL, 0);
|
waitpid(fork_pid, NULL, 0);
|
||||||
if (status == -1)
|
|
||||||
return (-1);
|
|
||||||
return (0);
|
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);
|
status = dup2(STDOUT_FILENO, cmd->outfile);
|
||||||
if (status == -1)
|
if (status == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
if (cmd->cmd[0] != '/')
|
status = switch_cmd_path(cmd, env_t);
|
||||||
cmd->cmd = get_cmd_path(cmd->cmd, env_t);
|
if (!status)
|
||||||
if (!(cmd->cmd))
|
|
||||||
return (-1);
|
return (-1);
|
||||||
fork_pid = fork();
|
fork_pid = fork();
|
||||||
if (fork_pid == -1)
|
if (fork_pid == -1)
|
||||||
@ -94,8 +108,6 @@ int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
waitpid(fork_pid, NULL, 0);
|
waitpid(fork_pid, NULL, 0);
|
||||||
if (status == -1)
|
|
||||||
return (-1);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,19 +6,64 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/05/19 01:42:17 by mmoussou #+# #+# */
|
/* 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"
|
#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)
|
int get_path_list(char *path, t_list *list_entry)
|
||||||
{
|
{
|
||||||
DIR *path_dir;
|
DIR *path_dir;
|
||||||
struct dirent *dir_entry;
|
struct dirent *dir_entry;
|
||||||
char *abs_path;
|
int status;
|
||||||
struct stat entry;
|
|
||||||
t_list *new_list_entry;
|
|
||||||
|
|
||||||
path_dir = opendir(path);
|
path_dir = opendir(path);
|
||||||
if (!path_dir)
|
if (!path_dir)
|
||||||
@ -26,40 +71,41 @@ int get_path_list(char *path, t_list *list_entry)
|
|||||||
dir_entry = readdir(path_dir);
|
dir_entry = readdir(path_dir);
|
||||||
while (dir_entry)
|
while (dir_entry)
|
||||||
{
|
{
|
||||||
if (!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, "..",
|
|| !ft_strncmp(dir_entry->d_name, "..", 3))
|
||||||
ft_strlen(dir_entry->d_name)))
|
|
||||||
{
|
{
|
||||||
dir_entry = readdir(path_dir);
|
dir_entry = readdir(path_dir);
|
||||||
continue ;
|
continue ;
|
||||||
}
|
}
|
||||||
abs_path = ft_calloc(sizeof(char), strlen(path)
|
status = add_path_to_list(path, dir_entry, list_entry);
|
||||||
+ strlen(dir_entry->d_name) + 2);
|
if (status)
|
||||||
if (!abs_path)
|
closedir(path_dir);
|
||||||
|
if (status)
|
||||||
return (-1);
|
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);
|
dir_entry = readdir(path_dir);
|
||||||
}
|
}
|
||||||
closedir(path_dir);
|
closedir(path_dir);
|
||||||
return (0);
|
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 **get_path(char *path)
|
||||||
{
|
{
|
||||||
char **path_dir;
|
char **path_dir;
|
||||||
@ -79,14 +125,7 @@ char **get_path(char *path)
|
|||||||
get_path_list(path_dir[i], list_entry);
|
get_path_list(path_dir[i], list_entry);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
i = 0;
|
path_list = path_trans_list_to_char(list_entry);
|
||||||
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++;
|
|
||||||
}
|
|
||||||
ft_free("al", &path_dir, &list_entry);
|
ft_free("al", &path_dir, &list_entry);
|
||||||
return (path_list);
|
return (path_list);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/05/20 09:19:39 by mmoussou #+# #+# */
|
/* 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
|
# mode
|
||||||
0: ouvre un nouveau fichier
|
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
|
-1: pour signifier qu'il faut decrémenter la static
|
||||||
*/
|
*/
|
||||||
int fd_manager(int mode)
|
int fd_manager(int mode)
|
||||||
@ -25,13 +25,10 @@ int fd_manager(int mode)
|
|||||||
char *path;
|
char *path;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if (mode > 0)
|
if (mode < 0)
|
||||||
close(mode);
|
|
||||||
if (mode)
|
|
||||||
{
|
|
||||||
index--;
|
index--;
|
||||||
|
if (mode < 0)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
|
||||||
path = ft_calloc(sizeof(char), 24 + 3);
|
path = ft_calloc(sizeof(char), 24 + 3);
|
||||||
if (!path)
|
if (!path)
|
||||||
return (-1);
|
return (-1);
|
||||||
@ -41,9 +38,12 @@ int fd_manager(int mode)
|
|||||||
if (!index_itoa)
|
if (!index_itoa)
|
||||||
return (-1);
|
return (-1);
|
||||||
index++;
|
index++;
|
||||||
strcat(path, "/tmp/.minishell-heredoc-");
|
ft_strlcat(path, "/tmp/.minishell-heredoc-", 24);
|
||||||
strcat(path, index_itoa);
|
ft_strlcat(path, index_itoa, ft_strlen(index_itoa));
|
||||||
fd = open(path, O_RDONLY | O_WRONLY | O_TRUNC | O_CREAT, 0644);
|
if (mode > 0)
|
||||||
|
fd = open(path, O_RDONLY);
|
||||||
|
else
|
||||||
|
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
|
||||||
return (fd);
|
return (fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +52,6 @@ static int get_input(char *delimiter, int fd)
|
|||||||
char *line;
|
char *line;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
printf("%s | ", delimiter);
|
|
||||||
line = readline("heredoc> ");
|
line = readline("heredoc> ");
|
||||||
while (ft_strcmp(line, delimiter))
|
while (ft_strcmp(line, delimiter))
|
||||||
{
|
{
|
||||||
@ -73,9 +72,7 @@ static int get_input(char *delimiter, int fd)
|
|||||||
status = write(fd, "\0", 1);
|
status = write(fd, "\0", 1);
|
||||||
if (status == -1)
|
if (status == -1)
|
||||||
fd_manager(fd);
|
fd_manager(fd);
|
||||||
if (status == -1)
|
return (-(status == -1));
|
||||||
return (-1);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_heredoc(char *delimiter)
|
int ft_heredoc(char *delimiter)
|
||||||
@ -92,7 +89,8 @@ int ft_heredoc(char *delimiter)
|
|||||||
fork_pid = fork();
|
fork_pid = fork();
|
||||||
if (fork_pid == -1)
|
if (fork_pid == -1)
|
||||||
{
|
{
|
||||||
fd_manager(fd);
|
fd_manager(-1);
|
||||||
|
close(fd);
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
if (!fork_pid)
|
if (!fork_pid)
|
||||||
@ -102,5 +100,5 @@ int ft_heredoc(char *delimiter)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
waitpid(fork_pid, NULL, 0);
|
waitpid(fork_pid, NULL, 0);
|
||||||
return (fd);
|
return (fd_manager(1));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user