Skip to content

Commit

Permalink
Refactor and improve builtin commands
Browse files Browse the repository at this point in the history
  • Loading branch information
algacyr-melo committed Mar 7, 2023
1 parent 912faa2 commit be466bb
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 197 deletions.
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
NAME = minishell

SRC = main.c signal.c env_queue.c env_list.c \
token_queue.c token_list.c handle_execution.c \
get_next_argv.c list_to_envp.c get_pathname.c list_free.c \
SRC = main.c \
signal.c \
env_queue.c \
env_list.c \
token_queue.c \
token_list.c \
handle_execution.c \
get_next_argv.c \
list_to_envp.c \
get_pathname.c \
list_free.c \
handle_builtin.c \
echo.c \
cd.c \
pwd.c \
export.c \
unset.c \
env.c \
exit.c \

OBJ = $(SRC:.c=.o)

Expand Down
30 changes: 30 additions & 0 deletions cd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/07 17:51:35 by almelo #+# #+# */
/* Updated: 2023/03/07 17:52:08 by almelo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

//to do: update OLDPWD & PWD
int cd(int argc, char **argv, t_envl *env_lst)
{
char *path;

if (argc == 1)
path = env_lst->home->value;
else
path = argv[1];
if (chdir(path) == -1)
{
printf("minishell: cd: %s: %s\n", argv[1], strerror(errno));
return (errno);
}
return (0);
}
39 changes: 39 additions & 0 deletions echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/07 17:45:47 by almelo #+# #+# */
/* Updated: 2023/03/07 17:50:25 by almelo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int echo(int argc, char **argv)
{
size_t i;
enum e_bool end_nl;

end_nl = TRUE;
if (argc > 1)
{
i = 1;
if (ft_strcmp(argv[i], "-n") == 0)
{
end_nl = FALSE;
i++;
}
while (argv[i])
{
ft_putstr_fd(argv[i], STDOUT_FILENO);
ft_putchar_fd(' ', STDOUT_FILENO);
i++;
}
}
if (end_nl == TRUE)
ft_putchar_fd('\n', STDOUT_FILENO);
return (0);
}
26 changes: 26 additions & 0 deletions env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/07 18:06:38 by almelo #+# #+# */
/* Updated: 2023/03/07 18:07:07 by almelo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int env(char **envp)
{
size_t i;

i = 0;
while (envp[i])
{
ft_putendl_fd(envp[i], STDOUT_FILENO);
i++;
}
return (0);
}
38 changes: 38 additions & 0 deletions exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/07 18:08:10 by almelo #+# #+# */
/* Updated: 2023/03/07 18:09:09 by almelo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

static int is_numeric_arg(char *arg)
{
size_t i;

i = 0;
while (arg[i])
{
if (!ft_isdigit(arg[i]))
return (0);
i++;
}
return (1);
}

void ft_exit(int argc, char **argv)
{
int n;

n = 0;
if (argc == 2 && is_numeric_arg(argv[1]))
n = ft_atoi(argv[1]);
ft_putendl_fd("exit", STDOUT_FILENO);
exit(n);
}
45 changes: 45 additions & 0 deletions export.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/07 17:56:07 by almelo #+# #+# */
/* Updated: 2023/03/07 18:18:32 by almelo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

t_env *get_env(t_envl *env_lst, char *key)
{
t_env *tmp;

tmp = env_lst->head;
while (tmp)
{
if (ft_strcmp(tmp->key, key) == 0)
return (tmp);
tmp = tmp->next;
}
return (tmp);
}

int ft_export(char **argv, t_envl *env_lst)
{
char *key;
char *value;
t_env *tmp;

key = get_key(argv[1]);
value = get_value(argv[1]);
if (key == NULL || value == NULL)
return (1);
tmp = get_env(env_lst, key);
if (tmp)
tmp->value = value;
else
queue_env(env_lst, new_env(key, value));
return (0);
}
45 changes: 45 additions & 0 deletions handle_builtin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/07 18:11:01 by almelo #+# #+# */
/* Updated: 2023/03/07 18:18:21 by almelo ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

static int get_argc(char **argv)
{
size_t argc;

argc = 0;
while (argv[argc])
argc++;
return (argc);
}

int handle_builtin(char **argv, char **envp, t_envl *env_lst)
{
int argc;

argc = get_argc(argv);
if (ft_strcmp(argv[0], "echo") == 0)
return (echo(argc, argv));
else if (ft_strcmp(argv[0], "cd") == 0)
return (cd(argc, argv, env_lst));
else if (ft_strcmp(argv[0], "pwd") == 0)
return (pwd());
else if (ft_strcmp(argv[0], "export") == 0)
return (ft_export(argv, env_lst));
else if (ft_strcmp(argv[0], "unset") == 0)
return (unset(argv, env_lst));
else if (ft_strcmp(argv[0], "env") == 0)
return (env(envp));
else if (ft_strcmp(argv[0], "exit") == 0)
ft_exit(argc, argv);
return (-1);
}
Loading

0 comments on commit be466bb

Please sign in to comment.