Compare commits

...

5 Commits

Author SHA1 Message Date
Tanguy Maze
e89ccb64b2 feature: added echo cmd
changed printf ref to ft_printf
2019-11-03 00:45:38 +01:00
Tanguy Maze
e6304a9f40 fix leak on ext need test 2019-11-02 00:36:21 +01:00
Tanguy Maze
aa5f198c46 feature: added completion for ~ and ${}
WIP leaks detected on ${} completion
2019-10-31 14:59:47 +01:00
Tanguy Maze
e32efb9312 Merge branch 'dev' of github.com:tvdu29/minishell into dev 2019-10-18 14:22:39 +02:00
Tanguy Maze
cfd4a67957 Changed comportment on error for check_path functions 2019-10-18 14:17:34 +02:00
10 changed files with 169 additions and 34 deletions

3
.ccls Normal file
View File

@@ -0,0 +1,3 @@
clang
-Iincludes
-Ilibft/includes

View File

@@ -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/09/27 16:13:51 by tmaze ### ########.fr # # Updated: 2019/10/31 14:05:15 by tmaze ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
@@ -43,7 +43,8 @@ SRC = main.c \
cmd_cd.c \ cmd_cd.c \
cmd_echo.c \ cmd_echo.c \
ms_exec.c \ ms_exec.c \
ms_env.c ms_env.c \
ms_ext.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)

View File

