-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and improve builtin commands
- Loading branch information
1 parent
912faa2
commit be466bb
Showing
11 changed files
with
327 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.