split enviroment functions, and util files
This commit is contained in:
parent
57fb0c0af1
commit
3d0d062495
12
.editorconfig
Normal file
12
.editorconfig
Normal 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
|
||||||
12
Makefile
12
Makefile
@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a
|
|||||||
|
|
||||||
OBJ_PATH = obj
|
OBJ_PATH = obj
|
||||||
|
|
||||||
VPATH = src
|
VPATH = src:src/enviroment
|
||||||
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))
|
||||||
@ -32,12 +32,10 @@ CC = cc
|
|||||||
RM = rm -rf
|
RM = rm -rf
|
||||||
|
|
||||||
INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH)
|
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)
|
all: $(NAME)
|
||||||
|
|
||||||
@ -61,7 +59,7 @@ $(OBJ_PATH)/%.o: %.c $(LIBFT) | $(OBJ_PATH)
|
|||||||
clean:
|
clean:
|
||||||
$(RM) $(OBJECTS) $(OBJ_PATH)
|
$(RM) $(OBJECTS) $(OBJ_PATH)
|
||||||
$(MAKE) -C $(LIBFT_PATH) clean
|
$(MAKE) -C $(LIBFT_PATH) clean
|
||||||
$(MAKE) -C $(MLX42_PATH)/build clean
|
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
$(RM) $(NAME)
|
$(RM) $(NAME)
|
||||||
|
|||||||
4
compile_flags.txt
Normal file
4
compile_flags.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
-I
|
||||||
|
./inc
|
||||||
|
-I
|
||||||
|
./lib/libft/inc
|
||||||
16
inc/enviroment.h
Normal file
16
inc/enviroment.h
Normal 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
|
||||||
@ -162,4 +162,10 @@
|
|||||||
# include <termios.h>
|
# include <termios.h>
|
||||||
# include <term.h>
|
# include <term.h>
|
||||||
|
|
||||||
|
# include <readline/readline.h>
|
||||||
|
# include <readline/history.h>
|
||||||
|
|
||||||
|
# include "libft.h"
|
||||||
|
# include "enviroment.h"
|
||||||
|
|
||||||
#endif // MINISHELL_H
|
#endif // MINISHELL_H
|
||||||
@ -1 +1 @@
|
|||||||
Subproject commit 27f7d3e9e02cccf7092f3101b939d381198a595b
|
Subproject commit 716aeca641571c4880639eaa3994bdaeec5b8f0f
|
||||||
24
src/enviroment/add_enviroment.c
Normal file
24
src/enviroment/add_enviroment.c
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/enviroment/free_enviroment.c
Normal file
20
src/enviroment/free_enviroment.c
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/enviroment/get_enviroment.c
Normal file
15
src/enviroment/get_enviroment.c
Normal 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;
|
||||||
|
}
|
||||||
11
src/enviroment/print_enviroment.c
Normal file
11
src/enviroment/print_enviroment.c
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
101
src/prompt.c
101
src/prompt.c
@ -1,88 +1,33 @@
|
|||||||
|
#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)
|
void print_prompt(void)
|
||||||
{
|
{
|
||||||
char *cwd = getcwd(NULL, 0);
|
char *cwd = getcwd(NULL, 0);
|
||||||
if (cwd == NULL)
|
if (cwd == NULL)
|
||||||
{
|
{
|
||||||
perror("getcwd");
|
perror("getcwd");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
printf("%s$ ", cwd);
|
printf("%s$ ", cwd);
|
||||||
free(cwd);
|
free(cwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv, char **envp)
|
int main(int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
char **env;
|
char **env;
|
||||||
t_enviroment *enviroment = NULL;
|
t_enviroment *enviroment = NULL;
|
||||||
|
|
||||||
while (*envp != NULL)
|
while (*envp != NULL)
|
||||||
{
|
{
|
||||||
env = ft_split(*envp, '=');
|
env = ft_split(*envp, '=');
|
||||||
add_enviroment(&enviroment, env[0], env[1]);
|
add_enviroment(&enviroment, env[0], env[1]);
|
||||||
envp++;
|
ft_free_arr(env);
|
||||||
}
|
envp++;
|
||||||
|
}
|
||||||
|
|
||||||
print_enviroment(enviroment);
|
print_enviroment(enviroment);
|
||||||
return 0;
|
free_enviroment(enviroment);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user