Skip to content

Commit

Permalink
Merge pull request #80 from facetint/main
Browse files Browse the repository at this point in the history
merge with main
  • Loading branch information
facetint authored Mar 30, 2024
2 parents ea231bd + 3377893 commit 47a0602
Show file tree
Hide file tree
Showing 26 changed files with 506 additions and 523 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ LIBFT_DIR = ./libft
LIBFT_PATH = $(LIBFT_DIR)/libft.a

CC = gcc
FLAGS = -g -Wall -Wextra -Werror -fsanitize=address
FLAGS = -g -Wall -Wextra -Werror

MEMORY_ALLOCATOR_SOURCES = memory-allocator/aborter.c memory-allocator/allocator.c
SOURCES = src/execute/execute_utils.c src/builtin/cd.c src/builtin/exit.c src/builtin/export.c src/builtin/echo.c \
src/builtin/env.c src/builtin/pwd.c src/builtin/builtin.c src/builtin/unset.c get_next_line/get_next_line.c \
src/expander/expander_nonvariables.c src/env/env_utils.c src/lexer/utils.c src/handler.c src/env/env.c\
SOURCES = src/execute/execute_utils.c src/builtin/cd.c src/builtin/exit.c src/builtin/export.c src/builtin/export_utils.c \
src/builtin/env.c src/builtin/pwd.c src/builtin/echo.c src/builtin/builtin.c src/builtin/unset.c src/expander/expander_nonvariables.c \
src/env/env_utils.c src/lexer/utils.c src/handler.c src/env/env.c\
src/lexer/lexer.c src/lexer/unquote.c src/lexer/lexer_utils.c src/execute/execute.c \
src/lexer/lexer_error_message.c src/lexer/is_valid.c src/execute/error_message.c src/execute/dup2.c \
src/lexer/lexer_error_message.c src/lexer/is_valid.c src/execute/error_message.c src/execute/fd_utils.c \
src/parser/parser.c src/parser/parser_state.c src/parser/parser_utils.c src/execute/heredoc.c \
src/expander/expander.c src/splitter.c src/lexer/syntax_analyzer.c src/signal.c $(MEMORY_ALLOCATOR_SOURCES) \
src/redirections/redirections.c
src/redirections/redirections.c src/env/global_env.c src/unsafe_utils/unsafe_utils.c

MINISHELL_SOURCES = src/main.c $(SOURCES)
MINISHELL_OBJECTS = $(MINISHELL_SOURCES:.c=.o)
Expand Down
54 changes: 0 additions & 54 deletions get_next_line/get_next_line.c

This file was deleted.

14 changes: 0 additions & 14 deletions get_next_line/get_next_line.h

This file was deleted.

1 change: 1 addition & 0 deletions includes/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ t_list *create_node(char *key, char *value);
void free_list(t_list *lst);
char *find_env(char *key);
void unset_env(char *varname);
t_list *find_node(t_list *env, char *key);
#endif
30 changes: 23 additions & 7 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ typedef struct s_token
struct s_token *next;
} t_token;

typedef struct s_file_descriptors
{
int inp_fd;
int out_fd;
int *prev_p;
int *next_p;
} t_file_descriptors;

#define LEXER_STATE_FUNCTION_PARAMETERS t_token **lexer_data, char *input, int *const i

Expand Down Expand Up @@ -96,6 +103,9 @@ void expand(t_token **head);
void internal_field_split(t_token **token);
void insert_uword_tokens(t_token **token_ptr, char **strings);
void expand_string(char **string);
char *replace_string(char *input, int p_start, int p_len, char *replacement);
int is_empty_variable(t_token *token);


// parser
t_command *parse(t_token *lexer_data);
Expand All @@ -121,19 +131,25 @@ void execute(t_command *cmds);
void handle_command(t_command *cmd, int *prev_p, int *next_p);
char *find_path(char *cmd);
void pid_error(int *prev_pipe, int *next_pipe);
void close_fds(int inp_fd, int out_fd, int *prev_p, int *next_p);
void path_error(t_command *cmd);
void close_fds(t_file_descriptors fds);
void close_redirections(t_file_descriptors fds);
int get_input_fd(int *pipe, t_command *cmd);
int get_output_fd(int *pipe, t_command *cmd);
void close_pipe(int *pipe);

void path_error(char *cmd);

// builtin

void execute_builtin(t_command *cmd, int fd[2]);
void execute_builtin(t_command *cmd, int fd[2]);
int isbuiltin(char *cmd);
void builtin_exit(t_command *cmd);
void builtin_echo(t_command *cmd, int fd[2]);
void builtin_pwd(t_command *cmd);
void builtin_export(t_command *cmd, int fd[2]);
void builtin_unset(t_command *cmd, int fd[2]);
int ft_strcmp(char *s1, char *s2);
void builtin_pwd(t_command *cmd);
void builtin_export(t_command *cmd, int fd[2]);
void builtin_unset(t_command *cmd, int fd[2]);
int ft_strcmp(char *s1, char *s2);
int should_run_in_child(t_command *cmd);

