Compare commits
6 Commits
e89ccb64b2
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
5dbce99ee2 | ||
|
b9c1a344b2 | ||
|
7db099a010 | ||
|
e550d2bb37 | ||
|
f0837247bd | ||
|
2a2f5bd4d3 |
16
Makefile
16
Makefile
@@ -6,7 +6,7 @@
|
|||||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
|
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
|
||||||
# Updated: 2019/10/31 14:05:15 by tmaze ### ########.fr #
|
# Updated: 2020/02/14 11:56:31 by tmaze ### ########.fr #
|
||||||
# #
|
# #
|
||||||
#******************************************************************************#
|
#******************************************************************************#
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ endif
|
|||||||
|
|
||||||
# Compilator
|
# Compilator
|
||||||
CC = gcc
|
CC = gcc
|
||||||
FLAGS = -Wall -Wextra -Werror -g
|
FLAGS = -Wall -Wextra -Werror
|
||||||
|
|
||||||
# Folders
|
# Folders
|
||||||
LIBDIR = libft
|
LIBDIR = libft
|
||||||
@@ -38,13 +38,17 @@ INCDIR = includes libft/includes
|
|||||||
# Source files
|
# Source files
|
||||||
SRC = main.c \
|
SRC = main.c \
|
||||||
cmd_env.c \
|
cmd_env.c \
|
||||||
cmd_setenv.c \
|
cmd_setenv.c \
|
||||||
cmd_unsetenv.c \
|
cmd_unsetenv.c \
|
||||||
cmd_cd.c \
|
cmd_cd.c \
|
||||||
|
cmd_cd_utils.c \
|
||||||
cmd_echo.c \
|
cmd_echo.c \
|
||||||
ms_exec.c \
|
ms_exec.c \
|
||||||
ms_env.c \
|
ms_ext.c \
|
||||||
ms_ext.c
|
put_error_cd.c \
|
||||||
|
ms_exec_utils.c \
|
||||||
|
def_env.c \
|
||||||
|
check_path_slash_cd.c
|
||||||
|
|
||||||
OBJ = $(SRC:.c=.o)
|
OBJ = $(SRC:.c=.o)
|
||||||
|
|
||||||
|
33
README.org
33
README.org
@@ -1,33 +0,0 @@
|
|||||||
# **************************************************************************** #
|
|
||||||
# #
|
|
||||||
# ::: :::::::: #
|
|
||||||
# README.org :+: :+: :+: #
|
|
||||||
# +:+ +:+ +:+ #
|
|
||||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
|
||||||
# +#+#+#+#+#+ +#+ #
|
|
||||||
# Created: 2018/10/26 13:33:24 by tmaze #+# #+# #
|
|
||||||
# Updated: 2018/11/18 16:57:11 by tmaze ### ########.fr #
|
|
||||||
# #
|
|
||||||
# **************************************************************************** #
|
|
||||||
|
|
||||||
#+AUTHOR: tmaze
|
|
||||||
#+TITLE: 42Minishell
|
|
||||||
|
|
||||||
/Minishell project from Ecole 42/
|
|
||||||
|
|
||||||
* Todo list
|
|
||||||
** TODO étudier les différents man du sujet [5/10]
|
|
||||||
- [X] access
|
|
||||||
- [X] open, close, read, write
|
|
||||||
- [X] getcwd, chdir
|
|
||||||
- [X] stat, lstat, fstat
|
|
||||||
- [X] fork, execve
|
|
||||||
- [ ] wait, waitpid, wait3, wait4
|
|
||||||
- [ ] signal, kill
|
|
||||||
- [ ] exit
|
|
||||||
- [ ] environ
|
|
||||||
- [ ] builtin
|
|
||||||
** TODO réaliser un shell qui accepte les commandes
|
|
||||||
- [ ] lanceur de binaires
|
|
||||||
- [ ] gestion de l'environement
|
|
||||||
** TODO réaliser les builtins
|
|
@@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
|
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/10/31 14:38:16 by tmaze ### ########.fr */
|
/* Updated: 2020/02/01 15:57:09 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -19,13 +19,6 @@
|
|||||||
|
|
||||||
# define S_BIN 5
|
# define S_BIN 5
|
||||||
|
|
||||||
typedef struct s_env
|
|
||||||
{
|
|
||||||
char *key;
|
|
||||||
char *val;
|
|
||||||
struct s_env *next;
|
|
||||||
} t_env;
|
|
||||||
|
|
||||||
typedef struct s_builtin
|
typedef struct s_builtin
|
||||||
{
|
{
|
||||||
char *cmd;
|
char *cmd;
|
||||||
@@ -38,15 +31,25 @@ int cmd_unsetenv(char **argv, t_env **env);
|
|||||||
int cmd_cd(char **argv, t_env **env);
|
int cmd_cd(char **argv, t_env **env);
|
||||||
int cmd_echo(char **argv, t_env **env);
|
int cmd_echo(char **argv, t_env **env);
|
||||||
|
|
||||||
void lstdelelem(t_env **elem);
|
|
||||||
void lstdel(t_env **env);
|
|
||||||
t_env *lstnew(char *env);
|
|
||||||
t_env *lstaddend(t_env **alst, t_env *new);
|
|
||||||
t_env *lstgetelem(char *key, t_env *env);
|
|
||||||
char **lsttotab(t_env *env);
|
|
||||||
|
|
||||||
char **res_ext(char **argv, t_env *env);
|
char **res_ext(char **argv, t_env *env);
|
||||||
|
|
||||||
int exec_cmd(char **argv, t_env **env);
|
int exec_cmd(char **argv, t_env **env);
|
||||||
|
void *ft_strdel_null(char **s);
|
||||||
|
|
||||||
|
void *put_error_cd(char *file, char *msg);
|
||||||
|
int put_error_cd2(char *file, char *msg);
|
||||||
|
|
||||||
|
char *check_path_slash_cd(char *exec);
|
||||||
|
|
||||||
|
void exec_cmd_child(char *path, char **argv, char **env_tab,
|
||||||
|
t_env **env);
|
||||||
|
int ft_isin(char *str, char c);
|
||||||
|
void *put_error(char *file, char *msg);
|
||||||
|
|
||||||
|
t_env *def_env(void);
|
||||||
|
|
||||||
|
void *return_null(char *msg);
|
||||||
|
|
||||||
|
t_env *cd_updateenv(char *key, char *val, t_env **env);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2018/04/07 12:47:06 by tmaze #+# #+# #
|
# Created: 2018/04/07 12:47:06 by tmaze #+# #+# #
|
||||||
# Updated: 2019/03/27 17:46:28 by tmaze ### ########.fr #
|
# Updated: 2020/01/25 16:24:08 by tmaze ### ########.fr #
|
||||||
# #
|
# #
|
||||||
#******************************************************************************#
|
#******************************************************************************#
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ CCSTD =
|
|||||||
NAME = libft.a
|
NAME = libft.a
|
||||||
|
|
||||||
SRCS = ft_memalloc.c \
|
SRCS = ft_memalloc.c \
|
||||||
ft_memdel.c \
|
ft_memdel.c \
|
||||||
ft_memset.c \
|
ft_memset.c \
|
||||||
ft_bzero.c \
|
ft_bzero.c \
|
||||||
ft_memcpy.c \
|
ft_memcpy.c \
|
||||||
@@ -38,13 +38,13 @@ SRCS = ft_memalloc.c \
|
|||||||
ft_iswhitespace.c \
|
ft_iswhitespace.c \
|
||||||
ft_issign.c \
|
ft_issign.c \
|
||||||
\
|
\
|
||||||
ft_strnew.c \
|
ft_strnew.c \
|
||||||
ft_strdel.c \
|
ft_strdel.c \
|
||||||
ft_strclr.c \
|
ft_strclr.c \
|
||||||
ft_striter.c \
|
ft_striter.c \
|
||||||
ft_striteri.c \
|
ft_striteri.c \
|
||||||
ft_strmap.c \
|
ft_strmap.c \
|
||||||
ft_strmapi.c \
|
ft_strmapi.c \
|
||||||
ft_strequ.c \
|
ft_strequ.c \
|
||||||
ft_strnequ.c \
|
ft_strnequ.c \
|
||||||
ft_strsub.c \
|
ft_strsub.c \
|
||||||
@@ -90,7 +90,7 @@ SRCS = ft_memalloc.c \
|
|||||||
ft_putnbr_fd.c \
|
ft_putnbr_fd.c \
|
||||||
ft_putstrpad.c \
|
ft_putstrpad.c \
|
||||||
ft_putnbrpad.c \
|
ft_putnbrpad.c \
|
||||||
ft_putstrn.c \
|
ft_putstrn.c \
|
||||||
ft_print_words_tables.c \
|
ft_print_words_tables.c \
|
||||||
ft_putendl2.c \
|
ft_putendl2.c \
|
||||||
ft_putendl_fd2.c \
|
ft_putendl_fd2.c \
|
||||||
@@ -123,8 +123,8 @@ SRCS = ft_memalloc.c \
|
|||||||
ft_printf_tools_float.c \
|
ft_printf_tools_float.c \
|
||||||
ft_printf_tools_hexa.c \
|
ft_printf_tools_hexa.c \
|
||||||
ft_printf_tools_int.c \
|
ft_printf_tools_int.c \
|
||||||
ft_printf_tools_lenght.c \
|
ft_printf_tools_lenght.c \
|
||||||
ft_printf_tools_malloc_size.c \
|
ft_printf_tools_malloc_size.c \
|
||||||
ft_printf_tools_oct.c \
|
ft_printf_tools_oct.c \
|
||||||
ft_printf_tools_prec_size.c \
|
ft_printf_tools_prec_size.c \
|
||||||
ft_printf_tools_str.c \
|
ft_printf_tools_str.c \
|
||||||
@@ -136,7 +136,17 @@ SRCS = ft_memalloc.c \
|
|||||||
ft_getline.c \
|
ft_getline.c \
|
||||||
\
|
\
|
||||||
ft_sort_params.c \
|
ft_sort_params.c \
|
||||||
ft_realpath.c
|
ft_realpath.c \
|
||||||
|
\
|
||||||
|
ft_envaddend.c \
|
||||||
|
ft_envclean.c \
|
||||||
|
ft_envdel.c \
|
||||||
|
ft_envdelelem.c \
|
||||||
|
ft_envgetelem.c \
|
||||||
|
ft_envnew.c \
|
||||||
|
ft_envtochar.c \
|
||||||
|
ft_envtotab.c \
|
||||||
|
ft_envupdate.c
|
||||||
|
|
||||||
SRCDIR = srcs
|
SRCDIR = srcs
|
||||||
OBJDIR = objs
|
OBJDIR = objs
|
||||||
@@ -160,6 +170,6 @@ clean:
|
|||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
rm -f $(NAME)
|
rm -f $(NAME)
|
||||||
rm -rf $(OBJDIR)
|
rm -rf $(OBJDIR)
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */
|
/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/03/20 16:46:41 by tmaze ### ########.fr */
|
/* Updated: 2020/02/14 12:28:38 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -119,6 +119,13 @@ typedef struct s_conv
|
|||||||
int str_size;
|
int str_size;
|
||||||
} t_conv;
|
} t_conv;
|
||||||
|
|
||||||
|
typedef struct s_env
|
||||||
|
{
|
||||||
|
char *key;
|
||||||
|
char *val;
|
||||||
|
struct s_env *next;
|
||||||
|
} t_env;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Print functions
|
** Print functions
|
||||||
*/
|
*/
|
||||||
@@ -364,4 +371,15 @@ int ft_getline(char **line);
|
|||||||
int ft_putendl2(char const *s);
|
int ft_putendl2(char const *s);
|
||||||
int ft_putendl_fd2(char const *s, int fd);
|
int ft_putendl_fd2(char const *s, int fd);
|
||||||
|
|
||||||
|
void ft_envdelelem(t_env **elem);
|
||||||
|
void ft_envdel(t_env **env);
|
||||||
|
t_env *ft_envnew(char *env);
|
||||||
|
t_env *ft_envnewinit(char *key, char *val);
|
||||||
|
t_env *ft_envaddend(t_env **alst, t_env *new);
|
||||||
|
t_env *ft_envgetelem(char *key, t_env *env);
|
||||||
|
char *ft_envtochar(t_env *env);
|
||||||
|
char **ft_envtotab(t_env *env);
|
||||||
|
void ft_envclean(t_env **env);
|
||||||
|
char *ft_envupdate(char *key, t_env *env, char *val);;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
31
libft/srcs/ft_envaddend.c
Normal file
31
libft/srcs/ft_envaddend.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envaddend.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/05 15:02:33 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2019/12/09 13:00:42 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
t_env *ft_envaddend(t_env **alst, t_env *new)
|
||||||
|
{
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
if (new == NULL)
|
||||||
|
return (NULL);
|
||||||
|
if (*alst == NULL)
|
||||||
|
*alst = new;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tmp = *alst;
|
||||||
|
while (tmp->next)
|
||||||
|
tmp = tmp->next;
|
||||||
|
tmp->next = new;
|
||||||
|
}
|
||||||
|
return (*alst);
|
||||||
|
}
|
32
libft/srcs/ft_envclean.c
Normal file
32
libft/srcs/ft_envclean.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envclean.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/06 09:53:25 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2019/12/06 09:53:47 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_envclean(t_env **env)
|
||||||
|
{
|
||||||
|
t_env **it;
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
it = env;
|
||||||
|
while (*it)
|
||||||
|
{
|
||||||
|
if (ft_strequ((*it)->val, ""))
|
||||||
|
{
|
||||||
|
tmp = *it;
|
||||||
|
*it = tmp->next;
|
||||||
|
ft_envdelelem(&tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
it = &((*it)->next);
|
||||||
|
}
|
||||||
|
}
|
25
libft/srcs/ft_envdel.c
Normal file
25
libft/srcs/ft_envdel.c
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envdel.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/05 14:51:20 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2019/12/09 13:00:46 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_envdel(t_env **env)
|
||||||
|
{
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
while (*env)
|
||||||
|
{
|
||||||
|
tmp = *env;
|
||||||
|
*env = (*env)->next;
|
||||||
|
ft_envdelelem(&tmp);
|
||||||
|
}
|
||||||
|
}
|
23
libft/srcs/ft_envdelelem.c
Normal file
23
libft/srcs/ft_envdelelem.c
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envdelelem.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/05 14:43:45 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2019/12/09 13:00:45 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_envdelelem(t_env **elem)
|
||||||
|
{
|
||||||
|
if (elem)
|
||||||
|
{
|
||||||
|
ft_strdel(&((*elem)->key));
|
||||||
|
ft_strdel(&((*elem)->val));
|
||||||
|
ft_memdel((void**)elem);
|
||||||
|
}
|
||||||
|
}
|
23
libft/srcs/ft_envgetelem.c
Normal file
23
libft/srcs/ft_envgetelem.c
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envgetelem.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/05 16:46:17 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2019/12/05 16:47:00 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
t_env *ft_envgetelem(char *key, t_env *env)
|
||||||
|
{
|
||||||
|
t_env *it;
|
||||||
|
|
||||||
|
it = env;
|
||||||
|
while (it && !ft_strequ(it->key, key))
|
||||||
|
it = it->next;
|
||||||
|
return (it);
|
||||||
|
}
|
44
libft/srcs/ft_envnew.c
Normal file
44
libft/srcs/ft_envnew.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envnew.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/05 14:54:55 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2020/02/14 12:28:20 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
t_env *ft_envnew(char *env)
|
||||||
|
{
|
||||||
|
t_env *ret;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
ret = NULL;
|
||||||
|
while (env[i] && env[i] != '=')
|
||||||
|
i++;
|
||||||
|
if (env[i] == '=' && (ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
|
||||||
|
if ((ret->val = ft_strsub(env, i + 1, ft_strlen(env) - i - 1)) == NULL
|
||||||
|
|| (ret->key = ft_strsub(env, 0, i)) == NULL)
|
||||||
|
ft_envdelelem(&ret);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_env *ft_envnewinit(char *key, char *val)
|
||||||
|
{
|
||||||
|
t_env *ret;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
ret = NULL;
|
||||||
|
if ((ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
|
||||||
|
if ((ret->key = ft_strdup(key)) != NULL)
|
||||||
|
if ((ret->val = ft_strdup(val)) != NULL)
|
||||||
|
return (ret);
|
||||||
|
ft_envdelelem(&ret);
|
||||||
|
return (ret);
|
||||||
|
}
|
28
libft/srcs/ft_envtochar.c
Normal file
28
libft/srcs/ft_envtochar.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envtochar.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/05 17:28:25 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2019/12/09 13:00:44 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_envtochar(t_env *env)
|
||||||
|
{
|
||||||
|
int size;
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
size = ft_strlen(env->key) + ft_strlen(env->val) + 2;
|
||||||
|
if ((ret = ft_strnew(size)) != NULL)
|
||||||
|
{
|
||||||
|
ft_strlcpy(ret, env->key, size);
|
||||||
|
ft_strlcat(ret, "=", size);
|
||||||
|
ft_strlcat(ret, env->val, size);
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
|
}
|
41
libft/srcs/ft_envtotab.c
Normal file
41
libft/srcs/ft_envtotab.c
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envtotab.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/05 17:34:22 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2019/12/05 17:34:41 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char **ft_envtotab(t_env *env)
|
||||||
|
{
|
||||||
|
t_env *it;
|
||||||
|
char *new;
|
||||||
|
char **ret;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
ret = NULL;
|
||||||
|
it = env;
|
||||||
|
while (it && (++i))
|
||||||
|
it = it->next;
|
||||||
|
if ((ret = (char**)ft_memalloc(sizeof(char*) * (i + 1))) == NULL)
|
||||||
|
return (NULL);
|
||||||
|
it = env;
|
||||||
|
i = 0;
|
||||||
|
while (it)
|
||||||
|
{
|
||||||
|
if ((new = ft_envtochar(it)) == NULL)
|
||||||
|
ft_del_words_tables(&ret);
|
||||||
|
if (new == NULL)
|
||||||
|
break ;
|
||||||
|
ret[i++] = new;
|
||||||
|
it = it->next;
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
|
}
|
28
libft/srcs/ft_envupdate.c
Normal file
28
libft/srcs/ft_envupdate.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envupdate.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/12/05 19:10:38 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2020/02/07 15:26:10 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_envupdate(char *key, t_env *env, char *val)
|
||||||
|
{
|
||||||
|
t_env *elem;
|
||||||
|
char *new;
|
||||||
|
|
||||||
|
new = NULL;
|
||||||
|
if ((elem = ft_envgetelem(key, env)) != NULL)
|
||||||
|
if ((new = ft_strdup(val)) != NULL)
|
||||||
|
{
|
||||||
|
ft_strdel(&elem->val);
|
||||||
|
elem->val = new;
|
||||||
|
}
|
||||||
|
return (new);
|
||||||
|
}
|
@@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2018/04/07 17:00:34 by tmaze #+# #+# */
|
/* Created: 2018/04/07 17:00:34 by tmaze #+# #+# */
|
||||||
/* Updated: 2018/04/10 14:15:23 by tmaze ### ########.fr */
|
/* Updated: 2020/01/22 17:51:11 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
36
srcs/check_path_slash_cd.c
Normal file
36
srcs/check_path_slash_cd.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* check_path_slach_cd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/02/01 15:29:57 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2020/02/01 15:31:19 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
char *check_path_slash_cd(char *exec)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
struct stat info;
|
||||||
|
struct stat info2;
|
||||||
|
|
||||||
|
if (exec[0] == '/')
|
||||||
|
{
|
||||||
|
if ((i = access(exec, F_OK)) != 0)
|
||||||
|
return (put_error_cd(exec, "no such file or directory"));
|
||||||
|
if ((i = lstat(exec, &info)) != 0)
|
||||||
|
return (put_error_cd(exec, "can't determine info"));
|
||||||
|
if ((S_ISLNK(info.st_mode)
|
||||||
|
&& ((i = stat(exec, &info2)) != 0 || !S_ISDIR(info2.st_mode)))
|
||||||
|
&& !S_ISDIR(info.st_mode))
|
||||||
|
return (put_error_cd(exec, "not a directory"));
|
||||||
|
if ((i = access(exec, X_OK)) != 0)
|
||||||
|
return (put_error_cd(exec, "permission denied"));
|
||||||
|
return (exec);
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
108
srcs/cmd_cd.c
108
srcs/cmd_cd.c
@@ -5,16 +5,114 @@
|
|||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/09/20 14:42:42 by tmaze #+# #+# */
|
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/11/03 00:39:59 by tmaze ### ########.fr */
|
/* Updated: 2020/02/01 15:32:59 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int cmd_cd(char **argv, t_env **env)
|
int cmd_cd_update_env(char *path, t_env **env, char opt)
|
||||||
{
|
{
|
||||||
(void)argv;
|
t_env *pwd;
|
||||||
(void)env;
|
char *oldpwd;
|
||||||
|
char *c_path;
|
||||||
|
char p_path[4096];
|
||||||
|
|
||||||
|
ft_bzero(p_path, 4096);
|
||||||
|
if (!(pwd = ft_envgetelem("PWD", *env)))
|
||||||
|
return (put_error_cd2(path, "env var PWD undefined"));
|
||||||
|
if (getcwd(p_path, 4096) == NULL || (oldpwd = ft_strdup(pwd->val)) == NULL
|
||||||
|
|| ft_realpath(path, &c_path) == NULL)
|
||||||
|
return (put_error_cd2(path, "error"));
|
||||||
|
if (cd_updateenv("PWD", ((opt == 'P') ? p_path : c_path), env) == NULL
|
||||||
|
|| cd_updateenv("OLDPWD", oldpwd, env) == NULL)
|
||||||
|
{
|
||||||
|
ft_strdel(&oldpwd);
|
||||||
|
ft_strdel(&c_path);
|
||||||
|
return (put_error_cd2(path, "error"));
|
||||||
|
}
|
||||||
|
ft_strdel(&oldpwd);
|
||||||
|
ft_strdel(&c_path);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int cmd_cd_core(char *path, t_env **env, char opt)
|
||||||
|
{
|
||||||
|
t_env *pwd;
|
||||||
|
|
||||||
|
if (check_path_slash_cd(path) == NULL)
|
||||||
|
return (1);
|
||||||
|
if (chdir(path) == -1
|
||||||
|
|| (pwd = ft_envgetelem("PWD", *env)) == NULL)
|
||||||
|
{
|
||||||
|
put_error_cd(path, "error");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (cmd_cd_update_env(path, env, opt));
|
||||||
|
}
|
||||||
|
|
||||||
|
char cd_getparams(char **argv, size_t *i)
|
||||||
|
{
|
||||||
|
char ret[2];
|
||||||
|
size_t j;
|
||||||
|
|
||||||
|
ret[1] = '\0';
|
||||||
|
ret[0] = 'L';
|
||||||
|
*i = 0;
|
||||||
|
while (argv[++(*i)] && argv[*i][0] == '-' && ft_strlen(argv[*i]) > 1)
|
||||||
|
{
|
||||||
|
j = 1;
|
||||||
|
while (argv[*i][j] && (argv[*i][j] == 'L' || argv[*i][j] == 'P'))
|
||||||
|
ret[0] = argv[*i][j++];
|
||||||
|
if (argv[*i][j] && argv[*i][j] != 'L' && argv[*i][j] != 'P')
|
||||||
|
{
|
||||||
|
ret[0] = argv[*i][j];
|
||||||
|
put_error_cd(ret, "invalid option");
|
||||||
|
put_error_cd("usage", "cd [-L|-P] [dir]");
|
||||||
|
ret[0] = '\0';
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (ret[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmd_cd_switchboard(char **av, t_env **env, char opt, int i)
|
||||||
|
{
|
||||||
|
t_env *e;
|
||||||
|
char *p;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if ((!av[i] && (e = ft_envgetelem("HOME", *env)) != NULL && e->val
|
||||||
|
!= NULL) || (av[i] && av[i][0] == '-'
|
||||||
|
&& (e = ft_envgetelem("OLDPWD", *env)) != NULL))
|
||||||
|
return (cmd_cd_core(e->val, env, opt));
|
||||||
|
else if (av[i] && av[i][0] == '/')
|
||||||
|
return (cmd_cd_core(av[i], env, opt));
|
||||||
|
else if (av[i] && av[i][0] != '/'
|
||||||
|
&& (e = ft_envgetelem("PWD", *env)) != NULL && e->val != NULL)
|
||||||
|
{
|
||||||
|
if ((p = ft_strnew(ft_strlen(e->val) + ft_strlen(av[i]) + 1)) == NULL)
|
||||||
|
put_error_cd(av[i], "memory error");
|
||||||
|
if (p == NULL)
|
||||||
|
return (1);
|
||||||
|
ft_strcpy(p, e->val);
|
||||||
|
p[ft_strlen(p)] = '/';
|
||||||
|
ft_strcat(p, av[i]);
|
||||||
|
ret = cmd_cd_core(p, env, opt);
|
||||||
|
ft_strdel(&p);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmd_cd(char **argv, t_env **env)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char opt;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if ((opt = cd_getparams(argv, &i)) == '\0')
|
||||||
|
return (1);
|
||||||
|
return (cmd_cd_switchboard(argv, env, opt, i));
|
||||||
|
}
|
||||||
|
31
srcs/cmd_cd_utils.c
Normal file
31
srcs/cmd_cd_utils.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cmd_cd_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/02/14 11:55:54 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2020/02/14 11:56:53 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_env *cd_updateenv(char *key, char *val, t_env **env)
|
||||||
|
{
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
if ((tmp = ft_envgetelem(key, *env)) != NULL)
|
||||||
|
ft_envupdate(key, *env, val);
|
||||||
|
else if ((tmp = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
|
||||||
|
{
|
||||||
|
if ((tmp->key = ft_strdup(key)) == NULL)
|
||||||
|
ft_envdelelem(&tmp);
|
||||||
|
if (tmp && (tmp->val = ft_strdup(val)) == NULL)
|
||||||
|
ft_envdelelem(&tmp);
|
||||||
|
if (tmp)
|
||||||
|
ft_envaddend(env, tmp);
|
||||||
|
}
|
||||||
|
return (tmp);
|
||||||
|
}
|
@@ -14,9 +14,9 @@
|
|||||||
|
|
||||||
int cmd_echo(char **argv, t_env **env)
|
int cmd_echo(char **argv, t_env **env)
|
||||||
{
|
{
|
||||||
(void)env;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
(void)env;
|
||||||
i = 1;
|
i = 1;
|
||||||
while (argv[i])
|
while (argv[i])
|
||||||
ft_printf("%s ", argv[i++]);
|
ft_printf("%s ", argv[i++]);
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */
|
/* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/10/17 10:07:21 by tmaze ### ########.fr */
|
/* Updated: 2020/01/30 15:41:08 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -35,10 +35,36 @@ t_env *envcpy(t_env *env)
|
|||||||
if (env != NULL && (cpy = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
|
if (env != NULL && (cpy = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
|
||||||
if ((cpy->key = ft_strdup(env->key)) == NULL
|
if ((cpy->key = ft_strdup(env->key)) == NULL
|
||||||
|| (cpy->val = ft_strdup(env->val)) == NULL)
|
|| (cpy->val = ft_strdup(env->val)) == NULL)
|
||||||
lstdelelem(&cpy);
|
ft_envdelelem(&cpy);
|
||||||
return (cpy);
|
return (cpy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int env_cleanup(t_env *env_cp, int code)
|
||||||
|
{
|
||||||
|
ft_envdel(&env_cp);
|
||||||
|
if (code == 2)
|
||||||
|
ft_putstr_fd("minishell: env: Env copy error\n", 2);
|
||||||
|
return (code);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_env *inline_env(char **argv, int *i, t_env **env_cp)
|
||||||
|
{
|
||||||
|
t_env *new;
|
||||||
|
|
||||||
|
while (ft_strchr(argv[*i], '='))
|
||||||
|
{
|
||||||
|
if ((new = ft_envnew(argv[*i])) == NULL)
|
||||||
|
{
|
||||||
|
ft_printf("env: memory error\n");
|
||||||
|
ft_envdel(env_cp);
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
ft_envaddend(env_cp, new);
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
return (*env_cp);
|
||||||
|
}
|
||||||
|
|
||||||
int cmd_env(char **argv, t_env **env)
|
int cmd_env(char **argv, t_env **env)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -47,28 +73,23 @@ int cmd_env(char **argv, t_env **env)
|
|||||||
t_env *env_cp;
|
t_env *env_cp;
|
||||||
|
|
||||||
i = 1;
|
i = 1;
|
||||||
env_cp = NULL;
|
if (!(env_cp = NULL) && !ft_strequ(argv[i], "-i") && (env != NULL))
|
||||||
if (!ft_strequ(argv[i], "-i") && (env != NULL))
|
|
||||||
{
|
{
|
||||||
it = *env;
|
it = *env;
|
||||||
while (it)
|
while (it)
|
||||||
{
|
{
|
||||||
if ((new = envcpy(it)) == NULL)
|
if ((new = envcpy(it)) == NULL)
|
||||||
{
|
return (env_cleanup(env_cp, 2));
|
||||||
lstdel(&env_cp);
|
ft_envaddend(&env_cp, new);
|
||||||
ft_putstr_fd("minishell: env: Env copy error\n", 2);
|
|
||||||
return (2);
|
|
||||||
}
|
|
||||||
lstaddend(&env_cp, new);
|
|
||||||
it = it->next;
|
it = it->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ft_strequ(argv[i], "-i"))
|
else if (ft_strequ(argv[i], "-i"))
|
||||||
i++;
|
i++;
|
||||||
|
inline_env(argv, &i, &env_cp);
|
||||||
if (argv[i])
|
if (argv[i])
|
||||||
exec_cmd(argv + i, &env_cp);
|
exec_cmd(argv + i, &env_cp);
|
||||||
else
|
else
|
||||||
put_env(env_cp);
|
put_env(env_cp);
|
||||||
lstdel(&env_cp);
|
return (env_cleanup(env_cp, 0));
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/09/20 14:41:08 by tmaze #+# #+# */
|
/* Created: 2019/09/20 14:41:08 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/11/02 23:10:32 by tmaze ### ########.fr */
|
/* Updated: 2020/02/07 15:56:06 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -15,20 +15,22 @@
|
|||||||
int cmd_setenv(char **argv, t_env **env)
|
int cmd_setenv(char **argv, t_env **env)
|
||||||
{
|
{
|
||||||
t_env *new;
|
t_env *new;
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
if (argv[1] && (new = lstnew(argv[1])) != NULL)
|
if (argv[1] && (new = ft_envnew(argv[1])) != NULL)
|
||||||
{
|
{
|
||||||
lstaddend(env, new);
|
if (argv[1] && (tmp = ft_envgetelem(new->key, *env)) != NULL)
|
||||||
|
{
|
||||||
|
ft_envupdate(new->key, *env, new->val);
|
||||||
|
ft_envdelelem(&new);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ft_envaddend(env, new);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
else if (argv[1])
|
else if (argv[1] && argv[1][0] != '=' && !ft_strchr(argv[1], '='))
|
||||||
{
|
|
||||||
ft_printf("minishell: setenv: memory error\n");
|
ft_printf("minishell: setenv: memory error\n");
|
||||||
return (2);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
ft_printf("usage: setenv [KEY]=[value]\n");
|
ft_printf("usage: setenv [KEY]=[value]\n");
|
||||||
return (1);
|
return (1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/09/20 14:42:01 by tmaze #+# #+# */
|
/* Created: 2019/09/20 14:42:01 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/10/17 10:58:23 by tmaze ### ########.fr */
|
/* Updated: 2019/11/10 15:52:09 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ int cmd_unsetenv(char **argv, t_env **env)
|
|||||||
{
|
{
|
||||||
tmp = *it;
|
tmp = *it;
|
||||||
*it = tmp->next;
|
*it = tmp->next;
|
||||||
lstdelelem(&tmp);
|
ft_envdelelem(&tmp);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
it = &((*it)->next);
|
it = &((*it)->next);
|
||||||
|
40
srcs/def_env.c
Normal file
40
srcs/def_env.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* def_env.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/02/01 15:53:22 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2020/02/01 15:53:34 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_env *def_env(void)
|
||||||
|
{
|
||||||
|
t_env *ret;
|
||||||
|
t_env *n;
|
||||||
|
char p[PATH_MAX];
|
||||||
|
|
||||||
|
n = ft_memalloc(sizeof(t_env));
|
||||||
|
if (n != NULL && ((n->key = ft_strnew(3)) == NULL || !getcwd(p, PATH_MAX)))
|
||||||
|
ft_envdelelem(&n);
|
||||||
|
if (n != NULL)
|
||||||
|
ft_strcpy(n->key, "PWD");
|
||||||
|
if (n != NULL && (n->val = ft_strdup(p)) == NULL)
|
||||||
|
ft_envdelelem(&n);
|
||||||
|
if (n != NULL)
|
||||||
|
ft_envaddend(&ret, n);
|
||||||
|
n = ft_memalloc(sizeof(t_env));
|
||||||
|
if (n != NULL && (n->key = ft_strnew(5)) == NULL)
|
||||||
|
ft_envdelelem(&n);
|
||||||
|
if (n != NULL)
|
||||||
|
ft_strcpy(n->key, "SHLVL");
|
||||||
|
if (n != NULL && (n->val = ft_strdup("1")) == NULL)
|
||||||
|
ft_envdelelem(&n);
|
||||||
|
if (n != NULL)
|
||||||
|
ft_envaddend(&ret, n);
|
||||||
|
return (ret);
|
||||||
|
}
|
107
srcs/main.c
107
srcs/main.c
@@ -6,12 +6,24 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
|
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/10/31 14:36:15 by tmaze ### ########.fr */
|
/* Updated: 2020/02/07 15:11:58 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
char *inc_shlvl(char **shlvl)
|
||||||
|
{
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
if ((ret = ft_itoa(ft_atoi(*shlvl) + 1)) != NULL)
|
||||||
|
{
|
||||||
|
ft_strdel(shlvl);
|
||||||
|
*shlvl = ret;
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
t_env *env2lst(char **env)
|
t_env *env2lst(char **env)
|
||||||
{
|
{
|
||||||
t_env *ret;
|
t_env *ret;
|
||||||
@@ -20,84 +32,77 @@ t_env *env2lst(char **env)
|
|||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
ret = NULL;
|
ret = NULL;
|
||||||
while (env[i])
|
while (env && env[i])
|
||||||
{
|
{
|
||||||
if ((new = lstnew(env[i])) != NULL)
|
if ((new = ft_envnew(env[i])) != NULL)
|
||||||
lstaddend(&ret, new);
|
{
|
||||||
|
if (ft_strequ(new->key, "SHLVL") && ft_str_is_numeric(new->val))
|
||||||
|
inc_shlvl(&new->val);
|
||||||
|
ft_envaddend(&ret, new);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lstdelelem(&new);
|
ft_envdelelem(&new);
|
||||||
lstdel(&ret);
|
ft_envdel(&ret);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
if (i == 0 && ret == NULL && (ret = def_env()) == NULL)
|
||||||
|
ft_printf("Env: Memory Error\n");
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
void check_exec(char **argv, t_env *env)
|
||||||
{
|
{
|
||||||
extern char **environ;
|
int i;
|
||||||
static t_builtin built[S_BIN] = {{"env", &cmd_env}, {"cd", &cmd_cd}
|
static t_builtin built[S_BIN] = {{"env", &cmd_env}, {"cd", &cmd_cd}
|
||||||
, {"setenv", &cmd_setenv}, {"echo", &cmd_echo}
|
, {"setenv", &cmd_setenv}, {"echo", &cmd_echo}
|
||||||
, {"unsetenv", &cmd_unsetenv}};
|
, {"unsetenv", &cmd_unsetenv}};
|
||||||
char **argv;
|
|
||||||
t_env *env;
|
|
||||||
char *cmd;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
env = NULL;
|
i = -1;
|
||||||
if ((env = env2lst(environ)) == NULL)
|
while (++i < S_BIN)
|
||||||
|
if (ft_strequ(argv[0], built[i].cmd))
|
||||||
|
{
|
||||||
|
built[i].f(argv, &env);
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
if (i == S_BIN)
|
||||||
|
exec_cmd(argv, &env);
|
||||||
|
}
|
||||||
|
|
||||||
|
int cleanup(char **argv, t_env *env)
|
||||||
|
{
|
||||||
|
ft_del_words_tables(&argv);
|
||||||
|
ft_envdel(&env);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int ac , char **av, char **envp)
|
||||||
|
{
|
||||||
|
char **argv;
|
||||||
|
t_env *env;
|
||||||
|
char *cmd;
|
||||||
|
|
||||||
|
(void)ac;
|
||||||
|
(void)av;
|
||||||
|
if ((env = env2lst(envp)) == NULL)
|
||||||
return (2);
|
return (2);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
cmd = NULL;
|
|
||||||
ft_printf("%s$>%s ", FT_COLOR_GREEN, FT_RESET);
|
ft_printf("%s$>%s ", FT_COLOR_GREEN, FT_RESET);
|
||||||
if (ft_getline(&cmd) >= 0 && cmd != NULL
|
if (ft_getline(&cmd) >= 0 && cmd != NULL
|
||||||
&& (argv = ft_strsplit(cmd, ' ')) != NULL)
|
&& (argv = ft_strsplit(cmd, ' ')) != NULL && !ft_strdel_null(&cmd))
|
||||||
{
|
{
|
||||||
ft_strdel(&cmd);
|
|
||||||
i = 0;
|
|
||||||
ft_printf("before:\n");
|
|
||||||
while (argv[i])
|
|
||||||
{
|
|
||||||
ft_printf("%s\n", argv[i++]);
|
|
||||||
}
|
|
||||||
if ((res_ext(argv, env)) != NULL)
|
if ((res_ext(argv, env)) != NULL)
|
||||||
{
|
{
|
||||||
i = 0;
|
|
||||||
ft_printf("after:\n");
|
|
||||||
while (argv[i])
|
|
||||||
{
|
|
||||||
ft_printf("%s\n", argv[i++]);
|
|
||||||
}
|
|
||||||
if (ft_strequ(argv[0], "exit"))
|
if (ft_strequ(argv[0], "exit"))
|
||||||
{
|
return (cleanup(argv, env));
|
||||||
ft_del_words_tables(&argv);
|
check_exec(argv, env);
|
||||||
lstdel(&env);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
i = 0;
|
|
||||||
while (i < S_BIN)
|
|
||||||
{
|
|
||||||
if (ft_strequ(argv[0], built[i].cmd))
|
|
||||||
{
|
|
||||||
built[i].f(argv, &env);
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (i == S_BIN)
|
|
||||||
{
|
|
||||||
exec_cmd(argv, &env);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ft_strdel(&cmd);
|
ft_strdel(&cmd);
|
||||||
ft_del_words_tables(&argv);
|
ft_del_words_tables(&argv);
|
||||||
}
|
}
|
||||||
ft_del_words_tables(&argv);
|
|
||||||
ft_strdel(&cmd);
|
|
||||||
lstdel(&env);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
118
srcs/ms_env.c
118
srcs/ms_env.c
@@ -1,118 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ms_env.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2019/09/20 11:43:53 by tmaze #+# #+# */
|
|
||||||
/* Updated: 2019/10/18 14:20:41 by tmaze ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
void lstdelelem(t_env **elem)
|
|
||||||
{
|
|
||||||
if (elem)
|
|
||||||
{
|
|
||||||
ft_strdel(&((*elem)->key));
|
|
||||||
ft_strdel(&((*elem)->val));
|
|
||||||
ft_memdel((void**)elem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void lstdel(t_env **env)
|
|
||||||
{
|
|
||||||
t_env *tmp;
|
|
||||||
|
|
||||||
while (*env)
|
|
||||||
{
|
|
||||||
tmp = *env;
|
|
||||||
*env = (*env)->next;
|
|
||||||
lstdelelem(&tmp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t_env *lstnew(char *env)
|
|
||||||
{
|
|
||||||
t_env *ret;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
ret = NULL;
|
|
||||||
while (env[i] && env[i] != '=')
|
|
||||||
i++;
|
|
||||||
if (env[i] == '=' && (ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
|
|
||||||
if ((ret->val = ft_strsub(env, i + 1, ft_strlen(env) - i - 1)) == NULL
|
|
||||||
|| (ret->key = ft_strsub(env, 0, i)) == NULL)
|
|
||||||
lstdelelem(&ret);
|
|
||||||
return (ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_env *lstaddend(t_env **alst, t_env *new)
|
|
||||||
{
|
|
||||||
t_env *tmp;
|
|
||||||
|
|
||||||
if (new == NULL)
|
|
||||||
return (NULL);
|
|
||||||
if (*alst == NULL)
|
|
||||||
*alst = new;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tmp = *alst;
|
|
||||||
while (tmp->next)
|
|
||||||
tmp = tmp->next;
|
|
||||||
tmp->next = new;
|
|
||||||
}
|
|
||||||
return (*alst);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_env *lstgetelem(char *key, t_env *env)
|
|
||||||
{
|
|
||||||
t_env *it;
|
|
||||||
|
|
||||||
it = env;
|
|
||||||
while (it && !ft_strequ(it->key, key))
|
|
||||||
it = it->next;
|
|
||||||
return (it);
|
|
||||||
}
|
|
||||||
|
|
||||||
char **lsttotab(t_env *env)
|
|
||||||
{
|
|
||||||
t_env *it;
|
|
||||||
char *new;
|
|
||||||
char **ret;
|
|
||||||
size_t size;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
ret = NULL;
|
|
||||||
it = env;
|
|
||||||
while (it)
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
it = it->next;
|
|
||||||
}
|
|
||||||
if ((ret = (char**)ft_memalloc(sizeof(char*) * (i + 1))) == NULL)
|
|
||||||
return (NULL);
|
|
||||||
while (i >= 0)
|
|
||||||
ret[i--] = NULL;
|
|
||||||
it = env;
|
|
||||||
i = 0;
|
|
||||||
while (it)
|
|
||||||
{
|
|
||||||
size = ft_strlen(it->key) + ft_strlen(it->val) + 1;
|
|
||||||
if ((new = ft_strnew(size)) == NULL)
|
|
||||||
{
|
|
||||||
ft_del_words_tables(&ret);
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
ft_strlcpy(new, it->key, size);
|
|
||||||
ft_strlcat(new, "=", size);
|
|
||||||
ft_strlcat(new, it->val, size);
|
|
||||||
ret[i++] = new;
|
|
||||||
it = it->next;
|
|
||||||
}
|
|
||||||
return (ret);
|
|
||||||
}
|
|
118
srcs/ms_exec.c
118
srcs/ms_exec.c
@@ -6,31 +6,12 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
|
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/10/18 14:22:23 by tmaze ### ########.fr */
|
/* Updated: 2020/02/01 15:47:08 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int ft_isin(char *str, char c)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (str && str[i])
|
|
||||||
if (str[i++] == c)
|
|
||||||
return (i - 1);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void put_error(char *file, char *msg)
|
|
||||||
{
|
|
||||||
ft_putstr("minishell: ");
|
|
||||||
ft_putstr(file);
|
|
||||||
ft_putstr(": ");
|
|
||||||
ft_putendl(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *check_path_slash(char *exec)
|
char *check_path_slash(char *exec)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -62,12 +43,9 @@ char *check_path_dot(char *exec, t_env *env)
|
|||||||
char *tmp;
|
char *tmp;
|
||||||
char *ret;
|
char *ret;
|
||||||
|
|
||||||
if ((path = lstgetelem("PWD", env)) == NULL
|
if ((path = ft_envgetelem("PWD", env)) == NULL
|
||||||
|| path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL)
|
|| path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL)
|
||||||
{
|
return (put_error(exec, "memory error"));
|
||||||
put_error(exec, "memory error");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
||||||
put_error(exec, "memory error");
|
put_error(exec, "memory error");
|
||||||
ft_strdel(&tmp);
|
ft_strdel(&tmp);
|
||||||
@@ -77,13 +55,40 @@ char *check_path_dot(char *exec, t_env *env)
|
|||||||
{
|
{
|
||||||
if (access(ret, X_OK) == 0)
|
if (access(ret, X_OK) == 0)
|
||||||
return (exec);
|
return (exec);
|
||||||
put_error(exec, "permission denied");
|
|
||||||
ft_strdel(&ret);
|
ft_strdel(&ret);
|
||||||
return (NULL);
|
return (put_error(exec, "permission denied"));
|
||||||
}
|
}
|
||||||
put_error(exec, "no such file or directory");
|
|
||||||
ft_strdel(&ret);
|
ft_strdel(&ret);
|
||||||
return (NULL);
|
return (put_error(exec, "no such file or directory"));
|
||||||
|
}
|
||||||
|
|
||||||
|
char *check_path_elems(char **path_elems, char *exec)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *tmp;
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
ret = NULL;
|
||||||
|
while (path_elems[++i])
|
||||||
|
{
|
||||||
|
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL
|
||||||
|
&& !put_error(exec, "memory error"))
|
||||||
|
break ;
|
||||||
|
if ((ret = ft_strjoin(tmp, exec)) == NULL
|
||||||
|
&& !put_error(exec, "memory error") && !ft_strdel_null(&tmp))
|
||||||
|
break ;
|
||||||
|
if (!ft_strdel_null(&tmp) && access(ret, F_OK) == 0)
|
||||||
|
{
|
||||||
|
if (access(ret, X_OK) == 0)
|
||||||
|
break ;
|
||||||
|
ft_strdel(&ret);
|
||||||
|
ft_del_words_tables(&path_elems);
|
||||||
|
return (put_error(exec, "permission denied"));
|
||||||
|
}
|
||||||
|
ft_strdel(&ret);
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *check_path(char *exec, t_env *env)
|
char *check_path(char *exec, t_env *env)
|
||||||
@@ -91,7 +96,6 @@ char *check_path(char *exec, t_env *env)
|
|||||||
size_t i;
|
size_t i;
|
||||||
t_env *path;
|
t_env *path;
|
||||||
char **path_elems;
|
char **path_elems;
|
||||||
char *tmp;
|
|
||||||
char *ret;
|
char *ret;
|
||||||
|
|
||||||
ret = NULL;
|
ret = NULL;
|
||||||
@@ -101,41 +105,16 @@ char *check_path(char *exec, t_env *env)
|
|||||||
return (check_path_slash(exec));
|
return (check_path_slash(exec));
|
||||||
if (exec[0] == '.')
|
if (exec[0] == '.')
|
||||||
return (check_path_dot(exec, env));
|
return (check_path_dot(exec, env));
|
||||||
if ((path = lstgetelem("PATH", env)) == NULL
|
if ((path = ft_envgetelem("PATH", env)) == NULL
|
||||||
|| path->val == NULL
|
|| path->val == NULL
|
||||||
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
|
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
|
||||||
{
|
return (put_error(exec, "could not resolve path"));
|
||||||
put_error(exec, "could not resolve path");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (path_elems[i])
|
if ((ret = check_path_elems(path_elems, exec)) == NULL)
|
||||||
{
|
{
|
||||||
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL)
|
ft_del_words_tables(&path_elems);
|
||||||
put_error(exec, "memory error");
|
return (put_error(exec, "command not found"));
|
||||||
if (tmp == NULL)
|
|
||||||
break ;
|
|
||||||
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
|
||||||
{
|
|
||||||
put_error(exec, "memory error");
|
|
||||||
ft_strdel(&tmp);
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
ft_strdel(&tmp);
|
|
||||||
if (access(ret, F_OK) == 0)
|
|
||||||
{
|
|
||||||
if (access(ret, X_OK) == 0)
|
|
||||||
break ;
|
|
||||||
put_error(exec, "permission denied");
|
|
||||||
ft_strdel(&ret);
|
|
||||||
ft_del_words_tables(&path_elems);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
ft_strdel(&ret);
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
if (path_elems[i] == NULL)
|
|
||||||
put_error(exec, "command not found");
|
|
||||||
ft_del_words_tables(&path_elems);
|
ft_del_words_tables(&path_elems);
|
||||||
}
|
}
|
||||||
return (ret);
|
return (ret);
|
||||||
@@ -143,27 +122,18 @@ char *check_path(char *exec, t_env *env)
|
|||||||
|
|
||||||
int exec_cmd(char **argv, t_env **env)
|
int exec_cmd(char **argv, t_env **env)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
char **env_tab;
|
char **env_tab;
|
||||||
char *path;
|
char *path;
|
||||||
|
|
||||||
if ((env_tab = lsttotab(*env)) == NULL)
|
if ((env_tab = ft_envtotab(*env)) == NULL)
|
||||||
return (-1);
|
return (-1);
|
||||||
if ((path = check_path(argv[0], *env)) == NULL)
|
if ((path = check_path(argv[0], *env)) == NULL)
|
||||||
ft_del_words_tables(&env_tab);
|
ft_del_words_tables(&env_tab);
|
||||||
ft_printf("path after resolve: %s\n", path);
|
|
||||||
if (path == NULL)
|
if (path == NULL)
|
||||||
return (-1);
|
return (-1);
|
||||||
if ((ret = fork()) == 0)
|
if ((ret = fork()) == 0)
|
||||||
{
|
exec_cmd_child(path, argv, env_tab, env);
|
||||||
execve(path, argv, env_tab);
|
|
||||||
ft_putendl_fd("minishell: error", 1);
|
|
||||||
if (ft_strcmp(path, argv[0]) != 0)
|
|
||||||
ft_strdel(&path);
|
|
||||||
ft_del_words_tables(&env_tab);
|
|
||||||
lstdel(env);
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
else if (ret == -1)
|
else if (ret == -1)
|
||||||
ft_putendl_fd("minishell: error", 1);
|
ft_putendl_fd("minishell: error", 1);
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
|
56
srcs/ms_exec_utils.c
Normal file
56
srcs/ms_exec_utils.c
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ms_exec_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/02/01 15:43:41 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2020/02/01 15:56:49 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void exec_cmd_child(char *path, char **argv, char **env_tab, t_env **env)
|
||||||
|
{
|
||||||
|
execve(path, argv, env_tab);
|
||||||
|
ft_putendl_fd("minishell: error", 1);
|
||||||
|
if (ft_strcmp(path, argv[0]) != 0)
|
||||||
|
ft_strdel(&path);
|
||||||
|
ft_del_words_tables(&env_tab);
|
||||||
|
ft_envdel(env);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *ft_strdel_null(char **s)
|
||||||
|
{
|
||||||
|
ft_strdel(s);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_isin(char *str, char c)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str && str[i])
|
||||||
|
if (str[i++] == c)
|
||||||
|
return (i - 1);
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *put_error(char *file, char *msg)
|
||||||
|
{
|
||||||
|
ft_putstr("minishell: ");
|
||||||
|
ft_putstr(file);
|
||||||
|
ft_putstr(": ");
|
||||||
|
ft_putendl(msg);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *return_null(char *msg)
|
||||||
|
{
|
||||||
|
ft_printf("%s\n", msg);
|
||||||
|
return (NULL);
|
||||||
|
}
|
137
srcs/ms_ext.c
137
srcs/ms_ext.c
@@ -6,100 +6,105 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
|
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/11/02 00:35:13 by tmaze ### ########.fr */
|
/* Updated: 2020/02/07 15:55:59 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void ft_strreplace(char **dst, char *src, int *index, t_env *elem)
|
||||||
|
{
|
||||||
|
if (*dst != NULL)
|
||||||
|
ft_strdel(dst);
|
||||||
|
*dst = src;
|
||||||
|
*index = ft_strlen(elem->val);
|
||||||
|
}
|
||||||
|
|
||||||
t_env *search_key(t_env *env, char *cmd, int index)
|
t_env *search_key(t_env *env, char *cmd, int index)
|
||||||
{
|
{
|
||||||
t_env *it;
|
t_env *it;
|
||||||
|
t_env *ret;
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
it = env;
|
it = env;
|
||||||
while (it && cmd[index + 2])
|
ret = NULL;
|
||||||
|
while (it && cmd[index + 1])
|
||||||
{
|
{
|
||||||
i = 0;
|
i = 0;
|
||||||
j = index;
|
j = index;
|
||||||
while (it->key[i] && cmd[j] && cmd[j] != '}' && it->key[i] == cmd[j])
|
while (it->key[i] && cmd[j] && it->key[i] == cmd[j])
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
if (!it->key[i] && cmd[j] == '}')
|
if (!it->key[i] && (!ret || ft_strlen(it->key) > ft_strlen(ret->key)))
|
||||||
return (it);
|
ret = it;
|
||||||
it = it->next;
|
it = it->next;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
char **res_ext(char **argv, t_env *env)
|
char *replace_env(char *src, t_env *elem, int key_size, int i)
|
||||||
{
|
{
|
||||||
int i;
|
char *ret;
|
||||||
int j;
|
|
||||||
int k;
|
|
||||||
int res_len;
|
int res_len;
|
||||||
t_env *elem;
|
|
||||||
|
res_len = ft_strlen(src) - key_size + ft_strlen(elem->val);
|
||||||
|
if ((ret = ft_strnew(res_len)) != NULL)
|
||||||
|
{
|
||||||
|
ft_strncpy(ret, src, i);
|
||||||
|
ft_strlcat(ret, elem->val, res_len + 1);
|
||||||
|
ft_strlcat(ret, &src[i] + key_size, res_len + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ft_printf("minishell: memory error\n");
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *resolve_complete_key(char **src, int *index, t_env *env)
|
||||||
|
{
|
||||||
|
t_env *tmp;
|
||||||
char *ret;
|
char *ret;
|
||||||
|
|
||||||
k = 0;
|
if ((tmp = search_key(env, *src, *index + 1)) != NULL)
|
||||||
while (argv && argv[k])
|
|
||||||
{
|
{
|
||||||
i = 0;
|
if ((ret = replace_env(*src, tmp, ft_strlen(tmp->key)
|
||||||
while (argv[k] && argv[k][i]) {
|
+ 1, *index)) != NULL)
|
||||||
if (i == 0 && argv[k][i] == '~' && lstgetelem("HOME", env)) {
|
{
|
||||||
res_len =
|
ft_strreplace(src, ret, index, tmp);
|
||||||
ft_strlen(argv[k]) - 1 + ft_strlen(lstgetelem("HOME", env)->val);
|
return (*src);
|
||||||
if ((ret = ft_strnew(res_len)) != NULL) {
|
|
||||||
ft_strcpy(ret, lstgetelem("HOME", env)->val);
|
|
||||||
if (ret[ft_strlen(ret) - 1] != '/')
|
|
||||||
ft_strcat(ret, "/");
|
|
||||||
ft_strcat(ret, argv[k] + 1);
|
|
||||||
ft_strdel(&(argv[k]));
|
|
||||||
argv[k] = ret;
|
|
||||||
i = ft_strlen(lstgetelem("HOME", env)->val);
|
|
||||||
} else {
|
|
||||||
ft_printf("minishell: memory error\n");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
} else if (argv[k][i] == '$' && argv[k][i + 1] && argv[k][i + 1] == '{') {
|
|
||||||
j = i + 1;
|
|
||||||
while (argv[k][j] && argv[k][j] != '}')
|
|
||||||
j++;
|
|
||||||
if (!argv[k][j])
|
|
||||||
{
|
|
||||||
ft_printf("minishell: missing }\n");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
else if ((elem = search_key(env, argv[k], i + 2)) != NULL)
|
|
||||||
{
|
|
||||||
if ((ret = ft_strnew(i + ft_strlen(argv[k]) - j +
|
|
||||||
ft_strlen(elem->val))) != NULL)
|
|
||||||
{
|
|
||||||
ft_strncpy(ret, argv[k], i);
|
|
||||||
ft_strcat(ret, elem->val);
|
|
||||||
ft_strcat(ret, argv[k] + j + 1);
|
|
||||||
ft_strdel(&(argv[k]));
|
|
||||||
argv[k] = ret;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ft_printf("minishell: memory error\n");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ft_printf("minishell: variable %*s not found\n", j - i - 1,
|
|
||||||
argv[k] + i + 2);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
k++;
|
else
|
||||||
|
return (return_null("minishell: memory error"));
|
||||||
}
|
}
|
||||||
return (argv);
|
else
|
||||||
|
return (return_null("minishell: variable not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
char **res_ext(char **av, t_env *env)
|
||||||
|
{
|
||||||
|
int i[2];
|
||||||
|
t_env *tmp;
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
i[0] = -1;
|
||||||
|
while (av && av[++i[0]] && !(i[1] = 0))
|
||||||
|
{
|
||||||
|
while (av[i[0]][i[1]])
|
||||||
|
{
|
||||||
|
if (av[i[0]][i[1]] != '$' && av[i[0]][i[1]] != '~')
|
||||||
|
i[1]++;
|
||||||
|
else if (av[i[0]][i[1]] == '~' && (tmp = ft_envgetelem("HOME", env))
|
||||||
|
!= NULL && (ret = replace_env(av[i[0]], tmp, 1, i[1])) != NULL)
|
||||||
|
ft_strreplace(&av[i[0]], ret, &i[1], tmp);
|
||||||
|
else if (av[i[0]][i[1]] == '~' && (tmp = ft_envgetelem("HOME", env))
|
||||||
|
== NULL)
|
||||||
|
return (return_null("Entry HOME not found\n"));
|
||||||
|
else if (av[i[0]][i[1]] == '$'
|
||||||
|
&& resolve_complete_key(&av[i[0]], &i[1], env) == NULL)
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (av);
|
||||||
}
|
}
|
||||||
|
31
srcs/put_error_cd.c
Normal file
31
srcs/put_error_cd.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* put_error_cd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/01/26 22:18:09 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2020/01/26 22:18:24 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void *put_error_cd(char *file, char *msg)
|
||||||
|
{
|
||||||
|
ft_putstr("cd: ");
|
||||||
|
ft_putstr(file);
|
||||||
|
ft_putstr(": ");
|
||||||
|
ft_putendl(msg);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int put_error_cd2(char *file, char *msg)
|
||||||
|
{
|
||||||
|
ft_putstr("cd: ");
|
||||||
|
ft_putstr(file);
|
||||||
|
ft_putstr(": ");
|
||||||
|
ft_putendl(msg);
|
||||||
|
return (1);
|
||||||
|
}
|
Reference in New Issue
Block a user