Compare commits

..

6 Commits

Author SHA1 Message Date
Tanguy MAZE
5dbce99ee2 corrected make' 2020-02-14 12:31:39 +01:00
Tanguy MAZE
b9c1a344b2 bug fix env -i && cd error message 2020-02-14 12:22:35 +01:00
Tanguy MAZE
7db099a010 Corrected extension in setenv 2020-02-07 15:57:21 +01:00
Tanguy MAZE
e550d2bb37 protect empty env 2020-02-07 15:12:10 +01:00
Tanguy MAZE
f0837247bd file norming 2020-02-01 16:02:38 +01:00
tvdu29
2a2f5bd4d3 Dev (#2)
* Restart from scratch

branched out on dev
removed all previous source files
started test on new env2lst function

* norm: splitted functions in main into appropriate file

Moved lstdelenvelem & env2lst to ms_env.c
Normed out files

* Feat(WIP): Added possibility to cmd_env to execute commands with
altered env.

Added dummy function for cd, echo, setenv & unsetenv
Started work on env copy feature

* feature: switched env from ft_list to specific list

* feature: added execution of commands from path

* feature: added env, setenv & unsetenv builtins

added -i option & command execution to env
added setenv & unsetenv builtins

* Clean-up: normed out files

* Changed comportment on error for check_path functions

* feature: added completion for ~ and ${}

WIP leaks detected on ${} completion

* fix leak on  ext need test

* feature: added echo cmd

changed printf ref to ft_printf

* feature: add cd built-in

* Simple norm-out

* moved env functions to libft

* fixed out-of-memory access in extension

* Resolved infinite loop in extension revolving

WIP norming in cd

* Normedout cmd_cd.c

* Normed out functions in cmd_cd

* Normed out funtions

sorting out needed

* removed -fsanitize from Makefile

* corrected env -i crash & SHLVL

* Delete norm.txt

* added put_error_cd2 in put_error_cd.c

* added inline environment
2020-01-31 15:06:17 +01:00
16 changed files with 219 additions and 156 deletions

4
.ccls
View File

@@ -1,4 +0,0 @@
clang
-Iincludes
-Ilibft/includes
%c %h -Wall -Wextra -Werror -g-Wall -Wextra -Werror -g

View File

@@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2020/01/30 12:00:42 by tmaze ### ########.fr #
# Updated: 2020/02/14 11:56:31 by tmaze ### ########.fr #
# #
#******************************************************************************#
@@ -38,13 +38,17 @@ INCDIR = includes libft/includes
# Source files
SRC = main.c \
cmd_env.c \
cmd_setenv.c \
cmd_unsetenv.c \
cmd_setenv.c \
cmd_unsetenv.c \
cmd_cd.c \
cmd_cd_utils.c \
cmd_echo.c \
ms_exec.c \
ms_ext.c \
put_error_cd.c
put_error_cd.c \
ms_exec_utils.c \
def_env.c \
check_path_slash_cd.c
OBJ = $(SRC:.c=.o)

View File

@@ -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

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
/* Updated: 2020/01/26 22:19:25 by tmaze ### ########.fr */
/* Updated: 2020/02/01 15:57:09 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@@ -39,4 +39,17 @@ 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

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */
/* Updated: 2019/12/06 09:57:17 by tmaze ### ########.fr */
/* Updated: 2020/02/14 12:28:38 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@@ -374,6 +374,7 @@ 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);

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 14:54:55 by tmaze #+# #+# */
/* Updated: 2019/12/05 17:12:59 by tmaze ### ########.fr */
/* Updated: 2020/02/14 12:28:20 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@@ -27,3 +27,18 @@ t_env *ft_envnew(char *env)
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);
}

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 19:10:38 by tmaze #+# #+# */
/* Updated: 2019/12/05 19:10:52 by tmaze ### ########.fr */
/* Updated: 2020/02/07 15:26:10 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */

View 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);
}

View File

