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> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:23:27 by lderidde #+# #+# */ /* 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 #ifndef AST_H
# define AST_H # define AST_H
# include "env.h" # include "../lib/libft/libft.h"
typedef enum e_state 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 #ifndef BUILTINS_H
# define BUILTINS_H # define BUILTINS_H
@@ -13,13 +25,13 @@
# include <string.h> # include <string.h>
// void echo(char *msg, int flag); // void echo(char *msg, int flag);
void builtin_echo(char **arg, char **envp); int builtin_echo(char **arg, char **envp);
void builtin_exit(char *arg, bool depth); int builtin_exit(char **arg, bool depth);
void builtin_pwd(char *arg); int builtin_pwd(char **arg);
void builtin_env(char *str, char **envp); int builtin_env(char **arg, char **envp);
int builtin_unset(char **arg, t_data *data); int builtin_unset(char **arg, t_ast_n *head);
int builtin_cd(char **arg, t_data *data); int builtin_cd(char **arg, t_ast_n *head);
void builtin_export(char **arg, t_data *data); int builtin_export(char **arg, t_ast_n *head);
//UTILS //UTILS
int count_char(char *str); 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 #ifndef ENV_H
# define ENV_H # define ENV_H
# include "../lib/libft/libft.h" # include "../lib/libft/libft.h"
# include "ast.h"
# include <unistd.h> # include <unistd.h>
# include <stdlib.h> # include <stdlib.h>
# include <stdbool.h> # include <stdbool.h>
typedef struct s_data
{
char **env;
} t_data;
//VAR //VAR
int count_var(char **envp); 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); void free_null_ptr(void *ptr);
char **copy_env_var(char **envp, int count); char **copy_env_var(char **envp, int count);
char *get_var_value(char *key, char **envp); char *get_var_value(char *key, char **envp);
bool is_valid_key(char *key, t_data *data); bool is_valid_key(char *key, t_ast_n *head);
int remove_env_var(char *key, t_data *data); int remove_env_var(char *key, t_ast_n *head);
void set_var_env(char *key, char *value, t_data *t_data); void set_var_env(char *key, char *value, t_ast_n *head);
char **init_env(char **envp); char **init_env(char **envp);
#endif #endif

View File

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

View File

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

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:22:16 by lderidde #+# #+# */ /* 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); 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) void setup_cmd(t_ast_n *node, char *cmd, char *args)
{ {
node->cmd = ft_strdup(cmd); 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 = created_ast_n(_CMD, NULL, NULL);
head->head = head; head->head = head;
setup_cmd(head, "ls", "ls -l"); setup_cmd(head, "sdd", "ls -l");
// head = created_ast_n(_AND, NULL, NULL); // head = created_ast_n(_AND, NULL, NULL);
head->env = init_env(envp); head->env = init_env(envp);
// head->left = created_ast_n(_CMD, head, head); // head->left = created_ast_n(_CMD, head, head);

View File

@@ -6,21 +6,21 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:31:49 by lderidde #+# #+# */ /* 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" #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("OLDPWD", src, head);
set_var_env("PWD", dest, data); set_var_env("PWD", dest, head);
free_null_ptr(src); free_null_ptr(src);
free_null_ptr(dest); free_null_ptr(dest);
} }
int exec_cd(char *path, t_data *data) int exec_cd(char *path, t_ast_n *head)
{ {
char *src; char *src;
char *dest; char *dest;
@@ -29,29 +29,29 @@ int exec_cd(char *path, t_data *data)
if (chdir(path) == -1) if (chdir(path) == -1)
return (err_msg_cmd("cd", path, strerror(errno), EXIT_FAILURE)); return (err_msg_cmd("cd", path, strerror(errno), EXIT_FAILURE));
dest = getcwd(NULL, 0); dest = getcwd(NULL, 0);
pwd_update(data, src, dest); pwd_update(head, src, dest);
return (0); return (0);
} }
int builtin_cd(char **arg, t_data *data) int builtin_cd(char **arg, t_ast_n *head)
{ {
char *path; char *path;
if (count_var(arg) == 1 || (count_var(arg) == 2 && ft_strncmp(arg[1], "~", 1) == 0)) 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) if (!path)
return (err_msg_cmd("cd", NULL, "HOME not set\n", EXIT_FAILURE)); 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) if (count_var(arg) > 2)
return (err_msg_cmd("cd", NULL, "too many arguments\n", EXIT_FAILURE)); return (err_msg_cmd("cd", NULL, "too many arguments\n", EXIT_FAILURE));
if (ft_strncmp(arg[1], "-", 1) == 0) if (ft_strncmp(arg[1], "-", 1) == 0)
{ {
path = get_var_value("OLDPWD", data->env); path = get_var_value("OLDPWD", head->env);
if (!path) if (!path)
return (err_msg_cmd("cd", NULL, "OLDPWD not set\n", EXIT_FAILURE)); 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> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:31:56 by lderidde #+# #+# */ /* 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; int i;
bool flag; bool flag;
@@ -104,4 +104,5 @@ void builtin_echo(char **arg, char **envp)
echo_print(arg, i, envp); echo_print(arg, i, envp);
if (!flag) if (!flag)
printf("\n"); printf("\n");
return (0);
} }

