builtins working

This commit is contained in:
whaffman 2025-02-20 15:10:38 +01:00
parent d289065ade
commit eda2954279
10 changed files with 88 additions and 104 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:33:07 by whaffman #+# #+# */ /* Created: 2025/02/20 11:33:07 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:03 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 12:49:36 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,28 @@
int builtin_cd(t_minishell *minishell, t_command *cmd) int builtin_cd(t_minishell *minishell, t_command *cmd)
{ {
(void)minishell; t_environment *env;
(void)cmd; char *path;
if (cmd->args[1] == NULL)
{
env = environment_get(minishell->environment, "HOME");
if (env == NULL || env->value == NULL)
{
ft_putendl_fd("minishell: cd: HOME not set", STDERR_FILENO);
return (FAILURE);
}
path = env->value;
}
else
path = cmd->args[1];
if (chdir(path) == -1)
{
ft_putstr_fd("minishell: cd: ", STDERR_FILENO);
ft_putstr_fd(path, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
ft_putendl_fd(strerror(errno), STDERR_FILENO);
return (FAILURE);
}
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:33:05 by whaffman #+# #+# */ /* Created: 2025/02/20 11:33:05 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:10 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 12:37:42 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,25 @@
int builtin_echo(t_minishell *minishell, t_command *cmd) int builtin_echo(t_minishell *minishell, t_command *cmd)
{ {
int i;
int n_flag;
(void)minishell; (void)minishell;
(void)cmd; i = 1;
n_flag = 0;
if (cmd->args[i] != NULL && ft_strncmp(cmd->args[i], "-n", 3) == 0)
{
n_flag = 1;
i++;
}
while (cmd->args[i] != NULL)
{
printf("%s", cmd->args[i]);
if (cmd->args[i + 1] != NULL)
printf(" ");
i++;
}
if (!n_flag)
printf("\n");
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:33:02 by whaffman #+# #+# */ /* Created: 2025/02/20 11:33:02 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:17 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 12:17:10 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
int builtin_env(t_minishell *minishell, t_command *cmd) int builtin_env(t_minishell *minishell, t_command *cmd)
{ {
(void)minishell;
(void)cmd; (void)cmd;
environment_print(minishell->environment);
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:25 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 12:17:40 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,8 @@
int builtin_exit(t_minishell *minishell, t_command *cmd) int builtin_exit(t_minishell *minishell, t_command *cmd)
{ {
(void)minishell;
(void)cmd; (void)cmd;
free_minishell(minishell);
exit(EXIT_SUCCESS);
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:32 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 12:30:21 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,30 @@
int builtin_export(t_minishell *minishell, t_command *cmd) int builtin_export(t_minishell *minishell, t_command *cmd)
{ {
(void)minishell; t_environment *env;
(void)cmd; char **arr;
int i;
i = 1;
while (cmd->args[i] != NULL)
{
arr = ft_split(cmd->args[i], '=');
if (arr[1] == NULL)
{
ft_free_arr(arr);
i++;
continue ;
}
env = environment_get(minishell->environment, arr[0]);
if (env != NULL)
{
free(env->value);
env->value = ft_strdup(arr[1]); //TODO: malloc check
}
else
environment_add(&(minishell->environment), arr[0], arr[1]);
ft_free_arr(arr);
i++;
}
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:37 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 13:57:01 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,12 @@
int builtin_pwd(t_minishell *minishell, t_command *cmd) int builtin_pwd(t_minishell *minishell, t_command *cmd)
{ {
char *cwd;
(void)minishell; (void)minishell;
(void)cmd; (void)cmd;
cwd = getcwd(NULL, 0);
printf("%s\n", cwd);
free(cwd);
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */ /* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:41:28 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 13:56:10 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:25:43 by whaffman #+# #+# */ /* Created: 2025/02/20 11:25:43 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:31:25 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 15:04:59 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */ /* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:12:25 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 13:54:04 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ int is_builtin(char *cmd)
while (builtins[i]) while (builtins[i])
{ {
if (ft_strcmp(cmd, builtins[i]) == 0) if (ft_strcmp(cmd, builtins[i]) == 0)
return (1); return (i);
i++; i++;
} }
return (FALSE); return (FALSE);

View File

@ -6,101 +6,17 @@
/* 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/20 11:42:29 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 15:06:40 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void builtin_export_simple(t_minishell *minishell)
{
t_list *tmp;
t_environment *env;
char **arr;
tmp = minishell->tokens->next;
while (tmp != NULL)
{
arr = ft_split(((t_token *)tmp->content)->value, '=');
if (arr[1] == NULL)
{
ft_free_arr(arr);
tmp = tmp->next;
continue ;
}
env = environment_get(minishell->environment, arr[0]);
if (env != NULL)
{
free(env->value);
env->value = ft_strdup(arr[1]);
}
else
environment_add(&(minishell->environment), arr[0], arr[1]);
ft_free_arr(arr);
tmp = tmp->next;
}
}
static int cmp_value(t_list *list, char *str)
{
if (list != NULL
&& ft_strncmp(
((t_token *)list->content)->value,
str,
ft_strlen(str) + 1) == 0)
return (TRUE);
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);
ft_free_arr(argv);
waitpid(pid, &status, 0);
printf("exit status of pid(%d): %d\n", pid, (((status) & 0xff00) >> 8));
}
}
void simple_builtins(t_minishell *minishell) void simple_builtins(t_minishell *minishell)
{ {
if (!minishell->tokens) if (minishell->commands == NULL)
return ; return ;
if (cmp_value(minishell->tokens, "clear")) if (!builtin_router(minishell, minishell->commands->content))
printf("\033[2J\033[1;1H");
else if (cmp_value(minishell->tokens, "env"))
environment_print(minishell->environment);
else if (cmp_value(minishell->tokens, "exit"))
{
free_minishell(minishell);
exit(EXIT_SUCCESS);
}
else if (cmp_value(minishell->tokens, "export"))
builtin_export_simple(minishell);
else if (cmp_value(minishell->tokens, "unset"))
{
environment_del(&(minishell->environment),
((t_token *)minishell->tokens->next->content)->value);
}
else
{
executor_execute_pipeline(minishell); executor_execute_pipeline(minishell);
}
} }