Revert "「」 feat(exec_cmd): can now execute commands ! still kind of a wip tho"

This reverts commit 32e09ff98a41d05937627c5a302c430c91220474.
This commit is contained in:
yosyo
2024-06-02 21:18:42 +02:00
parent 32e09ff98a
commit 06f2480711
2 changed files with 11 additions and 148 deletions

View File

@ -1,113 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_split_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
/* Updated: 2024/06/02 21:15:03 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *get_cmd_path(char *cmd, t_env *env)
{
int i;
char *path;
char **path_list;
path = NULL;
path_list = get_path(env_get_value("PATH2", env));
i = 0;
while (path_list[i])
{
if (!ft_strcmp(ft_strrchr(path_list[i], '/') + 1, cmd))
{
path = ft_strdup(path_list[i]);
return (path);
}
i++;
}
free(cmd);
return (path);
}
int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
{
int status;
status = dup2(cmd->infile, STDIN_FILENO);
if (status == -1)
return (-1);
if (cmd->outfile != STDOUT_FILENO)
status = dup2(STDOUT_FILENO, cmd->outfile);
else
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))
return (-1);
int fork_pid = fork();
if (fork_pid == -1)
return (-1);
if (!fork_pid)
{
status = execve(cmd->cmd, cmd->argv, env);
exit(-1);
}
else
waitpid(fork_pid, NULL, 0);
if (status == -1)
return (-1);
return (0);
}
int exec_split_cmd(t_list *list_cmd, t_env *env)
{
char **env_array;
int status;
t_cmd *cmd;
env_array = env_get(env);
if (!env_array)
return (-1);
while (list_cmd->next)
{
status = exec_single_cmd(list_cmd->content, env_array, env);
if (status == -1)
{
ft_free("a", &env_array);
return (-1);
}
list_cmd = list_cmd->next;
}
cmd = list_cmd->content;
status = dup2(cmd->infile, STDIN_FILENO);
if (status == -1)
return (-1);
status = dup2(STDOUT_FILENO, cmd->outfile);
if (status == -1)
return (-1);
if (cmd->cmd[0] != '/')
cmd->cmd = get_cmd_path(cmd->cmd, env);
if (!(cmd->cmd))
return (-1);
int fork_pid = fork();
if (fork_pid == -1)
return (-1);
if (!fork_pid)
{
status = execve(cmd->cmd, cmd->argv, env_array);
exit(-1);
}
else
waitpid(fork_pid, NULL, 0);
if (status == -1)
return (-1);
return (0);
}

View File

@ -6,19 +6,18 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 01:42:17 by mmoussou #+# #+# */
/* Updated: 2024/06/02 21:05:25 by mmoussou ### ########.fr */
/* Updated: 2024/05/19 04:33:52 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_path_list(char *path, t_list *list_entry)
int get_path_list(char *path)
{
DIR *path_dir;
struct dirent *dir_entry;
char *abs_path;
struct stat entry;
t_list *new_list_entry;
path_dir = opendir(path);
if (!path_dir)
@ -27,64 +26,41 @@ int get_path_list(char *path, t_list *list_entry)
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)))
|| !ft_strncmp(dir_entry->d_name, "..", ft_strlen(dir_entry->d_name)))
{
dir_entry = readdir(path_dir);
continue ;
}
abs_path = ft_calloc(sizeof(char), strlen(path)
+ strlen(dir_entry->d_name) + 2);
abs_path = ft_calloc(sizeof(char), strlen(path) + strlen(dir_entry->d_name) + 1);
if (!abs_path)
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);
}
}
printf("%s\n", dir_entry->d_name);
free(abs_path);
dir_entry = readdir(path_dir);
}
closedir(path_dir);
return (0);
}
char **get_path(char *path)
int get_path(char *path)
{
char **path_dir;
char **path_list;
t_list *list_entry;
int i;
list_entry = ft_lstnew(NULL);
path_dir = ft_split(path, ':');
if (!path_dir)
return (NULL);
return (-1);
i = 0;
while (path_dir[i])
{
get_path_list(path_dir[i], list_entry);
get_path_list(path_dir[i]);
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++;
}
ft_free("al", &path_dir, &list_entry);
return (path_list);
ft_free("a", &path_dir);
return (0);
}