diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..38ed16e --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/Makefile b/Makefile index 304bd12..cc40639 100644 --- a/Makefile +++ b/Makefile @@ -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)) @@ -31,13 +31,11 @@ DEPENDS = ${OBJECTS:.o=.d} CC = cc RM = rm -rf -INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -CFLAGS = -Wall -Wextra -Werror -MMD +INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) +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) diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..5f334a1 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,4 @@ +-I +./inc +-I +./lib/libft/inc diff --git a/inc/enviroment.h b/inc/enviroment.h new file mode 100644 index 0000000..5facf9e --- /dev/null +++ b/inc/enviroment.h @@ -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 diff --git a/inc/minishell.h b/inc/minishell.h index 86b4cac..752f279 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -1,147 +1,147 @@ /** * @file minishell.h - * + * * @brief Header file for the Minishell project. - * + * * This header file includes all necessary libraries and headers required for the Minishell project. * The following is a list of included headers and the functions or system calls they provide: - * + * * @headerfile - * - int printf(const char *format, ...); + * - int printf(const char *format, ...); * - Prints formatted data to stdout. - * - void perror(const char *s); + * - void perror(const char *s); * - Prints a descriptive error message to stderr. - * + * * @headerfile - * - void* malloc(size_t size); + * - void* malloc(size_t size); * - Allocates memory dynamically. - * - void free(void *ptr); + * - void free(void *ptr); * - Frees dynamically allocated memory. - * - void exit(int status); + * - void exit(int status); * - Terminates the calling process. - * - char* getenv(const char *name); + * - char* getenv(const char *name); * - Retrieves the value of an environment variable. - * + * * @headerfile - * - ssize_t write(int fd, const void *buf, size_t count); + * - ssize_t write(int fd, const void *buf, size_t count); * - Writes data to a file descriptor. - * - int access(const char *pathname, int mode); + * - int access(const char *pathname, int mode); * - Checks user's permissions for a file. - * - ssize_t read(int fd, void *buf, size_t count); + * - ssize_t read(int fd, void *buf, size_t count); * - Reads data from a file descriptor. - * - int close(int fd); + * - int close(int fd); * - Closes a file descriptor. - * - pid_t fork(void); + * - pid_t fork(void); * - Creates a new process. - * - char* getcwd(char *buf, size_t size); + * - char* getcwd(char *buf, size_t size); * - Gets the current working directory. - * - int chdir(const char *path); + * - int chdir(const char *path); * - Changes the current working directory. - * - int unlink(const char *pathname); + * - int unlink(const char *pathname); * - Deletes a name from the filesystem. - * - int execve(const char *pathname, char *const argv[], char *const envp[]); + * - int execve(const char *pathname, char *const argv[], char *const envp[]); * - Executes a program. - * - int dup(int oldfd); + * - int dup(int oldfd); * - Duplicates a file descriptor. - * - int dup2(int oldfd, int newfd); + * - int dup2(int oldfd, int newfd); * - Duplicates a file descriptor to a specific descriptor number. - * - int pipe(int pipefd[2]); + * - int pipe(int pipefd[2]); * - Creates a pipe. - * - int isatty(int fd); + * - int isatty(int fd); * - Checks if a file descriptor refers to a terminal. - * - char* ttyname(int fd); + * - char* ttyname(int fd); * - Returns the name of the terminal associated with a file descriptor. - * - int ttyslot(void); + * - int ttyslot(void); * - Returns the index of the current terminal. - * + * * @headerfile - * - char* strerror(int errnum); + * - char* strerror(int errnum); * - Returns a string describing the error number. - * + * * @headerfile * - Required for various system calls and data types. - * + * * @headerfile - * - pid_t wait(int *wstatus); + * - pid_t wait(int *wstatus); * - Waits for a child process to change state. - * - pid_t waitpid(pid_t pid, int *wstatus, int options); + * - pid_t waitpid(pid_t pid, int *wstatus, int options); * - Waits for a specific child process to change state. - * - pid_t wait3(int *wstatus, int options, struct rusage *rusage); + * - pid_t wait3(int *wstatus, int options, struct rusage *rusage); * - Waits for a child process to change state with resource usage information. - * - pid_t wait4(pid_t pid, int *wstatus, int options, struct rusage *rusage); + * - pid_t wait4(pid_t pid, int *wstatus, int options, struct rusage *rusage); * - Waits for a specific child process to change state with resource usage information. - * + * * @headerfile - * - int open(const char *pathname, int flags, ...); + * - int open(const char *pathname, int flags, ...); * - Opens a file. - * + * * @headerfile * - Provides access to the errno variable. - * + * * @headerfile - * - void (*signal(int signum, void (*handler)(int)))(int); + * - void (*signal(int signum, void (*handler)(int)))(int); * - Sets a signal handler. - * - int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); + * - int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); * - Examines and changes a signal action. - * - int sigemptyset(sigset_t *set); + * - int sigemptyset(sigset_t *set); * - Initializes a signal set to empty. - * - int sigaddset(sigset_t *set, int signum); + * - int sigaddset(sigset_t *set, int signum); * - Adds a signal to a signal set. - * - int kill(pid_t pid, int sig); + * - int kill(pid_t pid, int sig); * - Sends a signal to a process. - * + * * @headerfile - * - char* readline(const char *prompt); + * - char* readline(const char *prompt); * - Reads a line of input from the user. - * - void rl_clear_history(void); + * - void rl_clear_history(void); * - Clears the history list. - * - void rl_on_new_line(void); + * - void rl_on_new_line(void); * - Informs the readline library that the cursor is on a new line. - * - void rl_replace_line(const char *text, int clear_undo); + * - void rl_replace_line(const char *text, int clear_undo); * - Replaces the contents of the current line. - * - void rl_redisplay(void); + * - void rl_redisplay(void); * - Redisplays the current line. - * - void add_history(const char *line); + * - void add_history(const char *line); * - Adds a line to the history list. - * + * * @headerfile - * - DIR* opendir(const char *name); + * - DIR* opendir(const char *name); * - Opens a directory stream. - * - struct dirent* readdir(DIR *dirp); + * - struct dirent* readdir(DIR *dirp); * - Reads a directory entry. - * - int closedir(DIR *dirp); + * - int closedir(DIR *dirp); * - Closes a directory stream. - * + * * @headerfile - * - int stat(const char *pathname, struct stat *statbuf); + * - int stat(const char *pathname, struct stat *statbuf); * - Gets file status. - * - int lstat(const char *pathname, struct stat *statbuf); + * - int lstat(const char *pathname, struct stat *statbuf); * - Gets file status, not following symbolic links. - * - int fstat(int fd, struct stat *statbuf); + * - int fstat(int fd, struct stat *statbuf); * - Gets file status for an open file descriptor. - * + * * @headerfile - * - int ioctl(int fd, unsigned long request, ...); + * - int ioctl(int fd, unsigned long request, ...); * - Manipulates the underlying device parameters of special files. - * + * * @headerfile - * - int tcsetattr(int fd, int optional_actions, const struct termios *termios_p); + * - int tcsetattr(int fd, int optional_actions, const struct termios *termios_p); * - Sets the parameters associated with the terminal. - * - int tcgetattr(int fd, struct termios *termios_p); + * - int tcgetattr(int fd, struct termios *termios_p); * - Gets the parameters associated with the terminal. - * + * * @headerfile - * - int tgetent(char *bp, const char *name); + * - int tgetent(char *bp, const char *name); * - Loads the terminal entry for a given terminal name. - * - int tgetflag(const char *id); + * - int tgetflag(const char *id); * - Gets the boolean value of a terminal capability. - * - int tgetnum(const char *id); + * - int tgetnum(const char *id); * - Gets the numeric value of a terminal capability. - * - char* tgetstr(const char *id, char **area); + * - char* tgetstr(const char *id, char **area); * - Gets the string value of a terminal capability. - * - char* tgoto(const char *cap, int col, int row); + * - char* tgoto(const char *cap, int col, int row); * - Computes a cursor movement string. - * - int tputs(const char *str, int affcnt, int (*putc)(int)); + * - int tputs(const char *str, int affcnt, int (*putc)(int)); * - Outputs a string with padding. */ #ifndef MINISHELL_H @@ -162,4 +162,10 @@ # include # include -#endif // MINISHELL_H \ No newline at end of file +# include +# include + +# include "libft.h" +# include "enviroment.h" + +#endif // MINISHELL_H diff --git a/lib/libft b/lib/libft index 27f7d3e..716aeca 160000 --- a/lib/libft +++ b/lib/libft @@ -1 +1 @@ -Subproject commit 27f7d3e9e02cccf7092f3101b939d381198a595b +Subproject commit 716aeca641571c4880639eaa3994bdaeec5b8f0f diff --git a/src/enviroment/add_enviroment.c b/src/enviroment/add_enviroment.c new file mode 100644 index 0000000..7cf4fd7 --- /dev/null +++ b/src/enviroment/add_enviroment.c @@ -0,0 +1,24 @@ +#include "enviroment.h" +#include "libft.h" +#include +#include + + +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; + } +} diff --git a/src/enviroment/free_enviroment.c b/src/enviroment/free_enviroment.c new file mode 100644 index 0000000..cbdb89f --- /dev/null +++ b/src/enviroment/free_enviroment.c @@ -0,0 +1,20 @@ +#include "enviroment.h" +#include + + +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; + } +} diff --git a/src/enviroment/get_enviroment.c b/src/enviroment/get_enviroment.c new file mode 100644 index 0000000..d14b039 --- /dev/null +++ b/src/enviroment/get_enviroment.c @@ -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; +} diff --git a/src/enviroment/print_enviroment.c b/src/enviroment/print_enviroment.c new file mode 100644 index 0000000..f3fdac3 --- /dev/null +++ b/src/enviroment/print_enviroment.c @@ -0,0 +1,11 @@ +#include "enviroment.h" +#include + +void print_enviroment(t_enviroment *enviroment) +{ + while (enviroment != NULL) + { + printf("%s=%s\n", enviroment->name, enviroment->value); + enviroment = enviroment->next; + } +} diff --git a/src/prompt.c b/src/prompt.c index c67e98f..5f32aed 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -1,88 +1,33 @@ +#include "minishell.h" -#include -#include -#include -#include -#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); - if (cwd == NULL) - { - perror("getcwd"); - return; - } - printf("%s$ ", cwd); - free(cwd); + char *cwd = getcwd(NULL, 0); + if (cwd == NULL) + { + perror("getcwd"); + return; + } + printf("%s$ ", cwd); + free(cwd); } int main(int argc, char **argv, char **envp) { - (void)argc; - (void)argv; - char **env; - t_enviroment *enviroment = NULL; + (void)argc; + (void)argv; + char **env; + t_enviroment *enviroment = NULL; - while (*envp != NULL) - { - env = ft_split(*envp, '='); - add_enviroment(&enviroment, env[0], env[1]); - envp++; - } + while (*envp != NULL) + { + env = ft_split(*envp, '='); + add_enviroment(&enviroment, env[0], env[1]); + ft_free_arr(env); + envp++; + } - print_enviroment(enviroment); - return 0; -} \ No newline at end of file + print_enviroment(enviroment); + free_enviroment(enviroment); + return 0; +}