Compare commits
16 Commits
master
...
4f7e1a56ef
Author | SHA1 | Date | |
---|---|---|---|
|
4f7e1a56ef | ||
|
8a421600f4 | ||
|
2362474fd6 | ||
|
4123c5dca3 | ||
|
e89ccb64b2 | ||
|
e6304a9f40 | ||
|
aa5f198c46 | ||
|
e32efb9312 | ||
|
cfd4a67957 | ||
|
85af14cd9f | ||
|
d22bc173f1 | ||
|
58459bc0c1 | ||
|
aa8e971688 | ||
|
05d8424f75 | ||
|
cf62dae53b | ||
|
852248636f |
4
.ccls
Normal file
4
.ccls
Normal file
@@ -0,0 +1,4 @@
|
||||
clang
|
||||
-Iincludes
|
||||
-Ilibft/includes
|
||||
%c %h -Wall -Wextra -Werror -g-Wall -Wextra -Werror -g
|
11
Makefile
11
Makefile
@@ -6,7 +6,7 @@
|
||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
|
||||
# Updated: 2020/02/14 11:56:31 by tmaze ### ########.fr #
|
||||
# Updated: 2020/01/22 17:50:24 by tmaze ### ########.fr #
|
||||
# #
|
||||
#******************************************************************************#
|
||||
|
||||
@@ -27,7 +27,7 @@ endif
|
||||
|
||||
# Compilator
|
||||
CC = gcc
|
||||
FLAGS = -Wall -Wextra -Werror
|
||||
FLAGS = -Wall -Wextra -Werror -g -fsanitize=address
|
||||
|
||||
# Folders
|
||||
LIBDIR = libft
|
||||
@@ -41,14 +41,9 @@ SRC = main.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 \
|
||||
ms_exec_utils.c \
|
||||
def_env.c \
|
||||
check_path_slash_cd.c
|
||||
ms_ext.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
|
33
README.org
Normal file
33
README.org
Normal file
@@ -0,0 +1,33 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# 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> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/01 15:57:09 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/12/06 09:54:47 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -34,22 +34,5 @@ int cmd_echo(char **argv, t_env **env);
|
||||
char **res_ext(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
|
||||
|
@@ -6,7 +6,7 @@
|
||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2018/04/07 12:47:06 by tmaze #+# #+# #
|
||||
# Updated: 2020/01/25 16:24:08 by tmaze ### ########.fr #
|
||||
# Updated: 2019/12/06 09:59:05 by tmaze ### ########.fr #
|
||||
# #
|
||||
#******************************************************************************#
|
||||
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/14 12:28:38 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/12/06 09:57:17 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -374,7 +374,6 @@ 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);
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* 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 */
|
||||
/* Updated: 2019/12/05 17:12:59 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -27,18 +27,3 @@ 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);
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ char *ft_envtochar(t_env *env)
|
||||
int size;
|
||||
char *ret;
|
||||
|
||||
size = ft_strlen(env->key) + ft_strlen(env->val) + 2;
|
||||
size = ft_strlen(env->key) + ft_strlen(env->val) + 1;
|
||||
if ((ret = ft_strnew(size)) != NULL)
|
||||
{
|
||||
ft_strlcpy(ret, env->key, size);
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* 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 */
|
||||
/* Updated: 2019/12/05 19:10:52 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@@ -1,36 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
110
srcs/cmd_cd.c
110
srcs/cmd_cd.c
@@ -6,12 +6,50 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/01 15:32:59 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/11/11 01:09:36 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);
|
||||
}
|
||||
|
||||
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)
|
||||
put_error_cd(exec, "no such file or directory");
|
||||
if (i != 0)
|
||||
return (NULL);
|
||||
if ((i = lstat(exec, &info)) != 0)
|
||||
put_error_cd(exec, "can't determine info");
|
||||
if (i != 0)
|
||||
return (NULL);
|
||||
if (!S_ISDIR(info.st_mode) || (S_ISLNK(info.st_mode) && ((i = stat(exec, &info2)) != 0 || !S_ISDIR(info2.st_mode))))
|
||||
{
|
||||
put_error_cd(exec, "not a directory");
|
||||
return (NULL);
|
||||
}
|
||||
if ((i = access(exec, X_OK)) != 0)
|
||||
put_error_cd(exec, "permission denied");
|
||||
if (i != 0)
|
||||
return (NULL);
|
||||
return (exec);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int cmd_cd_update_env(char *path, t_env **env, char opt)
|
||||
{
|
||||
t_env *pwd;
|
||||
@@ -21,16 +59,23 @@ int cmd_cd_update_env(char *path, t_env **env, char opt)
|
||||
|
||||
ft_bzero(p_path, 4096);
|
||||
if (!(pwd = ft_envgetelem("PWD", *env)))
|
||||
return (put_error_cd2(path, "env var PWD undefined"));
|
||||
{
|
||||
put_error_cd(path, "env var PWD undefined");
|
||||
return (1);
|
||||
}
|
||||
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)
|
||||
{
|
||||
put_error_cd(path, "error");
|
||||
return (1);
|
||||
}
|
||||
if (ft_envupdate("PWD", *env, ((opt == 'P') ? p_path : c_path)) == NULL
|
||||
|| ft_envupdate("OLDPWD", *env, oldpwd) == NULL)
|
||||
{
|
||||
put_error_cd(path, "error");
|
||||
ft_strdel(&oldpwd);
|
||||
ft_strdel(&c_path);
|
||||
return (put_error_cd2(path, "error"));
|
||||
return (1);
|
||||
}
|
||||
ft_strdel(&oldpwd);
|
||||
ft_strdel(&c_path);
|
||||
@@ -77,42 +122,35 @@ char cd_getparams(char **argv, size_t *i)
|
||||
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)
|
||||
{
|
||||
t_env *elem;
|
||||
size_t i;
|
||||
char *path;
|
||||
char opt;
|
||||
int ret;
|
||||
|
||||
i = 0;
|
||||
if ((opt = cd_getparams(argv, &i)) == '\0')
|
||||
return (1);
|
||||
return (cmd_cd_switchboard(argv, env, opt, i));
|
||||
if (!argv[i] && (elem = ft_envgetelem("HOME", *env)) != NULL && elem->val != NULL)
|
||||
return (cmd_cd_core(elem->val, env, opt));
|
||||
else if (argv[i] && argv[i][0] == '/')
|
||||
return (cmd_cd_core(argv[i], env, opt));
|
||||
else if (argv[i] && argv[i][0] == '-' && (elem = ft_envgetelem("OLDPWD", *env)) != NULL)
|
||||
return (cmd_cd_core(elem->val, env, opt));
|
||||
else if (argv[i] && argv[i][0] != '/' && (elem = ft_envgetelem("PWD", *env)) != NULL && elem->val != NULL)
|
||||
{
|
||||
if ((path = ft_strnew(ft_strlen(elem->val) + ft_strlen(argv[i]) + 1)) == NULL)
|
||||
put_error_cd(argv[i], "memory error");
|
||||
if (path == NULL)
|
||||
return (1);
|
||||
ft_strcpy(path, elem->val);
|
||||
path[ft_strlen(path)] = '/';
|
||||
ft_strcat(path, argv[i]);
|
||||
ret = cmd_cd_core(path, env, opt);
|
||||
ft_strdel(&path);
|
||||
return (ret);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
@@ -1,31 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */
|
||||
/* Updated: 2020/01/30 15:41:08 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/12/09 09:59:58 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -47,24 +47,6 @@ int env_cleanup(t_env *env_cp, int code)
|
||||
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 i;
|
||||
@@ -73,7 +55,8 @@ int cmd_env(char **argv, t_env **env)
|
||||
t_env *env_cp;
|
||||
|
||||
i = 1;
|
||||
if (!(env_cp = NULL) && !ft_strequ(argv[i], "-i") && (env != NULL))
|
||||
env_cp = NULL;
|
||||
if (!ft_strequ(argv[i], "-i") && (env != NULL))
|
||||
{
|
||||
it = *env;
|
||||
while (it)
|
||||
@@ -84,9 +67,8 @@ int cmd_env(char **argv, t_env **env)
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
else if (ft_strequ(argv[i], "-i"))
|
||||
if (ft_strequ(argv[i], "-i"))
|
||||
i++;
|
||||
inline_env(argv, &i, &env_cp);
|
||||
if (argv[i])
|
||||
exec_cmd(argv + i, &env_cp);
|
||||
else
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/09/20 14:41:08 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/07 15:56:06 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/11/10 21:38:30 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -21,11 +21,14 @@ int cmd_setenv(char **argv, t_env **env)
|
||||
{
|
||||
if (argv[1] && (tmp = ft_envgetelem(new->key, *env)) != NULL)
|
||||
{
|
||||
ft_envupdate(new->key, *env, new->val);
|
||||
ft_envdelelem(&new);
|
||||
ft_strdel(&(tmp->val));
|
||||
tmp->val = new->val;
|
||||
ft_strdel(&(new->key));
|
||||
ft_memdel((void**)&new);
|
||||
}
|
||||
else
|
||||
ft_envaddend(env, new);
|
||||
ft_envclean(env);
|
||||
return (0);
|
||||
}
|
||||
else if (argv[1] && argv[1][0] != '=' && !ft_strchr(argv[1], '='))
|
||||
|
@@ -1,40 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
89
srcs/main.c
89
srcs/main.c
@@ -6,24 +6,12 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/07 15:11:58 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/11/10 21:24:36 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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 *ret;
|
||||
@@ -32,14 +20,10 @@ t_env *env2lst(char **env)
|
||||
|
||||
i = 0;
|
||||
ret = NULL;
|
||||
while (env && env[i])
|
||||
while (env[i])
|
||||
{
|
||||
if ((new = ft_envnew(env[i])) != NULL)
|
||||
{
|
||||
if (ft_strequ(new->key, "SHLVL") && ft_str_is_numeric(new->val))
|
||||
inc_shlvl(&new->val);
|
||||
ft_envaddend(&ret, new);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_envdelelem(&new);
|
||||
@@ -48,61 +32,58 @@ t_env *env2lst(char **env)
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (i == 0 && ret == NULL && (ret = def_env()) == NULL)
|
||||
ft_printf("Env: Memory Error\n");
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void check_exec(char **argv, t_env *env)
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
extern char **environ;
|
||||
static t_builtin built[S_BIN] = {{"env", &cmd_env}, {"cd", &cmd_cd}
|
||||
, {"setenv", &cmd_setenv}, {"echo", &cmd_echo}
|
||||
, {"unsetenv", &cmd_unsetenv}};
|
||||
char **argv;
|
||||
t_env *env;
|
||||
char *cmd;
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (++i < S_BIN)
|
||||
env = NULL;
|
||||
if ((env = env2lst(environ)) == NULL)
|
||||
return (2);
|
||||
while (1)
|
||||
{
|
||||
cmd = NULL;
|
||||
ft_printf("%s$>%s ", FT_COLOR_GREEN, FT_RESET);
|
||||
if (ft_getline(&cmd) >= 0 && cmd != NULL
|
||||
&& (argv = ft_strsplit(cmd, ' ')) != NULL)
|
||||
{
|
||||
ft_strdel(&cmd);
|
||||
if ((res_ext(argv, env)) != NULL)
|
||||
{
|
||||
if (ft_strequ(argv[0], "exit"))
|
||||
{
|
||||
ft_del_words_tables(&argv);
|
||||
ft_envdel(&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);
|
||||
}
|
||||
|
||||
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);
|
||||
while (1)
|
||||
{
|
||||
ft_printf("%s$>%s ", FT_COLOR_GREEN, FT_RESET);
|
||||
if (ft_getline(&cmd) >= 0 && cmd != NULL
|
||||
&& (argv = ft_strsplit(cmd, ' ')) != NULL && !ft_strdel_null(&cmd))
|
||||
{
|
||||
if ((res_ext(argv, env)) != NULL)
|
||||
{
|
||||
if (ft_strequ(argv[0], "exit"))
|
||||
return (cleanup(argv, env));
|
||||
check_exec(argv, env);
|
||||
}
|
||||
}
|
||||
ft_strdel(&cmd);
|
||||
ft_del_words_tables(&argv);
|
||||
}
|
||||
ft_del_words_tables(&argv);
|
||||
ft_strdel(&cmd);
|
||||
ft_envdel(&env);
|
||||
return (0);
|
||||
}
|
||||
|
108
srcs/ms_exec.c
108
srcs/ms_exec.c
@@ -6,12 +6,31 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/01 15:47:08 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/11/10 15:52:54 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);
|
||||
}
|
||||
|
||||
char *check_path_slash(char *exec)
|
||||
{
|
||||
int i;
|
||||
@@ -45,7 +64,10 @@ char *check_path_dot(char *exec, t_env *env)
|
||||
|
||||
if ((path = ft_envgetelem("PWD", env)) == 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)
|
||||
put_error(exec, "memory error");
|
||||
ft_strdel(&tmp);
|
||||
@@ -55,40 +77,13 @@ char *check_path_dot(char *exec, t_env *env)
|
||||
{
|
||||
if (access(ret, X_OK) == 0)
|
||||
return (exec);
|
||||
put_error(exec, "permission denied");
|
||||
ft_strdel(&ret);
|
||||
return (put_error(exec, "permission denied"));
|
||||
return (NULL);
|
||||
}
|
||||
put_error(exec, "no such file or directory");
|
||||
ft_strdel(&ret);
|
||||
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);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *check_path(char *exec, t_env *env)
|
||||
@@ -96,6 +91,7 @@ char *check_path(char *exec, t_env *env)
|
||||
size_t i;
|
||||
t_env *path;
|
||||
char **path_elems;
|
||||
char *tmp;
|
||||
char *ret;
|
||||
|
||||
ret = NULL;
|
||||
@@ -108,13 +104,38 @@ char *check_path(char *exec, t_env *env)
|
||||
if ((path = ft_envgetelem("PATH", env)) == NULL
|
||||
|| path->val == NULL
|
||||
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
|
||||
return (put_error(exec, "could not resolve path"));
|
||||
i = 0;
|
||||
if ((ret = check_path_elems(path_elems, exec)) == NULL)
|
||||
{
|
||||
ft_del_words_tables(&path_elems);
|
||||
return (put_error(exec, "command not found"));
|
||||
put_error(exec, "could not resolve path");
|
||||
return (NULL);
|
||||
}
|
||||
i = 0;
|
||||
while (path_elems[i])
|
||||
{
|
||||
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL)
|
||||
put_error(exec, "memory error");
|
||||
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);
|
||||
}
|
||||
return (ret);
|
||||
@@ -130,10 +151,19 @@ int exec_cmd(char **argv, t_env **env)
|
||||
return (-1);
|
||||
if ((path = check_path(argv[0], *env)) == NULL)
|
||||
ft_del_words_tables(&env_tab);
|
||||
ft_printf("path after resolve: %s\n", path);
|
||||
if (path == NULL)
|
||||
return (-1);
|
||||
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);
|
||||
ft_envdel(env);
|
||||
exit(-1);
|
||||
}
|
||||
else if (ret == -1)
|
||||
ft_putendl_fd("minishell: error", 1);
|
||||
if (ret == -1)
|
||||
|
@@ -1,56 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
@@ -6,18 +6,16 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
|
||||
/* Updated: 2020/02/07 15:55:59 by tmaze ### ########.fr */
|
||||
/* Updated: 2020/01/23 00:23:24 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void ft_strreplace(char **dst, char *src, int *index, t_env *elem)
|
||||
void *return_null(char *msg)
|
||||
{
|
||||
if (*dst != NULL)
|
||||
ft_strdel(dst);
|
||||
*dst = src;
|
||||
*index = ft_strlen(elem->val);
|
||||
ft_printf("%s\n", msg);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_env *search_key(t_env *env, char *cmd, int index)
|
||||
@@ -39,7 +37,10 @@ t_env *search_key(t_env *env, char *cmd, int index)
|
||||
j++;
|
||||
}
|
||||
if (!it->key[i] && (!ret || ft_strlen(it->key) > ft_strlen(ret->key)))
|
||||
{
|
||||
ft_printf("found key: %s\n", it->key);
|
||||
ret = it;
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
return (ret);
|
||||
@@ -53,9 +54,9 @@ char *replace_env(char *src, t_env *elem, int key_size, int i)
|
||||
res_len = ft_strlen(src) - key_size + ft_strlen(elem->val);
|
||||
if ((ret = ft_strnew(res_len)) != NULL)
|
||||
{
|
||||
ft_strncpy(ret, src, i);
|
||||
ft_strlcpy(ret, src, i + 1);
|
||||
ft_strlcat(ret, elem->val, res_len + 1);
|
||||
ft_strlcat(ret, &src[i] + key_size, res_len + 1);
|
||||
ft_strlcat(ret, src + 1 + key_size, res_len + 1);
|
||||
}
|
||||
else
|
||||
ft_printf("minishell: memory error\n");
|
||||
@@ -72,7 +73,9 @@ char *resolve_complete_key(char **src, int *index, t_env *env)
|
||||
if ((ret = replace_env(*src, tmp, ft_strlen(tmp->key)
|
||||
+ 1, *index)) != NULL)
|
||||
{
|
||||
ft_strreplace(src, ret, index, tmp);
|
||||
ft_strdel(src);
|
||||
*src = ret;
|
||||
*index += ft_strlen(tmp->val);
|
||||
return (*src);
|
||||
}
|
||||
else
|
||||
@@ -88,23 +91,25 @@ char **res_ext(char **av, t_env *env)
|
||||
t_env *tmp;
|
||||
char *ret;
|
||||
|
||||
i[0] = -1;
|
||||
while (av && av[++i[0]] && !(i[1] = 0))
|
||||
i[0] = 0;
|
||||
while (av && av[i[0]] && !(i[1] = 0))
|
||||
{
|
||||
while (av[i[0]][i[1]])
|
||||
while (av[i[0]] && 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))
|
||||
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"));
|
||||
{
|
||||
ft_strdel(&av[i[0]]);
|
||||
av[i[0]] = ret;
|
||||
i[1] += ft_strlen(tmp->val);
|
||||
}
|
||||
else if (av[i[0]][i[1]] == '$'
|
||||
&& resolve_complete_key(&av[i[0]], &i[1], env) == NULL)
|
||||
return (NULL);
|
||||
else if (av[i[0]][i[1]] != '$' && av[i[0]][i[1]] == '~')
|
||||
i[1]++;
|
||||
}
|
||||
i[0]++;
|
||||
}
|
||||
return (av);
|
||||
}
|
||||
|
@@ -1,31 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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