excutor_absolute_path WIP checks for file existence

This commit is contained in:
whaffman 2025-02-08 19:30:24 +01:00
parent 869eb0d857
commit f4bf733040
5 changed files with 118 additions and 4 deletions

View File

@ -6,7 +6,7 @@
# By: qmennen <qmennen@student.codam.nl> +#+ # # By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ # # +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/02/05 16:02:39 by whaffman ######## odam.nl # # Updated: 2025/02/08 17:13:50 by willem ######## odam.nl #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:src/executor
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
@ -40,6 +40,7 @@ ifeq ($(UNAME_S),Linux)
endif endif
all: $(NAME) all: $(NAME)
echo $(SOURCES)
$(NAME): $(LIBFT) $(OBJECTS) $(NAME): $(LIBFT) $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) $(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME)

20
inc/executor.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* executor.h :+: :+: */
/* +:+ */
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/08 17:06:07 by willem #+# #+# */
/* Updated: 2025/02/08 17:15:24 by willem ######## odam.nl */
/* */
/* ************************************************************************** */
#ifndef EXECUTER_H
# define EXECUTER_H
# include "minishell.h"
char *executor_absolute_path(t_list *env, char *cmd);
#endif // EXECUTER_H

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:28:59 by whaffman ######## odam.nl */ /* Updated: 2025/02/08 17:15:32 by willem ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,6 +19,7 @@
# include "environment.h" # include "environment.h"
# include "prompt.h" # include "prompt.h"
# include "tokenizer.h" # include "tokenizer.h"
# include "executor.h"
# include "utils.h" # include "utils.h"
# define TRUE 1 # define TRUE 1

View File

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* executor_absolute_path.c :+: :+: */
/* +:+ */
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/08 17:00:24 by willem #+# #+# */
/* Updated: 2025/02/08 19:27:05 by willem ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *executor_absolute_path(t_list *env, char *cmd)
{
char **path;
char *executable;
int i;
if (cmd[0] == '/' || cmd[0] == '.')
{
if (access(cmd, F_OK) == 0)
{
executable = ft_strdup(cmd);
if (!executable)
return (NULL);
return (executable);
}
return (NULL);
}
path = ft_split(environment_get(env, "PATH")->value, ':');
if (!path)
return (NULL);
i = 0;
while (path[i] != NULL)
{
executable = malloc(ft_strlen(path[i]) + ft_strlen(cmd) + 2);
if (!executable)
return (ft_free_arr(path), NULL);
ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1);
ft_strlcat(executable, "/", ft_strlen(path[i]) + 2);
ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2);
if (access(executable, F_OK) == 0)
{
ft_free_arr(path);
return (executable);
}
free(executable);
i++;
}
return (ft_free_arr(path), NULL);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */ /* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
/* Updated: 2025/02/08 14:41:19 by willem ######## odam.nl */ /* Updated: 2025/02/08 19:24:05 by willem ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -53,8 +53,35 @@ static int cmp_value(t_list *list, char *str)
return (FALSE); return (FALSE);
} }
void fork_execve(t_minishell *minishell, char *path, char **argv)
{
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
execve(path, argv, environment_get_arr(minishell->environment));
while (*argv != NULL)
printf("%s\n", *argv++);
printf("minishell->: %s: %s\n", path, strerror(errno));
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
printf("minishell: %s\n", strerror(errno));
}
else
{
free(path);
waitpid(pid, &status, 0);
}
}
void simple_builtins(t_minishell *minishell) void simple_builtins(t_minishell *minishell)
{ {
char *path;
if (cmp_value(minishell->tokens, "clear")) if (cmp_value(minishell->tokens, "clear"))
printf("\033[2J\033[1;1H"); printf("\033[2J\033[1;1H");
else if (cmp_value(minishell->tokens, "env")) else if (cmp_value(minishell->tokens, "env"))
@ -68,4 +95,16 @@ void simple_builtins(t_minishell *minishell)
{ {
builtin_export(minishell); builtin_export(minishell);
} }
else
{
path = executor_absolute_path(minishell->environment, ((t_token *)minishell->tokens->content)->value);
if (path == NULL)
printf("minishell: %s: command not found\n", ((t_token *)minishell->tokens->content)->value);
else
{
printf("found excutable: %s\n", path);
fork_execve(minishell, path, ft_split(((t_token *)minishell->tokens->content)->value, ' '));
}
}
} }