enviroNment

This commit is contained in:
whaffman 2025-02-07 16:01:12 +01:00
parent 8e6f9a23cd
commit 6c726f67f1
14 changed files with 60 additions and 60 deletions

View File

@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = src:src/enviroment:src/prompt:src/lexer:src/token:src/utils VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* enviroment.h :+: :+: */ /* environment.h :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
@ -10,13 +10,13 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef ENVIROMENT_H #ifndef environment_H
# define ENVIROMENT_H # define environment_H
void add_enviroment(t_enviroment **enviroment, char *name, char *value); void add_environment(t_environment **environment, char *name, char *value);
void print_enviroment(t_enviroment *enviroment); void print_environment(t_environment *environment);
char *get_enviroment(t_enviroment *enviroment, char *name); char *get_environment(t_environment *environment, char *name);
void free_enviroment(t_enviroment *enviroment); void free_environment(t_environment *environment);
int parse_enviroment(char **envp, t_enviroment **enviroment); int parse_environment(char **envp, t_environment **environment);
#endif // ENVIROMENT_H #endif // environment_H

View File

@ -16,7 +16,7 @@
# include "allowed.h" # include "allowed.h"
# include "libft.h" # include "libft.h"
# include "typedef.h" # include "typedef.h"
# include "enviroment.h" # include "environment.h"
# include "prompt.h" # include "prompt.h"
# include "tokenizer.h" # include "tokenizer.h"
# include "utils.h" # include "utils.h"

View File

@ -24,12 +24,12 @@ typedef enum e_token_type
T_ERROR T_ERROR
} t_token_type; } t_token_type;
typedef struct s_enviroment typedef struct s_environment
{ {
char *name; char *name;
char *value; char *value;
struct s_enviroment *next; struct s_environment *next;
} t_enviroment; } t_environment;
typedef struct s_token typedef struct s_token
{ {
@ -48,7 +48,7 @@ typedef struct s_lexer
typedef struct s_minishell typedef struct s_minishell
{ {
t_enviroment *enviroment; t_environment *environment;
char *line; char *line;
t_lexer *lexer; t_lexer *lexer;
t_list *tokens; t_list *tokens;

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* add_enviroment.c :+: :+: */ /* add_environment.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
@ -12,21 +12,21 @@
#include "minishell.h" #include "minishell.h"
void add_enviroment(t_enviroment **enviroment, char *name, char *value) void add_environment(t_environment **environment, char *name, char *value)
{ {
t_enviroment *new_enviroment; t_environment *new_environment;
if (name != NULL && value != NULL) if (name != NULL && value != NULL)
{ {
new_enviroment = malloc(sizeof(t_enviroment)); new_environment = malloc(sizeof(t_environment));
if (new_enviroment == NULL) if (new_environment == NULL)
{ {
perror("malloc"); perror("malloc");
return ; return ;
} }
new_enviroment->name = ft_strdup(name); new_environment->name = ft_strdup(name);
new_enviroment->value = ft_strdup(value); new_environment->value = ft_strdup(value);
new_enviroment->next = *enviroment; new_environment->next = *environment;
*enviroment = new_enviroment; *environment = new_environment;
} }
} }

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* free_enviroment.c :+: :+: */ /* free_environment.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
@ -12,19 +12,19 @@
#include "minishell.h" #include "minishell.h"
void free_enviroment(t_enviroment *enviroment) void free_environment(t_environment *environment)
{ {
t_enviroment *next; t_environment *next;
while (enviroment != NULL) while (environment != NULL)
{ {
if (enviroment->next) if (environment->next)
next = enviroment->next; next = environment->next;
else else
next = NULL; next = NULL;
free(enviroment->name); free(environment->name);
free(enviroment->value); free(environment->value);
free(enviroment); free(environment);
enviroment = next; environment = next;
} }
} }

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* get_enviroment.c :+: :+: */ /* get_environment.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
@ -12,15 +12,15 @@
#include "minishell.h" #include "minishell.h"
char *get_enviroment(t_enviroment *enviroment, char *name) char *get_environment(t_environment *environment, char *name)
{ {
while (enviroment != NULL) while (environment != NULL)
{ {
if (ft_strcmp(enviroment->name, name) == 0) if (ft_strcmp(environment->name, name) == 0)
{ {
return (enviroment->value); return (environment->value);
} }
enviroment = enviroment->next; environment = environment->next;
} }
return (NULL); return (NULL);
} }

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* parse_enviroment.c :+: :+: */ /* parse_environment.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
@ -12,17 +12,17 @@
#include "minishell.h" #include "minishell.h"
int parse_enviroment(char **envp, t_enviroment **enviroment) int parse_environment(char **envp, t_environment **environment)
{ {
char **env; char **env;
*enviroment = NULL; *environment = NULL;
if (envp == NULL) if (envp == NULL)
return (FAILURE); return (FAILURE);
while (*envp != NULL) while (*envp != NULL)
{ {
env = ft_split(*envp, '='); env = ft_split(*envp, '=');
add_enviroment(enviroment, env[0], env[1]); add_environment(environment, env[0], env[1]);
ft_free_arr(env); ft_free_arr(env);
envp++; envp++;
} }

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* print_enviroment.c :+: :+: */ /* print_environment.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
@ -12,11 +12,11 @@
#include "minishell.h" #include "minishell.h"
void print_enviroment(t_enviroment *enviroment) void print_environment(t_environment *environment)
{ {
while (enviroment != NULL) while (environment != NULL)
{ {
printf("%s=%s\n", enviroment->name, enviroment->value); printf("%s=%s\n", environment->name, environment->value);
enviroment = enviroment->next; environment = environment->next;
} }
} }

View File

@ -21,7 +21,7 @@ int main(int argc, char **argv, char **envp)
print_banner(); print_banner();
ft_load_history(); ft_load_history();
minishell = init_minishell(); minishell = init_minishell();
parse_enviroment(envp, &(minishell->enviroment)); parse_environment(envp, &(minishell->environment));
while (TRUE) while (TRUE)
{ {
minishell->line = ft_prompt(minishell); minishell->line = ft_prompt(minishell);

View File

@ -12,12 +12,12 @@
#include "minishell.h" #include "minishell.h"
char *get_user(t_enviroment *enviroment) char *get_user(t_environment *environment)
{ {
char *str1; char *str1;
char *str2; char *str2;
str1 = ft_strdup(get_enviroment(enviroment, "USER")); str1 = ft_strdup(get_environment(environment, "USER"));
if (str1 == NULL) if (str1 == NULL)
{ {
str1 = ft_strdup("guest"); str1 = ft_strdup("guest");
@ -29,14 +29,14 @@ char *get_user(t_enviroment *enviroment)
return (str1); return (str1);
} }
char *get_path(t_enviroment *enviroment) char *get_path(t_environment *environment)
{ {
char *home; char *home;
char *temp; char *temp;
char *cwd; char *cwd;
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
home = get_enviroment(enviroment, "HOME"); home = get_environment(environment, "HOME");
if (cwd == NULL) if (cwd == NULL)
{ {
perror("getcwd"); perror("getcwd");
@ -63,10 +63,10 @@ char *ft_prompt(t_minishell *minishell)
char *user; char *user;
char *temp; char *temp;
cwd = get_path(minishell->enviroment); cwd = get_path(minishell->environment);
if (cwd == NULL) if (cwd == NULL)
return (NULL); return (NULL);
user = get_user(minishell->enviroment); user = get_user(minishell->environment);
temp = ft_strjoin(user, cwd); temp = ft_strjoin(user, cwd);
free(user); free(user);
free(cwd); free(cwd);

View File

@ -16,7 +16,7 @@ void free_minishell(t_minishell *minishell)
{ {
if (minishell->line) if (minishell->line)
free_minishell_line(minishell); free_minishell_line(minishell);
if (minishell->enviroment) if (minishell->environment)
free_enviroment(minishell->enviroment); free_environment(minishell->environment);
free(minishell); free(minishell);
} }

View File

@ -22,7 +22,7 @@ t_minishell *init_minishell(void)
perror("failed assigning minishell memory"); perror("failed assigning minishell memory");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
minishell->enviroment = NULL; minishell->environment = NULL;
minishell->line = NULL; minishell->line = NULL;
minishell->lexer = NULL; minishell->lexer = NULL;
minishell->tokens = NULL; minishell->tokens = NULL;

View File

@ -28,7 +28,7 @@ void simple_builtins(t_minishell *minishell)
if (cmp_value(minishell->tokens, "clear")) if (cmp_value(minishell->tokens, "clear"))
printf("\033[2J\033[1;1H"); printf("\033[2J\033[1;1H");
else if (cmp_value(minishell->tokens, "env")) else if (cmp_value(minishell->tokens, "env"))
print_enviroment(minishell->enviroment); print_environment(minishell->environment);
else if (cmp_value(minishell->tokens, "exit")) else if (cmp_value(minishell->tokens, "exit"))
{ {
free_minishell(minishell); free_minishell(minishell);