enviroNment
This commit is contained in:
parent
8e6f9a23cd
commit
6c726f67f1
2
Makefile
2
Makefile
@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a
|
||||
|
||||
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"))
|
||||
|
||||
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* enviroment.h :+: :+: */
|
||||
/* environment.h :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
@ -10,13 +10,13 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ENVIROMENT_H
|
||||
# define ENVIROMENT_H
|
||||
#ifndef environment_H
|
||||
# define environment_H
|
||||
|
||||
void add_enviroment(t_enviroment **enviroment, char *name, char *value);
|
||||
void print_enviroment(t_enviroment *enviroment);
|
||||
char *get_enviroment(t_enviroment *enviroment, char *name);
|
||||
void free_enviroment(t_enviroment *enviroment);
|
||||
int parse_enviroment(char **envp, t_enviroment **enviroment);
|
||||
void add_environment(t_environment **environment, char *name, char *value);
|
||||
void print_environment(t_environment *environment);
|
||||
char *get_environment(t_environment *environment, char *name);
|
||||
void free_environment(t_environment *environment);
|
||||
int parse_environment(char **envp, t_environment **environment);
|
||||
|
||||
#endif // ENVIROMENT_H
|
||||
#endif // environment_H
|
||||
@ -16,7 +16,7 @@
|
||||
# include "allowed.h"
|
||||
# include "libft.h"
|
||||
# include "typedef.h"
|
||||
# include "enviroment.h"
|
||||
# include "environment.h"
|
||||
# include "prompt.h"
|
||||
# include "tokenizer.h"
|
||||
# include "utils.h"
|
||||
|
||||
@ -24,12 +24,12 @@ typedef enum e_token_type
|
||||
T_ERROR
|
||||
} t_token_type;
|
||||
|
||||
typedef struct s_enviroment
|
||||
typedef struct s_environment
|
||||
{
|
||||
char *name;
|
||||
char *value;
|
||||
struct s_enviroment *next;
|
||||
} t_enviroment;
|
||||
struct s_environment *next;
|
||||
} t_environment;
|
||||
|
||||
typedef struct s_token
|
||||
{
|
||||
@ -48,7 +48,7 @@ typedef struct s_lexer
|
||||
|
||||
typedef struct s_minishell
|
||||
{
|
||||
t_enviroment *enviroment;
|
||||
t_environment *environment;
|
||||
char *line;
|
||||
t_lexer *lexer;
|
||||
t_list *tokens;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* add_enviroment.c :+: :+: */
|
||||
/* add_environment.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
@ -12,21 +12,21 @@
|
||||
|
||||
#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)
|
||||
{
|
||||
new_enviroment = malloc(sizeof(t_enviroment));
|
||||
if (new_enviroment == NULL)
|
||||
new_environment = malloc(sizeof(t_environment));
|
||||
if (new_environment == NULL)
|
||||
{
|
||||
perror("malloc");
|
||||
return ;
|
||||
}
|
||||
new_enviroment->name = ft_strdup(name);
|
||||
new_enviroment->value = ft_strdup(value);
|
||||
new_enviroment->next = *enviroment;
|
||||
*enviroment = new_enviroment;
|
||||
new_environment->name = ft_strdup(name);
|
||||
new_environment->value = ft_strdup(value);
|
||||
new_environment->next = *environment;
|
||||
*environment = new_environment;
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* free_enviroment.c :+: :+: */
|
||||
/* free_environment.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
@ -12,19 +12,19 @@
|
||||
|
||||
#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)
|
||||
next = enviroment->next;
|
||||
if (environment->next)
|
||||
next = environment->next;
|
||||
else
|
||||
next = NULL;
|
||||
free(enviroment->name);
|
||||
free(enviroment->value);
|
||||
free(enviroment);
|
||||
enviroment = next;
|
||||
free(environment->name);
|
||||
free(environment->value);
|
||||
free(environment);
|
||||
environment = next;
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* get_enviroment.c :+: :+: */
|
||||
/* get_environment.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
@ -12,15 +12,15 @@
|
||||
|
||||
#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);
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* parse_enviroment.c :+: :+: */
|
||||
/* parse_environment.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
@ -12,17 +12,17 @@
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int parse_enviroment(char **envp, t_enviroment **enviroment)
|
||||
int parse_environment(char **envp, t_environment **environment)
|
||||
{
|
||||
char **env;
|
||||
|
||||
*enviroment = NULL;
|
||||
*environment = NULL;
|
||||
if (envp == NULL)
|
||||
return (FAILURE);
|
||||
while (*envp != NULL)
|
||||
{
|
||||
env = ft_split(*envp, '=');
|
||||
add_enviroment(enviroment, env[0], env[1]);
|
||||
add_environment(environment, env[0], env[1]);
|
||||
ft_free_arr(env);
|
||||
envp++;
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* print_enviroment.c :+: :+: */
|
||||
/* print_environment.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
@ -12,11 +12,11 @@
|
||||
|
||||
#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);
|
||||
enviroment = enviroment->next;
|
||||
printf("%s=%s\n", environment->name, environment->value);
|
||||
environment = environment->next;
|
||||
}
|
||||
}
|
||||
@ -21,7 +21,7 @@ int main(int argc, char **argv, char **envp)
|
||||
print_banner();
|
||||
ft_load_history();
|
||||
minishell = init_minishell();
|
||||
parse_enviroment(envp, &(minishell->enviroment));
|
||||
parse_environment(envp, &(minishell->environment));
|
||||
while (TRUE)
|
||||
{
|
||||
minishell->line = ft_prompt(minishell);
|
||||
|
||||
@ -12,12 +12,12 @@
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *get_user(t_enviroment *enviroment)
|
||||
char *get_user(t_environment *environment)
|
||||
{
|
||||
char *str1;
|
||||
char *str2;
|
||||
|
||||
str1 = ft_strdup(get_enviroment(enviroment, "USER"));
|
||||
str1 = ft_strdup(get_environment(environment, "USER"));
|
||||
if (str1 == NULL)
|
||||
{
|
||||
str1 = ft_strdup("guest");
|
||||
@ -29,14 +29,14 @@ char *get_user(t_enviroment *enviroment)
|
||||
return (str1);
|
||||
}
|
||||
|
||||
char *get_path(t_enviroment *enviroment)
|
||||
char *get_path(t_environment *environment)
|
||||
{
|
||||
char *home;
|
||||
char *temp;
|
||||
char *cwd;
|
||||
|
||||
cwd = getcwd(NULL, 0);
|
||||
home = get_enviroment(enviroment, "HOME");
|
||||
home = get_environment(environment, "HOME");
|
||||
if (cwd == NULL)
|
||||
{
|
||||
perror("getcwd");
|
||||
@ -63,10 +63,10 @@ char *ft_prompt(t_minishell *minishell)
|
||||
char *user;
|
||||
char *temp;
|
||||
|
||||
cwd = get_path(minishell->enviroment);
|
||||
cwd = get_path(minishell->environment);
|
||||
if (cwd == NULL)
|
||||
return (NULL);
|
||||
user = get_user(minishell->enviroment);
|
||||
user = get_user(minishell->environment);
|
||||
temp = ft_strjoin(user, cwd);
|
||||
free(user);
|
||||
free(cwd);
|
||||
|
||||
@ -16,7 +16,7 @@ void free_minishell(t_minishell *minishell)
|
||||
{
|
||||
if (minishell->line)
|
||||
free_minishell_line(minishell);
|
||||
if (minishell->enviroment)
|
||||
free_enviroment(minishell->enviroment);
|
||||
if (minishell->environment)
|
||||
free_environment(minishell->environment);
|
||||
free(minishell);
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ t_minishell *init_minishell(void)
|
||||
perror("failed assigning minishell memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
minishell->enviroment = NULL;
|
||||
minishell->environment = NULL;
|
||||
minishell->line = NULL;
|
||||
minishell->lexer = NULL;
|
||||
minishell->tokens = NULL;
|
||||
|
||||
@ -28,7 +28,7 @@ void simple_builtins(t_minishell *minishell)
|
||||
if (cmp_value(minishell->tokens, "clear"))
|
||||
printf("\033[2J\033[1;1H");
|
||||
else if (cmp_value(minishell->tokens, "env"))
|
||||
print_enviroment(minishell->enviroment);
|
||||
print_environment(minishell->environment);
|
||||
else if (cmp_value(minishell->tokens, "exit"))
|
||||
{
|
||||
free_minishell(minishell);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user