Skip to content

Commit

Permalink
Minishell
Browse files Browse the repository at this point in the history
  • Loading branch information
bahimzabir committed Aug 18, 2023
0 parents commit 6485ceb
Show file tree
Hide file tree
Showing 62 changed files with 3,449 additions and 0 deletions.
146 changes: 146 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: yagnaou <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/05/27 11:27:39 by yagnaou #+# #+# #
# Updated: 2022/09/10 21:03:31 by yagnaou ### ########.fr #
# #
# **************************************************************************** #

# =============================================================================
# Color Variables
# =============================================================================

BLACK = "\033[1;30m"
GRAY = "\033[1;30m"
RED = "\033[1;31m"
GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
PURPLE = "\033[1;35m"
CYAN = "\033[1;36m"

# =============================================================================
# GCC & Flags Variables
# =============================================================================

CC = gcc
CFLAGS = -Wall -Werror -Wextra
READLINE = -lreadline -L /Users/yagnaou/goinfre/.brew/Cellar/readline/8.1.2/lib -I /Users/yagnaou/goinfre/.brew/Cellar/readline/8.1.2/include


# =============================================================================
# Name & File Names Variables
# =============================================================================

NAME = minishell
EXECUTION = srcs/execution/
INCLUDE = srcs/include/
LIBFT = srcs/libft/

SRCS = ${EXECUTION}check_path.c \
${EXECUTION}ft_cd.c \
${EXECUTION}ft_echo.c \
${EXECUTION}ft_env.c \
${EXECUTION}ft_execve.c \
${EXECUTION}ft_exit.c \
${EXECUTION}ft_export.c \
${EXECUTION}ft_pwd.c \
${EXECUTION}ft_unset.c \
${EXECUTION}heredoc.c \
${EXECUTION}is_last_heredoc.c \
${EXECUTION}utils.c \
${INCLUDE}check_char.c \
${INCLUDE}check_heredoc.c \
${INCLUDE}check_last.c \
${INCLUDE}childs_sighand.c \
${INCLUDE}cmd_array_join.c \
${INCLUDE}cmd_create.c \
${INCLUDE}dollar.c \
${INCLUDE}fill_data_list.c \
${INCLUDE}lexer.c \
${INCLUDE}lexer1.c \
${INCLUDE}lexer2.c \
${INCLUDE}main.c \
${INCLUDE}parce_utils.c \
${INCLUDE}redirections_utils.c \
${INCLUDE}sighandl.c \
${INCLUDE}syntax_checker.c \
${INCLUDE}token.c \
${LIBFT}cmds_count.c \
${LIBFT}free_array.c \
${LIBFT}ft_calloc.c \
${LIBFT}ft_divide.c \
${LIBFT}ft_isalnum.c \
${LIBFT}ft_isdigit.c \
${LIBFT}ft_itoa.c \
${LIBFT}ft_lstadd_back_new.c \
${LIBFT}ft_lstadd_back.c \
${LIBFT}ft_lstnew_new.c \
${LIBFT}ft_lstnew.c \
${LIBFT}ft_putendl_fd.c \
${LIBFT}ft_splite.c \
${LIBFT}ft_strcat.c \
${LIBFT}ft_strchr.c \
${LIBFT}ft_strcmp.c \
${LIBFT}ft_strdup.c \
${LIBFT}ft_strjoin.c \
${LIBFT}ft_strjoin3.c \
${LIBFT}ft_strlen.c \
${LIBFT}ft_strncmp.c \
${LIBFT}ft_strnstr.c \
${LIBFT}get_next_char.c \
${LIBFT}is_buildin.c \
${LIBFT}is_special.c \
${LIBFT}lstfree.c \
${LIBFT}make_str.c \
${LIBFT}strjoin2.c \
${LIBFT}unclosed_quotes.c \

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

# =============================================================================
# Drawing o Dakchi Hahahahaha
# =============================================================================

define HEADER
_
/ \ _
| | /_\
| | _ _ _
| | /_\ /_\ /_\ _____
_ | | ___ ___ ___ ___ ___ ___ ___ / \
/ \ | \____/ \____/ |__| |__| \____/ \___/ \___/ \_____/ _ O \
\ \____/ \_____/ \_________________________/ \_____/ \_____/ \_______/ \____/
\________/ _ _ _ _ _ _
/_\ /_\ /_\ /_\ /_\ /_\

endef

export HEADER

# =============================================================================
# Rules
# =============================================================================

all : $(NAME)

$(NAME) : $(SRCS)
@echo ${YELLOW}"---> Compiling \n$${HEADER}"
@${CC} $(CFLAGS) ${READLINE} $(SRCS) -o $(NAME)
@echo ${GREEN}"---> Compilation done."

clean :
@rm -rf $(OBJS)
@echo ${PURPLE}"---> Cleaning object files..."
@echo ${PURPLE}"---> Cleaned!"