@@ -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/09/27 16:09:09 by tmaze ### ########.fr */ /* Updated: 2019/10/31 14:38:16 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -45,6 +45,8 @@ t_env *lstaddend(t_env **alst, t_env *new);
t_env *lstgetelem(char *key, t_env *env); t_env *lstgetelem(char *key, t_env *env);
char **lsttotab(t_env *env); char **lsttotab(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);
#endif #endif

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:42:42 by tmaze #+# #+# */ /* Created: 2019/09/20 14:42:42 by tmaze #+# #+# */
/* Updated: 2019/10/17 10:59:38 by tmaze ### ########.fr */ /* Updated: 2019/11/03 00:39:59 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:42:30 by tmaze #+# #+# */ /* Created: 2019/09/20 14:42:30 by tmaze #+# #+# */
/* Updated: 2019/10/17 10:58:52 by tmaze ### ########.fr */ /* Updated: 2019/11/02 23:04:23 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -14,7 +14,12 @@
int cmd_echo(char **argv, t_env **env) int cmd_echo(char **argv, t_env **env)
{ {
(void)argv;
(void)env; (void)env;
int i;
i = 1;
while (argv[i])
ft_printf("%s ", argv[i++]);
ft_putchar('\n');
return (0); return (0);
} }

View File

@@ -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/10/17 09:59:40 by tmaze ### ########.fr */ /* Updated: 2019/11/02 23:10:32 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -28,7 +28,7 @@ int cmd_setenv(char **argv, t_env **env)
} }
else else
{ {
printf("usage: setenv [KEY]=[value]\n"); ft_printf("usage: setenv [KEY]=[value]\n");
return (1); return (1);
} }
} }

View File

@@ -6,7 +6,7 @@
/* 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/09/27 16:12:10 by tmaze ### ########.fr */ /* Updated: 2019/10/31 14:36:15 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -57,27 +57,43 @@ int main(void)
&& (argv = ft_strsplit(cmd, ' ')) != NULL) && (argv = ft_strsplit(cmd, ' ')) != NULL)
{ {
ft_strdel(&cmd); ft_strdel(&cmd);
if (ft_strequ(argv[0], "exit"))
{
ft_del_words_tables(&argv);
lstdel(&env);
return (0);
}
i = 0; i = 0;
while (i < S_BIN) ft_printf("before:\n");
while (argv[i])
{ {
if (ft_strequ(argv[0], built[i].cmd)) ft_printf("%s\n", argv[i++]);
{
built[i].f(argv, &env);
break ;
}
i++;
} }
if (i == S_BIN) if ((res_ext(argv, env)) != NULL)
{ {
exec_cmd(argv, &env); i = 0;
ft_printf("after:\n");
while (argv[i])
{
ft_printf("%s\n", argv[i++]);
}
if (ft_strequ(argv[0], "exit"))
{
ft_del_words_tables(&argv);
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_del_words_tables(&argv); ft_del_words_tables(&argv);
} }
ft_del_words_tables(&argv); ft_del_words_tables(&argv);

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 11:43:53 by tmaze #+# #+# */ /* Created: 2019/09/20 11:43:53 by tmaze #+# #+# */
/* Updated: 2019/10/17 11:02:54 by tmaze ### ########.fr */ /* Updated: 2019/10/18 14:20:41 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* 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/17 11:01:38 by tmaze ### ########.fr */ /* Updated: 2019/10/18 14:22:23 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -39,18 +39,20 @@ char *check_path_slash(char *exec)
if ((i = access(exec, F_OK)) != 0) if ((i = access(exec, F_OK)) != 0)
put_error(exec, "no such file or directory"); put_error(exec, "no such file or directory");
if (i != 0) if (i != 0)
return (exec); return (NULL);
if ((i = stat(exec, &info)) != 0) if ((i = stat(exec, &info)) != 0)
put_error(exec, "can't determine info"); put_error(exec, "can't determine info");
if (i != 0) if (i != 0)
return (exec); return (NULL);
if (!S_ISREG(info.st_mode)) if (!S_ISREG(info.st_mode))
{ {
put_error(exec, "not an executable file"); put_error(exec, "not an executable file");
return (exec); return (NULL);
} }
if ((i = access(exec, X_OK)) != 0) if ((i = access(exec, X_OK)) != 0)
put_error(exec, "permission denied"); put_error(exec, "permission denied");
if (i != 0)
return (NULL);
return (exec); return (exec);
} }
@@ -64,24 +66,24 @@ char *check_path_dot(char *exec, t_env *env)
|| path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL) || path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL)
{ {
put_error(exec, "memory error"); put_error(exec, "memory error");
return (exec); 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);
if (ret == NULL) if (ret == NULL)
return (exec); return (NULL);
if (access(ret, F_OK) == 0) if (access(ret, F_OK) == 0)
{ {
if (access(ret, X_OK) == 0) if (access(ret, X_OK) == 0)
return (ret); return (exec);
put_error(exec, "permission denied"); put_error(exec, "permission denied");
ft_strdel(&ret); ft_strdel(&ret);
return (exec); return (NULL);
} }
put_error(exec, "no such file or directory"); put_error(exec, "no such file or directory");
ft_strdel(&ret); ft_strdel(&ret);
return (exec); return (NULL);
} }
char *check_path(char *exec, t_env *env) char *check_path(char *exec, t_env *env)
@@ -159,6 +161,7 @@ int exec_cmd(char **argv, t_env **env)
if (ft_strcmp(path, argv[0]) != 0) if (ft_strcmp(path, argv[0]) != 0)
ft_strdel(&path); ft_strdel(&path);
ft_del_words_tables(&env_tab); ft_del_words_tables(&env_tab);
lstdel(env);
exit(-1); exit(-1);
} }
else if (ret == -1) else if (ret == -1)

105
srcs/ms_ext.c Normal file
View File

@@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_ext.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
/* Updated: 2019/11/02 00:35:13 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_env *search_key(t_env *env, char *cmd, int index)
{
t_env *it;
int i;
int j;
it = env;
while (it && cmd[index + 2])
{
i = 0;
j = index;
while (it->key[i] && cmd[j] && cmd[j] != '}' && it->key[i] == cmd[j])
{
i++;
j++;
}
if (!it->key[i] && cmd[j] == '}')
return (it);
it = it->next;
}
return (NULL);
}
char **res_ext(char **argv, t_env *env)
{
int i;
int j;
int k;
int res_len;
t_env *elem;
char *ret;
k = 0;
while (argv && argv[k])
{
i = 0;
while (argv[k] && argv[k][i]) {
if (i == 0 && argv[k][i] == '~' && lstgetelem("HOME", env)) {
res_len =
ft_strlen(argv[k]) - 1 + ft_strlen(lstgetelem("HOME", env)->val);
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++;
}
return (argv);
}