Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final #29

Merged
merged 9 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"system_error": "c",
"functional": "c",
"tuple": "c",
"utility": "c"
"utility": "c",
"types.h": "c",
"structs.h": "c"
}
}
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NAME = minishell
SRC = main \
utils/error \
utils/error_extra \
utils/miscellaneous \
structs/token \
structs/env \
Expand Down Expand Up @@ -28,6 +29,7 @@ SRC = main \
built_in/export \
built_in/echo \
built_in/cd \
built_in/cd_err \
built_in/utils \
built_in/unset \
built_in/env \
Expand All @@ -44,7 +46,7 @@ LIBFT = libft/libft.a

DEBUG ?= 0
DEBUG_FLAGS = -g
CODAM_FLAGS = -Wall -Wextra -Werror
CODAM_FLAGS = -Wall -Wextra -Werror -fsanitize=address
LINKER = -lreadline
INCLUDES = -I $(CURDIR)/includes -I $(CURDIR)/libft/includes

Expand Down
9 changes: 6 additions & 3 deletions includes/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */
/* Updated: 2023/11/20 20:59:54 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/23 14:16:14 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -56,7 +56,10 @@ typedef enum e_exit
MALLOC,
NOT_FOUND,
PERMISSION,
PERMISSION_CMD,
NO_SUCH,
NO_SUCH_CMD,
IS_DIRECT,
SYNTAX,
SYNTAX_MINI,
SIGNAL_C,
Expand All @@ -68,11 +71,11 @@ typedef enum e_error
NO_ERROR = 0,
CATCH_ALL = 1,
MISUSE_OF_SHELL = 2,
PERMISSION_DENIED = 126,
PERMISSION_DENIED = 1,
COMMAND_NOT_FOUND = 127,
NOT_VALID = 128,
UNEXPECTED_EOF = 130,
SYNTAX_ERROR = 258,
SYNTAX_ERROR = 2,
} t_error;

/**
Expand Down
7 changes: 5 additions & 2 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */
/* Updated: 2023/11/22 14:20:29 by mdekker ######## odam.nl */
/* Updated: 2023/11/23 14:15:44 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -24,6 +24,7 @@
# include <stdio.h>
# include <stdlib.h>
# include <structs.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <unistd.h>
Expand Down Expand Up @@ -102,7 +103,7 @@ char **ft_export_split(char *src, char delimter);
size_t **return_sorted_arr(t_vector *env);
void ft_free_size_t(size_t **arr, size_t len);
char *string_handler(char *input);

void write_err_cd(t_shell *data, size_t option, char *path, char *extra);
/* signals */
void signal_main(int signal_num);
void setup_hdoc_signals(void);
Expand All @@ -128,7 +129,9 @@ bool compare_env_key(void *item, void *key);
void exit_mini(char *str, int exit_code);
bool set_err(t_exit type, char *msg, t_shell *data);
void exec_err(char *str, t_exit type);
void exec_err_extra(char *str, t_exit type);
void write_err(t_shell *data);
void write_err_extra(t_shell *data);
bool rm_quotes(t_token *token, bool set_string);
bool type_compare(size_t num_args, t_types type, ...);
bool filter_operators(void *item);
Expand Down
2 changes: 1 addition & 1 deletion includes/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */
/* Updated: 2023/11/22 15:20:21 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/23 14:15:54 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down
66 changes: 30 additions & 36 deletions src/built_in/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/14 17:29:00 by mdekker #+# #+# */
/* Updated: 2023/11/21 15:32:46 by mdekker ######## odam.nl */
/* Updated: 2023/11/23 15:12:58 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

/**
* @brief Updates the value of an environment variable when it already exists
* @param env_var The env struct
* @param key The key to compare to
* @return void
*/
static void update_env(t_shell *data, char *oldpwd)
{
char *buff;

buff = getcwd(NULL, 0);
update_or_create_env(&data->env, "OLDPWD", oldpwd);
update_or_create_env(&data->env, "PWD", buff);
free(buff);
}

