Skip to content
Open
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
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/minishell",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}

]
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"string": "c",
"string_view": "c",
"vector": "c",
"minishell.h": "c"
"minishell.h": "c",
"locale": "c"
}
}
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,44 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: jalwahei <jalwahei@student.42.fr> +#+ +:+ +#+ #
# By: hbui-vu <hbui-vu@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/02/09 11:48:59 by jalwahei #+# #+# #
# Updated: 2023/04/01 07:07:55 by jalwahei ### ########.fr #
# Updated: 2023/04/11 14:49:49 by hbui-vu ### ########.fr #
# #
# **************************************************************************** #

NAME = minishell
SRCS = minishell.c parse/ts_signals.c parse/ts_malloc.c parse/ts_count_record_cmd.c parse/parse_pipe.c parse/ts_find_redirect.c parse/ts_error.c \
parse/ts_free.c parse/ts_record_val.c ts_utils.c parse/ts_quotation_marks.c parse/ts_record_arr.c parse/ts_measure_size_file_name.c parse/ts_init_env.c \
parse/ts_found_dollar.c utils.c \
builtin/builtin.c builtin/cd.c builtin/echo.c builtin/env.c builtin/exit.c builtin/export.c builtin/pwd.c builtin/unset.c \
env_var.c utils_h.c pipex.c init_data.c heredoc.c testers.c

CC = cc

CFLAGS = -I. -g -fsanitize=address -Wall -Wextra
CFLAGS = -I. -g # -fsanitize=address -Wall -Wextra

OBJS = $(SRCS:.c=.o)

all: $(NAME)

lib = libft/libft.a
lib = libft/libft.a libft_h/libft_h.a

$(NAME): $(OBJS)
@make -C libft
@make -C libft_h
@$(CC) $(CFLAGS) $(OBJS) $(lib) -I/usr/local/opt/readline/include -L/usr/local/opt/readline/lib -l readline -o $(NAME)
@echo "\033[35mTinyshell Ready\033[0m"

clean:
@rm -f $(OBJS)
@make clean -C libft
@make clean -C libft_h

fclean: clean
@rm -f $(NAME)
@make fclean -C libft
@make fclean -C libft_h

re: fclean all
86 changes: 86 additions & 0 deletions builtin/builtin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

#include "../minishell.h"

char *lower_str(char *str, t_data *data)
{
int i;
char *low_str;

i = 0;
low_str = (char *)ft_calloc_e(ft_strlen(str) + 1, sizeof(char), data);
while (str[i])
{
if (str[i] >= 'A' && str[i] <= 'Z')
low_str[i] = str[i] + 32;
else
low_str[i] = str[i];
i++;
}
return (low_str);
}

int check_builtin(char *str, t_data *data)
{
char *command;
int i;

command = lower_str(str, data);
i = 0;
if (ft_strncmp(command, "echo", ft_strlen(command)) == 0)
i = 1;
else if (ft_strncmp(command, "cd", ft_strlen(command)) == 0)
i = 2;
else if (ft_strncmp(command, "pwd", ft_strlen(command)) == 0)
i = 3;
else if (ft_strncmp(command, "export", ft_strlen(command)) == 0)
i = 4;
else if (ft_strncmp(command, "unset", ft_strlen(command)) == 0)
i = 5;
else if (ft_strncmp(command, "env", ft_strlen(command)) == 0)
i = 6;
else if (ft_strncmp(command, "exit", ft_strlen(command)) == 0)
i = 7;
free(command);
return (i);
}

void execute_builtin(char **arg, int i, t_data *data)
{
if (i == 1)
ft_echo(arg, data);
else if (i == 2)
ft_cd(arg, data);
else if (i == 3)
ft_pwd(data);
else if (i == 4)
ft_export(arg, data);
else if (i == 5)
ft_unset(arg, data);
else if (i == 6)
ft_env(data);
// else if (i == 7)
// ft_exit(data);
exit(0);
}

// void execute_builtin(char **arg, int i, t_data *data)
// {
// char *command;

// command = lower_str(arg[0], data);
// if (ft_strncmp(command, "echo", ft_strlen(command)) == 0)
// ft_echo(arg, data);
// else if (ft_strncmp(command, "cd", ft_strlen(command)) == 0)
// ft_cd(arg, data);
// else if (ft_strncmp(command, "pwd", ft_strlen(command)) == 0)
// ft_pwd(data);
// else if (ft_strncmp(command, "export", ft_strlen(command)) == 0)
// ft_export(arg, data);
// else if (ft_strncmp(command, "unset", ft_strlen(command)) == 0)
// ft_unset(arg, data);
// else if (ft_strncmp(command, "env", ft_strlen(command)) == 0)
// ft_env(data);
// // else if (ft_strncmp(command, "exit", ft_strlen(command)) == 0)
// // ft_exit(data);
// free(command);
// }
Binary file added builtin/builtin.o
Binary file not shown.
42 changes: 42 additions & 0 deletions builtin/cd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "../minishell.h"
//change directory display??????

