This commit is contained in:
Loic Deridder
2025-01-28 10:56:15 +01:00
parent 5c3cd44e5a
commit 66ebd61d6f
17 changed files with 252 additions and 254 deletions

View File

@@ -6,14 +6,14 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:23:27 by lderidde #+# #+# */
/* Updated: 2025/01/27 14:15:24 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:49:53 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AST_H
# define AST_H
# include "env.h"
# include "../lib/libft/libft.h"
typedef enum e_state
{

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 09:01:33 by lderidde #+# #+# */
/* Updated: 2025/01/28 10:35:06 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUILTINS_H
# define BUILTINS_H
@@ -13,13 +25,13 @@
# include <string.h>
// void echo(char *msg, int flag);
void builtin_echo(char **arg, char **envp);
void builtin_exit(char *arg, bool depth);
void builtin_pwd(char *arg);
void builtin_env(char *str, char **envp);
int builtin_unset(char **arg, t_data *data);
int builtin_cd(char **arg, t_data *data);
void builtin_export(char **arg, t_data *data);
int builtin_echo(char **arg, char **envp);
int builtin_exit(char **arg, bool depth);
int builtin_pwd(char **arg);
int builtin_env(char **arg, char **envp);
int builtin_unset(char **arg, t_ast_n *head);
int builtin_cd(char **arg, t_ast_n *head);
int builtin_export(char **arg, t_ast_n *head);
//UTILS
int count_char(char *str);

View File

@@ -1,25 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 10:11:07 by lderidde #+# #+# */
/* Updated: 2025/01/28 10:31:54 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ENV_H
# define ENV_H
# include "../lib/libft/libft.h"
# include "ast.h"
# include <unistd.h>
# include <stdlib.h>
# include <stdbool.h>
typedef struct s_data
{
char **env;
} t_data;
//VAR
int count_var(char **envp);
int get_var_index(char *key, t_data *data);
int get_var_index(char *key, t_ast_n *head);
void free_null_ptr(void *ptr);
char **copy_env_var(char **envp, int count);
char *get_var_value(char *key, char **envp);
bool is_valid_key(char *key, t_data *data);
int remove_env_var(char *key, t_data *data);
void set_var_env(char *key, char *value, t_data *t_data);
bool is_valid_key(char *key, t_ast_n *head);
int remove_env_var(char *key, t_ast_n *head);
void set_var_env(char *key, char *value, t_ast_n *head);
char **init_env(char **envp);
#endif

View File

@@ -3,14 +3,14 @@
/* ::: :::::::: */
/* create_elem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 10:06:11 by nalebrun #+# #+# */
/* Updated: 2025/01/24 10:06:11 by nalebrun ### ########.fr */
/* Created: 2025/01/24 10:06:11 by lderidde #+# #+# */
/* Updated: 2025/01/28 10:45:23 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../drawio.h"
#include "../lib_drawio.h"
#include "../header/dio_private.h"
int draw_rect(int fd, int id, t_dio_elem *elem)

View File

@@ -3,14 +3,14 @@
/* ::: :::::::: */
/* drawio.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:47:41 by nalebrun #+# #+# */
/* Updated: 2025/01/24 08:47:41 by nalebrun ### ########.fr */
/* Created: 2025/01/24 08:47:41 by lderidde #+# #+# */
/* Updated: 2025/01/28 10:45:02 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../drawio.h"
#include "../lib_drawio.h"
#include "../header/dio_private.h"
int drawio_init(char *file_path)

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:22:16 by lderidde #+# #+# */
/* Updated: 2025/01/27 15:40:58 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:50:43 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -38,6 +38,36 @@ t_ast_n *created_ast_n(t_state st, t_ast_n *prt, t_ast_n *he)
return (node);
}
static int count_var(char **envp)
{
int i;
i = 0;
if (!envp)
return (-1);
while (envp[i])
i++;
return (i);
}
static char **init_env(char **envp)
{
char **env;
int i;
i = 0;
env = malloc(sizeof(char *) * (count_var(envp) + 1));
if (!env)
return (NULL);
env[count_var(envp)] = NULL;
while (envp[i])
{
env[i] = ft_strdup(envp[i]);
i++;
}
return (env);
}
void setup_cmd(t_ast_n *node, char *cmd, char *args)
{
node->cmd = ft_strdup(cmd);
@@ -50,7 +80,7 @@ t_ast_n *return_hardcode_ast(char **envp)
head = created_ast_n(_CMD, NULL, NULL);
head->head = head;
setup_cmd(head, "ls", "ls -l");
setup_cmd(head, "sdd", "ls -l");
// head = created_ast_n(_AND, NULL, NULL);
head->env = init_env(envp);
// head->left = created_ast_n(_CMD, head, head);

View File

@@ -6,21 +6,21 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:31:49 by lderidde #+# #+# */
/* Updated: 2025/01/24 14:31:49 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:06:11 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/builtins.h"
void pwd_update(t_data *data, char *src, char *dest)
void pwd_update(t_ast_n *head, char *src, char *dest)
{
set_var_env("OLDPWD", src, data);
set_var_env("PWD", dest, data);
set_var_env("OLDPWD", src, head);
set_var_env("PWD", dest, head);
free_null_ptr(src);
free_null_ptr(dest);
}
int exec_cd(char *path, t_data *data)
int exec_cd(char *path, t_ast_n *head)
{
char *src;
char *dest;
@@ -29,29 +29,29 @@ int exec_cd(char *path, t_data *data)
if (chdir(path) == -1)
return (err_msg_cmd("cd", path, strerror(errno), EXIT_FAILURE));
dest = getcwd(NULL, 0);
pwd_update(data, src, dest);
pwd_update(head, src, dest);
return (0);
}
int builtin_cd(char **arg, t_data *data)
int builtin_cd(char **arg, t_ast_n *head)
{
char *path;
if (count_var(arg) == 1 || (count_var(arg) == 2 && ft_strncmp(arg[1], "~", 1) == 0))
{
path = get_var_value("HOME", data->env);
path = get_var_value("HOME", head->env);
if (!path)
return (err_msg_cmd("cd", NULL, "HOME not set\n", EXIT_FAILURE));
return (exec_cd(path, data));
return (exec_cd(path, head));
}
if (count_var(arg) > 2)
return (err_msg_cmd("cd", NULL, "too many arguments\n", EXIT_FAILURE));
if (ft_strncmp(arg[1], "-", 1) == 0)
{
path = get_var_value("OLDPWD", data->env);
path = get_var_value("OLDPWD", head->env);
if (!path)
return (err_msg_cmd("cd", NULL, "OLDPWD not set\n", EXIT_FAILURE));
return (exec_cd(path, data));
return (exec_cd(path, head));
}
return (exec_cd(arg[1], data));
return (exec_cd(arg[1], head));
}

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:31:56 by lderidde #+# #+# */
/* Updated: 2025/01/24 14:31:56 by lderidde ### ########.fr */
/* Updated: 2025/01/28 09:34:41 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -89,7 +89,7 @@ static void echo_print(char **arg, int j, char **envp)
}
}
void builtin_echo(char **arg, char **envp)
int builtin_echo(char **arg, char **envp)
{
int i;
bool flag;
@@ -104,4 +104,5 @@ void builtin_echo(char **arg, char **envp)
echo_print(arg, i, envp);
if (!flag)
printf("\n");
return (0);
}

View File

@@ -6,22 +6,27 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:04 by lderidde #+# #+# */
/* Updated: 2025/01/24 14:32:04 by lderidde ### ########.fr */
/* Updated: 2025/01/28 09:54:32 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/builtins.h"
void builtin_env(char *str, char **envp)
int builtin_env(char **arg, char **envp)
{
int i;
i = 0;
(void)str;
if (count_args(arg) > 1)
{
err_msg_cmd("env", NULL, "too many arguments", 1);
return (1);
}
while (envp[i])
{
if (envp[i][0] && ft_strchr(envp[i], '=') != NULL)
printf("%s\n", envp[i]);
i++;
}
return (0);
}

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:13 by lderidde #+# #+# */
/* Updated: 2025/01/27 11:19:17 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:39:44 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -38,86 +38,36 @@ void bash_exit(int code)
void bash_exit_errornum(char *arg)
{
ft_putendl_fd("exit", 2);
ft_put_s_fd("minishell: exit: ", 2);
write(2, arg, ft_strlen(arg));
ft_putendl_fd(": numeric argument required", 2);
err_msg_cmd("exit", arg, "numeric argument required", 2);
exit(2);
}
static char *get_arg(char *str)
{
int i;
i = 4;
if (!str[i])
return (NULL);
while (str[i] && str[i] == ' ')
i++;
return (&str[i]);
}
int is_sep(char c)
{
if ((c >= 9 && c <= 13) || c == ' ' || c == '\0')
return (1);
else
return (0);
}
int count_arg(char *str)
{
int count;
int i;
count = 0;
i = 0;
while (str[i] && str[i + 1])
{
if (!is_sep(str[i]) && is_sep(str[i + 1]))
count++;
i++;
}
return (count);
}
void bash_exiterrorcount(void)
int bash_exiterrorcount(void)
{
ft_putendl_fd("exit", 2);
ft_putendl_fd("minishell: exit: too many arguments", 2);
/*
* SET EXIT CODE WITHOUT EXITING MINISHELL
*/
return (2);
}
void builtin_exit(char *str, bool depth)
int builtin_exit(char **arg, bool depth)
{
char *arg;
long res;
arg = get_arg(str);
long res = ft_atol(arg);
(void)res;
if (errno == ERANGE)
ft_printf("error numeric aarg\n");
if (ft_isnumeric(arg[1]))
res = ft_atol(arg[1]);
if (depth == true)
{
if (count_arg(str) >= 2)
{
ft_putendl_fd("minishell: exit: too many arguments", 2);
exit (1);
}
if (!ft_isnumeric(arg))
{
ft_put_s_fd("minishell: exit: ", 2);
write(2, arg, ft_strlen(arg));
ft_putendl_fd(": numeric argument required", 2);
exit(2);
}
exit(ft_atol(arg) % 256);
if (count_args(arg) > 2 && ft_isnumeric(arg[1]))
return (err_msg_cmd("exit", NULL, "too many arguments", 1));
else if (!ft_isnumeric(arg[1]) || errno == ERANGE)
return (err_msg_cmd("exit", arg[1], "numeric argument required", 2));
return (res % 256);
}
if (count_arg(str) >= 2)
bash_exiterrorcount();
else if (ft_isnumeric(arg))
bash_exit(ft_atol(arg) % 256);
if (count_args(arg) > 2 && ft_isnumeric(arg[1]))
return (bash_exiterrorcount());
else if (!ft_isnumeric(arg[1]) || errno == ERANGE)
bash_exit_errornum(arg[1]);
else
bash_exit_errornum(arg);
bash_exit(res % 256);
return (1);
}

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:20 by lderidde #+# #+# */
/* Updated: 2025/01/24 14:32:20 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:19:06 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -59,7 +59,7 @@ void print_arr(char **envp)
}
}
void print_export(char **envp)
int print_export(char **envp)
{
int i;
int j;
@@ -85,25 +85,29 @@ void print_export(char **envp)
}
}
print_arr(envp);
return (0);
}
void builtin_export(char **arg, t_data *data)
int builtin_export(char **arg, t_ast_n *head)
{
int i;
char **tmp;
i = 0;
if (count_var(arg) == 1)
return (print_export(data->env));
while (++i < count_var(arg))
if (count_args(arg) == 1)
return (print_export(head->env));
while (++i < count_args(arg))
{
if (ft_strchr(arg[i], '=') != NULL)
{
tmp = key_value(arg[i]);
set_var_env(tmp[0], tmp[1], data);
if (!tmp)
return (1);
set_var_env(tmp[0], tmp[1], head);
free_tmp(tmp);
}
else
set_var_env(arg[i], NULL, data);
set_var_env(arg[i], NULL, head);
}
return (0);
}

View File

@@ -6,43 +6,27 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:30 by lderidde #+# #+# */
/* Updated: 2025/01/24 14:32:30 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:38:07 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/builtins.h"
int count_char(char *str)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
if (str[i] != ' ')
count++;
i++;
}
return (count);
}
void builtin_pwd(char *arg)
int builtin_pwd(char **arg)
{
char *cwd;
if (count_char(arg) > 3)
ft_putendl_fd("pwd: too many arguments", 2);
else
(void)arg;
cwd = getcwd(NULL, 0);
if (cwd != NULL)
{
cwd = getcwd(NULL, 0);
if (cwd != NULL)
{
printf("%s\n", cwd);
free(cwd);
}
if (!cwd)
perror("pwd");
ft_printf("%s\n", cwd);
free(cwd);
return (0);
}
else
{
perror("pwd");
return (1);
}
}

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:36 by lderidde #+# #+# */
/* Updated: 2025/01/24 14:32:36 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:32:54 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -33,7 +33,7 @@
// envp[i][0] = '\0';
// }
//
int builtin_unset(char **arg, t_data *data)
int builtin_unset(char **arg, t_ast_n *head)
{
int i;
int ret;
@@ -42,6 +42,6 @@ int builtin_unset(char **arg, t_data *data)
if (count_var(arg) == 1)
return (err_msg_cmd("unset", NULL, "not enough arguments", EXIT_FAILURE));
while (++i < count_var(arg))
ret = remove_env_var(arg[i], data);
ret = remove_env_var(arg[i], head);
return (ret);
}

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:41 by lderidde #+# #+# */
/* Updated: 2025/01/24 14:32:41 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:53:44 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,13 +14,21 @@
int err_msg_cmd(char *cmd, char *arg, char *msg, int code)
{
ft_put_s_fd(cmd, 2);
ft_put_s_fd(": ", 2);
ft_put_s_fd(msg, 2);
if (arg)
{
ft_put_s_fd(": ", 2);
ft_putendl_fd(arg, 2);
}
if (arg != NULL)
ft_fprintf(2, "minishell: %s: %s: %s\n", cmd, arg, msg);
else
ft_fprintf(2, "minishell: %s: %s\n", cmd, msg);
return (code);
}
int count_args(char **tab)
{
int i;
i = 0;
if (!tab || !*tab)
return (0);
while (tab[i])
i++;
return (i);
}

64
srcs/env/var.c vendored
View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* var.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 10:08:49 by lderidde #+# #+# */
/* Updated: 2025/01/28 10:09:55 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
void free_null_ptr(void *ptr)
@@ -63,7 +75,7 @@ char *get_var_value(char *key, char **envp)
return (NULL);
}
bool is_valid_key(char *key, t_data *data)
bool is_valid_key(char *key, t_ast_n *head)
{
char *tmp;
int i;
@@ -71,9 +83,9 @@ bool is_valid_key(char *key, t_data *data)
tmp = ft_strjoin(key, "=");
if (!tmp)
return (false);
while (data->env[i])
while (head->env[i])
{
if (ft_strncmp(data->env[i], tmp, ft_strlen(tmp)) == 0)
if (ft_strncmp(head->env[i], tmp, ft_strlen(tmp)) == 0)
{
free_null_ptr(tmp);
return (true);
@@ -84,7 +96,7 @@ bool is_valid_key(char *key, t_data *data)
return (false);
}
int get_var_index(char *key, t_data *data)
int get_var_index(char *key, t_ast_n *head)
{
int i;
char *new_key;
@@ -93,16 +105,16 @@ int get_var_index(char *key, t_data *data)
if (!new_key)
return (-1);
i = 0;
while (data->env[i])
while (head->env[i])
{
if (ft_strncmp(data->env[i], new_key, ft_strlen(new_key)) == 0)
if (ft_strncmp(head->env[i], new_key, ft_strlen(new_key)) == 0)
{
free_null_ptr(new_key);
return (i);
}
else if (ft_strncmp(data->env[i], key, ft_strlen(key)) == 0
&& (ft_strchr(data->env[i], '=') == NULL &&
(ft_strlen(data->env[i]) == ft_strlen(key))))
else if (ft_strncmp(head->env[i], key, ft_strlen(key)) == 0
&& (ft_strchr(head->env[i], '=') == NULL &&
(ft_strlen(head->env[i]) == ft_strlen(key))))
{
free_null_ptr(new_key);
return (i);
@@ -114,51 +126,51 @@ int get_var_index(char *key, t_data *data)
}
int remove_env_var(char *key, t_data *data)
int remove_env_var(char *key, t_ast_n *head)
{
int i;
int j;
i = get_var_index(key, data);
i = get_var_index(key, head);
if (i == -1)
return (0);
j = i;
free_null_ptr(data->env[i]);
while (data->env[i + 1])
free_null_ptr(head->env[i]);
while (head->env[i + 1])
{
data->env[i] = ft_strdup(data->env[i + 1]);
free_null_ptr(data->env[i + 1]);
head->env[i] = ft_strdup(head->env[i + 1]);
free_null_ptr(head->env[i + 1]);
j++;
i++;
}
data->env = copy_env_var(data->env, j);
if (data->env)
head->env = copy_env_var(head->env, j);
if (head->env)
return (1);
return (0);
}
void set_var_env(char *key, char *value, t_data *data)
void set_var_env(char *key, char *value, t_ast_n *head)
{
int i;
char *tmp;
i = get_var_index(key, data);
i = get_var_index(key, head);
if (value)
tmp = ft_strjoin(key, "=");
if (!tmp)
return ;
if (i != -1 && data->env[i])
if (i != -1 && head->env[i])
{
free_null_ptr(data->env[i]);
data->env[i] = ft_strjoin(tmp, value);
free_null_ptr(head->env[i]);
head->env[i] = ft_strjoin(tmp, value);
}
else
{
i = count_var(data->env);
data->env = copy_env_var(data->env, i + 1);
if (!data->env)
i = count_var(head->env);
head->env = copy_env_var(head->env, i + 1);
if (!head->env)
return ;
data->env[i] = ft_strjoin(tmp, value);
head->env[i] = ft_strjoin(tmp, value);
}
free_null_ptr(tmp);
return ;

View File

@@ -6,57 +6,49 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
/* Updated: 2025/01/27 15:43:24 by lderidde ### ########.fr */
/* Updated: 2025/01/28 10:41:39 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
// int is_builtin(char *str)
// {
// if (ft_strncmp(str, "exit", 4) == 0)
// return (1);
// if (ft_strncmp(str, "pwd", 3) == 0)
// return (1);
// if (ft_strncmp(str, "echo", 4) == 0)
// return (1);
// if (ft_strncmp(str, "env", 3) == 0)
// return (1);
// if (ft_strncmp(str, "unset", 5) == 0)
// return (1);
// if (ft_strncmp(str, "cd", 2) == 0)
// return (1);
// if (ft_strncmp(str, "export", 6) == 0)
// return (1);
// }
int is_builtin(char *str)
{
if (ft_strncmp(str, "exit", 4) == 0)
return (1);
else if (ft_strncmp(str, "pwd", 3) == 0)
return (1);
else if (ft_strncmp(str, "echo", 4) == 0)
return (1);
else if (ft_strncmp(str, "env", 3) == 0)
return (1);
else if (ft_strncmp(str, "unset", 5) == 0)
return (1);
else if (ft_strncmp(str, "cd", 2) == 0)
return (1);
else if (ft_strncmp(str, "export", 6) == 0)
return (1);
else
return (0);
}
// int exec_builtin(t_ast_n *node)
// {
// if (ft_strncmp(node->cmd, "exit", 4) == 0)
// return (builtin_exit(node->cmd, true));
// if (ft_strncmp(node->cmd, "pwd", 3) == 0)
// return (builtin_pwd(node->cmd));
// if (ft_strncmp(node->cmd, "echo", 4) == 0)
// return (builtin_echo(ft_split(node->arg, " "), node->head->env));
// if (ft_strncmp(node->cmd, "env", 3) == 0)
// return (builtin_env(node->cmd, node->head->env));
// if (ft_strncmp(node->cmd, "unset", 5) == 0)
// return (builtin_unset(ft_split(node->arg, " "), node->head));
// if (ft_strncmp(node->cmd, "cd", 2) == 0)
// return (builtin_cd(ft_split(node->arg, " "), node->head));
// if (ft_strncmp(node->cmd, "export", 6) == 0)
// return (builtin_export(ft_split(node->arg, " "), node->head));
// }
// void free_tab(char **tab)
// {
// int i;
//
// i = -1;
// while (tab[++i])
// free(tab[i]);
// free(tab);
// }
int exec_builtin(t_ast_n *node)
{
if (ft_strncmp(node->cmd, "exit", 4) == 0)
return (builtin_exit(node->args, true));
else if (ft_strncmp(node->cmd, "pwd", 3) == 0)
return (builtin_pwd(node->args));
else if (ft_strncmp(node->cmd, "echo", 4) == 0)
return (builtin_echo(node->args, node->head->env));
else if (ft_strncmp(node->cmd, "env", 3) == 0)
return (builtin_env(node->args, node->head->env));
else if (ft_strncmp(node->cmd, "unset", 5) == 0)
return (builtin_unset(node->args, node->head));
else if (ft_strncmp(node->cmd, "cd", 2) == 0)
return (builtin_cd(node->args, node->head));
else
return (builtin_export(node->args, node->head));
}
char *find_path(char *cmd, char **env)
{
@@ -65,10 +57,9 @@ char *find_path(char *cmd, char **env)
char **paths;
int i;
// if (access(cmd, F_OK) == 0)
// return (cmd);
if (access(cmd, F_OK) == 0)
return (cmd);
i = 0;
ft_printf("test2\n");
while (ft_strnstr(env[i], "PATH=", 5) == NULL)
i++;
paths = ft_split(env[i] + 5, ":");
@@ -77,7 +68,6 @@ char *find_path(char *cmd, char **env)
{
tmp = ft_strjoin(paths[i], "/");
path = ft_strjoin(tmp, cmd);
printf("path: %s\n", path);
free(tmp);
if (access(path, F_OK) == 0)
return (free_tab(paths), path);
@@ -121,7 +111,7 @@ void handle_redir(t_ast_n *node)
else if (node->redir == _RED_L)
handle_file(node, 1);
else if (node->redir == _RED_R)
handle_file(node , 2);
handle_file(node, 2);
else if (node->redir == _RED_DR)
handle_file(node, 3);
if (node->redir == _RED_L)
@@ -141,10 +131,7 @@ int exec(t_ast_n *node)
char *path;
handle_redir(node);
ft_printf("test1\n");
path = find_path(node->cmd, node->head->env);
ft_printf("test3\n");
printf("path: %s\n", path);
if (!path)
return_error(node->cmd, "command not found", 127);
if (access(path, X_OK) != 0)
@@ -163,10 +150,10 @@ int exec_scmd(t_ast_n *node)
pid_t pid;
int status;
// if (is_builtin(node->cmd))
// return (exec_builtin(node));
// else
// {
if (is_builtin(node->cmd))
return (exec_builtin(node));
else
{
pid = fork();
if (pid == 0)
{
@@ -174,22 +161,20 @@ int exec_scmd(t_ast_n *node)
}
else
{
wait(NULL);
return (0);
waitpid(pid, &status, 0);
if (WIFEXITED(status))
return (WEXITSTATUS(status));
else
return (-1);
}
return (0);
// }
return (1);
}
}
int execute_command(t_ast_n *node)
{
int status;
printf("test100\n");
if (node->state == _CMD)
return (exec_scmd(node));
else if (node->state == _AND)

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */
/* Updated: 2025/01/27 15:13:16 by lderidde ### ########.fr */
/* Updated: 2025/01/28 08:46:01 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -71,7 +71,7 @@ int main(int ac, char **av, char **envp)
// if (!envp[0])
// env = ft_setnewenv();
node = return_hardcode_ast(envp);
execute_command(node);
return (execute_command(node));
// while (1)
// {
// input = powerline();
@@ -91,5 +91,4 @@ int main(int ac, char **av, char **envp)
// builtin_export(ft_split(input, " "), data);
// free(input);
// }
return (0);
}