/**
* @brief Helper function to safely get an environment variable
* @param *env The environment vector
Expand All @@ -44,6 +28,26 @@ static char *safe_env_get(t_vector *env, char *key)
return (NULL);
}

/**
* @brief Updates the value of an environment variable when it already exists
* @param env_var The env struct
* @param key The key to compare to
* @return void
*/
static void update_env(t_shell *data)
{
char *buff;
char *env;

env = safe_env_get(&data->env, "PWD");
if (!env)
return ;
buff = getcwd(NULL, 0);
update_or_create_env(&data->env, "OLDPWD", env);
update_or_create_env(&data->env, "PWD", buff);
free(buff);
}

/**
* @brief Prints the error message for cd
* @param path The path that caused the error
Expand All @@ -53,9 +57,11 @@ static bool error_check(t_shell *data, t_group *group)
{
char *path;

if (!group->args[1])
return (true);
if (group->args[2])
{
printf("cd: too many arguments\n");
write(2, "minishell: cd: too many arguments\n", 35);
data->error_type = CATCH_ALL;
return (false);
}
Expand All @@ -64,12 +70,11 @@ static bool error_check(t_shell *data, t_group *group)
path = safe_env_get(&data->env, "OLDPWD");
if (!path)
{
printf("cd: OLDPWD not set\n");
write(2, "minishell: cd: OLDPWD not set\n", 31);
data->error_type = CATCH_ALL;
return (false);
}
printf("%s\n", path);
data->error_type = CATCH_ALL;
return (false);
}
return (true);
Expand All @@ -86,12 +91,14 @@ static char *get_path(t_group *group, t_shell *data)
{
char *path;

if (!group->args)
return (NULL);
if (!group->args[1] || !ft_strcmp(group->args[1], "~"))
{
path = ft_strdup(safe_env_get(&data->env, "HOME"));
if (!path)
{
printf("cd: HOME not set\n");
write(2, "minishell: cd: HOME not set\n", 29);
data->error_type = CATCH_ALL;
return (NULL);
}
Expand All @@ -114,28 +121,15 @@ static char *get_path(t_group *group, t_shell *data)
void ft_cd(t_group *group, t_shell *data)
{
char *path;
char *oldpwd;

if (!error_check(data, group))
return ;
oldpwd = getcwd(NULL, 0);
if (!oldpwd)
{
printf("cd: error retrieving current directory: %s\n", strerror(errno));
data->error_type = CATCH_ALL;
return ;
}
path = get_path(group, data);
if (!path)
return ;
if (chdir(path) == -1)
{
printf("cd: %s: %s\n", path, strerror(errno));
data->error_type = CATCH_ALL;
}
return (write_err_cd(data, 2, path, strerror(errno)), free(path));
else
update_env(data, oldpwd);
data->error_type = NO_ERROR;
free(oldpwd);
update_env(data);
free(path);
}
40 changes: 40 additions & 0 deletions src/built_in/cd_err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* cd_err.c :+: :+: */
/* +:+ */
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/22 17:42:21 by mdekker/jde #+# #+# */
/* Updated: 2023/11/22 21:37:32 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

/**
* @brief Prints an error message based on the type of error
* @details If the type is GOOD, nothing is printed
* @details the t_Signal exit_status is set to the correct value
*
* @param data general data struct
*/
void write_err_cd(t_shell *data, size_t option, char *path, char *extra)
{
if (option)
{
write(2, "minishell: cd: ", 16);
write(2, path, ft_strlen(path));
write(2, ": No such file or directory\n", 29);
data->error_type = CATCH_ALL;
}
else if (option == 2)
{
write(2, "minishell: cd: ", 15);
write(2, path, ft_strlen(path));
write(2, ": ", 2);
write(2, extra, ft_strlen(extra));
write(2, "\n", 1);
data->error_type = CATCH_ALL;
}
}
12 changes: 11 additions & 1 deletion src/built_in/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/16 23:12:07 by mdekker/jde #+# #+# */
/* Updated: 2023/11/21 18:15:44 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 22:55:19 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

/**
* @brief The env command built-in
* @details Prints the enviroment variables
*
* @warning This function also sorts the enviroment variables alphabetically
* which is technically not part of the env command but if you want me to
* remove it, I can. I just thought it would be nice to have it sorted.
*
* @param env_vec The enviroment vector
*/
void ft_env(t_vector *env_vec)
{
size_t i;
Expand Down
Loading
Loading