split enviroment functions, and util files

This commit is contained in:
whaffman 2025-02-04 15:57:26 +01:00
parent 57fb0c0af1
commit 3d0d062495
11 changed files with 210 additions and 159 deletions

12
.editorconfig Normal file
View File

@ -0,0 +1,12 @@
# Generated by editorconfig.timseverien.com
# https://editor.timseverien.com/#config
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
tab_width = 4
trim_trailing_whitespace = true

View File

@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a
OBJ_PATH = obj
VPATH = src
VPATH = src:src/enviroment
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
@ -32,12 +32,10 @@ CC = cc
RM = rm -rf
INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH)
CFLAGS = -Wall -Wextra -Werror -MMD
CFLAGS = -Wall -Wextra -Werror -MMD -fsanitize=address,undefined -g
LDLIBS := -L$(LIBFT_PATH) -lft
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LDLIBS := -L$(LIBFT_PATH) -lft
endif
all: $(NAME)
@ -61,7 +59,7 @@ $(OBJ_PATH)/%.o: %.c $(LIBFT) | $(OBJ_PATH)
clean:
$(RM) $(OBJECTS) $(OBJ_PATH)
$(MAKE) -C $(LIBFT_PATH) clean
$(MAKE) -C $(MLX42_PATH)/build clean
fclean: clean
$(RM) $(NAME)

4
compile_flags.txt Normal file
View File

@ -0,0 +1,4 @@
-I
./inc
-I
./lib/libft/inc

16
inc/enviroment.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef ENVIROMENT_H
# define ENVIROMENT_H
typedef struct s_enviroment
{
char *name;
char *value;
struct s_enviroment *next;
} t_enviroment;
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);
#endif

View File

@ -162,4 +162,10 @@
# include <termios.h>
# include <term.h>
# include <readline/readline.h>
# include <readline/history.h>
# include "libft.h"
# include "enviroment.h"
#endif // MINISHELL_H

@ -1 +1 @@
Subproject commit 27f7d3e9e02cccf7092f3101b939d381198a595b
Subproject commit 716aeca641571c4880639eaa3994bdaeec5b8f0f

View File

@ -0,0 +1,24 @@
#include "enviroment.h"
#include "libft.h"
#include <stdlib.h>
#include <stdio.h>
void add_enviroment(t_enviroment **enviroment, char *name, char *value)
{
t_enviroment *new_enviroment;
if (name != NULL && value != NULL)
{
new_enviroment = malloc(sizeof(t_enviroment));
if (new_enviroment == NULL)
{
perror("malloc");
return ;
}
new_enviroment->name = ft_strdup(name);
new_enviroment->value = ft_strdup(value);
new_enviroment->next = *enviroment;
*enviroment = new_enviroment;
}
}

View File

@ -0,0 +1,20 @@
#include "enviroment.h"
#include <stdlib.h>
void free_enviroment(t_enviroment *enviroment)
{
t_enviroment *next;
while (enviroment != NULL)
{
if (enviroment->next)
next = enviroment->next;
else
next = NULL;
free(enviroment->name);
free(enviroment->value);
free(enviroment);
enviroment = next;
}
}

View File

@ -0,0 +1,15 @@
#include "enviroment.h"
#include "libft.h"
char *get_enviroment(t_enviroment *enviroment, char *name)
{
while (enviroment != NULL)
{
if (ft_strcmp(enviroment->name, name) == 0)
{
return enviroment->value;
}
enviroment = enviroment->next;
}
return NULL;
}

View File

@ -0,0 +1,11 @@
#include "enviroment.h"
#include <stdio.h>
void print_enviroment(t_enviroment *enviroment)
{
while (enviroment != NULL)
{
printf("%s=%s\n", enviroment->name, enviroment->value);
enviroment = enviroment->next;
}
}

View File

@ -1,62 +1,5 @@
#include "minishell.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "libft.h"
// /**
// * @brief Prints the prompt for the Minishell.
// *
// * This function prints the prompt for the Minishell, which is the current working directory followed by a dollar sign.
// *
// * @param void
// * @return void
// */
typedef struct s_enviroment
{
char *name;
char *value;
struct s_enviroment *next;
} t_enviroment;
void add_enviroment(t_enviroment **enviroment, char *name, char *value)
{
t_enviroment *new_enviroment = malloc(sizeof(t_enviroment));
if (new_enviroment == NULL)
{
perror("malloc");
return;
}
new_enviroment->name = name;
new_enviroment->value = value;
new_enviroment->next = *enviroment;
*enviroment = new_enviroment;
}
void print_enviroment(t_enviroment *enviroment)
{
while (enviroment != NULL)
{
printf("%s=%s\n", enviroment->name, enviroment->value);
enviroment = enviroment->next;
}
}
char *get_enviroment(t_enviroment *enviroment, char *name)
{
while (enviroment != NULL)
{
if (ft_strcmp(enviroment->name, name) == 0)
{
return enviroment->value;
}
enviroment = enviroment->next;
}
return NULL;
}
void print_prompt(void)
{
char *cwd = getcwd(NULL, 0);
@ -80,9 +23,11 @@ int main(int argc, char **argv, char **envp)
{
env = ft_split(*envp, '=');
add_enviroment(&enviroment, env[0], env[1]);
ft_free_arr(env);
envp++;
}
print_enviroment(enviroment);
free_enviroment(enviroment);
return 0;
}