Compare commits
5 Commits
85af14cd9f
...
e89ccb64b2
Author | SHA1 | Date | |
---|---|---|---|
|
e89ccb64b2 | ||
|
e6304a9f40 | ||
|
aa5f198c46 | ||
|
e32efb9312 | ||
|
cfd4a67957 |
5
Makefile
5
Makefile
@@ -6,7 +6,7 @@
|
||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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_echo.c \
|
||||
ms_exec.c \
|
||||
ms_env.c
|
||||
ms_env.c \
|
||||
ms_ext.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
char **lsttotab(t_env *env);
|
||||
|
||||
char **res_ext(char **argv, t_env *env);
|
||||
|
||||
int exec_cmd(char **argv, t_env **env);
|
||||
|
||||
#endif
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
(void)argv;
|
||||
(void)env;
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
while (argv[i])
|
||||
ft_printf("%s ", argv[i++]);
|
||||
ft_putchar('\n');
|
||||
return (0);
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
{
|
||||
printf("usage: setenv [KEY]=[value]\n");
|
||||
ft_printf("usage: setenv [KEY]=[value]\n");
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
18
srcs/main.c
18
srcs/main.c
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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,6 +57,20 @@ int main(void)
|
||||
&& (argv = ft_strsplit(cmd, ' ')) != NULL)
|
||||
{
|
||||
ft_strdel(&cmd);
|
||||
i = 0;
|
||||
ft_printf("before:\n");
|
||||
while (argv[i])
|
||||
{
|
||||
ft_printf("%s\n", argv[i++]);
|
||||
}
|
||||
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"))
|
||||
{
|
||||
ft_del_words_tables(&argv);
|
||||
@@ -78,6 +92,8 @@ int main(void)
|
||||
exec_cmd(argv, &env);
|
||||
}
|
||||
}
|
||||
}
|
||||
ft_strdel(&cmd);
|
||||
ft_del_words_tables(&argv);
|
||||
}
|
||||
ft_del_words_tables(&argv);
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
put_error(exec, "no such file or directory");
|
||||
if (i != 0)
|
||||
return (exec);
|
||||
return (NULL);
|
||||
if ((i = stat(exec, &info)) != 0)
|
||||
put_error(exec, "can't determine info");
|
||||
if (i != 0)
|
||||
return (exec);
|
||||
return (NULL);
|
||||
if (!S_ISREG(info.st_mode))
|
||||
{
|
||||
put_error(exec, "not an executable file");
|
||||
return (exec);
|
||||
return (NULL);
|
||||
}
|
||||
if ((i = access(exec, X_OK)) != 0)
|
||||
put_error(exec, "permission denied");
|
||||
if (i != 0)
|
||||
return (NULL);
|
||||
return (exec);
|
||||
}
|
||||
|
||||
@@ -64,24 +66,24 @@ char *check_path_dot(char *exec, t_env *env)
|
||||
|| path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL)
|
||||
{
|
||||
put_error(exec, "memory error");
|
||||
return (exec);
|
||||
return (NULL);
|
||||
}
|
||||
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
||||
put_error(exec, "memory error");
|
||||
ft_strdel(&tmp);
|
||||
if (ret == NULL)
|
||||
return (exec);
|
||||
return (NULL);
|
||||
if (access(ret, F_OK) == 0)
|
||||
{
|
||||
if (access(ret, X_OK) == 0)
|
||||
return (ret);
|
||||
return (exec);
|
||||
put_error(exec, "permission denied");
|
||||
ft_strdel(&ret);
|
||||
return (exec);
|
||||
return (NULL);
|
||||
}
|
||||
put_error(exec, "no such file or directory");
|
||||
ft_strdel(&ret);
|
||||
return (exec);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
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)
|
||||
ft_strdel(&path);
|
||||
ft_del_words_tables(&env_tab);
|
||||
lstdel(env);
|
||||
exit(-1);
|
||||
}
|
||||
else if (ret == -1)
|
||||
|
105
srcs/ms_ext.c
Normal file
105
srcs/ms_ext.c
Normal 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);
|
||||
}
|
Reference in New Issue
Block a user