View File

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

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:13 by lderidde #+# #+# */ /* 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) void bash_exit_errornum(char *arg)
{ {
ft_putendl_fd("exit", 2); ft_putendl_fd("exit", 2);
ft_put_s_fd("minishell: exit: ", 2); err_msg_cmd("exit", arg, "numeric argument required", 2);
write(2, arg, ft_strlen(arg));
ft_putendl_fd(": numeric argument required", 2);
exit(2); exit(2);
} }
static char *get_arg(char *str) int bash_exiterrorcount(void)
{
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)
{ {
ft_putendl_fd("exit", 2); ft_putendl_fd("exit", 2);
ft_putendl_fd("minishell: exit: too many arguments", 2); ft_putendl_fd("minishell: exit: too many arguments", 2);
/* return (2);
* SET EXIT CODE WITHOUT EXITING MINISHELL
*/
} }
void builtin_exit(char *str, bool depth) int builtin_exit(char **arg, bool depth)
{ {
char *arg; long res;
arg = get_arg(str); if (ft_isnumeric(arg[1]))
long res = ft_atol(arg); res = ft_atol(arg[1]);
(void)res;
if (errno == ERANGE)
ft_printf("error numeric aarg\n");
if (depth == true) if (depth == true)
{ {
if (count_arg(str) >= 2) if (count_args(arg) > 2 && ft_isnumeric(arg[1]))
{ return (err_msg_cmd("exit", NULL, "too many arguments", 1));
ft_putendl_fd("minishell: exit: too many arguments", 2); else if (!ft_isnumeric(arg[1]) || errno == ERANGE)
exit (1); return (err_msg_cmd("exit", arg[1], "numeric argument required", 2));
return (res % 256);
} }
if (!ft_isnumeric(arg)) if (count_args(arg) > 2 && ft_isnumeric(arg[1]))
{ return (bash_exiterrorcount());
ft_put_s_fd("minishell: exit: ", 2); else if (!ft_isnumeric(arg[1]) || errno == ERANGE)
write(2, arg, ft_strlen(arg)); bash_exit_errornum(arg[1]);
ft_putendl_fd(": numeric argument required", 2);
exit(2);
}
exit(ft_atol(arg) % 256);
}
if (count_arg(str) >= 2)
bash_exiterrorcount();
else if (ft_isnumeric(arg))
bash_exit(ft_atol(arg) % 256);
else else
bash_exit_errornum(arg); bash_exit(res % 256);
return (1);
} }

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:20 by lderidde #+# #+# */ /* 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 i;
int j; int j;
@@ -85,25 +85,29 @@ void print_export(char **envp)
} }
} }
print_arr(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; int i;
char **tmp; char **tmp;
i = 0; i = 0;
if (count_var(arg) == 1) if (count_args(arg) == 1)
return (print_export(data->env)); return (print_export(head->env));
while (++i < count_var(arg)) while (++i < count_args(arg))
{ {
if (ft_strchr(arg[i], '=') != NULL) if (ft_strchr(arg[i], '=') != NULL)
{ {
tmp = key_value(arg[i]); 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); free_tmp(tmp);
} }
else 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> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:30 by lderidde #+# #+# */ /* 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" #include "../../includes/builtins.h"
int count_char(char *str) int builtin_pwd(char **arg)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
if (str[i] != ' ')
count++;
i++;
}
return (count);
}
void builtin_pwd(char *arg)
{ {
char *cwd; char *cwd;
if (count_char(arg) > 3) (void)arg;
ft_putendl_fd("pwd: too many arguments", 2);
else
{
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
if (cwd != NULL) if (cwd != NULL)
{ {
printf("%s\n", cwd); ft_printf("%s\n", cwd);
free(cwd); free(cwd);
return (0);
} }
if (!cwd) else
{
perror("pwd"); perror("pwd");
return (1);
} }
} }