void ft_cd(char **arg, t_data *data)
{
char *curr_dir;
char *old_dir;
t_env *pwd_var;
t_env *old_pwd_var;

//if no arguments -> go to home
if (arg[1] == NULL)
chdir(data->home_dir);
//change directories
else
{
//execute chdir and if it errors out that write error code
if (chdir(arg[1]) != 0)
{
printf("-bash: cd: %s No such file or directory\n", arg[1]);
error(data);
}
}
//copy over previous directory
pwd_var = find_var_envlist("PWD", data);
old_dir = ft_strdup_lim(pwd_var->val, '\0', data);
//find oldpwd if it exists
old_pwd_var = find_var_envlist("OLDPWD", data);
//if oldpwd doesn't exist, add env; else, free old value and create new value. also modify in our_env
if (old_pwd_var == NULL)
add_env_var("OLDPWD", old_dir, data);
else
{
free(old_pwd_var->val);
old_pwd_var->val = old_dir;
modify_our_env(old_pwd_var, data);
}
//change pwd var
curr_dir = getcwd(NULL, 0);
pwd_var->val = curr_dir;
modify_our_env(pwd_var, data);
}
Binary file added builtin/cd.o
Binary file not shown.
31 changes: 31 additions & 0 deletions builtin/echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "../minishell.h"

void ft_echo(char **arg, t_data *data)
{
int i;
int nl;

//check for -n flag. if there is, start the printing at index 2, else start at index 1
if (arg[1] && ft_strncmp(arg[1], "-n", 3) == 0)
{
i = 2;
nl = 0;
}
else
{
i = 1;
nl = 1;
}
//loop through args and print
while (arg[i])
{
if (arg[i + 1] != NULL)
print_string(2, data, arg[i], " ");
else if (arg[i + 1] == NULL)
print_string(1, data, arg[i]);
i++;
}
//if no -n, also print /n after the last string is printed
if (nl == 1)
print_string(1, data, "\n");
}
Binary file added builtin/echo.o
Binary file not shown.
15 changes: 15 additions & 0 deletions builtin/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "../minishell.h"

void ft_env(t_data *data)
{
char **our_env;
int i;

our_env = data->our_env;
i = 0;
while(our_env[i])
{
print_string(2, data, our_env[i], "\n");
i++;
}
}
Binary file added builtin/env.o
Binary file not shown.
6 changes: 6 additions & 0 deletions builtin/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "../minishell.h"

//return last exit status
//exit out of shell
//if more than one argument, do not exit and send an error message
void ft_exit(t_data *data);
Binary file added builtin/exit.o
Binary file not shown.
88 changes: 88 additions & 0 deletions builtin/export.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "../minishell.h"

/* print export */
void print_export(t_data *data)
{
t_env *cur;
t_env *to_print;

while (1)
{
//find the first key that hasn't been printed
cur = *(data->env_list);
while (cur && cur->p == 1)
cur = cur->next;
//if everything has been printed, break out of loop
if (cur == NULL)
break ;
//iterate through list to find the next variable to be printed
to_print = cur;
cur = cur->next;
while (cur != NULL)
{
if (ft_strncmp(to_print->key, cur->key, ft_strlen(to_print->key) + 1) > 0 && cur->p == 0)
to_print = cur;
cur = cur->next;
}
//mark p as 1 (printed) and print key
to_print->p = 1;
print_string(3, data, "declare -x ", to_print->key, "=");
if (to_print->val == NULL)
print_string(1, data, "''\n");
else
print_string(2, data, to_print->val, "\n");
}
//reset all p to 0;
cur = *(data->env_list);
while (cur != NULL)
{
cur->p = 0;
cur = cur->next;
}
}

/* export variables */
void ft_export(char **arg, t_data *data)
{
t_env *env_var;
char **split_arg;
int i;

i = 1;
//if only export, print export
if (!arg[i])
{
print_export(data);
return ;
}
if (data->num_cmd <= 1)
return ;
while (arg[i])
{
//if no '=', add var if it doesn't exist, otherwise do nothing
//if '=', change value if var exists, otherwise add var and value
if (!detect_char(arg[i], '='))
{
if (!find_var_envlist(arg[i], data))
add_env_var(arg[i], NULL, data);
}
else
{
split_arg = split_env_var(arg[i], data);
env_var = find_var_envlist(split_arg[0], data);
if (!env_var)
add_env_var(split_arg[0], split_arg[1], data);
else
{
env_var->val = ft_strdup_lim(split_arg[1], '\0', data);
modify_our_env(env_var, data);
}
free_strlist(split_arg);
}
i++;
}
}




Binary file added builtin/export.o
Binary file not shown.
12 changes: 12 additions & 0 deletions builtin/pwd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "../minishell.h"

void ft_pwd(t_data *data)
{
char *cwd;

cwd = getcwd(NULL, 0);
if (!cwd)
error(data);
print_string(2, data, cwd, "\n");
free(cwd);
}
Binary file added builtin/pwd.o
Binary file not shown.
Loading