@@ -6,35 +6,12 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
/* Updated: 2020/01/26 22:17:48 by tmaze ### ########.fr */
/* Updated: 2020/02/01 15:32:59 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);
}
int cmd_cd_update_env(char *path, t_env **env, char opt)
{
t_env *pwd;
@@ -48,8 +25,8 @@ int cmd_cd_update_env(char *path, t_env **env, char opt)
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 (ft_envupdate("PWD", *env, ((opt == 'P') ? p_path : c_path)) == NULL
|| ft_envupdate("OLDPWD", *env, oldpwd) == NULL)
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);

31
srcs/cmd_cd_utils.c Normal file
View 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);
}

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:41:08 by tmaze #+# #+# */
/* Updated: 2019/11/10 21:38:30 by tmaze ### ########.fr */
/* Updated: 2020/02/07 15:56:06 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,14 +21,11 @@ int cmd_setenv(char **argv, t_env **env)
{
if (argv[1] && (tmp = ft_envgetelem(new->key, *env)) != NULL)
{
ft_strdel(&(tmp->val));
tmp->val = new->val;
ft_strdel(&(new->key));
ft_memdel((void**)&new);
ft_envupdate(new->key, *env, new->val);
ft_envdelelem(&new);
}
else
ft_envaddend(env, new);
ft_envclean(env);
return (0);
}
else if (argv[1] && argv[1][0] != '=' && !ft_strchr(argv[1], '='))

40
srcs/def_env.c Normal file
View 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);
}

View File

@@ -6,39 +6,12 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
/* Updated: 2020/01/30 13:47:35 by tmaze ### ########.fr */
/* Updated: 2020/02/07 15:11:58 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);
}
char *inc_shlvl(char **shlvl)
{
char *ret;
@@ -59,7 +32,7 @@ t_env *env2lst(char **env)
i = 0;
ret = NULL;
while (env[i])
while (env && env[i])
{
if ((new = ft_envnew(env[i])) != NULL)
{
@@ -105,14 +78,15 @@ int cleanup(char **argv, t_env *env)
return (0);
}
int main(void)
int main(int ac , char **av, char **envp)
{
extern char **environ;
char **argv;
t_env *env;
char *cmd;
if ((env = env2lst(environ)) == NULL)
(void)ac;
(void)av;
if ((env = env2lst(envp)) == NULL)
return (2);
while (1)
{

View File

@@ -6,32 +6,12 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
/* Updated: 2020/01/30 11:48:25 by tmaze ### ########.fr */
/* Updated: 2020/02/01 15:47:08 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#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);
return (NULL);
}
char *check_path_slash(char *exec)
{
int i;
@@ -82,12 +62,6 @@ char *check_path_dot(char *exec, t_env *env)
return (put_error(exec, "no such file or directory"));
}
void *ft_strdel_null(char **s)
{
ft_strdel(s);
return (NULL);
}
char *check_path_elems(char **path_elems, char *exec)
{
int i;
@@ -146,17 +120,6 @@ char *check_path(char *exec, t_env *env)
return (ret);
}
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);
}
int exec_cmd(char **argv, t_env **env)
{
int ret;

56
srcs/ms_exec_utils.c Normal file
View 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);
}

View File

@@ -6,18 +6,12 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
/* Updated: 2020/01/30 15:42:17 by tmaze ### ########.fr */
/* Updated: 2020/02/07 15:55:59 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void *return_null(char *msg)
{
ft_printf("%s\n", msg);
return (NULL);
}
void ft_strreplace(char **dst, char *src, int *index, t_env *elem)
{
if (*dst != NULL)
@@ -57,12 +51,11 @@ char *replace_env(char *src, t_env *elem, int key_size, int i)
int res_len;
res_len = ft_strlen(src) - key_size + ft_strlen(elem->val);
ft_printf("size replace: %d\n", res_len);
if ((ret = ft_strnew(res_len)) != NULL)
{
ft_strncpy(ret, src, i);
ft_strlcat(ret, elem->val, res_len + 1);
ft_strlcat(ret, src + key_size, res_len + 1);
ft_strlcat(ret, &src[i] + key_size, res_len + 1);
}
else
ft_printf("minishell: memory error\n");