Compare commits
45 Commits
camille
...
9ace235831
Author | SHA1 | Date | |
---|---|---|---|
9ace235831 | |||
4e92c03637 | |||
76bc8fbfd8 | |||
916f2c3c50 | |||
89c80e9bdb | |||
eb4a8ed663 | |||
3fb6eb65a8 | |||
1fdc51a668 | |||
03a2cc055c | |||
a136a66522 | |||
0ece156b97 | |||
4c45a2c603 | |||
b40762478a | |||
d7032849d6 | |||
a20fa2a4c6 | |||
255892e12e | |||
96de7639cd | |||
17ece4bc7b | |||
73899f5905 | |||
373629b541 | |||
5caeed312c | |||
5e0ae80404 | |||
69d6ac533b | |||
73d1f11269 | |||
e8deb0be19 | |||
ca09c8ed26 | |||
3682d0a348 | |||
e2a40e07fc | |||
cfd1dffbcf | |||
55e378e2cb | |||
fb8186ac78 | |||
3ef58c0116 | |||
cc74fda339 | |||
7eedf74847 | |||
c75c6838d6 | |||
4b5a50d7fe | |||
922db1e08f | |||
e709eb0dbb | |||
3ce5b64420 | |||
a183971a7a | |||
5175073708 | |||
72f9dd39cf | |||
8bd58675e9 | |||
afd71e5ed8 | |||
d59562899b |
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
|||||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c
|
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c
|
||||||
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c env_fill.c
|
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c builtins/cd.c
|
||||||
|
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
|
57
builtins/cd.c
Normal file
57
builtins/cd.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 14:41:58 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../minishell.h"
|
||||||
|
|
||||||
|
int move_folder(char *path, int fd)
|
||||||
|
{
|
||||||
|
char *join;
|
||||||
|
|
||||||
|
if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0)
|
||||||
|
{
|
||||||
|
if (chdir(path) == 0)
|
||||||
|
return (0);
|
||||||
|
ft_printf("chdir error");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
join = ft_strjoin("/", path);
|
||||||
|
join = ft_strfjoin(get_pwd(fd), join);
|
||||||
|
if (chdir(join) == 0)
|
||||||
|
{
|
||||||
|
free(join);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
free(join);
|
||||||
|
ft_printf("chdir error");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*int main(int argc, char *argv[]) {
|
||||||
|
char cwd[PATH_MAX];
|
||||||
|
if (getcwd(cwd, sizeof(cwd)) != NULL) {
|
||||||
|
printf("%s\n", cwd);
|
||||||
|
} else {
|
||||||
|
perror("getcwd() error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
move_folder(argv[1], 1);
|
||||||
|
if (getcwd(cwd, sizeof(cwd)) != NULL) {
|
||||||
|
printf("%s\n", cwd);
|
||||||
|
} else {
|
||||||
|
perror("getcwd() error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}*/
|
109
builtins/echo.c
Normal file
109
builtins/echo.c
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* echo.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
|
||||||
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
|
/* Updated: 2023/02/21 22:24:51 by erey-bet ### ########.fr */
|
||||||
|
=======
|
||||||
|
<<<<<<< HEAD
|
||||||
|
/* Updated: 2023/02/21 21:45:38 by cchauvet ### ########.fr */
|
||||||
|
=======
|
||||||
|
/* Updated: 2023/02/21 22:09:55 by erey-bet ### ########.fr */
|
||||||
|
>>>>>>> 1fdc51a668e616de94c33d5b3b1e9cb51182916d
|
||||||
|
>>>>>>> eb4a8ed663b2f93cd955f7608aa686bd8e767a65
|
||||||
|
=======
|
||||||
|
/* Updated: 2023/02/21 22:15:27 by cchauvet ### ########.fr */
|
||||||
|
>>>>>>> 76bc8fbfd8d5413e8f6e4f3eb6ec00db0fa3c884
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../minishell.h"
|
||||||
|
|
||||||
|
int is_space(char c)
|
||||||
|
{
|
||||||
|
return (c == ' ' || c == '\f' || c == '\v' || c == '\t'
|
||||||
|
|| c == '\r' || c == '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_argument(char *str, int *check, int i)
|
||||||
|
{
|
||||||
|
int y;
|
||||||
|
|
||||||
|
y = 0;
|
||||||
|
if (str[i] == '-')
|
||||||
|
{
|
||||||
|
while (!is_space(str[i]) || (str[i + 1] == '-' ))
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if (is_space(str[i]))
|
||||||
|
{
|
||||||
|
y = i;
|
||||||
|
*check = 1;
|
||||||
|
}
|
||||||
|
else if (str[i] == '-' && str[i - 1] != '-')
|
||||||
|
;
|
||||||
|
else if (str[i] != 'n')
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
i = y;
|
||||||
|
while (is_space(str[i]))
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *conca(char **strs)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
char *str;
|
||||||
|
char *p;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
len = 0;
|
||||||
|
while (strs[++i] != NULL)
|
||||||
|
len += ft_strlen(strs[i]) + 2;
|
||||||
|
str = (char *) ft_calloc(len + 1, sizeof(char));
|
||||||
|
if (str == NULL)
|
||||||
|
return NULL;
|
||||||
|
i = -1;
|
||||||
|
while (strs[++i] != NULL)
|
||||||
|
{
|
||||||
|
ft_strlcat(str, strs[i], ft_strlen(str) + ft_strlen(strs[i]) + 2);
|
||||||
|
if (strs[i + 1] != NULL)
|
||||||
|
str[ft_strlen(str)] = ' ';
|
||||||
|
}
|
||||||
|
str[ft_strlen(str)] = '\0';
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
int echo(int fd, char **strs)
|
||||||
|
{
|
||||||
|
int check;
|
||||||
|
int i;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = conca(strs);
|
||||||
|
check = 0;
|
||||||
|
i = 0;
|
||||||
|
while (is_space(str[i]))
|
||||||
|
i++;
|
||||||
|
i = check_argument(str, &check, i);
|
||||||
|
while (str[i])
|
||||||
|
ft_putchar_fd(str[i++], fd);
|
||||||
|
if (!check)
|
||||||
|
write(fd, "\n", 1);
|
||||||
|
free(str);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
echo(1, argv);
|
||||||
|
return (0);
|
||||||
|
}*/
|
@ -12,14 +12,17 @@
|
|||||||
|
|
||||||
#include "../minishell.h"
|
#include "../minishell.h"
|
||||||
|
|
||||||
int main(char **env)
|
int print_env(t_list **head, int fd)
|
||||||
{
|
{
|
||||||
t_list **head;
|
|
||||||
t_list *current;
|
t_list *current;
|
||||||
|
|
||||||
while (current != NULL)
|
current = *head;
|
||||||
|
while (current->next != NULL)
|
||||||
{
|
{
|
||||||
ft_putendl_fd(1, current->content);
|
ft_putstr_fd(((t_env *)(current->content))->key, fd);
|
||||||
|
ft_putstr_fd("=", fd);
|
||||||
|
ft_putstr_fd(((t_env *)(current->content))->value, fd);
|
||||||
|
write(fd, "\n", 1);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
@ -12,29 +12,20 @@
|
|||||||
|
|
||||||
#include "../minishell.h"
|
#include "../minishell.h"
|
||||||
|
|
||||||
int main(char **env)
|
int print_export(t_list **head, int fd)
|
||||||
{
|
{
|
||||||
t_list **env_lst;
|
|
||||||
t_list *current;
|
t_list *current;
|
||||||
char *key;
|
|
||||||
char *value;
|
|
||||||
|
|
||||||
env_lst = init_env(env);
|
current = *head;
|
||||||
current = *env_lst;
|
while (current->next != NULL)
|
||||||
while (current != NULL)
|
|
||||||
{
|
{
|
||||||
value = ft_strchr(current->content, '=') + 1;
|
write(fd, "declare -x ", 11);
|
||||||
key = ft_strndup(current->content,
|
ft_putstr_fd(((t_env *)(current->content))->key, fd);
|
||||||
ft_strlen(current->content) - ft_strlen(value));
|
ft_putstr_fd("=", fd);
|
||||||
if (key == NULL)
|
write(fd, "\"", 1);
|
||||||
{
|
ft_putstr_fd(((t_env *)(current->content))->value, fd);
|
||||||
ft_lstclear(env_lst, env_del);
|
write(fd, "\"\n", 2);
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
ft_printf("declare -x %s=\"%s\"\n", key, value);
|
|
||||||
free(key);
|
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
ft_lstclear(env_lst, env_del);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
41
builtins/pwd.c
Normal file
41
builtins/pwd.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pwd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 15:11:38 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../minishell.h"
|
||||||
|
|
||||||
|
int pwd(int fd)
|
||||||
|
{
|
||||||
|
char path[PATH_MAX];
|
||||||
|
|
||||||
|
if (getcwd(path, sizeof(path)) != NULL)
|
||||||
|
ft_putendl_fd(path, fd);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_putendl_fd("Error getcwd", fd);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_pwd(int fd)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = ft_calloc(PATH_MAX, sizeof(char *));
|
||||||
|
if (getcwd(str, PATH_MAX) != NULL)
|
||||||
|
return (str);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_putendl_fd("Error getcwd", fd);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
}
|
5
cmd.c
5
cmd.c
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
|
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/16 18:25:14 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/21 22:27:28 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -39,10 +39,7 @@ int ft_cmd_filler(t_list *element, char **args, t_list **env)
|
|||||||
{
|
{
|
||||||
temp = ft_env_filler(env, args[i]);
|
temp = ft_env_filler(env, args[i]);
|
||||||
if (temp == NULL)
|
if (temp == NULL)
|
||||||
{
|
|
||||||
ft_eprintf("minishell: malloc failed\n");
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
|
||||||
free(args[i]);
|
free(args[i]);
|
||||||
args[i] = temp;
|
args[i] = temp;
|
||||||
ft_quote_remover(temp);
|
ft_quote_remover(temp);
|
||||||
|
170
env.c
170
env.c
@ -6,154 +6,20 @@
|
|||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
||||||
/* Updated: 2023/02/17 13:05:29 by erey-bet ### ########.fr */
|
/* Updated: 2023/02/17 17:11:03 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int get_index(char *s, char c)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = -1;
|
|
||||||
while (s[++i])
|
|
||||||
if (s[i] == c)
|
|
||||||
return (i);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_export(t_list **head, int fd)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
|
|
||||||
current = *head;
|
|
||||||
while (current->next != NULL)
|
|
||||||
{
|
|
||||||
write(fd, "declare -x ", 11);
|
|
||||||
ft_putstr_fd(((t_env*)(current->content))->key, fd);
|
|
||||||
ft_putstr_fd("=", fd);
|
|
||||||
write(fd, "\"", 1);
|
|
||||||
ft_putstr_fd(((t_env*)(current->content))->value, fd);
|
|
||||||
write(fd, "\"\n", 2);
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_env(t_list **head, int fd)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
|
|
||||||
current = *head;
|
|
||||||
while (current->next != NULL)
|
|
||||||
{
|
|
||||||
ft_putstr_fd(((t_env*)(current->content))->key, fd);
|
|
||||||
ft_putstr_fd("=", fd);
|
|
||||||
ft_putstr_fd(((t_env*)(current->content))->value, fd);
|
|
||||||
write(fd, "\n", 1);
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int strcmp_alphabet(char *s1, char *s2)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (!s1 || !s2)
|
|
||||||
return (-2);
|
|
||||||
i = 0;
|
|
||||||
while (s1[i] && s2[i])
|
|
||||||
{
|
|
||||||
if (s1[i] < s2[i])
|
|
||||||
return (0);
|
|
||||||
else if (s1[i] > s2[i])
|
|
||||||
return (1);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ft_double_swap(void *a, void *b)
|
|
||||||
{
|
|
||||||
void *c;
|
|
||||||
|
|
||||||
c = a;
|
|
||||||
a = b;
|
|
||||||
b = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
void exchange(void *a, void *b, void *c)
|
|
||||||
{
|
|
||||||
void *d;
|
|
||||||
|
|
||||||
d = a;
|
|
||||||
a = b;
|
|
||||||
b = c;
|
|
||||||
c = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *get_value(char *str)
|
|
||||||
{
|
|
||||||
char *s;
|
|
||||||
int i;
|
|
||||||
int start;
|
|
||||||
|
|
||||||
s = ft_calloc(ft_strlen(str), sizeof(char));
|
|
||||||
start = get_index(str, '=');
|
|
||||||
i = start;
|
|
||||||
while (str[++i])
|
|
||||||
s[i - start - 1] = str[i];
|
|
||||||
return (s);
|
|
||||||
}
|
|
||||||
|
|
||||||
void env_del(void *ptr)
|
|
||||||
{
|
|
||||||
t_env *content;
|
|
||||||
|
|
||||||
content = ptr;
|
|
||||||
free(content->key);
|
|
||||||
free(content->value);
|
|
||||||
free(content);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *get_key(char *str)
|
|
||||||
{
|
|
||||||
char *s;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
s = ft_calloc(ft_strlen(str), sizeof(char));
|
|
||||||
i = -1;
|
|
||||||
while (str[++i] != '=')
|
|
||||||
s[i] = str[i];
|
|
||||||
return (s);
|
|
||||||
}
|
|
||||||
|
|
||||||
void swap_env_3(void **a, void **b, void **c)
|
|
||||||
{
|
|
||||||
void *d;
|
|
||||||
|
|
||||||
d = *a;
|
|
||||||
*a = *b;
|
|
||||||
*b = *c;
|
|
||||||
*c = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
void swap_env(void **a, void **b)
|
|
||||||
{
|
|
||||||
void *c;
|
|
||||||
|
|
||||||
c = *a;
|
|
||||||
*a = *b;
|
|
||||||
*b = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_sort(t_list **head, t_env *var)
|
void add_sort(t_list **head, t_env *var)
|
||||||
{
|
{
|
||||||
t_list *current;
|
t_list *current;
|
||||||
t_env *last;
|
t_env *last;
|
||||||
|
|
||||||
current = *head;
|
current = *head;
|
||||||
while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0)
|
while (current->next != NULL
|
||||||
|
&& ft_strcmp(var->key, ((t_env *)(current->content))->key) != 0)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
if (current->content == NULL)
|
if (current->content == NULL)
|
||||||
current->content = var;
|
current->content = var;
|
||||||
@ -182,7 +48,7 @@ char *get_value_by_key(char *key, t_list **head)
|
|||||||
return (((t_env *)current->content)->value);
|
return (((t_env *)current->content)->value);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return ("");
|
||||||
}
|
}
|
||||||
|
|
||||||
int set_value_by_key(char *key, char *value, t_list **head)
|
int set_value_by_key(char *key, char *value, t_list **head)
|
||||||
@ -218,25 +84,6 @@ int create_value_by_key(char *key, char *value, t_list **head)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
char **env_to_strs(t_list **head)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
t_env *content;
|
|
||||||
char **env;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
current = *head;
|
|
||||||
env = ft_calloc(ft_lstsize(*head), sizeof(char *));
|
|
||||||
i = 0;
|
|
||||||
while (current->content)
|
|
||||||
{
|
|
||||||
content = current->content;
|
|
||||||
env[i++] = ft_strmerger(3, content->key, "=", content->value);
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
return (env);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_list **init_env(char **env)
|
t_list **init_env(char **env)
|
||||||
{
|
{
|
||||||
t_list **head;
|
t_list **head;
|
||||||
@ -261,12 +108,3 @@ t_list **init_env(char **env)
|
|||||||
}
|
}
|
||||||
return (head);
|
return (head);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*int main(int argc, char *argv[], char **env)
|
|
||||||
{
|
|
||||||
t_list **new;
|
|
||||||
|
|
||||||
new = init_env(env);
|
|
||||||
ft_printf("%S", env_to_strs(new));
|
|
||||||
return (0);
|
|
||||||
}*/
|
|
||||||
|
77
env2.c
Normal file
77
env2.c
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/17 17:22:01 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/02/17 17:24:57 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libftx/libftx.h"
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int get_index(char *s, char c)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (s[++i])
|
||||||
|
if (s[i] == c)
|
||||||
|
return (i);
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap_env_3(void **a, void **b, void **c)
|
||||||
|
{
|
||||||
|
void *d;
|
||||||
|
|
||||||
|
d = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = *c;
|
||||||
|
*c = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap_env(void **a, void **b)
|
||||||
|
{
|
||||||
|
void *c;
|
||||||
|
|
||||||
|
c = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void env_del(void *ptr)
|
||||||
|
{
|
||||||
|
t_env *content;
|
||||||
|
|
||||||
|
if (ptr == NULL)
|
||||||
|
return ;
|
||||||
|
content = ptr;
|
||||||
|
if (content->key != NULL)
|
||||||
|
free(content->key);
|
||||||
|
if (content->key != NULL)
|
||||||
|
free(content->value);
|
||||||
|
if (content != NULL)
|
||||||
|
free(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **env_to_strs(t_list **head)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
t_env *content;
|
||||||
|
char **env;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
current = *head;
|
||||||
|
env = ft_calloc(ft_lstsize(*head), sizeof(char *));
|
||||||
|
i = 0;
|
||||||
|
while (current->content)
|
||||||
|
{
|
||||||
|
content = current->content;
|
||||||
|
env[i++] = ft_strmerger(3, content->key, "=", content->value);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (env);
|
||||||
|
}
|
63
env3.c
Normal file
63
env3.c
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env3.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 14:09:42 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libftx/libftx.h"
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
char *get_value(char *str)
|
||||||
|
{
|
||||||
|
char *s;
|
||||||
|
int i;
|
||||||
|
int start;
|
||||||
|
|
||||||
|
s = ft_calloc(ft_strlen(str), sizeof(char));
|
||||||
|
start = get_index(str, '=');
|
||||||
|
i = start;
|
||||||
|
while (str[++i])
|
||||||
|
s[i - start - 1] = str[i];
|
||||||
|
return (s);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_key(char *str)
|
||||||
|
{
|
||||||
|
char *s;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
s = ft_calloc(ft_strlen(str), sizeof(char));
|
||||||
|
i = -1;
|
||||||
|
while (str[++i] != '=')
|
||||||
|
s[i] = str[i];
|
||||||
|
return (s);
|
||||||
|
}
|
||||||
|
|
||||||
|
int create_value_by_key_dup(char *key, char *value, t_list **env)
|
||||||
|
{
|
||||||
|
char *key_dup;
|
||||||
|
char *value_dup;
|
||||||
|
|
||||||
|
key_dup = ft_strdup(key);
|
||||||
|
if (key_dup == NULL)
|
||||||
|
{
|
||||||
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
value_dup = ft_strdup(value);
|
||||||
|
if (value_dup == NULL)
|
||||||
|
{
|
||||||
|
free(key);
|
||||||
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (create_value_by_key(key_dup, value_dup, env))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
67
env_fill.c
67
env_fill.c
@ -6,37 +6,55 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
|
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/17 13:41:51 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/20 15:39:28 by starnakin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libftx/libftx.h"
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include "utils/utils.h"
|
|
||||||
|
|
||||||
static char *ft_get_value(t_list **env, const char *str, size_t start,
|
static char *ft_get_value(t_list **env, char *key)
|
||||||
size_t stop)
|
|
||||||
{
|
{
|
||||||
char *key;
|
|
||||||
char *value;
|
char *value;
|
||||||
|
|
||||||
key = ft_strndup(str + start, stop);
|
|
||||||
if (key == NULL)
|
|
||||||
{
|
|
||||||
ft_eprintf("minishell: malloc failed\n");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
value = get_value_by_key(key, env);
|
value = get_value_by_key(key, env);
|
||||||
if (value == NULL)
|
if (value == NULL)
|
||||||
value = "";
|
value = "";
|
||||||
free(key);
|
|
||||||
return (value);
|
return (value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *ft_get_key(char *str)
|
||||||
|
{
|
||||||
|
char *key;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
if (ft_strncmp(str, "$$", 2) == 0)
|
||||||
|
{
|
||||||
|
key = ft_strdup("$");
|
||||||
|
if (key == NULL)
|
||||||
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
|
return (key);
|
||||||
|
}
|
||||||
|
if (str[i] == ' ' || str[i] == '\0')
|
||||||
|
{
|
||||||
|
key = ft_strdup("");
|
||||||
|
if (key == NULL)
|
||||||
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
|
return (key);
|
||||||
|
}
|
||||||
|
while (str[i] != '\0' && str[i] != '$' && str[i] != ' '
|
||||||
|
&& str[i] != '"')
|
||||||
|
i++;
|
||||||
|
key = ft_strndup(str + 1, i - 1);
|
||||||
|
if (key == NULL)
|
||||||
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
|
return (key);
|
||||||
|
}
|
||||||
|
|
||||||
char *ft_env_filler(t_list **env, const char *str)
|
char *ft_env_filler(t_list **env, const char *str)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
size_t y;
|
char *key;
|
||||||
char *out;
|
char *out;
|
||||||
char *temp;
|
char *temp;
|
||||||
char *value;
|
char *value;
|
||||||
@ -54,13 +72,22 @@ char *ft_env_filler(t_list **env, const char *str)
|
|||||||
i++;
|
i++;
|
||||||
while (out[i] == '$')
|
while (out[i] == '$')
|
||||||
{
|
{
|
||||||
y = i + 1;
|
key = ft_get_key(out + i);
|
||||||
while (out[y] != '\0' && out[y] != '$' && out[y] != ' ')
|
if (key == NULL)
|
||||||
y++;
|
{
|
||||||
value = ft_get_value(env, out, i + 1, y - i - 1);
|
free(out);
|
||||||
if (value == NULL)
|
|
||||||
return (NULL);
|
return (NULL);
|
||||||
temp = ft_strreplace(out, value, i, y);
|
}
|
||||||
|
value = ft_get_value(env, key);
|
||||||
|
if (value == NULL)
|
||||||
|
{
|
||||||
|
free(key);
|
||||||
|
free(out);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
temp = ft_strreplace(out, value, i, ft_strlen(key) + 1 + i);
|
||||||
|
free(key);
|
||||||
|
i = i + ft_strlen(value);
|
||||||
free(out);
|
free(out);
|
||||||
if (temp == NULL)
|
if (temp == NULL)
|
||||||
{
|
{
|
||||||
|
66
execution.c
66
execution.c
@ -1,6 +1,17 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* execution.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 22:26:54 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libftx/libftx.h"
|
#include "libftx/libftx.h"
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include "utils/utils.h"
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static char *ft_get_executable_path(char *executable_name, t_list **env)
|
static char *ft_get_executable_path(char *executable_name, t_list **env)
|
||||||
@ -10,6 +21,8 @@ static char *ft_get_executable_path(char *executable_name, t_list **env)
|
|||||||
char **tab;
|
char **tab;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
|
if (executable_name == NULL)
|
||||||
|
return (NULL);
|
||||||
path = NULL;
|
path = NULL;
|
||||||
if (executable_name[0] == '.' || executable_name[0] == '/')
|
if (executable_name[0] == '.' || executable_name[0] == '/')
|
||||||
{
|
{
|
||||||
@ -83,10 +96,36 @@ static int ft_excutor(t_cmd *cmd, t_list **env)
|
|||||||
return (return_value);
|
return (return_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_cmds_executor(t_list **cmds, t_list **env)
|
static int ft_own_cmd(t_list **env, t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int return_code;
|
||||||
|
|
||||||
|
return_code = -1;
|
||||||
|
if (ft_strcmp(cmd->executable, "pwd") == 0)
|
||||||
|
return_code = pwd(cmd->fd_out);
|
||||||
|
else if (ft_strcmp(cmd->executable, "env") == 0)
|
||||||
|
return_code = print_env(env, cmd->fd_out);
|
||||||
|
else if (ft_strcmp(cmd->executable, "export") == 0)
|
||||||
|
return_code = (print_export(env, cmd->fd_out));
|
||||||
|
else if (ft_strcmp(cmd->executable, "cd") == 0)
|
||||||
|
return_code = (move_folder(cmd->args[0], cmd->fd_out));
|
||||||
|
/* if (ft_strcmp(cmd->executable, "unset") == 0) */
|
||||||
|
/* return_code = (unset(env, cmd->args, cmd->fd_out)); */
|
||||||
|
else if (ft_strcmp(cmd->executable, "echo") == 0)
|
||||||
|
return_code = (echo(cmd->fd_out, cmd->args + 1));
|
||||||
|
else if (ft_strcmp(cmd->executable, "exit") == 0)
|
||||||
|
return_code = -2;
|
||||||
|
if (return_code != -1)
|
||||||
|
cmd->executable = NULL;
|
||||||
|
return (return_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_cmds_executor(t_data *data, t_list **cmds)
|
||||||
{
|
{
|
||||||
t_cmd *content;
|
t_cmd *content;
|
||||||
t_list *current;
|
t_list *current;
|
||||||
|
char *return_value;
|
||||||
|
int cmd_return;
|
||||||
int fds[2];
|
int fds[2];
|
||||||
|
|
||||||
current = *cmds;
|
current = *cmds;
|
||||||
@ -103,13 +142,28 @@ int ft_cmds_executor(t_list **cmds, t_list **env)
|
|||||||
content->fd_out = fds[1];
|
content->fd_out = fds[1];
|
||||||
((t_cmd *) current->next->content)->fd_in = fds[0];
|
((t_cmd *) current->next->content)->fd_in = fds[0];
|
||||||
}
|
}
|
||||||
content->executable = ft_get_executable_path(content->executable, env);
|
cmd_return = ft_own_cmd(data->env, content);
|
||||||
|
if (cmd_return == -1)
|
||||||
|
{
|
||||||
|
content->executable = ft_get_executable_path(
|
||||||
|
content->executable, data->env);
|
||||||
if (content->executable != NULL)
|
if (content->executable != NULL)
|
||||||
ft_excutor(content, env);
|
cmd_return = ft_excutor(content, data->env);
|
||||||
if (content->fd_in != 0)
|
}
|
||||||
|
else if (cmd_return == -2)
|
||||||
|
return (-1);
|
||||||
|
data->exit_code = cmd_return;
|
||||||
|
return_value = ft_itoa(cmd_return);
|
||||||
|
if (return_value == NULL)
|
||||||
|
{
|
||||||
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (content->fd_in > 2)
|
||||||
close(content->fd_in);
|
close(content->fd_in);
|
||||||
if (content->fd_out != 1 && content->fd_out != 2)
|
if (content->fd_out > 2)
|
||||||
close(content->fd_out);
|
close(content->fd_out);
|
||||||
|
set_value_by_key("?", return_value, data->env);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
11
infile.c
11
infile.c
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
|
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/17 13:19:09 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -86,13 +86,10 @@ static int ft_remove_infile(char *line)
|
|||||||
while (tab[i] != NULL)
|
while (tab[i] != NULL)
|
||||||
{
|
{
|
||||||
if (tab[i][0] == '<')
|
if (tab[i][0] == '<')
|
||||||
{
|
ft_strshift(line + y,
|
||||||
ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
|
-1 * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 2));
|
||||||
i++;
|
|
||||||
ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
y = y + ft_strlen(tab[i]);
|
y = y + ft_strlen(tab[i]) + (y != 0);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
ft_freer_tab_ultimate(1, tab);
|
ft_freer_tab_ultimate(1, tab);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/17 16:24:42 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/21 13:28:19 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,6 +19,8 @@
|
|||||||
char *ft_ultoa_base(unsigned long long n, char *base);
|
char *ft_ultoa_base(unsigned long long n, char *base);
|
||||||
int ft_printf(const char *format, ...);
|
int ft_printf(const char *format, ...);
|
||||||
int ft_eprintf(const char *format, ...);
|
int ft_eprintf(const char *format, ...);
|
||||||
|
int ft_dprintf(int fd, const char *format, ...);
|
||||||
|
int ft_dprintf(int fd, const char *format, ...);
|
||||||
char *get_next_line(int fd);
|
char *get_next_line(int fd);
|
||||||
size_t ft_random_generator(size_t start, size_t stop);
|
size_t ft_random_generator(size_t start, size_t stop);
|
||||||
void ft_freer_tab_ultimate(size_t len, ...);
|
void ft_freer_tab_ultimate(size_t len, ...);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/06 22:01:28 by cchauvet #+# #+# */
|
/* Created: 2022/10/06 22:01:28 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/10/12 16:34:31 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/21 13:27:31 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -22,3 +22,14 @@ int ft_printf(const char *format, ...)
|
|||||||
va_end(va);
|
va_end(va);
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ft_dprintf(int fd, const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list va;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
va_start(va, format);
|
||||||
|
i = ft_vdprintf(fd, format, va);
|
||||||
|
va_end(va);
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/17 16:29:18 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/21 13:27:51 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -32,6 +32,7 @@ int ft_dprintarg(int fd, int c, va_list va);
|
|||||||
int ft_dprintstrtab(int fd, char **tab);
|
int ft_dprintstrtab(int fd, char **tab);
|
||||||
|
|
||||||
int ft_printf(const char *format, ...);
|
int ft_printf(const char *format, ...);
|
||||||
|
int ft_dprintf(int fd, const char *format, ...);
|
||||||
int ft_eprintf(const char *format, ...);
|
int ft_eprintf(const char *format, ...);
|
||||||
|
|
||||||
int ft_vdprintf(int fd, const char *format, va_list va);
|
int ft_vdprintf(int fd, const char *format, va_list va);
|
||||||
|
46
main.c
46
main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/17 16:06:02 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/21 22:05:04 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -17,8 +17,13 @@ static char *ft_get_user_input(t_list **env)
|
|||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
char *prompt;
|
char *prompt;
|
||||||
|
char *pwd;
|
||||||
|
|
||||||
prompt = ft_strmerger(2, get_value_by_key("PWD", env), "$ ");
|
pwd = get_pwd(2);
|
||||||
|
if (pwd == NULL)
|
||||||
|
return (NULL);
|
||||||
|
prompt = ft_strmerger(2, pwd, "$ ");
|
||||||
|
free(pwd);
|
||||||
if (prompt == NULL)
|
if (prompt == NULL)
|
||||||
{
|
{
|
||||||
ft_eprintf("minishell: malloc failed\n");
|
ft_eprintf("minishell: malloc failed\n");
|
||||||
@ -30,9 +35,10 @@ static char *ft_get_user_input(t_list **env)
|
|||||||
return (line);
|
return (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ft_minishell(t_list **env, char *line)
|
static int ft_minishell(t_data *data, char *line)
|
||||||
{
|
{
|
||||||
t_list **cmds;
|
t_list **cmds;
|
||||||
|
int code;
|
||||||
char *line_clean;
|
char *line_clean;
|
||||||
int infile;
|
int infile;
|
||||||
int outfile;
|
int outfile;
|
||||||
@ -58,7 +64,7 @@ static int ft_minishell(t_list **env, char *line)
|
|||||||
free(line_clean);
|
free(line_clean);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
cmds = ft_parse_cmds(line_clean, env, infile, outfile);
|
cmds = ft_parse_cmds(line_clean, data->env, infile, outfile);
|
||||||
if (cmds == NULL)
|
if (cmds == NULL)
|
||||||
{
|
{
|
||||||
close(outfile);
|
close(outfile);
|
||||||
@ -68,16 +74,11 @@ static int ft_minishell(t_list **env, char *line)
|
|||||||
free(line_clean);
|
free(line_clean);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
if (ft_cmds_executor(cmds, env) == 1)
|
code = ft_cmds_executor(data, cmds);
|
||||||
{
|
|
||||||
close(outfile);
|
|
||||||
close(infile);
|
|
||||||
ft_lstclear(cmds, ft_cmddel);
|
ft_lstclear(cmds, ft_cmddel);
|
||||||
free(cmds);
|
free(cmds);
|
||||||
free(line_clean);
|
free(line_clean);
|
||||||
return (1);
|
return (code);
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
@ -91,9 +92,6 @@ int main(int ac, char **av, char **env)
|
|||||||
data.env = init_env(env);
|
data.env = init_env(env);
|
||||||
if (data.env == NULL)
|
if (data.env == NULL)
|
||||||
return (1);
|
return (1);
|
||||||
free(data.env);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
if (ft_minishell(data.env, av[1]) == 1)
|
if (ft_minishell(data.env, av[1]) == 1)
|
||||||
{
|
{
|
||||||
ft_lstclear(data.env, env_del);
|
ft_lstclear(data.env, env_del);
|
||||||
@ -102,14 +100,11 @@ int main(int ac, char **av, char **env)
|
|||||||
}
|
}
|
||||||
ft_lstclear(data.env, env_del);
|
ft_lstclear(data.env, env_del);
|
||||||
free(data.env);
|
free(data.env);
|
||||||
free(line);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int ac, char **av, char **env)
|
int main(int ac, char **av, char **env)
|
||||||
{
|
{
|
||||||
t_data data;
|
t_data data;
|
||||||
@ -118,6 +113,13 @@ int main(int ac, char **av, char **env)
|
|||||||
data.env = init_env(env);
|
data.env = init_env(env);
|
||||||
if (data.env == NULL)
|
if (data.env == NULL)
|
||||||
return (1);
|
return (1);
|
||||||
|
if (create_value_by_key_dup("", "$", data.env) == 1
|
||||||
|
|| create_value_by_key_dup("?", "0", data.env) == 1)
|
||||||
|
{
|
||||||
|
ft_lstclear(data.env, env_del);
|
||||||
|
free(data.env);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
line = ft_get_user_input(data.env);
|
line = ft_get_user_input(data.env);
|
||||||
if (line == NULL)
|
if (line == NULL)
|
||||||
{
|
{
|
||||||
@ -127,13 +129,8 @@ int main(int ac, char **av, char **env)
|
|||||||
}
|
}
|
||||||
while (line != NULL)
|
while (line != NULL)
|
||||||
{
|
{
|
||||||
if (ft_minishell(data.env, line) == 1)
|
if (ft_minishell(&data, line) == -1)
|
||||||
{
|
break ;
|
||||||
ft_lstclear(data.env, env_del);
|
|
||||||
free(data.env);
|
|
||||||
free(line);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
free(line);
|
free(line);
|
||||||
line = ft_get_user_input(data.env);
|
line = ft_get_user_input(data.env);
|
||||||
if (line == NULL)
|
if (line == NULL)
|
||||||
@ -145,6 +142,7 @@ int main(int ac, char **av, char **env)
|
|||||||
}
|
}
|
||||||
ft_lstclear(data.env, env_del);
|
ft_lstclear(data.env, env_del);
|
||||||
free(data.env);
|
free(data.env);
|
||||||
|
return (data.exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
52
minishell.h
52
minishell.h
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/17 14:29:56 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/21 22:03:41 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,12 +19,19 @@
|
|||||||
# include <fcntl.h>
|
# include <fcntl.h>
|
||||||
# include <sys/wait.h>
|
# include <sys/wait.h>
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <limits.h>
|
||||||
|
# include <string.h>
|
||||||
# include <readline/readline.h>
|
# include <readline/readline.h>
|
||||||
# include <readline/history.h>
|
# include <readline/history.h>
|
||||||
# define DEBUG 0
|
# define DEBUG 0
|
||||||
t_list **init_env(char **env);
|
|
||||||
int set_value_by_key(char *key, char *value, t_list **env);
|
typedef struct s_data
|
||||||
char *get_value_by_key(char *key, t_list **head);
|
{
|
||||||
|
t_list **env;
|
||||||
|
int exit_code;
|
||||||
|
} t_data;
|
||||||
|
|
||||||
int ft_syntatic_verif(const char *str);
|
int ft_syntatic_verif(const char *str);
|
||||||
int ft_file_is_readable(const char *path);
|
int ft_file_is_readable(const char *path);
|
||||||
int ft_file_is_writable(const char *path);
|
int ft_file_is_writable(const char *path);
|
||||||
@ -34,7 +41,7 @@ int ft_infile(char *line);
|
|||||||
int ft_outfile(char *line);
|
int ft_outfile(char *line);
|
||||||
int ft_heredoc(char *stop);
|
int ft_heredoc(char *stop);
|
||||||
size_t ft_seglen_quoted(const char *str, char c);
|
size_t ft_seglen_quoted(const char *str, char c);
|
||||||
int ft_cmds_executor(t_list **cmds, t_list **env);
|
int ft_cmds_executor(t_data *data, t_list **cmds);
|
||||||
char **ft_split_quoted(const char *s, char c);
|
char **ft_split_quoted(const char *s, char c);
|
||||||
void ft_cmddel(void *content);
|
void ft_cmddel(void *content);
|
||||||
void env_del(void *content);
|
void env_del(void *content);
|
||||||
@ -44,6 +51,35 @@ char *ft_normalizer(char *str);
|
|||||||
char *ft_env_filler(t_list **env, const char *str);
|
char *ft_env_filler(t_list **env, const char *str);
|
||||||
char **env_to_strs(t_list **head);
|
char **env_to_strs(t_list **head);
|
||||||
|
|
||||||
|
/* Environnement */
|
||||||
|
t_list **init_env(char **env);
|
||||||
|
char **env_to_strs(t_list **head);
|
||||||
|
int create_value_by_key(char *key, char *value, t_list **head);
|
||||||
|
int create_value_by_key_dup(char *key, char *value, t_list **head);
|
||||||
|
int set_value_by_key(char *key, char *value, t_list **head);
|
||||||
|
char *get_value_by_key(char *key, t_list **head);
|
||||||
|
int get_index(char *s, char c);
|
||||||
|
void swap_env_3(void **a, void **b, void **c);
|
||||||
|
void swap_env(void **a, void **b);
|
||||||
|
void env_del(void *ptr);
|
||||||
|
char **env_to_strs(t_list **head);
|
||||||
|
char *get_value(char *str);
|
||||||
|
char *get_key(char *str);
|
||||||
|
|
||||||
|
/* Echo */
|
||||||
|
int echo(int fd, char **strs);
|
||||||
|
/* PWD */
|
||||||
|
int pwd(int fd);
|
||||||
|
char *get_pwd(int fd);
|
||||||
|
/* ENV */
|
||||||
|
int print_env(t_list **head, int fd);
|
||||||
|
/* EXPORT */
|
||||||
|
int print_export(t_list **head, int fd);
|
||||||
|
/* CD */
|
||||||
|
int move_folder(char *path, int fd);
|
||||||
|
/* UNSET */
|
||||||
|
int unset(t_list **env, char **args, int fd);
|
||||||
|
|
||||||
typedef struct s_cmd
|
typedef struct s_cmd
|
||||||
{
|
{
|
||||||
int fd_in;
|
int fd_in;
|
||||||
@ -52,12 +88,6 @@ typedef struct s_cmd
|
|||||||
char **args;
|
char **args;
|
||||||
} t_cmd;
|
} t_cmd;
|
||||||
|
|
||||||
typedef struct s_data
|
|
||||||
{
|
|
||||||
t_list **env;
|
|
||||||
int heredoc;
|
|
||||||
} t_data;
|
|
||||||
|
|
||||||
typedef struct s_env
|
typedef struct s_env
|
||||||
{
|
{
|
||||||
void *key;
|
void *key;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */
|
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/17 13:19:46 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 19:03:00 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -86,12 +86,11 @@ static int ft_remove_outfile(char *line)
|
|||||||
{
|
{
|
||||||
if (tab[i][0] == '>')
|
if (tab[i][0] == '>')
|
||||||
{
|
{
|
||||||
ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
|
ft_strshift(line + y,
|
||||||
i++;
|
-1 * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 2));
|
||||||
ft_strshift(line + y, -1 * (ft_strlen(tab[i]) + 1));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
y = y + ft_strlen(tab[i]);
|
y = y + ft_strlen(tab[i]) + (y != 0);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
ft_freer_tab_ultimate(1, tab);
|
ft_freer_tab_ultimate(1, tab);
|
||||||
|
12
syntatics.c
12
syntatics.c
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* syntatics.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 13:00:06 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libftx/libftx.h"
|
#include "libftx/libftx.h"
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include "utils/utils.h"
|
#include "utils/utils.h"
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_is_in_quote.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/21 12:59:34 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 23:08:10 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
int ft_is_in_quote(const char *str, size_t n)
|
int ft_is_in_quote(const char *str, size_t n)
|
||||||
@ -15,15 +27,13 @@ int ft_is_in_quote(const char *str, size_t n)
|
|||||||
{
|
{
|
||||||
if (simple_quoted == 0)
|
if (simple_quoted == 0)
|
||||||
double_quoted = !double_quoted;
|
double_quoted = !double_quoted;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (str[i] == '\'')
|
else if (str[i] == '\'')
|
||||||
{
|
{
|
||||||
if (double_quoted == 0)
|
if (double_quoted == 0)
|
||||||
simple_quoted = !simple_quoted;
|
simple_quoted = !simple_quoted;
|
||||||
|
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (simple_quoted == 1 + (double_quoted == 1) * 2);
|
return (simple_quoted + double_quoted * 2);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/02/15 14:12:00 by cchauvet #+# #+# */
|
/* Created: 2023/02/15 14:12:00 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/02/16 13:20:50 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/21 14:33:28 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,6 +19,7 @@ char *ft_quote_remover(char *str)
|
|||||||
ssize_t stop;
|
ssize_t stop;
|
||||||
|
|
||||||
start = -1;
|
start = -1;
|
||||||
|
stop = -1;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (str[i] != '\0')
|
while (str[i] != '\0')
|
||||||
{
|
{
|
||||||
@ -27,17 +28,18 @@ char *ft_quote_remover(char *str)
|
|||||||
if (start == -1)
|
if (start == -1)
|
||||||
start = i;
|
start = i;
|
||||||
else if (str[i] == str[start])
|
else if (str[i] == str[start])
|
||||||
{
|
|
||||||
stop = i;
|
stop = i;
|
||||||
break ;
|
|
||||||
}
|
}
|
||||||
}
|
if (stop != -1)
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (start != -1)
|
|
||||||
{
|
{
|
||||||
ft_strshift(str, -1);
|
ft_strshift(str + start, -1);
|
||||||
ft_strshift(str + stop - 1, -1);
|
ft_strshift(str + stop - 1, -1);
|
||||||
|
start = -1;
|
||||||
|
stop = -1;
|
||||||
|
i = i - 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
return (str);
|
return (str);
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strnchr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/21 12:59:06 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 12:59:07 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
ssize_t ft_strnchr(const char *str, char c)
|
ssize_t ft_strnchr(const char *str, char c)
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strncpy.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/21 12:59:16 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 12:59:19 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
size_t ft_strncpy(char *dst, const char *src, size_t n)
|
size_t ft_strncpy(char *dst, const char *src, size_t n)
|
||||||
|
@ -1,11 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strreplace.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/21 12:46:20 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/02/21 12:58:44 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
char *ft_strreplace(const char *str, const char *fill, size_t start, size_t stop)
|
char *ft_strreplace(const char *str, const char *fill,
|
||||||
|
size_t start, size_t stop)
|
||||||
{
|
{
|
||||||
char *out;
|
char *out;
|
||||||
size_t sum;
|
size_t sum;
|
||||||
|
|
||||||
out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char)));
|
out = malloc((ft_strlen(str) + ft_strlen(fill) -\
|
||||||
|
(stop - start) + 1 * sizeof(char)));
|
||||||
if (out == NULL)
|
if (out == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
ft_strncpy(out, str, start);
|
ft_strncpy(out, str, start);
|
||||||
|
Reference in New Issue
Block a user