fclean :
@rm -rf $(OBJS)
@rm -rf $(NAME)
@echo ${RED}"---> Cleaning ${NAME} with it's object files..."
@echo ${RED}"---> All cleaned!"

re : fclean all
84 changes: 84 additions & 0 deletions srcs/execution/check_path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yagnaou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/09 10:35:58 by azabir #+# #+# */
/* Updated: 2022/09/08 13:58:12 by yagnaou ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/minishell.h"

char *make_path(char *path, char *cmd)
{
char *cmd_path;
struct stat buf;

cmd_path = ft_strjoin(path, cmd);
if (!stat(cmd_path, &buf))
return (cmd_path);
free(cmd_path);
return (NULL);
}

int is_dir(char *cmd)
{
int i;

i = 0;
while (cmd && cmd[i])
{
if (cmd[i] == '/')
return (1);
i++;
}
return (0);
}

char *creat_path(t_data *data, char *cmd)
{
int i;
char *cmd_path;
char *part_of_path;

i = 0;
while (data->complete_path[i])
{
part_of_path = ft_strjoin(data->complete_path[i], "/");
cmd_path = ft_strjoin(part_of_path, cmd);
free(part_of_path);
if (access(cmd_path, F_OK) == 0)
{
free_array(data->complete_path);
return (cmd_path);
}
free(cmd_path);
i++;
}
free_array(data->complete_path);
return (NULL);
}

char *path_checker(t_data *data, char *cmd, char **env)
{
int i;
struct stat buf;

i = 0;
if (!cmd || !*cmd)
return (NULL);
stat(cmd, &buf);
if (S_ISDIR(buf.st_mode))
return (NULL);
if (!access(cmd, F_OK) && !access(cmd, X_OK))
return (ft_strjoin(cmd, ""));
while (env[i] && ft_strnstr(env[i], "PATH", 4) == 0)
i++;
if (env[i] == NULL)
return (NULL);
data->complete_path = ft_split(env[i] + 5, ':');
return (creat_path(data, cmd));
}
123 changes: 123 additions & 0 deletions srcs/execution/ft_cd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yagnaou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/13 19:20:49 by azabir #+# #+# */
/* Updated: 2022/09/10 21:08:04 by yagnaou ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/minishell.h"

void set_old_pwd(t_data *data)
{
char *old_pwd;
char *tmp;
int index;
char **tmp2;

old_pwd = getcwd(NULL, 0);
index = return_index(data->env, "OLDPWD");
if (index == -1)
{
tmp2 = malloc(sizeof(char *) * 2);
tmp2[0] = "OLDPWD=";
tmp2[1] = NULL;
ft_export(tmp2, data);
free(tmp2);
index = return_index(data->env, "OLDPWD");
}
if (old_pwd)
tmp = ft_strjoin("OLDPWD=", old_pwd);
else
tmp = ft_strjoin("OLDPWD=", data->env[index] + 7);
free(data->env[index]);
data->env[index] = ft_strdup(tmp);
free(old_pwd);
free(tmp);
}

void set_new_pwd(t_data *data)
{
char *new_pwd;
char *tmp;
int index;
char **tmp2;

new_pwd = getcwd(NULL, 0);
index = return_index(data->env, "PWD");
if (index == -1)
{
tmp2 = malloc(sizeof(char *) * 2);
tmp2[0] = "PWD=";
tmp2[1] = NULL;
ft_export(tmp2, data);
free(tmp2);
index = return_index(data->env, "PWD");
}
if (new_pwd)
tmp = ft_strjoin("PWD=", new_pwd);
else
tmp = ft_strjoin("PWD=", data->env[index] + 4);
free(data->env[index]);
data->env[index] = ft_strdup(tmp);
free(new_pwd);
free(tmp);
}

char *get_from_env(t_data *data, char *str, int size, int start)
{
int i;

i = 0;
while (data->env[i])
{
if (ft_strncmp(data->env[i], str, size) == 0)
return (data->env[i] + start);
i++;
}
return (NULL);
}

void go_to_env(t_data *data, char *path)
{
int ret;

set_old_pwd(data);
ret = chdir(path);
if (ret == -1)
{
printf("minishel: cd: no such file or directory\n");
g_exit_code = 1;
}
set_new_pwd(data);
}

void ft_cd(t_data *data, char *path)
{
char *env;

if (!path)
{
env = get_from_env(data, "HOME=", 5, 5);
if (!env)
{
printf("minishell: cd: HOME not set\n");
g_exit_code = 1;
return ;
}
if (env && !env[0])
{
chdir(".");
return ;
}
go_to_env(data, env);
}
else
{
go_to_env(data, path);
}
}
Loading

0 comments on commit 6485ceb

Please sign in to comment.