migrated to msh struct and normed
This commit is contained in:
25
srcs/alias.c
25
srcs/alias.c
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/16 23:16:07 by marde-vr #+# #+# */
|
||||
/* Updated: 2024/02/21 13:12:07 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:39:40 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -39,11 +39,11 @@ char *get_alias_value(t_cmd *arg)
|
||||
return (res);
|
||||
}
|
||||
|
||||
void print_aliases(t_alias **aliases)
|
||||
void print_aliases(t_alias *aliases)
|
||||
{
|
||||
t_alias *cur_alias;
|
||||
|
||||
cur_alias = *aliases;
|
||||
cur_alias = aliases;
|
||||
while (cur_alias)
|
||||
{
|
||||
ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value);
|
||||
@ -51,24 +51,27 @@ void print_aliases(t_alias **aliases)
|
||||
}
|
||||
}
|
||||
|
||||
int alias(t_cmd *args, t_alias **aliases)
|
||||
int alias(t_msh *msh)
|
||||
{
|
||||
char *name;
|
||||
char *value;
|
||||
|
||||
if (!args->next || args->next->type != ARG)
|
||||
print_aliases(aliases);
|
||||
if (!msh->cmds->next || msh->cmds->next->type != ARG)
|
||||
print_aliases(msh->aliases);
|
||||
else
|
||||
{
|
||||
name = get_alias_name(args->next);
|
||||
if (ft_strchr(args->next->token, '='))
|
||||
name = get_alias_name(msh->cmds->next);
|
||||
if (ft_strchr(msh->cmds->next->token, '='))
|
||||
{
|
||||
value = get_alias_value(args->next);
|
||||
*aliases = alias_add_back(*aliases, name, value);
|
||||
value = get_alias_value(msh->cmds->next);
|
||||
msh->aliases = alias_add_back(msh->aliases, name, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_printf("alias %s=%s\n", name, get_alias(*aliases, name));
|
||||
if (get_alias(msh->aliases, name))
|
||||
ft_printf("alias %s=%s\n", name, get_alias(msh->aliases, name));
|
||||
else
|
||||
ft_printf("minishell: alias %s: not found\n", name);
|
||||
free(name);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
|
||||
/* Updated: 2024/02/20 21:04:53 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:30:36 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,7 +16,6 @@ int cd(t_cmd *args)
|
||||
{
|
||||
char *new_wd;
|
||||
|
||||
ft_printf_fd(2, "%s\n", args->next->next->token);
|
||||
if (args->next && args->next->next && args->next->next->type == ARG)
|
||||
{
|
||||
ft_printf_fd(2, "minishell: cd: too many arguments\n");
|
||||
|
89
srcs/exec.c
89
srcs/exec.c
@ -6,32 +6,32 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/21 15:54:56 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:46:47 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int exec_builtin(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
|
||||
int exec_builtin(t_msh *msh)
|
||||
{
|
||||
if (!ft_strcmp(parsed_cmd->token, "echo"))
|
||||
g_return_code = echo(parsed_cmd->next);
|
||||
else if (!ft_strcmp(parsed_cmd->token, "ret"))
|
||||
g_return_code = ft_atoi(parsed_cmd->next->token);
|
||||
else if (!ft_strcmp(parsed_cmd->token, "env"))
|
||||
g_return_code = print_env(*env);
|
||||
else if (!ft_strcmp(parsed_cmd->token, "exit"))
|
||||
exit_bt(parsed_cmd, *env, *aliases);
|
||||
else if (!ft_strcmp(parsed_cmd->token, "pwd"))
|
||||
if (!ft_strcmp(msh->cmds->token, "echo"))
|
||||
g_return_code = echo(msh->cmds->next);
|
||||
else if (!ft_strcmp(msh->cmds->token, "ret"))
|
||||
g_return_code = ft_atoi(msh->cmds->next->token);
|
||||
else if (!ft_strcmp(msh->cmds->token, "env"))
|
||||
g_return_code = print_env(msh->env);
|
||||
else if (!ft_strcmp(msh->cmds->token, "exit"))
|
||||
exit_bt(msh);
|
||||
else if (!ft_strcmp(msh->cmds->token, "pwd"))
|
||||
g_return_code = pwd();
|
||||
else if (!ft_strcmp(parsed_cmd->token, "cd"))
|
||||
g_return_code = cd(parsed_cmd);
|
||||
else if (!ft_strcmp(parsed_cmd->token, "export"))
|
||||
g_return_code = ft_export(parsed_cmd, env);
|
||||
else if (!ft_strcmp(parsed_cmd->token, "alias"))
|
||||
g_return_code = alias(parsed_cmd, aliases);
|
||||
else if (!ft_strcmp(parsed_cmd->token, "unalias"))
|
||||
g_return_code = unalias(parsed_cmd, aliases);
|
||||
else if (!ft_strcmp(msh->cmds->token, "cd"))
|
||||
g_return_code = cd(msh->cmds);
|
||||
else if (!ft_strcmp(msh->cmds->token, "export"))
|
||||
g_return_code = ft_export(msh);
|
||||
else if (!ft_strcmp(msh->cmds->token, "alias"))
|
||||
g_return_code = alias(msh);
|
||||
else if (!ft_strcmp(msh->cmds->token, "unalias"))
|
||||
g_return_code = unalias(msh);
|
||||
else
|
||||
return (STDIN_FILENO);
|
||||
return (STDOUT_FILENO);
|
||||
@ -57,13 +57,13 @@ void ft_exit(t_msh *msh, int exit_code)
|
||||
exit(exit_code);
|
||||
}
|
||||
|
||||
int get_args_count(t_cmd *parsed_cmd)
|
||||
int get_args_count(t_cmd *cmds)
|
||||
{
|
||||
int count;
|
||||
t_cmd *cur_cmd;
|
||||
|
||||
count = 0;
|
||||
cur_cmd = parsed_cmd;
|
||||
cur_cmd = cmds;
|
||||
while (cur_cmd->next != 0 && cur_cmd->type == ARG)
|
||||
{
|
||||
cur_cmd = cur_cmd->next;
|
||||
@ -111,15 +111,15 @@ void find_cmd_path(t_msh *msh, char **paths, int *found)
|
||||
tmp = ft_strjoin(path, "/");
|
||||
if (!tmp)
|
||||
ft_exit(msh, 1);
|
||||
path = ft_strjoin(tmp, cmd->token);
|
||||
path = ft_strjoin(tmp, msh->cmds->token);
|
||||
if (!path)
|
||||
ft_exit(cmd, env, 1);
|
||||
ft_exit(msh, 1);
|
||||
free(tmp);
|
||||
if (access(path, X_OK) != -1)
|
||||
{
|
||||
*found = 1;
|
||||
free(cmd->token);
|
||||
cmd->token = path;
|
||||
free(msh->cmds->token);
|
||||
msh->cmds->token = path;
|
||||
break ;
|
||||
}
|
||||
free(path);
|
||||
@ -127,30 +127,31 @@ void find_cmd_path(t_msh *msh, char **paths, int *found)
|
||||
}
|
||||
}
|
||||
|
||||
void get_cmd_path(t_cmd *cmd, t_env *env)
|
||||
void get_cmd_path(t_msh *msh)
|
||||
{
|
||||
char **paths;
|
||||
int found;
|
||||
|
||||
found = 0;
|
||||
if (ft_strchr(cmd->token, '/') && access(cmd->token, X_OK) != -1)
|
||||
if (ft_strchr(msh->cmds->token, '/')
|
||||
&& access(msh->cmds->token, X_OK) != -1)
|
||||
found = 1;
|
||||
else
|
||||
{
|
||||
paths = split_paths_from_env(env);
|
||||
paths = split_paths_from_env(msh->env);
|
||||
if (!paths)
|
||||
ft_exit(cmd, env, 1);
|
||||
find_cmd_path(cmd, env, paths, &found);
|
||||
ft_exit(msh, 1);
|
||||
find_cmd_path(msh, paths, &found);
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
ft_printf_fd(2, "%s: command not found\n", cmd->token);
|
||||
free(cmd->token);
|
||||
cmd->token = 0;
|
||||
ft_printf_fd(2, "%s: command not found\n", msh->cmds->token);
|
||||
free(msh->cmds->token);
|
||||
msh->cmds->token = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
|
||||
void exec_command(t_msh *msh)
|
||||
{
|
||||
t_cmd *cur_cmd;
|
||||
int args_count;
|
||||
@ -158,15 +159,15 @@ void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
|
||||
int i;
|
||||
pid_t pid;
|
||||
|
||||
if (!parsed_cmd || exec_builtin(parsed_cmd, env, aliases))
|
||||
if (!msh->cmds || exec_builtin(msh))
|
||||
return ;
|
||||
cur_cmd = parsed_cmd;
|
||||
args_count = get_args_count(parsed_cmd);
|
||||
cur_cmd = msh->cmds;
|
||||
args_count = get_args_count(msh->cmds);
|
||||
cmd_args = ft_calloc(args_count + 1, sizeof(char *));
|
||||
if (!cmd_args)
|
||||
ft_exit(parsed_cmd, *env, 1);
|
||||
ft_exit(msh, 1);
|
||||
i = 0;
|
||||
get_cmd_path(parsed_cmd, *env);
|
||||
get_cmd_path(msh);
|
||||
while (i < args_count)
|
||||
{
|
||||
cmd_args[i] = cur_cmd->token;
|
||||
@ -177,19 +178,19 @@ void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
|
||||
if (pid == -1)
|
||||
{
|
||||
perror("fork");
|
||||
ft_exit(parsed_cmd, *env, 1);
|
||||
ft_exit(msh, 1);
|
||||
}
|
||||
if (pid == 0)
|
||||
{
|
||||
if (parsed_cmd->token)
|
||||
execve(parsed_cmd->token, cmd_args, env_to_char_tab(*env));
|
||||
ft_exit(parsed_cmd, *env, 1);
|
||||
if (msh->cmds->token)
|
||||
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
||||
ft_exit(msh, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
rl_redisplay();
|
||||
if (waitpid(pid, 0, 0) < 0)
|
||||
ft_exit(parsed_cmd, *env, 1);
|
||||
ft_exit(msh, 1);
|
||||
}
|
||||
//if (cur_cmd->type == PIPE)
|
||||
// exec_command(cur_cmd->next, env);
|
||||
|
27
srcs/exit.c
27
srcs/exit.c
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/17 04:11:51 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:43:52 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,30 +19,31 @@ void print_numeric_arg_err(char *arg)
|
||||
ft_putstr_fd(": numeric argument required\n", 2);
|
||||
}
|
||||
|
||||
void exit_bt(t_cmd *args, t_env *env, t_alias *aliases)
|
||||
void exit_bt(t_msh *msh)
|
||||
{
|
||||
t_cmd *start;
|
||||
int exit_code;
|
||||
|
||||
start = args;
|
||||
args = args->next;
|
||||
start = msh->cmds;
|
||||
msh->cmds = msh->cmds->next;
|
||||
ft_printf("exit\n");
|
||||
if (args && args->next && args->next->type == ARG
|
||||
&& ft_strisnbr(args->token))
|
||||
if (msh->cmds && msh->cmds->next && msh->cmds->next->type == ARG
|
||||
&& ft_strisnbr(msh->cmds->token))
|
||||
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
|
||||
else
|
||||
{
|
||||
if (args && args->type == ARG && !ft_strisnbr(args->token))
|
||||
print_numeric_arg_err(args->token);
|
||||
if (args && args->type == ARG)
|
||||
exit_code = (unsigned char)ft_atoi(args->token);
|
||||
else if (args && args->type == ARG && !ft_strisnbr(args->token))
|
||||
if (msh->cmds && msh->cmds->type == ARG
|
||||
&& !ft_strisnbr(msh->cmds->token))
|
||||
print_numeric_arg_err(msh->cmds->token);
|
||||
if (msh->cmds && msh->cmds->type == ARG)
|
||||
exit_code = (unsigned char)ft_atoi(msh->cmds->token);
|
||||
else if (msh->cmds && msh->cmds->type == ARG
|
||||
&& !ft_strisnbr(msh->cmds->token))
|
||||
exit_code = 2;
|
||||
else
|
||||
exit_code = g_return_code;
|
||||
free_cmd(start);
|
||||
free_alias(aliases);
|
||||
free_env(env);
|
||||
free_msh(msh);
|
||||
exit(exit_code);
|
||||
}
|
||||
}
|
||||
|
@ -6,15 +6,17 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
|
||||
/* Updated: 2024/02/18 18:39:39 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:44:43 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int ft_export(t_cmd *cmd, t_env **env)
|
||||
int ft_export(t_msh *msh)
|
||||
{
|
||||
t_cmd *cmd;
|
||||
|
||||
cmd = msh->cmds;
|
||||
(void)cmd;
|
||||
(void)env;
|
||||
return (0);
|
||||
}
|
||||
|
@ -6,30 +6,30 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/17 02:54:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/21 13:09:40 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:21:27 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias)
|
||||
t_cmd *handle_alias(t_msh *msh)
|
||||
{
|
||||
t_cmd *res;
|
||||
t_cmd *tmp;
|
||||
char *alias_command;
|
||||
|
||||
alias_command = 0;
|
||||
if (!cmd)
|
||||
if (!msh->cmds)
|
||||
return (0);
|
||||
if (cmd->type == ARG)
|
||||
alias_command = get_alias(alias, cmd->token);
|
||||
if (msh->cmds->type == ARG)
|
||||
alias_command = get_alias(msh->aliases, msh->cmds->token);
|
||||
if (!alias_command)
|
||||
return (cmd);
|
||||
res = parse_command(alias_command, env);
|
||||
return (msh->cmds);
|
||||
res = parse_command(alias_command, msh->env);
|
||||
tmp = res;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = cmd->next;
|
||||
free(cmd);
|
||||
tmp->next = msh->cmds->next;
|
||||
free(msh->cmds);
|
||||
return (res);
|
||||
}
|
||||
|
26
srcs/main.c
26
srcs/main.c
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/21 15:53:24 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:45:16 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -68,27 +68,27 @@ t_env *get_env(char **envp)
|
||||
return (env);
|
||||
}
|
||||
|
||||
int init_minishell(t_msh **msh, int argc, char **envp)
|
||||
{
|
||||
int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
|
||||
{
|
||||
*msh = ft_calloc(1, sizeof(t_msh));
|
||||
if (!*msh)
|
||||
//
|
||||
ft_exit(*msh, 1);
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
(*msh)->env = get_env(envp);
|
||||
(*msh)->aliases = 0;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, char **envp)
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
char *command;
|
||||
t_cmd *parsed_cmd;
|
||||
char *prompt;
|
||||
t_msh *msh;
|
||||
|
||||
|
||||
command = (char *)1;
|
||||
init_minishell(&msh, argc, envp);
|
||||
handle_minishellrc(&env, &aliases);
|
||||
init_minishell(&msh, argc, argv, envp);
|
||||
handle_minishellrc(msh);
|
||||
while (msh->env && command)
|
||||
{
|
||||
prompt = get_prompt(msh->env);
|
||||
@ -97,13 +97,13 @@ int main(int argc, char **envp)
|
||||
command = readline(prompt);
|
||||
free(prompt);
|
||||
add_history(command);
|
||||
msh->cmd = parse_command(command, msh->env);
|
||||
msh->cmds = parse_command(command, msh->env);
|
||||
//print_parsed_cmd(parsed_cmd);//debug
|
||||
msh->cmd = handle_alias(msh->cmd, msh->env, msh->aliases);
|
||||
msh->cmds = handle_alias(msh);
|
||||
free(command);
|
||||
//print_parsed_cmd(parsed_cmd);//debug
|
||||
exec_command(msh->cmd, &env, &aliases);
|
||||
free_cmd(parsed_cmd);
|
||||
exec_command(msh);
|
||||
free_cmd(msh->cmds);
|
||||
}
|
||||
rl_clear_history();
|
||||
free(msh);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/21 15:46:50 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:43:17 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -63,9 +63,9 @@ extern int g_return_code;
|
||||
|
||||
t_cmd *cmd_add_back(t_cmd *res, char *token, t_token_type type);
|
||||
void free_cmd(t_cmd *cmd);
|
||||
void exec_command(t_cmd *cmd, t_env **env, t_alias **alias);
|
||||
void exec_command(t_msh *msh);
|
||||
int echo(t_cmd *args);
|
||||
void exit_bt(t_cmd *args, t_env *env, t_alias *aliases);
|
||||
void exit_bt(t_msh *msh);
|
||||
t_env *env_add_back(t_env *env, char *name, char *value);
|
||||
void free_env(t_env *env);
|
||||
int print_env(t_env *env);
|
||||
@ -79,14 +79,15 @@ int is_cmd_char(char c);
|
||||
void print_parsed_cmd(t_cmd *cmd);//debug
|
||||
void ft_exit(t_msh *msh, int error_code);
|
||||
char **env_to_char_tab(t_env *env);
|
||||
void handle_minishellrc(t_env **env, t_alias **aliases);
|
||||
t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias);
|
||||
void handle_minishellrc(t_msh *msh);
|
||||
t_cmd *handle_alias(t_msh *msh);
|
||||
int cd(t_cmd *args);
|
||||
int alias(t_cmd *args, t_alias **aliases);
|
||||
int alias(t_msh *msh);
|
||||
void free_alias(t_alias *alias);
|
||||
char *get_alias(t_alias *alias, char *var_name);
|
||||
t_alias *alias_add_back(t_alias *alias, char *name, char *value);
|
||||
int unalias(t_cmd *args, t_alias **aliases);
|
||||
int ft_export(t_cmd *cmd, t_env **env);
|
||||
int unalias(t_msh *msh);
|
||||
int ft_export(t_msh *msh);
|
||||
void free_msh(t_msh *msh);
|
||||
|
||||
#endif
|
||||
|
@ -6,49 +6,48 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */
|
||||
/* Updated: 2024/02/21 12:54:01 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:34:26 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void exec_rc_file(t_env **env, t_alias **aliases, int fd)
|
||||
void exec_rc_file(t_msh *msh, int fd)
|
||||
{
|
||||
char *line;
|
||||
t_cmd *parsed_cmd;
|
||||
|
||||
line = get_next_line(fd);
|
||||
while (line)
|
||||
{
|
||||
if (line[0] != '#')
|
||||
{
|
||||
parsed_cmd = parse_command(line, *env);
|
||||
exec_command(parsed_cmd, env, aliases);
|
||||
free_cmd(parsed_cmd);
|
||||
msh->cmds = parse_command(line, msh->env);
|
||||
exec_command(msh);
|
||||
free_cmd(msh->cmds);
|
||||
}
|
||||
free(line);
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_minishellrc(t_env **env, t_alias **aliases)
|
||||
void handle_minishellrc(t_msh *msh)
|
||||
{
|
||||
char *home;
|
||||
char *rc_path;
|
||||
int fd;
|
||||
|
||||
home = ft_get_env(*env, "HOME");
|
||||
home = ft_get_env(msh->env, "HOME");
|
||||
rc_path = ft_strjoin(home, "/.minishellrc");
|
||||
if (access(rc_path, R_OK) != -1)
|
||||
{
|
||||
fd = open(rc_path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
{
|
||||
free(env);
|
||||
free(msh->env);
|
||||
perror("open");
|
||||
return ;
|
||||
}
|
||||
exec_rc_file(env, aliases, fd);
|
||||
exec_rc_file(msh, fd);
|
||||
close(fd);
|
||||
}
|
||||
free(rc_path);
|
||||
|
@ -6,21 +6,21 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/21 13:11:45 by marde-vr #+# #+# */
|
||||
/* Updated: 2024/02/21 13:11:59 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:44:30 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int remove_alias(t_cmd *args, t_alias **aliases)
|
||||
int remove_alias(t_msh *msh)
|
||||
{
|
||||
t_alias *alias;
|
||||
|
||||
alias = *aliases;
|
||||
alias = msh->aliases;
|
||||
while (alias)
|
||||
{
|
||||
if (alias->next && args->next
|
||||
&& !ft_strcmp(alias->next->name, args->next->token))
|
||||
if (alias->next && msh->cmds->next
|
||||
&& !ft_strcmp(alias->next->name, msh->cmds->next->token))
|
||||
{
|
||||
if (alias->next->next)
|
||||
alias->next = alias->next->next;
|
||||
@ -38,18 +38,19 @@ int remove_alias(t_cmd *args, t_alias **aliases)
|
||||
return (0);
|
||||
}
|
||||
|
||||
int unalias(t_cmd *args, t_alias **aliases)
|
||||
int unalias(t_msh *msh)
|
||||
{
|
||||
if (args->next && !ft_strcmp(args->next->token, "-a"))
|
||||
if (msh->cmds->next && !ft_strcmp(msh->cmds->next->token, "-a"))
|
||||
{
|
||||
free_alias(*aliases);
|
||||
*aliases = 0;
|
||||
free_alias(msh->aliases);
|
||||
msh->aliases = 0;
|
||||
return (0);
|
||||
}
|
||||
if (remove_alias(args, aliases))
|
||||
if (remove_alias(msh))
|
||||
return (1);
|
||||
if (args->next && args->next->type == ARG)
|
||||
ft_printf("minishell: unalias: %s: not found\n", args->next->token);
|
||||
if (msh->cmds->next && msh->cmds->next->type == ARG)
|
||||
ft_printf("minishell: unalias: %s: not found\n",
|
||||
msh->cmds->next->token);
|
||||
else
|
||||
ft_printf("unalias: usage: unalias name\n");
|
||||
return (1);
|
||||
|
Reference in New Issue
Block a user