View File

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

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:41 by lderidde #+# #+# */ /* 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) int err_msg_cmd(char *cmd, char *arg, char *msg, int code)
{ {
ft_put_s_fd(cmd, 2); if (arg != NULL)
ft_put_s_fd(": ", 2); ft_fprintf(2, "minishell: %s: %s: %s\n", cmd, arg, msg);
ft_put_s_fd(msg, 2); else
if (arg) ft_fprintf(2, "minishell: %s: %s\n", cmd, msg);
{
ft_put_s_fd(": ", 2);
ft_putendl_fd(arg, 2);
}
return (code); 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" #include "../../includes/minishell.h"
void free_null_ptr(void *ptr) void free_null_ptr(void *ptr)
@@ -63,7 +75,7 @@ char *get_var_value(char *key, char **envp)
return (NULL); return (NULL);
} }
bool is_valid_key(char *key, t_data *data) bool is_valid_key(char *key, t_ast_n *head)
{ {
char *tmp; char *tmp;
int i; int i;
@@ -71,9 +83,9 @@ bool is_valid_key(char *key, t_data *data)
tmp = ft_strjoin(key, "="); tmp = ft_strjoin(key, "=");
if (!tmp) if (!tmp)
return (false); 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); free_null_ptr(tmp);
return (true); return (true);
@@ -84,7 +96,7 @@ bool is_valid_key(char *key, t_data *data)
return (false); return (false);
} }
int get_var_index(char *key, t_data *data) int get_var_index(char *key, t_ast_n *head)
{ {
int i; int i;
char *new_key; char *new_key;
@@ -93,16 +105,16 @@ int get_var_index(char *key, t_data *data)
if (!new_key) if (!new_key)
return (-1); return (-1);
i = 0; 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); free_null_ptr(new_key);
return (i); return (i);
} }
else if (ft_strncmp(data->env[i], key, ft_strlen(key)) == 0 else if (ft_strncmp(head->env[i], key, ft_strlen(key)) == 0
&& (ft_strchr(data->env[i], '=') == NULL && && (ft_strchr(head->env[i], '=') == NULL &&
(ft_strlen(data->env[i]) == ft_strlen(key)))) (ft_strlen(head->env[i]) == ft_strlen(key))))
{ {
free_null_ptr(new_key); free_null_ptr(new_key);
return (i); 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 i;
int j; int j;
i = get_var_index(key, data); i = get_var_index(key, head);
if (i == -1) if (i == -1)
return (0); return (0);
j = i; j = i;
free_null_ptr(data->env[i]); free_null_ptr(head->env[i]);
while (data->env[i + 1]) while (head->env[i + 1])
{ {
data->env[i] = ft_strdup(data->env[i + 1]); head->env[i] = ft_strdup(head->env[i + 1]);
free_null_ptr(data->env[i + 1]); free_null_ptr(head->env[i + 1]);
j++; j++;
i++; i++;
} }
data->env = copy_env_var(data->env, j); head->env = copy_env_var(head->env, j);
if (data->env) if (head->env)
return (1); return (1);
return (0); 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; int i;
char *tmp; char *tmp;
i = get_var_index(key, data); i = get_var_index(key, head);
if (value) if (value)
tmp = ft_strjoin(key, "="); tmp = ft_strjoin(key, "=");
if (!tmp) if (!tmp)
return ; return ;
if (i != -1 && data->env[i]) if (i != -1 && head->env[i])
{ {
free_null_ptr(data->env[i]); free_null_ptr(head->env[i]);
data->env[i] = ft_strjoin(tmp, value); head->env[i] = ft_strjoin(tmp, value);
} }
else else
{ {
i = count_var(data->env); i = count_var(head->env);
data->env = copy_env_var(data->env, i + 1); head->env = copy_env_var(head->env, i + 1);
if (!data->env) if (!head->env)
return ; return ;
data->env[i] = ft_strjoin(tmp, value); head->env[i] = ft_strjoin(tmp, value);
} }
free_null_ptr(tmp); free_null_ptr(tmp);
return ; return ;

View File

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

View File

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