// cd
void builtin_cd(t_command *cmd);
Expand Down
7 changes: 4 additions & 3 deletions libft/ft_lstnew.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hcoskun <hcoskun@student.42.fr> +#+ +:+ +#+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/08 16:13:30 by hcoskun #+# #+# */
/* Updated: 2024/03/16 14:25:05 by hcoskun ### ########.fr */
/* Updated: 2024/03/30 17:44:24 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"
#include "../memory-allocator/allocator.h"

t_list *ft_lstnew(void *content)
{
t_list *node;

node = (t_list *) malloc(sizeof(t_list));
node = (t_list *) safe_malloc(sizeof(t_list));
if (!node)
return (NULL);
node -> content = content;
Expand Down
4 changes: 2 additions & 2 deletions memory-allocator/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* allocator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hamza <hamza@student.42.fr> +#+ +:+ +#+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/16 15:13:21 by hamza #+# #+# */
/* Updated: 2024/03/25 15:35:20 by hamza ### ########.fr */
/* Updated: 2024/03/30 17:46:06 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
172 changes: 80 additions & 92 deletions src/builtin/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 11:48:16 by facetint #+# #+# */
/* Updated: 2024/03/25 14:56:13 by facetint ### ########.fr */
/* Updated: 2024/03/30 17:40:13 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,114 +17,102 @@
#include "../../includes/env.h"
#include <stdlib.h>

void change_old(char *str)
void change_old(char *str)
{
t_list *env;
t_entry *node;
t_list *env;
t_entry *node;

env = *get_global_env();
while (env)
{
node = env->content;
if (!ft_strcmp(node->key, "OLDPWD"))
{
env = *get_global_env();
while (env)
{
node = env->content;
if (!ft_strcmp(node->key, "OLDPWD"))
{
if (node->value)
free(node->value);
node->value = str;
break;
}
env = env->next;
}
node->value = str;
break ;
}
env = env->next;
}
}

char *ft_unsafe_strdup(const char *str)
void change_pwd(t_command *cmd)
{
char *res;
size_t size;
t_list *env;
t_entry *node;

size = ft_strlen(str) + 1;
res = malloc(sizeof(char) * size);
if (!res)
return (NULL);
ft_strlcpy(res, str, size);
return (res);
}

void change_pwd(t_command *cmd)
{
t_list *env;
t_entry *node;

env = *get_global_env();
while (env)
{
node = env->content;
if (!ft_strcmp(node->key, "PWD") && (!cmd->args[1] || ft_strcmp(cmd->args[1], "~") == 0))
{
env = *get_global_env();
while (env)
{
node = env->content;
if (!ft_strcmp(node->key, "PWD") && (!cmd->args[1]
|| ft_strcmp(cmd->args[1], "~") == 0))
{
if (node->value)
free(node->value);
node->value = ft_unsafe_strdup(find_env("HOME"));
}
else if (!ft_strcmp(node->key, "PWD"))
{
node->value = ft_unsafe_strdup(find_env("HOME"));
}
else if (!ft_strcmp(node->key, "PWD"))
{
if (node->value)
free(node->value);
node->value = malloc(sizeof(char) * 4097); // todo abort if malloc fail
if (getcwd(node->value, 4097) == NULL)
{
perror("getcwd");
return;
}
break;
}
env = env->next;
}
node->value = safe_malloc(sizeof(char) * 4097); // todo
if (getcwd(node->value, 4097) == NULL)
{
perror("getcwd");
return ;
}
break ;
}
env = env->next;
}
}

void execute_cd(char *str, t_command *cmd)
void execute_cd(char *str, t_command *cmd)
{
char *get_home;
char *get_home;

change_old(str);
chdir(find_env("HOME"));
perror("chdir");
get_home = find_env("HOME");
if(!get_home)
{
ft_putstr_fd("cd: HOME not set\n", 2);
*get_exit_status() = 1;
return;
}
change_pwd(cmd);
*get_exit_status() = 0;
change_old(str);
chdir(find_env("HOME"));
perror("chdir");
get_home = find_env("HOME");
if (!get_home)
{
ft_putstr_fd("cd: HOME not set\n", 2);
*get_exit_status() = 1;
return ;
}
change_pwd(cmd);
*get_exit_status() = 0;
}

void builtin_cd(t_command *cmd)
void builtin_cd(t_command *cmd)
{
char *str;
str = malloc(sizeof(char) * 4097); // todo abort if malloc fail
if (getcwd(str, 4097) == NULL)
{
perror("getcwd");
return;
}
if (cmd->args[1])
{
if (chdir(cmd->args[1]) == 0)
{
change_old(str);
change_pwd(cmd);
*get_exit_status() = 0;
}
else
{
if (str)
free(str);
*get_exit_status() = 1;
perror("cd");
}
}
else
execute_cd(str, cmd);
char *str;

str = malloc(sizeof(char) * 4097); // todo
if (getcwd(str, 4097) == NULL)
{
perror("getcwd");
return ;
}
if (cmd->args[1])
{
if (chdir(cmd->args[1]) == 0)
{
change_old(str);
change_pwd(cmd);
*get_exit_status() = 0;
}
else
{
if (str)
free(str);
*get_exit_status() = 1;
perror("cd");
}
}
else
execute_cd(str, cmd);
}
Loading

0 comments on commit 47a0602

Please sign in to comment.