diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..3e27da4 --- /dev/null +++ b/.vscode/launch.json @@ -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" + } + + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index c0ed3a9..b5f8bbe 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ "string": "c", "string_view": "c", "vector": "c", - "minishell.h": "c" + "minishell.h": "c", + "locale": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 2fa8183..351e7ae 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: jalwahei +#+ +:+ +#+ # +# By: hbui-vu +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 # # # # **************************************************************************** # @@ -14,28 +14,33 @@ 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 diff --git a/builtin/builtin.c b/builtin/builtin.c new file mode 100644 index 0000000..f8e6523 --- /dev/null +++ b/builtin/builtin.c @@ -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); +// } \ No newline at end of file diff --git a/builtin/builtin.o b/builtin/builtin.o new file mode 100644 index 0000000..e7f9623 Binary files /dev/null and b/builtin/builtin.o differ diff --git a/builtin/cd.c b/builtin/cd.c new file mode 100644 index 0000000..8df7dbe --- /dev/null +++ b/builtin/cd.c @@ -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); +} \ No newline at end of file diff --git a/builtin/cd.o b/builtin/cd.o new file mode 100644 index 0000000..e725063 Binary files /dev/null and b/builtin/cd.o differ diff --git a/builtin/echo.c b/builtin/echo.c new file mode 100644 index 0000000..460a22d --- /dev/null +++ b/builtin/echo.c @@ -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"); +} diff --git a/builtin/echo.o b/builtin/echo.o new file mode 100644 index 0000000..203cb2c Binary files /dev/null and b/builtin/echo.o differ diff --git a/builtin/env.c b/builtin/env.c new file mode 100644 index 0000000..9367675 --- /dev/null +++ b/builtin/env.c @@ -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++; + } +} \ No newline at end of file diff --git a/builtin/env.o b/builtin/env.o new file mode 100644 index 0000000..58d51fd Binary files /dev/null and b/builtin/env.o differ diff --git a/builtin/exit.c b/builtin/exit.c new file mode 100644 index 0000000..b0d033d --- /dev/null +++ b/builtin/exit.c @@ -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); \ No newline at end of file diff --git a/builtin/exit.o b/builtin/exit.o new file mode 100644 index 0000000..00a8ebf Binary files /dev/null and b/builtin/exit.o differ diff --git a/builtin/export.c b/builtin/export.c new file mode 100644 index 0000000..76e854f --- /dev/null +++ b/builtin/export.c @@ -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++; + } +} + + + + diff --git a/builtin/export.o b/builtin/export.o new file mode 100644 index 0000000..b9477e9 Binary files /dev/null and b/builtin/export.o differ diff --git a/builtin/pwd.c b/builtin/pwd.c new file mode 100644 index 0000000..6a1cddf --- /dev/null +++ b/builtin/pwd.c @@ -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); +} diff --git a/builtin/pwd.o b/builtin/pwd.o new file mode 100644 index 0000000..59021fd Binary files /dev/null and b/builtin/pwd.o differ diff --git a/builtin/unset.c b/builtin/unset.c new file mode 100644 index 0000000..ff73c23 --- /dev/null +++ b/builtin/unset.c @@ -0,0 +1,70 @@ +#include "../minishell.h" + +void free_env_var(t_env *env_var) +{ + if (env_var->key) + free(env_var->key); + if (env_var->val) + free(env_var->val); + free(env_var); +} + +void remove_var(t_env *env_var, t_data *data) +{ + t_env *temp; + + temp = env_var; + if (!env_var->prev) + *data->env_list = env_var->next; + else + env_var->prev->next = env_var->next; + if (env_var->next) + env_var->next->prev = env_var->prev; + free_env_var(temp); +} + +void clear_envlist(t_env **env_list) +{ + t_env *cur; + + cur = *env_list; + while (cur) + { + free(cur->key); + free(cur->val); + *env_list = cur->next; + free(cur); + cur = *env_list; + } + free(env_list); +} + +void ft_unset(char **arg, t_data *data) +{ + int i; + t_env *env_var; + + if (data->num_cmd <= 1) + return ; + i = 1; + if (!arg[i]) + { + clear_envlist(data->env_list); + free_strlist(data->our_env); + } + else + { + + while (arg[i]) + { + env_var = find_var_envlist(arg[i], data); + if (env_var) + remove_var(env_var, data); + i++; + } + modify_our_env(NULL, data); + } +} + + + diff --git a/builtin/unset.o b/builtin/unset.o new file mode 100644 index 0000000..d6dd998 Binary files /dev/null and b/builtin/unset.o differ diff --git a/env_var.c b/env_var.c new file mode 100644 index 0000000..d183015 --- /dev/null +++ b/env_var.c @@ -0,0 +1,126 @@ +#include "minishell.h" + +/* this is mostly for the builtins */ + +//find if a variable exists and return the pointer to the variable if it exists. If not return NULL +t_env *find_var_envlist(char *key, t_data *data) +{ + t_env *env_key; + + env_key = *(data->env_list); + while (env_key) + { + if (ft_strncmp(env_key->key, key, ft_strlen(key) + 1) == 0) + return (env_key); + env_key = env_key->next; + } + return (NULL); +} + +/* find if a variable exists in our_env. Return index if it exists, -1 if it doesn't */ +int find_var_ourenv(char *key, t_data *data) +{ + int i; + char *temp_key; + + i = 0; + while (data->our_env[i]) + { + temp_key = ft_strdup_lim(data->our_env[i], '=', data); + if (ft_strncmp(temp_key, key, ft_strlen(temp_key)) == 0) + { + free(temp_key); + return (i); + } + free(temp_key); + i++; + } + free(temp_key); + return (-1); +} + +/* create new entry for our_env: key=value and retrun the string */ +char *create_new_entry(t_env *env_var, t_data *data) +{ + int key_len; + int val_len; + char *new_entry; + + key_len = ft_strlen(env_var->key); + val_len = ft_strlen(env_var->val); + new_entry = (char *)ft_calloc_e(key_len + val_len + 2, sizeof(char), data); + ft_strlcpy(new_entry, env_var->key, key_len + 1); + ft_strlcat(new_entry, "=", key_len + 2); + ft_strlcat(new_entry, env_var->val, key_len + val_len + 2); + return (new_entry); +} + +/* free our_env and rewrite the whole array */ +void rewrite_our_env(t_data *data) +{ + int size; + int i; + t_env *cur_env_var; + char **new_env; + + size = 0; + cur_env_var = *data->env_list; + while (cur_env_var) + { + size++; + cur_env_var = cur_env_var->next; + } + new_env = (char **)ft_calloc_e(size + 1, sizeof(char *), data); + i = 0; + cur_env_var = *data->env_list; + while (i < size) + { + new_env[i] = create_new_entry(cur_env_var, data); + i++; + cur_env_var = cur_env_var->next; + } + new_env[i] = NULL; + if(data->our_env) + free_strlist(data->our_env); + data->our_env = new_env; +} + +/* modify our_env: rewrite or adjust a single variable */ +void modify_our_env(t_env *env_var, t_data *data) +{ + int i; + + //if env_var == NULL, we have added a new env var to the list -> rewrite entire array + if (env_var == NULL) + rewrite_our_env(data); + //else, we are only changing the env var to a new value -> adjust that var only + else + { + i = find_var_ourenv(env_var->key, data); + free(data->our_env[i]); + data->our_env[i] = create_new_entry(env_var, data); + } + +} + +/* add env*/ +void add_env_var(char *key, char *val, t_data *data) +{ + t_env *node; + t_env *last; + + node = (t_env *)ft_calloc(1, sizeof(t_env)); + node->key = ft_strdup_lim(key, '\0', data); + if (!val) + node->val = NULL; + else + node->val = ft_strdup_lim(val, '\0', data); + node->next = NULL; + last = *data->env_list; + while (last->next) + last = last->next; + last->next = node; + node->prev = last; + modify_our_env(NULL, data); +} + diff --git a/env_var.o b/env_var.o new file mode 100644 index 0000000..10ba697 Binary files /dev/null and b/env_var.o differ diff --git a/file b/file new file mode 100644 index 0000000..c3525fa --- /dev/null +++ b/file @@ -0,0 +1,6 @@ +hello dolly +hello world +hello hello +hi +heya +hrm diff --git a/file2 b/file2 new file mode 100644 index 0000000..f93d894 --- /dev/null +++ b/file2 @@ -0,0 +1,3 @@ +hello dolly +hello world +hello hello diff --git a/free.c b/free.c new file mode 100644 index 0000000..e8d48bf --- /dev/null +++ b/free.c @@ -0,0 +1,16 @@ +//remember that when freeing up t_cmd cmd, iterate through count_redir instead of going to null +// void free_data(t_data data) +// { +// //free cmd +// while (i < cmd->count_redir) +// if (cmd->file[i]) +// free(i); + +// } + +#include "minishell.h" + +void free_data(t_data *data) +{ + +} \ No newline at end of file diff --git a/heredoc.c b/heredoc.c new file mode 100644 index 0000000..1655956 --- /dev/null +++ b/heredoc.c @@ -0,0 +1,102 @@ +//For heredocs +//keep track of delimiters +//all delimiters must be typed out before exiting +//last delimiter must be at the end +//delimiters must be written in order +//only text written after the last delimiter is considered to be input + + + +//keep track of heredoc delimiter in an array +//if there a heredoc redirector, will have to open up terminal for writing +//use readline to print > everytime something is written +//heredoc delimiters must be written IN ORDER for it to exit out +//if more than one heredoc delimiter +//disregard everything written until second to the last delimiter passes +//start writing to temporary file everything after that 2nd to last delimiter +//unlink temporary file -> in close function??? does fd_array have to be a struct??????? +//other option is to create a temporary pipe - write to pipe and have command read from read end + +#include "minishell.h" + +int count_delimiters(t_cmd *cmd) +{ + int i; + int count; + + i = 0; + count = 0; + while (i < cmd->count_redir) + { + if (cmd->redir[i] == 5) + count++; + i++; + } + return (count); +} + +char *init_str(char *input, t_data *data) +{ + char *str; + int len; + + len = ft_strlen(input) + 2; + + str = (char *)ft_calloc_e(len, sizeof(char), data); + ft_strlcpy(str, input, len); + ft_strlcat(str, "\n", len); + free(input); + return (str); +} + +char *generate_heredoc(t_cmd *cmd, int record_hd, t_data *data) +{ + char *input; + char *str; + char *new_str; + int i; + + str = NULL; + while (i < cmd->count_hd) + { + input = readline("> "); + //while i = index of the delimiter, it has not hit the delimiter yet + + if (ft_strncmp(input, cmd->hd_array[i], ft_strlen(input)) == 0) + i++; + else if (i == cmd->count_hd - 1 && record_hd == 1) + { + if (!str) + str = init_str(input, data); + else + { + new_str = ft_strjoin_e(str, input, data); //do we need to make a new function for this too for malloc error + free(str); + free(input); + str = new_str; + } + } + } + printf("the final string is: %s\n", str); + return (str); +} + +void get_heredoc_fd(t_cmd *cmd, int record_hd, t_data *data) +{ + int fd[2]; + char *str; + + str = NULL; + //only create pipe if we have to input a heredoc + if (record_hd == 1) + if (pipe(fd) == -1) + error(data); + str = generate_heredoc(cmd, record_hd, data); + if (record_hd == 1) + { + write(fd[1], str, ft_strlen(str)); + free(str); + close(fd[1]); + cmd->fd_array[cmd->last_input] = fd[0]; + } +} \ No newline at end of file diff --git a/heredoc.o b/heredoc.o new file mode 100644 index 0000000..8482674 Binary files /dev/null and b/heredoc.o differ diff --git a/init_data.c b/init_data.c new file mode 100644 index 0000000..2328baa --- /dev/null +++ b/init_data.c @@ -0,0 +1,67 @@ +#include "minishell.h" + +/* initiate array of fd and pid in data */ +void init_fd_pid(t_data *data) +{ + int i; + + data->fd = (int **)ft_calloc_e(data->num_cmd - 1, sizeof(int *), data); + i = 0; + while (i < data->num_cmd - 1) + { + data->fd[i] = (int *)ft_calloc_e(2, sizeof(int), data); + i++; + } + data->pid = (int *)ft_calloc_e(data->num_cmd, sizeof(int), data); +} + +/* initiate linked list of env variables in data */ +void init_env(t_data *data, char **envp) +{ + t_env **env_list; + t_env *node; + t_env *cur; + char **split_var; + int i; + + i = -1; + env_list = (t_env **)ft_calloc_e(1, sizeof(t_env *), data); + cur = NULL; + while (envp[++i]) + { + node = (t_env *)ft_calloc_e(1, sizeof(t_env), data); + split_var = split_env_var(envp[i], data); + node->key = ft_strdup_lim(split_var[0], '\0', data); + node->val = ft_strdup_lim(split_var[1], '\0', data); + node->next = NULL; + node->prev = cur; + free_strlist(split_var); + if (!*env_list) + *env_list = node; + else + cur->next = node; + cur = node; + } + data->env_list = env_list; +} + +/* initialize data structure */ +void ts_init_data(t_data *data, char ***env, int first) +{ + //NOTES: if we calloc the struct, we can initiate eveyrthing to 0 and NULL + if (first == YES) + { + data->cur_dir = getcwd(NULL, 0); + data->num_prev_error = 0; // check header :P + data->num_error = 0; + ts_init_env(data, env); + data->name_file = NO; // flag to check if it's a file (YES, NO) + init_env(data, *env); //init linked list env variables + data->our_env = NULL; //init this to NULL + } + data->num_prev_error = data->num_error; + data->num_error = 0; + data->empty_str = NO; // flag to check if the string is empty (YES, NO) + data->home_dir = getenv("HOME"); + data->num_cmd = 0; +} \ No newline at end of file diff --git a/init_data.o b/init_data.o new file mode 100644 index 0000000..3d7bf79 Binary files /dev/null and b/init_data.o differ diff --git a/libft/ft_is_functions.o b/libft/ft_is_functions.o new file mode 100644 index 0000000..a04ff6f Binary files /dev/null and b/libft/ft_is_functions.o differ diff --git a/libft/ft_itermap_tolu.o b/libft/ft_itermap_tolu.o new file mode 100644 index 0000000..7210b39 Binary files /dev/null and b/libft/ft_itermap_tolu.o differ diff --git a/libft/ft_itoa_atoi.o b/libft/ft_itoa_atoi.o new file mode 100644 index 0000000..edb339b Binary files /dev/null and b/libft/ft_itoa_atoi.o differ diff --git a/libft/ft_join_calloc.o b/libft/ft_join_calloc.o new file mode 100644 index 0000000..f6f3cd6 Binary files /dev/null and b/libft/ft_join_calloc.o differ diff --git a/libft/ft_lstdel_clear.o b/libft/ft_lstdel_clear.o new file mode 100644 index 0000000..1210f97 Binary files /dev/null and b/libft/ft_lstdel_clear.o differ diff --git a/libft/ft_lstmap.o b/libft/ft_lstmap.o new file mode 100644 index 0000000..966c310 Binary files /dev/null and b/libft/ft_lstmap.o differ diff --git a/libft/ft_lsts.o b/libft/ft_lsts.o new file mode 100644 index 0000000..0e24e8f Binary files /dev/null and b/libft/ft_lsts.o differ diff --git a/libft/ft_mems.o b/libft/ft_mems.o new file mode 100644 index 0000000..9357c68 Binary files /dev/null and b/libft/ft_mems.o differ diff --git a/libft/ft_put_fd.o b/libft/ft_put_fd.o new file mode 100644 index 0000000..0231251 Binary files /dev/null and b/libft/ft_put_fd.o differ diff --git a/libft/ft_strchr.o b/libft/ft_strchr.o new file mode 100644 index 0000000..1057145 Binary files /dev/null and b/libft/ft_strchr.o differ diff --git a/libft/ft_strf.o b/libft/ft_strf.o new file mode 100644 index 0000000..52de53d Binary files /dev/null and b/libft/ft_strf.o differ diff --git a/libft/ft_strl_split.o b/libft/ft_strl_split.o new file mode 100644 index 0000000..62938b9 Binary files /dev/null and b/libft/ft_strl_split.o differ diff --git a/libft/get_next_line.o b/libft/get_next_line.o new file mode 100644 index 0000000..f9a7c78 Binary files /dev/null and b/libft/get_next_line.o differ diff --git a/libft/libft.a b/libft/libft.a new file mode 100644 index 0000000..3a1e253 Binary files /dev/null and b/libft/libft.a differ diff --git a/libft/libft.h b/libft/libft.h index 551aa87..b626e63 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -22,6 +22,7 @@ # include # include # include +# include typedef struct s_list { diff --git a/libft_h/Makefile b/libft_h/Makefile new file mode 100644 index 0000000..aa34d42 --- /dev/null +++ b/libft_h/Makefile @@ -0,0 +1,45 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: hbui-vu +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/09/22 15:05:32 by hbui-vu #+# #+# # +# Updated: 2022/09/27 13:08:32 by hbui-vu ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft_h.a + +SRCS = ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c \ + ft_strlen.c ft_memset.c ft_bzero.c ft_memcpy.c ft_memmove.c \ + ft_strlcpy.c ft_strlcat.c ft_toupper.c ft_tolower.c ft_strchr.c \ + ft_strrchr.c ft_strncmp.c ft_memchr.c ft_memcmp.c ft_strnstr.c \ + ft_atoi.c ft_calloc.c ft_strdup.c ft_substr.c ft_strjoin.c \ + ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c ft_striteri.c \ + ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \ + get_next_line.c ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c \ + ft_lstadd_back.c ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c + +CC = gcc +CFLAGS = -Wall -Wextra -Werror +OBJS = $(SRCS:.c=.o) + +$(NAME): $(OBJS) + ar -rc $(NAME) $(OBJS) + +all: $(NAME) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -rf *.o + +fclean: clean + rm -rf $(NAME) + +re: fclean all + +.PHONY: all clean fclean re bonus diff --git a/libft_h/ft_atoi.c b/libft_h/ft_atoi.c new file mode 100644 index 0000000..12a17e4 --- /dev/null +++ b/libft_h/ft_atoi.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 17:03:47 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 15:30:59 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi(const char *str) +{ + unsigned long long i; + int neg; + + i = 0; + neg = 1; + while (*str == 32 || (*str >= 9 && *str <= 13)) + str++; + if (*str == '+' || *str == '-') + if (*(str++) == '-') + neg *= -1; + while (ft_isdigit(*str)) + { + if (i > LLONG_MAX && neg == -1) + return (0); + else if (i > LLONG_MAX && neg == 1) + return (-1); + i = (i * 10) + (*(str++) - 48); + } + return ((int)i * neg); +} diff --git a/libft_h/ft_atoi.o b/libft_h/ft_atoi.o new file mode 100644 index 0000000..b20d322 Binary files /dev/null and b/libft_h/ft_atoi.o differ diff --git a/libft_h/ft_bzero.c b/libft_h/ft_bzero.c new file mode 100644 index 0000000..2f58060 --- /dev/null +++ b/libft_h/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 13:55:04 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 10:34:22 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + s = ft_memset(s, 0, n); +} diff --git a/libft_h/ft_bzero.o b/libft_h/ft_bzero.o new file mode 100644 index 0000000..1db799b Binary files /dev/null and b/libft_h/ft_bzero.o differ diff --git a/libft_h/ft_calloc.c b/libft_h/ft_calloc.c new file mode 100644 index 0000000..604dd22 --- /dev/null +++ b/libft_h/ft_calloc.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/20 10:08:53 by hbui-vu #+# #+# */ +/* Updated: 2023/04/11 14:39:04 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_calloc(size_t count, size_t size) +{ + void *buffer; + + if (size != 0 && count > SIZE_MAX / size) + return (NULL); + buffer = malloc(count * size); + if (!buffer) + return (NULL); + ft_bzero(buffer, count * size); + return (buffer); +} diff --git a/libft_h/ft_calloc.o b/libft_h/ft_calloc.o new file mode 100644 index 0000000..d035c8a Binary files /dev/null and b/libft_h/ft_calloc.o differ diff --git a/libft_h/ft_isalnum.c b/libft_h/ft_isalnum.c new file mode 100644 index 0000000..d4699ed --- /dev/null +++ b/libft_h/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 10:54:24 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 11:17:38 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) == 1 || ft_isdigit(c) == 1); +} diff --git a/libft_h/ft_isalnum.o b/libft_h/ft_isalnum.o new file mode 100644 index 0000000..a3a84e3 Binary files /dev/null and b/libft_h/ft_isalnum.o differ diff --git a/libft_h/ft_isalpha.c b/libft_h/ft_isalpha.c new file mode 100644 index 0000000..3b0e2b4 --- /dev/null +++ b/libft_h/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 10:07:30 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 12:25:42 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); +} diff --git a/libft_h/ft_isalpha.o b/libft_h/ft_isalpha.o new file mode 100644 index 0000000..8c5e260 Binary files /dev/null and b/libft_h/ft_isalpha.o differ diff --git a/libft_h/ft_isascii.c b/libft_h/ft_isascii.c new file mode 100644 index 0000000..cd32491 --- /dev/null +++ b/libft_h/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 11:27:33 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 11:18:23 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return (c >= 0 && c <= 127); +} diff --git a/libft_h/ft_isascii.o b/libft_h/ft_isascii.o new file mode 100644 index 0000000..3d4d47e Binary files /dev/null and b/libft_h/ft_isascii.o differ diff --git a/libft_h/ft_isdigit.c b/libft_h/ft_isdigit.c new file mode 100644 index 0000000..8872ee8 --- /dev/null +++ b/libft_h/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 10:31:38 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 11:15:47 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/libft_h/ft_isdigit.o b/libft_h/ft_isdigit.o new file mode 100644 index 0000000..24fd883 Binary files /dev/null and b/libft_h/ft_isdigit.o differ diff --git a/libft_h/ft_isprint.c b/libft_h/ft_isprint.c new file mode 100644 index 0000000..8ea9bd8 --- /dev/null +++ b/libft_h/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 11:32:07 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 11:18:29 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (c >= 32 && c <= 126); +} diff --git a/libft_h/ft_isprint.o b/libft_h/ft_isprint.o new file mode 100644 index 0000000..9e78abf Binary files /dev/null and b/libft_h/ft_isprint.o differ diff --git a/libft_h/ft_itoa.c b/libft_h/ft_itoa.c new file mode 100644 index 0000000..a6243b8 --- /dev/null +++ b/libft_h/ft_itoa.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/22 10:07:18 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 15:43:41 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int num_len(int n) +{ + int count; + + if (n <= 0 || n == 0) + count = 1; + else + count = 0; + while (n != 0) + { + n /= 10; + count++; + } + return (count); +} + +char *ft_itoa(int n) +{ + long num; + char *nbr; + int len; + + nbr = (char *)malloc(sizeof(char) * num_len(n) + 1); + if (!nbr) + return (0); + num = n; + len = num_len(n); + nbr[len--] = '\0'; + if (num == 0) + nbr[0] = '0'; + if (num < 0) + { + nbr[0] = '-'; + num *= -1; + } + while (num > 0) + { + nbr[len--] = (num % 10) + 48; + num /= 10; + } + return (nbr); +} diff --git a/libft_h/ft_itoa.o b/libft_h/ft_itoa.o new file mode 100644 index 0000000..e4e6f8b Binary files /dev/null and b/libft_h/ft_itoa.o differ diff --git a/libft_h/ft_lstadd_back.c b/libft_h/ft_lstadd_back.c new file mode 100644 index 0000000..e435d1b --- /dev/null +++ b/libft_h/ft_lstadd_back.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:56:12 by hbui-vu #+# #+# */ +/* Updated: 2022/09/26 13:59:07 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *tmp; + + if (!lst) + return ; + if (!*lst) + *lst = new; + else + { + tmp = ft_lstlast(*lst); + tmp->next = new; + } +} diff --git a/libft_h/ft_lstadd_back.o b/libft_h/ft_lstadd_back.o new file mode 100644 index 0000000..9eba8ee Binary files /dev/null and b/libft_h/ft_lstadd_back.o differ diff --git a/libft_h/ft_lstadd_front.c b/libft_h/ft_lstadd_front.c new file mode 100644 index 0000000..44d740f --- /dev/null +++ b/libft_h/ft_lstadd_front.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:47:09 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 17:19:02 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (!lst) + return ; + new->next = *lst; + *lst = new; +} diff --git a/libft_h/ft_lstadd_front.o b/libft_h/ft_lstadd_front.o new file mode 100644 index 0000000..d7d3cc5 Binary files /dev/null and b/libft_h/ft_lstadd_front.o differ diff --git a/libft_h/ft_lstclear.c b/libft_h/ft_lstclear.c new file mode 100644 index 0000000..94b8057 --- /dev/null +++ b/libft_h/ft_lstclear.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 14:01:54 by hbui-vu #+# #+# */ +/* Updated: 2022/09/26 14:12:15 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + if (!lst || !del || !*lst) + return ; + while (*lst) + { + tmp = *lst; + *lst = (*lst)->next; + ft_lstdelone(tmp, del); + } +} diff --git a/libft_h/ft_lstclear.o b/libft_h/ft_lstclear.o new file mode 100644 index 0000000..bf2887d Binary files /dev/null and b/libft_h/ft_lstclear.o differ diff --git a/libft_h/ft_lstdelone.c b/libft_h/ft_lstdelone.c new file mode 100644 index 0000000..88e82e5 --- /dev/null +++ b/libft_h/ft_lstdelone.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:59:56 by hbui-vu #+# #+# */ +/* Updated: 2022/09/26 14:08:45 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (!lst || !del) + return ; + del(lst->content); + free(lst); +} diff --git a/libft_h/ft_lstdelone.o b/libft_h/ft_lstdelone.o new file mode 100644 index 0000000..39d91bf Binary files /dev/null and b/libft_h/ft_lstdelone.o differ diff --git a/libft_h/ft_lstiter.c b/libft_h/ft_lstiter.c new file mode 100644 index 0000000..edd5649 --- /dev/null +++ b/libft_h/ft_lstiter.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 14:06:37 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 00:03:44 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + t_list *t; + + if (!lst || !f) + return ; + t = lst; + while (t) + { + f(t->content); + t = t->next; + } +} diff --git a/libft_h/ft_lstiter.o b/libft_h/ft_lstiter.o new file mode 100644 index 0000000..dddb552 Binary files /dev/null and b/libft_h/ft_lstiter.o differ diff --git a/libft_h/ft_lstlast.c b/libft_h/ft_lstlast.c new file mode 100644 index 0000000..71413f0 --- /dev/null +++ b/libft_h/ft_lstlast.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:52:28 by hbui-vu #+# #+# */ +/* Updated: 2022/09/26 14:49:23 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + t_list *last_node; + + if (!lst) + return (NULL); + last_node = lst; + while (last_node) + { + if (last_node->next == NULL) + return (last_node); + last_node = last_node->next; + } + return (NULL); +} diff --git a/libft_h/ft_lstlast.o b/libft_h/ft_lstlast.o new file mode 100644 index 0000000..260202d Binary files /dev/null and b/libft_h/ft_lstlast.o differ diff --git a/libft_h/ft_lstmap.c b/libft_h/ft_lstmap.c new file mode 100644 index 0000000..f529c2a --- /dev/null +++ b/libft_h/ft_lstmap.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 14:06:39 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 17:36:07 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *new_head; + t_list *node; + t_list *lst_ptr; + + if (!lst || !f || !del) + return (NULL); + new_head = NULL; + lst_ptr = lst; + while (lst_ptr) + { + node = ft_lstnew(f(lst_ptr->content)); + if (!node) + { + ft_lstclear(&new_head, del); + return (NULL); + } + ft_lstadd_back(&new_head, node); + lst_ptr = lst_ptr->next; + } + return (new_head); +} diff --git a/libft_h/ft_lstmap.o b/libft_h/ft_lstmap.o new file mode 100644 index 0000000..3ae8d01 Binary files /dev/null and b/libft_h/ft_lstmap.o differ diff --git a/libft_h/ft_lstnew.c b/libft_h/ft_lstnew.c new file mode 100644 index 0000000..c21fcec --- /dev/null +++ b/libft_h/ft_lstnew.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:43:33 by hbui-vu #+# #+# */ +/* Updated: 2022/09/26 13:45:30 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *t; + + t = malloc(sizeof(t_list)); + if (t) + { + t->content = content; + t->next = NULL; + return (t); + } + return (NULL); +} diff --git a/libft_h/ft_lstnew.o b/libft_h/ft_lstnew.o new file mode 100644 index 0000000..433b532 Binary files /dev/null and b/libft_h/ft_lstnew.o differ diff --git a/libft_h/ft_lstsize.c b/libft_h/ft_lstsize.c new file mode 100644 index 0000000..61141e7 --- /dev/null +++ b/libft_h/ft_lstsize.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:49:55 by hbui-vu #+# #+# */ +/* Updated: 2022/09/26 14:10:16 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int count; + t_list *tmp; + + count = 0; + tmp = lst; + while (tmp) + { + count++; + tmp = tmp->next; + } + return (count); +} diff --git a/libft_h/ft_lstsize.o b/libft_h/ft_lstsize.o new file mode 100644 index 0000000..a45681c Binary files /dev/null and b/libft_h/ft_lstsize.o differ diff --git a/libft_h/ft_memchr.c b/libft_h/ft_memchr.c new file mode 100644 index 0000000..3165fcb --- /dev/null +++ b/libft_h/ft_memchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 12:11:07 by hbui-vu #+# #+# */ +/* Updated: 2022/09/24 12:23:01 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + unsigned char *ret_s; + + i = 0; + ret_s = (unsigned char *)s; + while (i < n) + { + if (ret_s[i] == (unsigned char)c) + break ; + i++; + } + if (i == n) + return (NULL); + return (ret_s + i); +} diff --git a/libft_h/ft_memchr.o b/libft_h/ft_memchr.o new file mode 100644 index 0000000..fc493c6 Binary files /dev/null and b/libft_h/ft_memchr.o differ diff --git a/libft_h/ft_memcmp.c b/libft_h/ft_memcmp.c new file mode 100644 index 0000000..1c9128b --- /dev/null +++ b/libft_h/ft_memcmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 12:11:22 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 17:19:34 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *t1; + unsigned char *t2; + size_t i; + + t1 = (unsigned char *)s1; + t2 = (unsigned char *)s2; + i = 0; + if (n == 0) + return (0); + while (i < n) + { + if (t1[i] != t2[i]) + return (t1[i] - t2[i]); + i++; + } + return (0); +} diff --git a/libft_h/ft_memcmp.o b/libft_h/ft_memcmp.o new file mode 100644 index 0000000..164eda3 Binary files /dev/null and b/libft_h/ft_memcmp.o differ diff --git a/libft_h/ft_memcpy.c b/libft_h/ft_memcpy.c new file mode 100644 index 0000000..0704306 --- /dev/null +++ b/libft_h/ft_memcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 14:02:50 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 10:55:01 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + size_t i; + + i = 0; + if (dst == NULL && src == NULL) + return (NULL); + while (i < n) + { + ((unsigned char *)dst)[i] = ((unsigned char *)src)[i]; + i++; + } + return (dst); +} diff --git a/libft_h/ft_memcpy.o b/libft_h/ft_memcpy.o new file mode 100644 index 0000000..c3a4b25 Binary files /dev/null and b/libft_h/ft_memcpy.o differ diff --git a/libft_h/ft_memmove.c b/libft_h/ft_memmove.c new file mode 100644 index 0000000..bd12084 --- /dev/null +++ b/libft_h/ft_memmove.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/23 12:26:41 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 14:50:29 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + int i; + + i = len - 1; + if (dst == NULL && src == NULL) + return (NULL); + if (&((unsigned char *)src)[0] > &((unsigned char *)dst)[0]) + return (ft_memcpy(dst, src, len)); + while (i >= 0) + { + ((unsigned char *)dst)[i] = ((unsigned char *)src)[i]; + i--; + } + return (dst); +} diff --git a/libft_h/ft_memmove.o b/libft_h/ft_memmove.o new file mode 100644 index 0000000..11fbdd6 Binary files /dev/null and b/libft_h/ft_memmove.o differ diff --git a/libft_h/ft_memset.c b/libft_h/ft_memset.c new file mode 100644 index 0000000..5ce25cb --- /dev/null +++ b/libft_h/ft_memset.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 11:52:37 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 11:55:20 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *b, int c, size_t len) +{ + size_t i; + + i = 0; + while (i < len) + ((unsigned char *)b)[i++] = (unsigned char)(c); + return (b); +} diff --git a/libft_h/ft_memset.o b/libft_h/ft_memset.o new file mode 100644 index 0000000..0b4946b Binary files /dev/null and b/libft_h/ft_memset.o differ diff --git a/libft_h/ft_putchar_fd.c b/libft_h/ft_putchar_fd.c new file mode 100644 index 0000000..c0d560a --- /dev/null +++ b/libft_h/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/22 16:35:52 by hbui-vu #+# #+# */ +/* Updated: 2022/09/24 19:47:17 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write (fd, &c, 1); +} diff --git a/libft_h/ft_putchar_fd.o b/libft_h/ft_putchar_fd.o new file mode 100644 index 0000000..bdc5453 Binary files /dev/null and b/libft_h/ft_putchar_fd.o differ diff --git a/libft_h/ft_putendl_fd.c b/libft_h/ft_putendl_fd.c new file mode 100644 index 0000000..1da5bed --- /dev/null +++ b/libft_h/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/22 16:35:28 by hbui-vu #+# #+# */ +/* Updated: 2022/09/24 21:03:53 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + write(fd, "\n", 1); +} diff --git a/libft_h/ft_putendl_fd.o b/libft_h/ft_putendl_fd.o new file mode 100644 index 0000000..a00afef Binary files /dev/null and b/libft_h/ft_putendl_fd.o differ diff --git a/libft_h/ft_putnbr_fd.c b/libft_h/ft_putnbr_fd.c new file mode 100644 index 0000000..f81171a --- /dev/null +++ b/libft_h/ft_putnbr_fd.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/22 16:38:06 by hbui-vu #+# #+# */ +/* Updated: 2022/09/24 19:46:30 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n == -2147483648) + { + ft_putchar_fd('-', fd); + ft_putchar_fd('2', fd); + ft_putnbr_fd(147483648, fd); + } + else if (n < 0) + { + ft_putchar_fd('-', fd); + n = n * -1; + ft_putnbr_fd(n, fd); + } + else if (n <= 9) + { + ft_putchar_fd(n + 48, fd); + } + else + { + ft_putnbr_fd(n / 10, fd); + ft_putnbr_fd(n % 10, fd); + } +} diff --git a/libft_h/ft_putnbr_fd.o b/libft_h/ft_putnbr_fd.o new file mode 100644 index 0000000..1550cd2 Binary files /dev/null and b/libft_h/ft_putnbr_fd.o differ diff --git a/libft_h/ft_putstr_fd.c b/libft_h/ft_putstr_fd.c new file mode 100644 index 0000000..a379bdd --- /dev/null +++ b/libft_h/ft_putstr_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/22 16:35:45 by hbui-vu #+# #+# */ +/* Updated: 2023/03/15 16:18:41 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + if (!s) + return ; + while (*s) + write(fd, s++, 1); +} diff --git a/libft_h/ft_putstr_fd.o b/libft_h/ft_putstr_fd.o new file mode 100644 index 0000000..17feaa2 Binary files /dev/null and b/libft_h/ft_putstr_fd.o differ diff --git a/libft_h/ft_split.c b/libft_h/ft_split.c new file mode 100644 index 0000000..e59f579 --- /dev/null +++ b/libft_h/ft_split.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/20 11:20:56 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 15:37:17 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int num_strs(char const *s, char c) +{ + int i; + int count; + + i = 0; + count = 0; + if (s[i] && s[i] != c) + { + count++; + i++; + } + while (s[i]) + { + if (s[i] == c && s[i + 1] != c && s[i + 1]) + count++; + i++; + } + return (count + 1); +} + +static char *cpystr(char const *s, char c) +{ + char *buffer; + size_t i; + + i = 0; + while (s[i] && s[i] != c) + i++; + buffer = malloc (sizeof(char) * i + 1); + if (!buffer) + return (NULL); + ft_strlcpy(buffer, s, i + 1); + return (buffer); +} + +char **ft_split(char const *s, char c) +{ + char **buffer; + int i; + int strs; + + if (!s) + return (NULL); + strs = num_strs(s, c); + buffer = (char **)malloc(sizeof(char *) * strs); + if (!buffer) + return (NULL); + i = 0; + while (i < strs - 1) + { + while (*s == c) + s++; + buffer[i] = cpystr(s, c); + if (!buffer[i]) + return (NULL); + i++; + s = ft_strchr(s, c); + } + buffer[i] = NULL; + return (buffer); +} diff --git a/libft_h/ft_split.o b/libft_h/ft_split.o new file mode 100644 index 0000000..0ccaa80 Binary files /dev/null and b/libft_h/ft_split.o differ diff --git a/libft_h/ft_strchr.c b/libft_h/ft_strchr.c new file mode 100644 index 0000000..809d4c6 --- /dev/null +++ b/libft_h/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/23 15:04:16 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 15:48:55 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + size_t i; + + i = 0; + while (i <= ft_strlen(s)) + { + if (s[i] == (char)c) + break ; + i++; + } + if (i > ft_strlen(s)) + return (NULL); + return ((char *)s + i); +} diff --git a/libft_h/ft_strchr.o b/libft_h/ft_strchr.o new file mode 100644 index 0000000..bd14203 Binary files /dev/null and b/libft_h/ft_strchr.o differ diff --git a/libft_h/ft_strdup.c b/libft_h/ft_strdup.c new file mode 100644 index 0000000..7279442 --- /dev/null +++ b/libft_h/ft_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 18:06:53 by hbui-vu #+# #+# */ +/* Updated: 2022/09/24 18:14:42 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *ret_str; + + if (!s1) + return (NULL); + ret_str = malloc(sizeof(char) * (ft_strlen(s1) + 1)); + if (!ret_str) + return (NULL); + ft_strlcpy(ret_str, s1, ft_strlen(s1) + 1); + return (ret_str); +} diff --git a/libft_h/ft_strdup.o b/libft_h/ft_strdup.o new file mode 100644 index 0000000..e27c15e Binary files /dev/null and b/libft_h/ft_strdup.o differ diff --git a/libft_h/ft_striteri.c b/libft_h/ft_striteri.c new file mode 100644 index 0000000..06cf65c --- /dev/null +++ b/libft_h/ft_striteri.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 19:43:04 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 17:38:13 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + unsigned int i; + + if (!s || !f) + return ; + i = 0; + while (s[i]) + { + f(i, &s[i]); + i++; + } +} diff --git a/libft_h/ft_striteri.o b/libft_h/ft_striteri.o new file mode 100644 index 0000000..7772057 Binary files /dev/null and b/libft_h/ft_striteri.o differ diff --git a/libft_h/ft_strjoin.c b/libft_h/ft_strjoin.c new file mode 100644 index 0000000..f1acf8e --- /dev/null +++ b/libft_h/ft_strjoin.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/20 10:41:25 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 15:16:09 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *buffer; + int k; + + if (!s1 || !s2) + return (NULL); + k = ft_strlen(s1) + ft_strlen(s2) + 1; + buffer = (char *)malloc(sizeof(char) * k); + if (!buffer) + return (NULL); + ft_strlcpy(buffer, s1, k); + ft_strlcat(buffer, s2, k); + return (buffer); +} diff --git a/libft_h/ft_strjoin.o b/libft_h/ft_strjoin.o new file mode 100644 index 0000000..7f4f615 Binary files /dev/null and b/libft_h/ft_strjoin.o differ diff --git a/libft_h/ft_strlcat.c b/libft_h/ft_strlcat.c new file mode 100644 index 0000000..413d264 --- /dev/null +++ b/libft_h/ft_strlcat.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/23 15:26:35 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 15:08:01 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t dstsize) +{ + size_t i; + size_t j; + size_t dst_len; + + i = 0; + j = 0; + if (!dstsize) + return (ft_strlen(src)); + dst_len = ft_strlen(dst); + if (dst_len < dstsize && dstsize > 0) + { + while (dst[i]) + i++; + while (i < dstsize - 1 && src[j]) + dst[i++] = src[j++]; + dst[i] = '\0'; + } + else + dst_len = dstsize; + return (dst_len + ft_strlen(src)); +} diff --git a/libft_h/ft_strlcat.o b/libft_h/ft_strlcat.o new file mode 100644 index 0000000..f9a4ec8 Binary files /dev/null and b/libft_h/ft_strlcat.o differ diff --git a/libft_h/ft_strlcpy.c b/libft_h/ft_strlcpy.c new file mode 100644 index 0000000..ff72662 --- /dev/null +++ b/libft_h/ft_strlcpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/23 15:14:07 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 16:05:34 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) +{ + size_t i; + + i = 0; + if (dstsize > 0) + { + while (src[i] && i < dstsize - 1) + { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; + } + return (ft_strlen(src)); +} diff --git a/libft_h/ft_strlcpy.o b/libft_h/ft_strlcpy.o new file mode 100644 index 0000000..1a8875a Binary files /dev/null and b/libft_h/ft_strlcpy.o differ diff --git a/libft_h/ft_strlen.c b/libft_h/ft_strlen.c new file mode 100644 index 0000000..726f3c3 --- /dev/null +++ b/libft_h/ft_strlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 11:47:25 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 12:21:42 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t len; + + if (!s) + return (0); + len = 0; + while (s[len] != '\0') + len++; + return (len); +} diff --git a/libft_h/ft_strlen.o b/libft_h/ft_strlen.o new file mode 100644 index 0000000..b887669 Binary files /dev/null and b/libft_h/ft_strlen.o differ diff --git a/libft_h/ft_strmapi.c b/libft_h/ft_strmapi.c new file mode 100644 index 0000000..95dd448 --- /dev/null +++ b/libft_h/ft_strmapi.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 19:31:04 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 17:37:39 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + unsigned int i; + char *str; + + if (!s || !f) + return (NULL); + i = 0; + str = (char *)malloc(sizeof(char) * ft_strlen(s) + 1); + if (!str) + return (NULL); + while (s[i]) + { + str[i] = f(i, s[i]); + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft_h/ft_strmapi.o b/libft_h/ft_strmapi.o new file mode 100644 index 0000000..2e1b152 Binary files /dev/null and b/libft_h/ft_strmapi.o differ diff --git a/libft_h/ft_strncmp.c b/libft_h/ft_strncmp.c new file mode 100644 index 0000000..b750f76 --- /dev/null +++ b/libft_h/ft_strncmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 12:10:51 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 11:56:22 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + if (n == 0) + return (0); + while ((unsigned char)s1[i] == (unsigned char)s2[i] && (i < n -1) && s1[i]) + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/libft_h/ft_strncmp.o b/libft_h/ft_strncmp.o new file mode 100644 index 0000000..d3ad903 Binary files /dev/null and b/libft_h/ft_strncmp.o differ diff --git a/libft_h/ft_strnstr.c b/libft_h/ft_strnstr.c new file mode 100644 index 0000000..b3e4dcb --- /dev/null +++ b/libft_h/ft_strnstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 12:11:51 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 14:42:47 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *s1, const char *s2, size_t len) +{ + size_t i; + size_t j; + + i = 0; + if (!s1 && !len) + return (NULL); + if (!*s2) + return ((char *)s1); + while (s1[i] && i < len) + { + j = 0; + while (s1[i + j] == s2[j] && (i + j) < len) + { + if (s2[j + 1] == '\0') + return ((char *)s1 + i); + j++; + } + i++; + } + return (NULL); +} diff --git a/libft_h/ft_strnstr.o b/libft_h/ft_strnstr.o new file mode 100644 index 0000000..3562413 Binary files /dev/null and b/libft_h/ft_strnstr.o differ diff --git a/libft_h/ft_strrchr.c b/libft_h/ft_strrchr.c new file mode 100644 index 0000000..c36a24b --- /dev/null +++ b/libft_h/ft_strrchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 12:09:20 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 11:12:32 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + + i = ft_strlen(s); + while (i >= 0) + { + if (s[i] == (char)c) + break ; + i--; + } + if (i < 0) + return (NULL); + return ((char *)s + i); +} diff --git a/libft_h/ft_strrchr.o b/libft_h/ft_strrchr.o new file mode 100644 index 0000000..4578767 Binary files /dev/null and b/libft_h/ft_strrchr.o differ diff --git a/libft_h/ft_strtrim.c b/libft_h/ft_strtrim.c new file mode 100644 index 0000000..3d99065 --- /dev/null +++ b/libft_h/ft_strtrim.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/24 21:51:43 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 17:20:20 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int is_sep(char c, char const *charset) +{ + while (*charset) + if (c == *(charset++)) + return (1); + return (0); +} + +static int str_size(char const *s1, char const *set) +{ + int count; + int len; + + count = 0; + len = ft_strlen(s1) - 1; + while (s1[count] && is_sep(s1[count], set)) + count++; + if (count == (int)ft_strlen(s1)) + return (0); + while (s1[len] && is_sep(s1[len], set)) + { + len--; + count++; + } + len = ft_strlen(s1) - count + 1; + return (len); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + char *str; + int i; + int len; + + if (!s1) + return (NULL); + len = str_size(s1, set); + str = (char *)malloc(sizeof(char) * (len)); + if (!str) + return (NULL); + i = 0; + while (*s1 && is_sep(*s1, set)) + s1++; + while (i < len - 1) + { + str[i] = s1[i]; + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft_h/ft_strtrim.o b/libft_h/ft_strtrim.o new file mode 100644 index 0000000..9005133 Binary files /dev/null and b/libft_h/ft_strtrim.o differ diff --git a/libft_h/ft_substr.c b/libft_h/ft_substr.c new file mode 100644 index 0000000..5b27c7b --- /dev/null +++ b/libft_h/ft_substr.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/20 10:22:41 by hbui-vu #+# #+# */ +/* Updated: 2022/09/26 17:27:43 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + size_t i; + char *buffer; + + i = 0; + if (!s) + return (NULL); + if (len <= ft_strlen(s)) + buffer = (char *)malloc(sizeof(char) * len + 1); + else + buffer = (char *)malloc(sizeof(char) * ft_strlen(s) + 1); + if (!buffer) + return (NULL); + while (i < len && s[start] && start < ft_strlen(s)) + buffer[i++] = s[start++]; + buffer[i] = '\0'; + return (buffer); +} diff --git a/libft_h/ft_substr.o b/libft_h/ft_substr.o new file mode 100644 index 0000000..79784f2 Binary files /dev/null and b/libft_h/ft_substr.o differ diff --git a/libft_h/ft_tolower.c b/libft_h/ft_tolower.c new file mode 100644 index 0000000..4dddfa6 --- /dev/null +++ b/libft_h/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/23 14:58:13 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 15:00:34 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c + 32); + return (c); +} diff --git a/libft_h/ft_tolower.o b/libft_h/ft_tolower.o new file mode 100644 index 0000000..c4ce60d Binary files /dev/null and b/libft_h/ft_tolower.o differ diff --git a/libft_h/ft_toupper.c b/libft_h/ft_toupper.c new file mode 100644 index 0000000..c9f540c --- /dev/null +++ b/libft_h/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/23 14:57:31 by hbui-vu #+# #+# */ +/* Updated: 2022/09/23 15:01:39 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c - 32); + return (c); +} diff --git a/libft_h/ft_toupper.o b/libft_h/ft_toupper.o new file mode 100644 index 0000000..8a97e9e Binary files /dev/null and b/libft_h/ft_toupper.o differ diff --git a/libft_h/get_next_line.c b/libft_h/get_next_line.c new file mode 100644 index 0000000..5d25f3c --- /dev/null +++ b/libft_h/get_next_line.c @@ -0,0 +1,124 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/22 16:43:56 by hbui-vu #+# #+# */ +/* Updated: 2022/10/24 15:30:41 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int detect_line(char *line) +{ + int i; + + i = 0; + if (!line) + return (-1); + while (line[i]) + { + if (line[i] == '\n') + return (i); + i++; + } + return (-1); +} + +int ft_strlen_gnl(char *str) +{ + int i; + + i = 0; + if (!str) + return (0); + while (str[i]) + { + if (str[i] == '\n') + return (i + 1); + i++; + } + return (i); +} + +char *joinstr(char **line, char **overflow) +{ + int llen; + int olen; + int i; + int j; + char *return_line; + + llen = ft_strlen_gnl(*line); + olen = ft_strlen_gnl(*overflow); + if (llen == 0 && olen == 0) + return (NULL); + return_line = (char *)ft_calloc(llen + olen + 1, sizeof(char)); + if (!return_line) + return (NULL); + i = 0; + j = 0; + while (i < llen) + { + return_line[i] = (*line)[i]; + i++; + } + free(*line); + while (j < olen) + return_line[i++] = (*overflow)[j++]; + return_line[i] = '\0'; + return (return_line); +} + +char *clean(char **overflow) +{ + int i; + int j; + char *new_overflow; + + if (!(*overflow)) + { + new_overflow = (char *)ft_calloc(BUFFER_SIZE + 1, sizeof(char)); + if (!new_overflow) + return (NULL); + *overflow = new_overflow; + } + i = detect_line(*overflow) + 1; + j = 0; + while (i > 0 && (*overflow)[i]) + (*overflow)[j++] = (*overflow)[i++]; + while (j <= BUFFER_SIZE) + (*overflow)[j++] = '\0'; + return (*overflow); +} + +char *get_next_line(int fd) +{ + static char *overflow; + char *line; + ssize_t i; + + line = NULL; + i = 1; + if (fd < 0 || BUFFER_SIZE <= 0 || BUFFER_SIZE > 2147483646) + return (NULL); + while (detect_line(line) < 0 && i > 0) + { + line = joinstr(&line, &overflow); + overflow = clean(&overflow); + if (!overflow) + return (NULL); + if (detect_line(line) > -1) + break ; + i = read(fd, overflow, BUFFER_SIZE); + } + if (ft_strlen_gnl(overflow) == 0) + { + free(overflow); + overflow = NULL; + } + return (line); +} diff --git a/libft_h/get_next_line.o b/libft_h/get_next_line.o new file mode 100644 index 0000000..14df418 Binary files /dev/null and b/libft_h/get_next_line.o differ diff --git a/libft_h/libft.h b/libft_h/libft.h new file mode 100644 index 0000000..7f47cda --- /dev/null +++ b/libft_h/libft.h @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hbui-vu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/20 09:40:16 by hbui-vu #+# #+# */ +/* Updated: 2022/09/27 15:58:22 by hbui-vu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 1024 +# endif + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +size_t ft_strlen(const char *s); +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memmove(void *dst, const void *src, size_t len); +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); +size_t ft_strlcat(char *dst, const char *src, size_t dstsize); +int ft_toupper(int c); +int ft_tolower(int c); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +int ft_strncmp(const char *s1, const char *s2, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); +int ft_atoi(const char *str); +void *ft_calloc(size_t count, size_t size); +char *ft_strdup(const char *s1); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +char *get_next_line(int fd); + +#endif \ No newline at end of file diff --git a/libft_h/libft_h.a b/libft_h/libft_h.a new file mode 100644 index 0000000..0bcd800 Binary files /dev/null and b/libft_h/libft_h.a differ diff --git a/minishell b/minishell new file mode 100755 index 0000000..6df31b2 Binary files /dev/null and b/minishell differ diff --git a/minishell.c b/minishell.c index 7bbdca7..0deadd2 100644 --- a/minishell.c +++ b/minishell.c @@ -3,15 +3,16 @@ /* ::: :::::::: */ /* minishell.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jalwahei +#+ +:+ +#+ */ +/* By: hbui-vu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/08 21:33:56 by jalwahei #+# #+# */ -/* Updated: 2023/04/03 10:21:16 by jalwahei ### ########.fr */ +/* Updated: 2023/04/17 18:45:36 by hbui-vu ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +/* error if more than one argument passed to program */ static void ts_err_argc_argv(int argc, char **argv, char **env) { if (argc != 1 || argv == NULL || env == NULL) @@ -20,30 +21,8 @@ static void ts_err_argc_argv(int argc, char **argv, char **env) exit(127); } } -void ts_init_data(t_data *data, char ***env, int first) -{ - if (first == YES) - { - data->flag_old = 1; - data->prev_dir = NULL; - data->cur_dir = getcwd(NULL, 0); - data->num_prev_error = 0; // check header :P - data->num_error = 0; - data->num_tmp_var = 0; // just a tmp var might be useful - data->tmp_var = NULL; - ts_init_env(data, env); - data->name_file = NO; // flag to check if it's a file (YES, NO) - } - data->num_prev_error = data->num_error; - data->num_error = 0; - data->empty_str = NO; // flag to check if the string is empty (YES, NO) - data->home_dir = getenv("HOME"); - // data->build_in = YES; // flag to check if it's a build in command (YES, NO) - data->num_cmd = 0; - // data->fd_pipe[0] = 0; something you need - // data->fd_pipe[1] = 0; same as above -} +/* what is this for? */ static int ts_quote_checker(t_data *data, char *line) { int i = 0; @@ -65,36 +44,16 @@ static int ts_quote_checker(t_data *data, char *line) } return (0); } -// ts_found_dollar_sign(t_data *data, char *str) -// { -// int i = 0; -// int j = 0; -// while (str[i] != '\0') -// { -// if (str[i] == '$') -// { -// j = i; -// while (str[j] != '\0') -// { -// if (str[j] == ' ') -// break; -// j++; -// } -// ts_found_env_variable(data, str, i, j); -// } -// i++; -// } -// } +/* parse single line? */ int ts_parse(t_data *data, char *line) { int i; i = 0; - // int ik = 0; - // int jk = 0; ts_quote_checker(data, line); ts_count_and_record_cmd(data, line); + init_fd_pid(data); if (data->num_error != 0 || data->empty_str == YES) return (-1); @@ -110,7 +69,6 @@ int ts_parse(t_data *data, char *line) ts_found_env_variable(data, &data->cmd[i]); i++; } - i = 0; return (0); } @@ -125,68 +83,42 @@ int main(int argc, char **argv, char **env) while (1) { ts_get_signal(); - ts_init_data(&data, &env, NO); + ts_init_data(&data, &env, NO); line = readline("\033[1;35mTinyShell > \033[0m"); + //line = readline(data->cwd); ts_signal_ctrl_d(&data, &line); // ts_signal_ctrl_slash(&data, &line); - ts_parse(&data, line); - // print_t_data(data); + ts_parse(&data, line); add_history(line); - // print_t_cmd(data.cmd); - if (data.empty_str == NO) + if (data.empty_str == NO) { ts_record_array(&data); - // ts_execution(&data, &line); // here what it should look like i think when executing :D - for(int i = 0; i < data.num_cmd; i++) - { - print_t_cmd(&data.cmd[i]); + // print_t_cmd(data.cmd); + pipex(&data); + // ts_free_cycle(&data, &line); // we will have to free the memory something like this } - // printf("num_cmd = %d\n", data.num_cmd); - // ts_free_cycle(&data, &line); // we will have to free the memory something like this - } -} - + } } -void print_t_cmd(t_cmd *cmd) -{ - // printf("\033[1;35m t_cmd: \n\033[0m"); - // printf(" str: %s\n", cmd->str); - // printf(" num_arg: %d\n", cmd->num_arg); - // printf(" num_array_arg: %d\n", cmd->num_array_arg); - // // printf(" way_cmd: %s\n", cmd->way_cmd); - // printf(" count_redir: %d\n", cmd->count_redir); - // // printf(" bad_file: %d\n", cmd->bad_file); - // printf(" array_empty: %d\n", cmd->array_empty); - printf("\033[1;35m everything inside the cmd->arg.str :\n\033[0m"); - // for (int i = 0; i < cmd->num_arg; i++) - // { - // // printf(" arg[%d]:\n", i); - // printf(" str: %s\n", cmd->arg[i].str); - // // printf(" q_m: %d\n", cmd->arg[i].q_m); - // printf(" space: %d\n", cmd->arg[i].space); - // printf(" redir: %d\n", cmd->arg[i].redir); - // printf(" empty_key: %d\n", cmd->arg[i].empty_key); - // } - printf(" \033[1;35m array_arg:\033[0m\n"); - for (int i = 0; cmd->array_arg[i] != NULL; i++) - { - printf(" array_arg[%d]: %s\n", i, cmd->array_arg[i]); - } - - printf("\033[1;35m redir:\033[0m\n"); - for (int i = 0; i < cmd->count_redir; i++) - { - printf(" redir[%d]: %d\n", i, cmd->redir[i]); - } +// ts_found_dollar_sign(t_data *data, char *str) +// { +// int i = 0; +// int j = 0; - // printf("\033[1;35m file:\033[0m\n"); - for (int i = 0; i < cmd->count_redir; i++) - { - printf(" file[%d]: %s\n", i, cmd->file[i]); - } - // printf("\033[1;35m fd: [%d, %d]\033[0m\n", cmd->fd[0], cmd->fd[1]); - // printf(" redir_born: [%d, %d]\n", cmd->redir_born[0], cmd->redir_born[1]); - // printf(" last_redir: %d\n", cmd->last_redir); -} +// while (str[i] != '\0') +// { +// if (str[i] == '$') +// { +// j = i; +// while (str[j] != '\0') +// { +// if (str[j] == ' ') +// break; +// j++; +// } +// ts_found_env_variable(data, str, i, j); +// } +// i++; +// } +// } diff --git a/minishell.h b/minishell.h index 18a1b45..1fe9361 100644 --- a/minishell.h +++ b/minishell.h @@ -3,25 +3,27 @@ /* ::: :::::::: */ /* minishell.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jalwahei +#+ +:+ +#+ */ +/* By: hbui-vu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/09 10:52:58 by jalwahei #+# #+# */ -/* Updated: 2023/04/01 04:24:11 by jalwahei ### ########.fr */ +/* Updated: 2023/04/17 18:01:35 by hbui-vu ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H +# include +# include # include # include # include -# include -# include -# include "libft/libft.h" # include # include # include +# include +# include +# include "libft/libft.h" typedef enum e_value { @@ -44,7 +46,12 @@ typedef enum e_value ERR_TOKEN = 258, // is not a standard exit code in bash, but some applications may use it to indicate a syntax error or unexpected token in the command line. ERR_NUM_ONE = 1, // can be used to indicate a generic error or failure. ERR_FILE_OR_DIR = 126, //126 typically indicates that the specified file or directory cannot be found or accessed by the shell. -} t_value; +} t_value; + +// < = 2 input redirection +// > = 3 output redirection +// >> = 4 output redirection but open file for appending +// << = 5 heredoc typedef struct s_arg { @@ -54,56 +61,62 @@ typedef struct s_arg int redir; // idk might be usefl later for parsing int empty_key; // idk why i did this but i think it was for the envp and $ sign maybe -} t_arg; +} t_arg; + +typedef struct s_tmp +{ + int size_str; + int size_cut; + int count; +} t_tmp; typedef struct s_cmd { - char *str; // saving command as a string - t_arg *arg; + char *str; //parsing - saving command as a string + t_arg *arg; //parsing char **array_arg; - int *redir; + char **hd_array; //array of heredoc delimiters in order + char *path; char **file; - int fd[2]; // with redir_born[2] i think it will be used for piping - int redir_born[2]; // i think it will be used along with fd[2] - int last_redir; // last redir in the command + int *redir; + int *fd_array; //array of open files for redirections + int count_hd; //number of heredocs int num_arg; - int num_array_arg; // - char *way_cmd; // path to cmd - int count_redir; // c - int bad_file; // flag for error might need in excution (yes or no) + int num_array_arg; //parsing + int count_redir; int array_empty; + int last_output; + int last_input; +} t_cmd; -} t_cmd; - -typedef struct s_tmp +typedef struct s_env { - int size_str; - int size_cut; - int count; -} t_tmp; + char *key; + char *val; + int p; //printed: 1, not printed: 0 + struct s_env *next; + struct s_env *prev; +} t_env; typedef struct s_data { + t_env **env_list; t_cmd *cmd; - t_tmp tmp; // tmp struct to help with parsing and cutting file names - int num_cmd; // number of commands + t_tmp tmp; // tmp struct to help with parsing and cutting file names + char **env_paths; + char **our_env; + char *cur_dir; + char *home_dir; + int **fd; + int *pid; + int num_cmd; int num_error; // error token ERR_TOKEN / DOUBLE_Q_MARK etc.. int num_prev_error; // to give exit value a number - int num_env; - char **our_env; - char **tmp_var; // unsued empty tmp var - int num_tmp_var; // no need just an empty tmp var - char *prev_dir; // previous directory - char *cur_dir; // current directory - char *home_dir; // home directory - int flag_old; // something could be used to keep track of prev direc + int num_env; // -> do you really need this? can you use null instead, otherwise this number will have to be changed everytime int empty_str; // flag for main function to know o execute or no - int fd_pipe[2]; // your pipes no idea how to use 😇 ;) int name_file; // not used so far - int build_in; // flag to know if its a build in cmd (YES,NO) - // int *pid; - int n_end; -} t_data; + // int n_end; +} t_data; /* ********************* Quotation parse ********************* */ void ts_create_struct_without_qm(t_cmd *cmd); @@ -171,4 +184,51 @@ void ts_record_array(t_data *data); void print_t_data(struct s_data data); // void print_arg(t_arg *arg); void print_t_cmd(t_cmd *cmd); + +/* DATA */ +void ts_init_data(t_data *data, char ***env, int first); +void init_fd_pid(t_data *data); + +/* BUILTINS */ +void ft_cd(char **arg, t_data *data); +void ft_echo(char **str, t_data *data); +void ft_env(t_data *data); +void ft_export(char **arg, t_data *data); +void ft_pwd(t_data *data); +void ft_unset(char **arg, t_data *data); +void execute_builtin(char **arg, int i, t_data *data); +int check_builtin(char *str, t_data *data); + +/* ENV VAR */ +t_env *find_var_envlist(char *key, t_data *data); +void modify_our_env(t_env *env_var, t_data *data); +void add_env_var(char *key, char *val, t_data *data); + +/* UTILS */ +int detect_char(char *str, char c); +void print_string(int num_str, ...); +void error(t_data *data); +void print_string(int num_str, ...); +void *ft_calloc_e(size_t count, size_t size, t_data *data); +char *ft_strdup_lim(const char *s1, char c, t_data *data); +void free_strlist(char **str); +char **split_env_var(char *str, t_data *data); +void get_env_paths(t_data *data); +void ts_add_cmd_path(char *arg, t_cmd *cmd,t_data *data); +char *ft_strjoin_char(char const *s1, char const *s2, char c); +void dup2_e(int oldfd, int newfd, t_data *data); +char *ft_strjoin_e(char const *s1, char const *s2, t_data *data); + +/* EXECUTION */ +int pipex(t_data *data); + +/* HEREDOC */ +int count_delimiters(t_cmd *cmd); +void get_heredoc_fd(t_cmd *cmd, int record_hd, t_data *data); + +// /* TESTERS */ +void print_strlist(char **list); +// void print_data(t_data *data); +void print_t_cmd(t_cmd *cmd); + #endif \ No newline at end of file diff --git a/minishell.o b/minishell.o new file mode 100644 index 0000000..0320c3b Binary files /dev/null and b/minishell.o differ diff --git a/mod.c b/mod.c new file mode 100644 index 0000000..e69de29 diff --git a/outline b/outline new file mode 100644 index 0000000..4dc8df8 --- /dev/null +++ b/outline @@ -0,0 +1,14 @@ +1. check to make sure arg is ./minishell only and env exists +2. Initialize data +2a. Print data +3. Take input +4. Parse data +5. Run commands + +-have to use your copy of environmental variables +-make sure paths is also edited -> because that is linked to env variables +-unset and export only works as commands on their own + -except if export only, which produces a list of variables in alpha order + +2. Initialize data + \ No newline at end of file diff --git a/parse/parse_pipe.o b/parse/parse_pipe.o new file mode 100644 index 0000000..95cde78 Binary files /dev/null and b/parse/parse_pipe.o differ diff --git a/parse/ts_count_record_cmd.c b/parse/ts_count_record_cmd.c index ed1de1d..c0ec6a0 100644 --- a/parse/ts_count_record_cmd.c +++ b/parse/ts_count_record_cmd.c @@ -165,7 +165,6 @@ int ts_count_and_record_cmd(t_data *data, char *line) // printf(" int num_env: %d\n", data.num_env); // printf(" char **our_env: %p\n", data.our_env); // printf(" char **tmp_var: %p\n", data.tmp_var); -// printf(" int num_tmp_var: %d\n", data.num_tmp_var); // printf(" char *prev_dir: %p\n", data.prev_dir); // printf(" char *cur_dir: %s\n", data.cur_dir); // printf(" char *home_dir: %s\n", data.home_dir); diff --git a/parse/ts_count_record_cmd.o b/parse/ts_count_record_cmd.o new file mode 100644 index 0000000..f53a491 Binary files /dev/null and b/parse/ts_count_record_cmd.o differ diff --git a/parse/ts_error.o b/parse/ts_error.o new file mode 100644 index 0000000..4643fd2 Binary files /dev/null and b/parse/ts_error.o differ diff --git a/parse/ts_find_redirect.o b/parse/ts_find_redirect.o new file mode 100644 index 0000000..32c6555 Binary files /dev/null and b/parse/ts_find_redirect.o differ diff --git a/parse/ts_found_dollar.o b/parse/ts_found_dollar.o new file mode 100644 index 0000000..1ff6bf6 Binary files /dev/null and b/parse/ts_found_dollar.o differ diff --git a/parse/ts_free.o b/parse/ts_free.o new file mode 100644 index 0000000..9aa0cd0 Binary files /dev/null and b/parse/ts_free.o differ diff --git a/parse/ts_init_env.o b/parse/ts_init_env.o new file mode 100644 index 0000000..b84a52b Binary files /dev/null and b/parse/ts_init_env.o differ diff --git a/parse/ts_malloc.o b/parse/ts_malloc.o new file mode 100644 index 0000000..c58fd58 Binary files /dev/null and b/parse/ts_malloc.o differ diff --git a/parse/ts_measure_size_file_name.o b/parse/ts_measure_size_file_name.o new file mode 100644 index 0000000..db0d984 Binary files /dev/null and b/parse/ts_measure_size_file_name.o differ diff --git a/parse/ts_quotation_marks.o b/parse/ts_quotation_marks.o new file mode 100644 index 0000000..5dba57c Binary files /dev/null and b/parse/ts_quotation_marks.o differ diff --git a/parse/ts_record_arr.c b/parse/ts_record_arr.c index 17f6954..5c3805e 100644 --- a/parse/ts_record_arr.c +++ b/parse/ts_record_arr.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ts_record_arr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jalwahei +#+ +:+ +#+ */ +/* By: hbui-vu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/28 17:56:12 by jalwahei #+# #+# */ -/* Updated: 2023/04/01 05:20:23 by jalwahei ### ########.fr */ +/* Updated: 2023/04/11 15:47:02 by hbui-vu ### ########.fr */ /* */ /* ************************************************************************** */ @@ -100,6 +100,9 @@ void ts_record_array(t_data *data) } return ; } + /* we populate env_paths temporarily and then free it in the same function. + This way, we anticipate the modification of PATHS via unset or export*/ + get_env_paths(data); while (i < data->num_cmd) { data->cmd[i].array_empty = YES; @@ -107,9 +110,20 @@ void ts_record_array(t_data *data) { ts_count_arg_for_array(&data->cmd[i]); ts_connect_arg_for_array(&data->cmd[i]); + ts_add_cmd_path(data->cmd[i].array_arg[0], &data->cmd[i], data); + data->cmd[i].last_output = -1; + data->cmd[i].last_input = -1; + data->cmd[i].hd_array = NULL; //Is this calloc'd? If it is, we don't have to initiate to NULL + data->cmd[i].fd_array = NULL; + data->cmd[i].count_hd = 0; } i++; } + if (data->env_paths) + { + free_strlist(data->env_paths); + data->env_paths = NULL; + } // if (data->num_error == 0 && data->cmd[0].str != NULL) // ts_found_variable(data); } diff --git a/parse/ts_record_arr.o b/parse/ts_record_arr.o new file mode 100644 index 0000000..fdc02d2 Binary files /dev/null and b/parse/ts_record_arr.o differ diff --git a/parse/ts_record_val.o b/parse/ts_record_val.o new file mode 100644 index 0000000..87a0486 Binary files /dev/null and b/parse/ts_record_val.o differ diff --git a/parse/ts_signals.o b/parse/ts_signals.o new file mode 100644 index 0000000..8fd08d0 Binary files /dev/null and b/parse/ts_signals.o differ diff --git a/pipex.c b/pipex.c new file mode 100644 index 0000000..594827b --- /dev/null +++ b/pipex.c @@ -0,0 +1,249 @@ +#include "minishell.h" + +/* iterate through redirection array and look for doubles */ +/* double = filename same; redir signs same; redir sign > and >>, eliminate >> */ +void check_redir_doubles(t_cmd *cmd) +{ + int i; + int j; + + i = 0; + j = 1; + while (i < cmd->count_redir) + { + while (j < cmd->count_redir) + { + // if filenames match + if (cmd->file[i] && ft_strncmp(cmd->file[i], cmd->file[j], ft_strlen(cmd->file[i])) == 0) + { + //if redir signs match, free the second + //if first redir sign is > and second redir sign is >>, eliminate >> + if (cmd->redir[i] == cmd->redir[j] || (cmd->redir[i] == 3 && cmd->redir[j] == 4)) + { + free(cmd->file[j]); + cmd->file[j] = NULL; + cmd->redir[j] = -1; + } + //if first redir sign is >> and second is >, eliminate >> + else if (cmd->redir[i] == 4 && cmd->redir[j] == 3) + { + free(cmd->file[i]); + cmd->file[i] = NULL; + cmd->redir[i] = -1; + } + } + j++; + } + i++; + j = i + 1; + } +} + +//write null to pipe +void pipe_null(int index, t_data *data) +{ + int devnull; + devnull = open("dev/null", O_WRONLY); + if (devnull < 0) + error(data); + dup2(devnull, data->fd[index][1]); + close(devnull); +} + +void close_fd_array(t_cmd *cmd) +{ + int i; + + i = 0; + while (i < cmd->count_redir) + { + if (i != -1) + { + close(cmd->fd_array[i]); + cmd->fd_array[i] = -1; + } + i++; + } +} +/* finalize the cmd struct */ +/* populate hd_array, fd_array, count_hd, fd->last_output, fd->last_input */ +/* see ts_record_arr for initializiation of these values */ +void mod_cmd(t_cmd *cmd, t_data *data) +{ + int i; + int hd; + + i = 0; + hd = 0; + //create fd_array and set all values to -1 + cmd->fd_array = (int *)ft_calloc_e(cmd->count_redir, sizeof(int), data); + ft_memset(cmd->fd_array, -1, cmd->count_redir); + cmd->count_hd = count_delimiters(cmd); + //if number of heredocs is greater than 0, then calloc an hd_array + if (cmd->count_hd > 0) + cmd->hd_array = (char **)ft_calloc_e(cmd->count_hd + 1, sizeof(char *), data); + while (i < cmd->count_redir) + { + //if <, open file as rdonly. + //if error, that means the file doesn't exist + //the moment a file doesn't open, nothing else is checked, null sent to command + if (cmd->redir[i] == 2) + { + cmd->fd_array[i] = open(cmd->file[i], O_RDONLY); + if (cmd->fd_array[i] == -1) + { + printf("-bash: %s: No such file or directory\n", cmd->file[i]); + close_fd_array(cmd); + //send null to pipe output if it's not the last command + if (i != data->num_cmd - 1) + pipe_null(i, data); + //exit out of command, not sure if this is correct + error(data); + } + //track fd->last_input to dup2 + cmd->last_input = i; + } + //if >, create file and track last output to put into dup2 + else if (cmd->redir[i] == 3) + { + cmd->fd_array[i] = open(cmd->file[i], O_CREAT | O_RDWR | O_TRUNC, 0666); + if (cmd->fd_array[i] == -1) + error(data); //open error -> exit + cmd->last_output = i; + } + //if >>, open file for appending and track last output to put into dup2 + else if (cmd->redir[i] == 4) + { + cmd->fd_array[i] = open(cmd->file[i], O_CREAT | O_RDWR | O_APPEND, 0666); + if (cmd->fd_array[i] == -1) + error(data); //open error -> exit + cmd->last_output = i; + } + //if << (heredoc), append file to delimiter array + //there will be no file opened for this, so fd is -1 (no fd) + else if (cmd->redir[i] == 5) + { + cmd->hd_array[hd] = ft_strdup_lim(cmd->file[i], '\0', data); + hd++; + cmd->fd_array[i] = -1; + cmd->last_input = i; + } + //if this redirection was deleted, fd is -1 (no fd) + else if (cmd->redir[i] == -1) + cmd->fd_array[i] = -1; + i++; + } +} + +void redirect(int index, t_cmd *cmd, t_data *data) +{ + int i; + int hd; + int record_hd; + int fd; + + //if more than 1 redirection, check for double files/redir and remove + if (cmd->count_redir > 1) + check_redir_doubles(cmd); + mod_cmd(cmd, data); + //if last input redirection is a heredoc, record_hd = 1. Else record_hd is 0 + //record_hd means to record_hd the string in heredoc into a temp file + record_hd = 0; + if (cmd->last_input > -1 && cmd->redir[cmd->last_input] == 5) + record_hd = 1; + // printf("count_hd = %i\n", cmd->count_hd); + // printf("record_hd = %i\n", record_hd); + // print_strlist(cmd->hd_array); + if (cmd->count_hd > 0) + get_heredoc_fd(cmd, record_hd, data); + //if there is no < and it's not the first command, read input from previous pipe + if (cmd->last_input == -1 && index > 0) + dup2_e(data->fd[index - 1][0], STDIN_FILENO, data); + //if there is a < read input from file + else if (cmd->last_input > -1) + dup2_e(cmd->fd_array[cmd->last_input], STDIN_FILENO, data); + //if there is no > or >> and it is not the last command, write results into pipe + if (cmd->last_output == -1 && index != data->num_cmd - 1) + dup2_e(data->fd[index][1], STDOUT_FILENO, data); + //else if there is > or >>, write results into file + else if (cmd->last_output > -1) + { + dup2_e(cmd->fd_array[cmd->last_output], STDOUT_FILENO, data); + // if (index != data->num_cmd - 1) + // pipe_null(index, data); + } + //close all files + close_fd_array(cmd); + +} + +void child_process(int i, t_cmd *cmd, t_data *data) +{ + int k; + int j; + + if (cmd->count_redir > 0) + redirect(i, cmd, data); + j = 0; + while (j < data->num_cmd - 1) + { + close(data->fd[j][0]); + close(data->fd[j][1]); + j++; + } + k = check_builtin(data->cmd->array_arg[0], data); + // printf("k: %i\n", k); + if (k != 0) + execute_builtin(data->cmd->array_arg, k, data); + else + { + if (execve(cmd->path, cmd->array_arg, data->our_env) == -1) + error(data); + } +} + +int parent_process(t_data *data) +{ + int i; + int status; + + i = 0; + while (i < data->num_cmd - 1) + { + close(data->fd[i][0]); + close(data->fd[i][1]); + i++; + } + i = 0; + while (i < data->num_cmd) + { + waitpid(data->pid[i], &status, 0); + i++; + } + return (status); +} + +int pipex(t_data *data) +{ + int i; + int status; + + i = -1; + /* create pipes based on number of commands - 1*/ + while (++i < data->num_cmd - 1) + if (pipe(data->fd[i]) == -1) + error(data); + i = -1; + /* fork and run the child process */ + while (++i < data->num_cmd) + { + data->pid[i] = fork(); + if (data->pid[i] == -1) + error(data); + else if (data->pid[i] == 0) + child_process(i, &data->cmd[i], data); + } + /* run the parent process */ + status = parent_process(data); + return (WEXITSTATUS(status)); +} diff --git a/pipex.o b/pipex.o new file mode 100644 index 0000000..8a77cf0 Binary files /dev/null and b/pipex.o differ diff --git a/testers.c b/testers.c new file mode 100644 index 0000000..dd653b3 --- /dev/null +++ b/testers.c @@ -0,0 +1,47 @@ +#include "minishell.h" + +void print_t_cmd(t_cmd *cmd) +{ + // printf("\033[1;35m t_cmd: \n\033[0m"); + // printf(" str: %s\n", cmd->str); + // printf(" num_arg: %d\n", cmd->num_arg); + // printf(" num_array_arg: %d\n", cmd->num_array_arg); + // // printf(" way_cmd: %s\n", cmd->way_cmd); + // printf(" count_redir: %d\n", cmd->count_redir); + // // printf(" bad_file: %d\n", cmd->bad_file); + // printf(" array_empty: %d\n", cmd->array_empty); + + printf("\033[1;35m everything inside the cmd->arg.str :\n\033[0m"); + // for (int i = 0; i < cmd->num_arg; i++) + // { + // // printf(" arg[%d]:\n", i); + // printf(" str: %s\n", cmd->arg[i].str); + // // printf(" q_m: %d\n", cmd->arg[i].q_m); + // printf(" space: %d\n", cmd->arg[i].space); + // printf(" redir: %d\n", cmd->arg[i].redir); + // printf(" empty_key: %d\n", cmd->arg[i].empty_key); + // } + + printf(" \033[1;35m array_arg:\033[0m\n"); + fflush(stdout); + for (int i = 0; cmd->array_arg[i] != NULL; i++) + { + printf(" array_arg[%d]: %s\n", i, cmd->array_arg[i]); + printf(" array_path: %s\n", cmd->path); + } + + printf("\033[1;35m redir:\033[0m\n"); + for (int i = 0; i < cmd->count_redir; i++) + { + printf(" redir[%d]: %d\n", i, cmd->redir[i]); + } + + // printf("\033[1;35m file:\033[0m\n"); + for (int i = 0; i < cmd->count_redir; i++) + { + printf(" file[%d]: %s\n", i, cmd->file[i]); + } + // printf("\033[1;35m fd: [%d, %d]\033[0m\n", cmd->fd[0], cmd->fd[1]); + // printf(" redir_born: [%d, %d]\n", cmd->redir_born[0], cmd->redir_born[1]); + +} \ No newline at end of file diff --git a/testers.o b/testers.o new file mode 100644 index 0000000..16cf9fd Binary files /dev/null and b/testers.o differ diff --git a/ts_utils.c b/ts_utils.c index dc39509..9d2e418 100644 --- a/ts_utils.c +++ b/ts_utils.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ts_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jalwahei +#+ +:+ +#+ */ +/* By: hbui-vu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/24 03:29:53 by jalwahei #+# #+# */ -/* Updated: 2023/02/26 12:41:29 by jalwahei ### ########.fr */ +/* Updated: 2023/04/11 15:24:37 by hbui-vu ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,59 +55,63 @@ int ts_cut_qm_in_name_file(char **file) return (0); } -void print_t_data(struct s_data data) -{ - printf("t_data {\n"); - int k=0; - while(data.cmd[k].str != NULL) - {printf(" t_cmd *cmd: %s\n", data.cmd[k].str); - k++;} - // printf(" t_tmp tmp: %d\n", data.tmp); - // printf(" int num_cmd: %d\n", data.num_cmd); - // printf(" int num_error: %d\n", data.num_error); - // printf(" int num_prev_error: %d\n", data.num_prev_error); - // printf(" int num_env: %d\n", data.num_env); +// void print_t_data(struct s_data data) +// { +// printf("t_data {\n"); +// int k=0; + +// while(data.cmd[k].str != NULL) +// {printf(" t_cmd *cmd: %s\n", data.cmd[k].str); +// k++;} +// printf(" t_tmp tmp: %d\n", data.tmp); +// printf(" int num_cmd: %d\n", data.num_cmd); +// printf(" int num_error: %d\n", data.num_error); +// printf(" int num_prev_error: %d\n", data.num_prev_error); +// printf(" int num_env: %d\n", data.num_env); - // Print 2D array `our_env` - // printf(" char **our_env: {\n"); - // for (int i = 0; i < data.num_env; i++) { - // printf(" \"%s\",\n", data.our_env[i]); - // } - // printf(" }\n"); +// // Print 2D array `our_env` +// // printf(" char **our_env: {\n"); +// // for (int i = 0; i < data.num_env; i++) { +// // printf(" \"%s\",\n", data.our_env[i]); +// // } +// printf(" char **env_paths: {\n"); +// for (int i = 0; i < 12; i++) { +// printf(" \"%s\",\n", data.env_paths[i]); +// } +// printf(" }\n"); - // Print contents of 2D array `tmp_var` - // printf(" char **tmp_var: {\n"); - // for (int i = 0; i < data.num_tmp_var; i++) { - // printf(" \"%s\",\n", data.tmp_var[i]); - // } - // printf(" }\n"); +// // Print contents of 2D array `tmp_var` +// printf(" char **tmp_var: {\n"); +// for (int i = 0; i < dmakata.num_tmp_var; i++) { +// printf(" \"%s\",\n", data.tmp_var[i]); +// } +// printf(" }\n"); - // printf(" int num_tmp_var: %d\n", data.num_tmp_var); - // printf(" char *prev_dir: %p\n", data.prev_dir); - // // printf(" char *cur_dir: %p\n", data.cur_dir); - // // printf(" char *home_dir: %p\n", data.home_dir); - // printf(" int flag_old: %d\n", data.flag_old); - // printf(" int empty_str: %d\n", data.empty_str); - // printf(" int fd_pipe[2]: {%d, %d}\n", data.fd_pipe[0], data.fd_pipe[1]); - // printf(" int name_file: %d\n", data.name_file); - // printf(" int build_in: %d\n", data.build_in); - // printf(" int *pid: %p\n", data.pid); - // printf(" int n_end: %d\n", data.n_end); +// printf(" char *prev_dir: %p\n", data.prev_dir); +// // printf(" char *cur_dir: %p\n", data.cur_dir); +// // printf(" char *home_dir: %p\n", data.home_dir); +// printf(" int flag_old: %d\n", data.flag_old); +// printf(" int empty_str: %d\n", data.empty_str); +// // printf(" int fd_pipe[2]: {%d, %d}\n", data.fd_pipe[0], data.fd_pipe[1]); +// printf(" int name_file: %d\n", data.name_file); +// printf(" int build_in: %d\n", data.build_in); +// printf(" int *pid: %p\n", data.pid); +// printf(" int n_end: %d\n", data.n_end); - // // Print string `prev_dir` if not NULL - // if (data.prev_dir != NULL) { - // printf(" prev_dir: \"%s\"\n", data.prev_dir); - // } +// // Print string `prev_dir` if not NULL +// if (data.prev_dir != NULL) { +// printf(" prev_dir: \"%s\"\n", data.prev_dir); +// } - // // Print string `cur_dir` if not NULL - // if (data.cur_dir != NULL) { - // printf(" cur_dir: \"%s\"\n", data.cur_dir); - // } +// // Print string `cur_dir` if not NULL +// if (data.cur_dir != NULL) { +// printf(" cur_dir: \"%s\"\n", data.cur_dir); +// } - // // Print string `home_dir` if not NULL - // if (data.home_dir != NULL) { - // printf(" home_dir: \"%s\"\n", data.home_dir); - // } +// // Print string `home_dir` if not NULL +// if (data.home_dir != NULL) { +// printf(" home_dir: \"%s\"\n", data.home_dir); +// } - printf("}\n"); -} \ No newline at end of file +// printf("}\n"); +// } \ No newline at end of file diff --git a/ts_utils.o b/ts_utils.o new file mode 100644 index 0000000..1ad0d3d Binary files /dev/null and b/ts_utils.o differ diff --git a/utils.o b/utils.o new file mode 100644 index 0000000..32aa726 Binary files /dev/null and b/utils.o differ diff --git a/utils_h.c b/utils_h.c new file mode 100644 index 0000000..fb5a917 --- /dev/null +++ b/utils_h.c @@ -0,0 +1,290 @@ +#include "minishell.h" + +/* looks for char and returns index of that char if found. else it returns -1 */ +int detect_char(char *str, char c) +{ + int i; + + i = 0; + while (str[i]) + { + if (str[i] == c) + return (i); + i++; + } + return (-1); +} + +/* duplicate string up until a char c */ +char *ft_strdup_lim(const char *s1, char c, t_data *data) +{ + char *ret_str; + int len; + + len = 0; + while(s1[len] != c) + len++; + ret_str = (char *) ft_calloc(len + 1, sizeof(char)); + if (!ret_str) + error(data); + ft_strlcpy(ret_str, s1, len + 1); + return (ret_str); +} + +/* prints strings and accounts for printf errors */ +void print_string(int num_str, ...) +{ + int i; + t_data *data; + va_list str; + + va_start(str, num_str); + i = 0; + data = va_arg(str, t_data*); + while (i < num_str) + { + if (printf("%s", va_arg(str, char *)) == -1) + error(data); + i++; + } + va_end(str); +} + +/* calloc with error */ +void *ft_calloc_e(size_t count, size_t size, t_data *data) +{ + void *buf; + + buf = ft_calloc(count, size); + if (!buf) + error(data); + return (buf); +// void *buffer; + +// if (size != 0 && count > SIZE_MAX / size) +// error(data); +// buffer = malloc(count * size); +// if (!buffer) +// { +// printf("error\n"); +// error(data); +// } +// ft_bzero(buffer, count * size); +// return (buffer); +} + +/* placeholder error function */ +void error(t_data *data) +{ + (void)data; + exit(1); +} + +/* free an array of strings */ +void free_strlist(char **str) +{ + int i; + + i = 0; + if (str && *str) + { + while(str[i]) + { + + free(str[i]); + i++; + } + } + if (str) + free(str); +} + +/* split variable by only the first '=' and returns an array with key and value */ +char **split_env_var(char *str, t_data *data) +{ + int var_len; + int val_len; + char **ret_split; + ret_split = (char **)ft_calloc_e(3, sizeof(char *), data); + var_len = detect_char(str, '='); + ret_split[0] = (char *)ft_calloc_e(var_len + 1, sizeof(char), data); + val_len = ft_strlen(str) - (var_len + 1); + ret_split[1] = (char *)ft_calloc_e(val_len + 1, sizeof(char), data); + ft_strlcpy(ret_split[0], str, var_len + 1); + str += (var_len + 1); + ft_strlcpy(ret_split[1], str, val_len + 1); + ret_split[2] = NULL; + return (ret_split); +} + +/* get environmental paths */ +// void get_env_paths(t_data *data) +// { +// int i; +// t_env **env_list; + +// env_list = data->env_list; +// while (data->env_list) +// { +// if (ft_strncmp("PATH", (*data->env_list)->key, 5) == 0) +// break ; +// env_list = env_list->next; +// } +// if (data->env_list == NULL || data->env_list->val == NULL) +// return ; +// //this checks for garbage values in path +// while (*data->env_list->val && *(data->env_list->val) != '/') +// data->env_list->val++; +// if (data->env_list->val == NULL) +// return ; +// data->env_paths = ft_split(data->env_list->val, ':'); +// if (!data->env_paths) //malloc error +// return ; +// } + +void get_env_paths(t_data *data) +{ + int i; + t_env *cur; + + if (!data->env_list || !*data->env_list) + return ; + cur = *data->env_list; + while (cur) + { + if (ft_strncmp("PATH", cur->key, 5) == 0) + break ; + cur = cur->next; + } + if (cur == NULL || cur->val == NULL) + return ; + //this checks for garbage values in path + while (cur->val && *(cur->val) != '/') + cur->val++; + if (cur->val == NULL) + return ; + data->env_paths = ft_split(cur->val, ':'); + if (!data->env_paths) //malloc error + return ; +} + +// void get_env_paths(char **envp, t_data *data) +// { +// int i; +// // char **env_paths; + +// i = -1; +// while (envp[++i]) +// if (ft_strncmp("PATH=", envp[i], 5) == 0) +// break ; +// if (envp[i] == NULL) +// return ; +// while (*(envp[i]) != '/') +// envp[i]++; +// data->env_paths = ft_split(envp[i], ':'); +// if (!data->env_paths) +// return ; +// // return (env_paths); +// } + + +/* get path */ +void ts_add_cmd_path(char *arg, t_cmd *cmd,t_data *data) +{ + int i; + // char *path; + cmd->path = NULL; + i = 0; + // if (!arg || !detect_alnum(arg)) --> just detect one number or alphabet????? + // return (NULL); + if (access(arg, F_OK) == 0 && access(arg, X_OK) == 0) + { + cmd->path = ft_strdup_lim(arg, '\0', data); + return ; + } + if (!data->env_paths) + { + cmd->path = NULL; + return ; + } + //-bash: grep: NO such file or directory + while (data->env_paths[i]) + { + cmd->path = ft_strjoin_char(data->env_paths[i], arg, '/'); + if (access(cmd->path, F_OK) == 0 && access(cmd->path, X_OK) == 0) + return ; + else if (access(cmd->path, F_OK) == 0 && access(cmd->path, X_OK) < 0) + { + free(cmd->path); + cmd->path = NULL; + return ; + } + free(cmd->path); + cmd->path = NULL; + i++; + } +} + +/* join with a char */ +char *ft_strjoin_char(char const *s1, char const *s2, char c) +{ + char *buffer; + int k; + int i; + + if (!s1) + return (NULL); + if (!s2) + k = ft_strlen(s1) + 2; + else + k = ft_strlen(s1) + ft_strlen(s2) + 2; + buffer = (char *)ft_calloc(k, sizeof(char)); + if (!buffer) + return (NULL); + i = ft_strlcpy(buffer, s1, k); + buffer[i] = c; + if (s2) + ft_strlcat(buffer, s2, k); + return (buffer); +} + +/* dup2 with error */ +void dup2_e(int oldfd, int newfd, t_data *data) +{ + if (dup2(oldfd, newfd) == -1) + error(data); +} + +/* print list of strings */ +void print_strlist(char **list) +{ + int i; + + i = 0; + if (!list) + printf("(null)"); + while (list[i]) + { + printf("%s\n", list[i]); + i++; + } + // printf("\n"); +} + +/* ft_strjoin for heredoc with error management and addition of \n at the end */ +char *ft_strjoin_e(char const *s1, char const *s2, t_data *data) +{ + char *buffer; + int k; + + if (!s1 || !s2) + return (NULL); + k = ft_strlen(s1) + ft_strlen(s2) + 2; + buffer = (char *)malloc(sizeof(char) * k); + if (!buffer) + error(data); + ft_strlcpy(buffer, s1, k); + ft_strlcat(buffer, s2, k); + ft_strlcat(buffer, "\n", k); + return (buffer); +} diff --git a/utils_h.o b/utils_h.o new file mode 100644 index 0000000..b926f1b Binary files /dev/null and b/utils_h.o differ