update: executes commands only in /usr/bin

This commit is contained in:
sdevsantiago
2025-07-31 14:45:06 +02:00
parent 4cf27740de
commit 7b7d500db9
4 changed files with 158 additions and 7 deletions

11
.gitignore vendored
View File

@@ -1,5 +1,14 @@
# File
minishell
# Build path
build/
/build
# Libraries path
/lib
# VSCode
/.vscode
# Prerequisites
*.d

View File

@@ -6,14 +6,14 @@
# By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2025/07/30 20:22:21 by sede-san #+# #+# #
# Updated: 2025/07/31 03:16:03 by sede-san ### ########.fr #
# Updated: 2025/07/31 05:23:15 by sede-san ### ########.fr #
# #
# **************************************************************************** #
# ******************************* Output files ******************************* #
# Executable file name
NAME =
NAME = minishell
# ************************** Compilation variables *************************** #
@@ -24,7 +24,7 @@ CC = cc
CFLAGS = -Wall -Wextra -Werror -Wunreachable-code # -Ofast
# Additional headers
HEADERS = -I $(INCLUDE_PATH) # -I $(GNL_INCLUDE_PATH) -I $(PRINTF_INCLUDE_PATH) -I $(LIBFT_INCLUDE_PATH)
HEADERS = -I $(INCLUDE_PATH) -I $(GNL_INCLUDE_PATH) -I $(PRINTF_INCLUDE_PATH) -I $(LIBFT_INCLUDE_PATH)
# Debug flags, execute with DEBUG=1 -> make DEBUG=1
DFLAGS = -g3
@@ -58,7 +58,7 @@ SRC_PATH = src
# Source files
SRC = \
$(SRC_PATH)/minishell.c
# Include path
INCLUDE_PATH = ./include
@@ -118,12 +118,12 @@ re: fclean all
# Compile libraries
lib:
@$(MAKE) libft
@$(MAKE) ft_printf
# @$(MAKE) ft_printf
@$(MAKE) get_next_line
.PHONY: lib
# Compile file with libraries
# LIBS = $(GNL_BIN) $(LIBFT_BIN) $(PRINTF_BIN)
LIBS = -lreadline $(GNL_BIN) $(LIBFT_BIN) # $(PRINTF_BIN)
# Libraries path
LIB_PATH = lib
@@ -189,6 +189,7 @@ get_next_line:
echo "$(YELLOW)$(EMOJI_WRENCH) Cloning $(GNL)...$(RESET)"; \
git clone $(GNL_REPO) $(GNL_PATH); \
rm -rf $(GNL_PATH)/.git; \
rm -rf $(GNL_PATH)/tests; \
echo "$(GREEN)$(EMOJI_CHECK) $(GNL) cloned...$(RESET)"; \
fi
@if [ ! -f $(GNL_BIN) ]; then \

38
include/minishell.h Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 03:42:51 by sede-san #+# #+# */
/* Updated: 2025/07/31 14:01:36 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
# include "libft.h"
# include "get_next_line.h"
# include <signal.h>
# include <readline/history.h>
# include <readline/readline.h>
# include <sys/wait.h>
# define HISTORY_FILE "/.minishell_history"
typedef struct s_env
{
char *home;
char **path;
} t_env;
typedef struct s_minishell
{
char *prompt;
char *history_file;
t_env env;
} t_minishell;
#endif

103
src/minishell.c Normal file
View File

@@ -0,0 +1,103 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 03:42:25 by sede-san #+# #+# */
/* Updated: 2025/07/31 14:43:43 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static t_minishell init_minishell(void);
int main(
int argc,
char const *argv[],
char **envp
)
{
t_minishell minishell;
char *line;
minishell = init_minishell();
line = NULL;
(void)argv;
(void)envp;
if (argc != 1)
return (EXIT_FAILURE);
/* ====================================================================== */
char *cmd = "/usr/bin/whoami";
char *args[] = {"whoami", NULL};
__pid_t pid = fork(); // Creates a "secondary" process
if (pid == 0)
{
execve(cmd, args, envp);
exit(0);
}
waitpid(pid, NULL, 0); // Makes the "parent" process wait until the "child"
// has finished
/* ====================================================================== */
cmd = "/usr/bin/hostname";
args[0] = "hostname";
args[1] = NULL;
pid = fork();
if (pid == 0)
{
execve(cmd, args, envp);
exit(0);
}
waitpid(pid, NULL, 0);
read_history(minishell.history_file);
while (1)
{
if (line)
free(line);
line = readline(minishell.prompt);
if (!line)
break ;
if (*line)
{
add_history(line);
write_history(minishell.history_file);
if (!ft_strncmp("exit\0", line, 5))
break ;
cmd = ft_strjoin("/usr/bin/", line);
args[0] = line;
args[1] = NULL;
pid = fork();
if (pid == 0)
{
execve(cmd, args, envp);
exit(0);
}
waitpid(pid, NULL, 0);
free(cmd);
}
// ft_putendl(line);
}
rl_clear_history();
free(line);
free((char *)minishell.history_file);
ft_free_split(minishell.env.path);
return (EXIT_SUCCESS);
}
static t_minishell init_minishell(void)
{
t_minishell minishell;
ft_bzero(&minishell, sizeof(t_minishell));
minishell.prompt = RED_TEXT"minishell> "RESET;
minishell.env.home = getenv("HOME");
minishell.history_file = ft_strjoin(minishell.env.home, HISTORY_FILE);
minishell.env.path = ft_split(getenv("PATH"), ':');
return (minishell);
}