Compare commits
275 Commits
13d412ea71
...
master
Author | SHA1 | Date | |
---|---|---|---|
bd5e1671fc | |||
62e887cfbd | |||
3e7dcd1ba9 | |||
9ab9c06593 | |||
2f6daa4ce5 | |||
b502364a67 | |||
0b56e95868 | |||
882a39fc94 | |||
59d9ed3514 | |||
058c96849b | |||
37bd3efc98 | |||
425fbd15cd | |||
d3fa573054 | |||
227d16ca53 | |||
91ef62d8f9 | |||
d06eb570fc | |||
1ef3fa115e | |||
10db23e4ef | |||
a6a0293095 | |||
a48da783dd | |||
be1f7ebf8c | |||
e65a9f32c0 | |||
8a2eb85dc6 | |||
5d482fc65a | |||
e0fb7f1fd0 | |||
404fcaa15f | |||
fe535a21f0 | |||
8a245f0dd8 | |||
f93fed5cdd | |||
d99aa39070 | |||
f0afd69bf6 | |||
5cbb6f078d | |||
b3e9a23f32 | |||
b1805834ec | |||
0d231a062d | |||
10413e500b | |||
760b8b644e | |||
fdfe1c8b95 | |||
0bf045d4ab | |||
7e0bbd1203 | |||
b96b617e09 | |||
f6617e07c7 | |||
a7e75bb163 | |||
d9ae113b94 | |||
88d955fddc | |||
68c945ae05 | |||
8af6b42642 | |||
46fe9caded | |||
1e00fee53c | |||
4dc8793eec | |||
f66ab7790e | |||
41bbe2bc0b | |||
3374d21cad | |||
94f55af0be | |||
6f3b1ab918 | |||
6a81705b98 | |||
27b8874f75 | |||
cdfa81328b | |||
bb00de527a | |||
7e77c4fe48 | |||
13e455cbf4 | |||
471845b565 | |||
1c43d89fa1 | |||
a941a0138f | |||
011c801f1d | |||
f874ed9f7b | |||
dd49432d0d | |||
1c8892983e | |||
c1e61780e0 | |||
6498031d59 | |||
3e656abf5d | |||
a9df160295 | |||
2a0846fbf0 | |||
9787d71b85 | |||
8a6a0012dc | |||
4acefd9208 | |||
c8c4297f8b | |||
d05edefa0f | |||
392c2d94d0 | |||
94fa4de0a2 | |||
d54cd98197 | |||
147af02c6e | |||
67e8a60423 | |||
96a5856592 | |||
57dd5c9962 | |||
689a4e4eb6 | |||
db51c3c85b | |||
c5ddcdc892 | |||
45fc051e9d | |||
e195fe2c6d | |||
215b0dbb7e | |||
22dc9776d6 | |||
b3d060874d | |||
72ff3ba134 | |||
086fd3bcbe | |||
0fa822f6db | |||
a58f9a0c76 | |||
d56aac4b25 | |||
96696243d0 | |||
f5940ee99e | |||
ee3d535393 | |||
08261b1921 | |||
7ea602b12c | |||
7ffe556cd4 | |||
6ce54ea6fe | |||
74bbbd899a | |||
b1d827e106 | |||
bf15a86c2e | |||
1b17df6170 | |||
51d7e60790 | |||
4d3c25547e | |||
c43331dca3 | |||
ec752adbfa | |||
dbb39712ea | |||
e2c43eb973 | |||
3d68490320 | |||
0694b36a72 | |||
3d62d3dde9 | |||
69fc4c573a | |||
087a96e54b | |||
1a4ed128e7 | |||
d36d9c92f5 | |||
6c8e161a32 | |||
f20eab31dd | |||
86f01779cc | |||
c6d683a1fc | |||
f956986789 | |||
a18c4cae82 | |||
5c9be2d56b | |||
15c95b45b1 | |||
876c75bb92 | |||
8093b250ce | |||
2930755dd3 | |||
3540743135 | |||
5bfc613126 | |||
686ea5d0db | |||
e7f8373b4a | |||
9cbb1e139a | |||
06f6308bc4 | |||
8ea2e6c620 | |||
b2c7013cf9 | |||
960daa8658 | |||
7f4fd9d421 | |||
da75bdbf72 | |||
af7336fbf3 | |||
00dc583ff8 | |||
4342d9a015 | |||
46d30864ed | |||
e0e329a355 | |||
6e0d7288e8 | |||
5541a1f3fe | |||
d5eb265a3f | |||
b914753c47 | |||
5948237aad | |||
9a5cca3e2d | |||
00e570d92e | |||
77620211da | |||
07bc37cc71 | |||
ed546f5a8d | |||
ca18dd4205 | |||
370e5149d5 | |||
fc82eefd7b | |||
51f892350a | |||
0941f3a370 | |||
9f0b0f5422 | |||
3d66fbe9a9 | |||
1626424ed7 | |||
8e22eb210a | |||
57be71a168 | |||
0327d0f5d7 | |||
2f386686c6 | |||
7728ded62d | |||
405c1937cd | |||
b98717ae1a | |||
413d75b94c | |||
4f66624667 | |||
f31d805d83 | |||
28fedfdd75 | |||
0661f2fbd1 | |||
29a4a864c3 | |||
cdde24f0d0 | |||
a4befa5fa6 | |||
94f48602d1 | |||
2b66ce5bc3 | |||
f346c5c2ce | |||
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 | |||
daaa9dea51 | |||
d59562899b | |||
fa37938073 | |||
7bec120312 | |||
6090784c02 | |||
d4cead2b0f | |||
61cd1209ff | |||
6ecb66a60e | |||
642af26133 | |||
38e1b9834f | |||
e31fff5137 | |||
8b771d6615 | |||
96da8e54c3 | |||
e48ee8b693 | |||
f20038e37b | |||
edbd267c0d | |||
de8dfc41d4 | |||
82a0af80ad | |||
c44530728c | |||
1fb6e0a2c1 | |||
02f5815485 | |||
22b7a4feea | |||
8366b63821 | |||
0ba212410f | |||
56eead9241 | |||
99fdd578e9 | |||
c5467769d9 | |||
b20d4e0ddc | |||
afaf34f869 | |||
8283472d2e | |||
402b6e875e | |||
1423d42583 | |||
78b66b539d | |||
67fb6d0533 | |||
4533b7a75d | |||
cf4bacd42e | |||
6ab114eeeb | |||
7b3389c75d | |||
1a04f73b49 | |||
4258c1da62 | |||
b8017175e8 | |||
3654ff91bb | |||
4f77dde859 | |||
1d9d538e8d | |||
ece97a4882 | |||
088a3be5eb |
37
Makefile
37
Makefile
@ -1,5 +1,31 @@
|
|||||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c
|
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_charset_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c utils/ft_atoi_check.c ./utils/ft_get_executable.c ./utils/fd.c
|
||||||
SRCS = env.c ${UTILS_SRC}
|
|
||||||
|
BUILTINS_SRC = builtins/pwd.c \
|
||||||
|
builtins/export.c \
|
||||||
|
builtins/env.c \
|
||||||
|
builtins/cd.c \
|
||||||
|
builtins/exit.c \
|
||||||
|
builtins/unset.c \
|
||||||
|
builtins/echo.c
|
||||||
|
|
||||||
|
SRCS = ${BUILTINS_SRC} \
|
||||||
|
${UTILS_SRC} \
|
||||||
|
main.c \
|
||||||
|
./cmd/cmd.c \
|
||||||
|
./env/env1.c \
|
||||||
|
./env/env2.c \
|
||||||
|
./env/env3.c \
|
||||||
|
./env/env_fill.c \
|
||||||
|
./data/data.c \
|
||||||
|
./execution/execution.c \
|
||||||
|
./syntax/syntax.c \
|
||||||
|
./format/format.c \
|
||||||
|
./redirection/heredoc.c \
|
||||||
|
./redirection/file.c \
|
||||||
|
./redirection/redirection.c \
|
||||||
|
./redirection/check.c \
|
||||||
|
./parse/parse.c \
|
||||||
|
./signal/signal.c
|
||||||
|
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
@ -7,7 +33,7 @@ NAME = minishell
|
|||||||
|
|
||||||
CC = clang
|
CC = clang
|
||||||
|
|
||||||
CFLAGS = -g -Wall -Werror -Wextra
|
CFLAGS = -Werror -Wextra -Wall -g
|
||||||
|
|
||||||
LIBS = libftx/libftx.a
|
LIBS = libftx/libftx.a
|
||||||
|
|
||||||
@ -18,7 +44,7 @@ all: ${NAME}
|
|||||||
|
|
||||||
${NAME}: ${OBJS}
|
${NAME}: ${OBJS}
|
||||||
make -C libftx all
|
make -C libftx all
|
||||||
${CC} ${OBJS} -o ${NAME} ${LIBS}
|
${CC} ${OBJS} -o ${NAME} ${LIBS} -lreadline
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
make -C libftx clean
|
make -C libftx clean
|
||||||
@ -28,7 +54,8 @@ fclean: clean
|
|||||||
make -C libftx fclean
|
make -C libftx fclean
|
||||||
rm -f ${NAME}
|
rm -f ${NAME}
|
||||||
|
|
||||||
re: fclean all
|
re: fclean
|
||||||
|
make all
|
||||||
|
|
||||||
.PHONY: all clean fclean re
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
|
BIN
argprinter
BIN
argprinter
Binary file not shown.
34
bozoshell.h
Normal file
34
bozoshell.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* bozoshell.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/14 16:16:31 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef BOZOSHELL_H
|
||||||
|
# define BOZOSHELL_H
|
||||||
|
# include "./env/env.h"
|
||||||
|
# include "./cmd/cmd.h"
|
||||||
|
# include "./parse/parse.h"
|
||||||
|
# include "./syntax/syntax.h"
|
||||||
|
# include "./execution/execution.h"
|
||||||
|
# include "./builtins/builtins.h"
|
||||||
|
# include "./format/format.h"
|
||||||
|
# include "./redirection/redirection.h"
|
||||||
|
# include "./libftx/libftx.h"
|
||||||
|
# include "./utils/utils.h"
|
||||||
|
# include "./data/data.h"
|
||||||
|
# include "./signal/signal.h"
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <readline/readline.h>
|
||||||
|
# include <readline/history.h>
|
||||||
|
# include <signal.h>
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <sys/wait.h>
|
||||||
|
|
||||||
|
#endif
|
Binary file not shown.
26
builtins/builtins.h
Normal file
26
builtins/builtins.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* builtins.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/27 13:41:30 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/18 12:57:36 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef BUILTINS_H
|
||||||
|
# define BUILTINS_H
|
||||||
|
# include "../libftx/libft/list.h"
|
||||||
|
|
||||||
|
int echo(int fd, char **strs);
|
||||||
|
int pwd(int fd);
|
||||||
|
char *get_pwd(void);
|
||||||
|
int print_env(t_list **env, int fd);
|
||||||
|
int ft_export(t_list **env, char **args, int fd);
|
||||||
|
int move_folder(char **args, t_list **env, int fd);
|
||||||
|
int unset(t_list **env, char **args, int fd);
|
||||||
|
int ft_exit(char **args, int err);
|
||||||
|
|
||||||
|
#endif
|
22
builtins/builtins_private.h
Normal file
22
builtins/builtins_private.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* builtins_private.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/27 13:41:42 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/18 12:57:25 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef BUILTINS_PRIVATE_H
|
||||||
|
# define BUILTINS_PRIVATE_H
|
||||||
|
# include <limits.h>
|
||||||
|
# include "../libftx/libftx.h"
|
||||||
|
# include "../env/env.h"
|
||||||
|
# include "../utils/utils.h"
|
||||||
|
|
||||||
|
char *get_pwd(void);
|
||||||
|
|
||||||
|
#endif
|
62
builtins/cd.c
Normal file
62
builtins/cd.c
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/18 13:22:22 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./builtins_private.h"
|
||||||
|
|
||||||
|
int make_move(char *path, t_list **env)
|
||||||
|
{
|
||||||
|
char *join;
|
||||||
|
char *old;
|
||||||
|
|
||||||
|
join = ft_strjoin("/", path);
|
||||||
|
join = ft_strfjoin(get_pwd(), join);
|
||||||
|
old = get_pwd();
|
||||||
|
if (chdir(join) == 0)
|
||||||
|
{
|
||||||
|
set_value_by_key("OLDPWD", old, env);
|
||||||
|
set_value_by_key("PWD", get_pwd(), env);
|
||||||
|
free(join);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
free(old);
|
||||||
|
free(join);
|
||||||
|
write(2, "cd: No such file or directory\n", 30);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int move_folder(char **args, t_list **env)
|
||||||
|
{
|
||||||
|
char *path;
|
||||||
|
char *old;
|
||||||
|
|
||||||
|
if (args[0] == NULL || args[1] != NULL)
|
||||||
|
{
|
||||||
|
write(2, "cd: Wrong number's argument\n", 28);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
path = args[0];
|
||||||
|
if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0)
|
||||||
|
{
|
||||||
|
old = get_pwd();
|
||||||
|
if (chdir(path) == 0)
|
||||||
|
{
|
||||||
|
set_value_by_key("OLDPWD", old, env);
|
||||||
|
set_value_by_key("PWD", get_pwd(), env);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
free(old);
|
||||||
|
write(2, "cd: No such file or directory\n", 30);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (make_move(path, env));
|
||||||
|
}
|
63
builtins/echo.c
Normal file
63
builtins/echo.c
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* echo.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/11 14:57:00 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./builtins_private.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_backslash_n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (str[++i])
|
||||||
|
if (str[i] != '-' && str[i] != 'n')
|
||||||
|
return (1);
|
||||||
|
if (ft_strnstr(str, "n", ft_strlen(str)) && str[0] == '-')
|
||||||
|
*check_backslash_n = 1;
|
||||||
|
else
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int echo(int fd, char **strs)
|
||||||
|
{
|
||||||
|
int check_backslash_n;
|
||||||
|
int check_start_write;
|
||||||
|
int i;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
check_backslash_n = 0;
|
||||||
|
check_start_write = 0;
|
||||||
|
i = -1;
|
||||||
|
while (strs[++i])
|
||||||
|
{
|
||||||
|
y = -1;
|
||||||
|
while (is_space(strs[i][++y]))
|
||||||
|
;
|
||||||
|
if (check_start_write == 1
|
||||||
|
|| check_argument(strs[i], &check_backslash_n))
|
||||||
|
{
|
||||||
|
check_start_write = 1;
|
||||||
|
ft_putstr_fd(strs[i], fd);
|
||||||
|
if (strs[i + 1] != NULL)
|
||||||
|
write(fd, " ", 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!check_backslash_n)
|
||||||
|
write(fd, "\n", 1);
|
||||||
|
return (0);
|
||||||
|
}
|
32
builtins/env.c
Normal file
32
builtins/env.c
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/14 14:56:02 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/30 15:15:04 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./builtins_private.h"
|
||||||
|
|
||||||
|
int print_env(t_list **env, int fd)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
|
||||||
|
current = *env;
|
||||||
|
while (current->next != NULL)
|
||||||
|
{
|
||||||
|
if (((t_env *)(current->content))->value)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
43
builtins/exit.c
Normal file
43
builtins/exit.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* exit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/24 10:17:59 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/05 12:30:46 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./builtins_private.h"
|
||||||
|
|
||||||
|
static int error(int err, char *reason, char *problem)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("bozoshell: bozo_exit ", 2);
|
||||||
|
if (problem != NULL)
|
||||||
|
{
|
||||||
|
ft_putstr_fd(problem, 2);
|
||||||
|
write(2, ": ", 3);
|
||||||
|
}
|
||||||
|
ft_putstr_fd(reason, 2);
|
||||||
|
return (err);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_exit(char **args, int err)
|
||||||
|
{
|
||||||
|
if (args[0] == NULL)
|
||||||
|
{
|
||||||
|
write(1, "bozo_exit\n", 10);
|
||||||
|
return (err);
|
||||||
|
}
|
||||||
|
err = ft_atoi_check(args[0]);
|
||||||
|
if (err == 1)
|
||||||
|
return (error(err, "bozo_exit: numeric argument required\n", args[0]));
|
||||||
|
if (args[1] != NULL)
|
||||||
|
return (error(-1, "bozo_exit: too many arguments\n", NULL));
|
||||||
|
if (err > 0)
|
||||||
|
return (error(err, "bozo_exit: numeric argument required\n", args[0]));
|
||||||
|
write(1, "exit\n", 6);
|
||||||
|
return ((ft_atoi(args[0]) % 256 + 256) % 256);
|
||||||
|
}
|
114
builtins/export.c
Normal file
114
builtins/export.c
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* export.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/14 14:27:08 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/14 19:12:10 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./builtins_private.h"
|
||||||
|
|
||||||
|
static int error(char *str, char *to_free1, char *to_free2)
|
||||||
|
{
|
||||||
|
write(2, "bozoshell: export: `", 20);
|
||||||
|
write(2, str, ft_strlen(str));
|
||||||
|
write(2, "': not a valid identifier\n", 26);
|
||||||
|
if (to_free1 != NULL)
|
||||||
|
free(to_free1);
|
||||||
|
if (to_free2 != NULL)
|
||||||
|
free(to_free2);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_export(t_list **env, int fd)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
|
||||||
|
current = *env;
|
||||||
|
while (current->next != NULL)
|
||||||
|
{
|
||||||
|
if (ft_strcmp(((t_env *)(current->content))->key, "_"))
|
||||||
|
{
|
||||||
|
write(fd, "declare -x ", 11);
|
||||||
|
ft_putstr_fd(((t_env *)(current->content))->key, fd);
|
||||||
|
if (((t_env *)(current->content))->value != NULL)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("=", fd);
|
||||||
|
write(fd, "\"", 1);
|
||||||
|
ft_putstr_fd(((t_env *)(current->content))->value, fd);
|
||||||
|
write(fd, "\"\n", 2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
write(fd, "\n", 2);
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int set_key_value_export(t_list **env, char *args, char **key, char **value)
|
||||||
|
{
|
||||||
|
*key = ft_strndup(args, ft_strnchr(args, '='));
|
||||||
|
if (*key == NULL)
|
||||||
|
return (1);
|
||||||
|
if (ft_strlen(*key) == 0)
|
||||||
|
return (1);
|
||||||
|
if (possible_key(*key) == 2)
|
||||||
|
{
|
||||||
|
(*key)[ft_strlen(*key) - 1] = '\0';
|
||||||
|
if (get_value_by_key(*key, env) == NULL)
|
||||||
|
*value = ft_strdup(ft_strchr(args, '=') + 1);
|
||||||
|
else
|
||||||
|
*value = ft_strjoin(get_value_by_key(*key, env),
|
||||||
|
ft_strchr(args, '=') + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*value = ft_strdup(ft_strchr(args, '=') + 1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int add_export(t_list **env, char *args)
|
||||||
|
{
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
|
||||||
|
if (ft_strchr(args, '=') != NULL)
|
||||||
|
{
|
||||||
|
if (set_key_value_export(env, args, &key, &value))
|
||||||
|
return (error(args, key, NULL));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
key = ft_strdup(args);
|
||||||
|
value = get_value_by_key(key, env);
|
||||||
|
if (value != NULL)
|
||||||
|
value = ft_strdup(value);
|
||||||
|
if (possible_key(key) == 2)
|
||||||
|
return (error(key, key, value));
|
||||||
|
}
|
||||||
|
if (!possible_key(key))
|
||||||
|
return (error(args, key, value));
|
||||||
|
create_value_by_key(key, value, env);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_export(t_list **env, char **args, int fd)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
err = 0;
|
||||||
|
if (args[0] == NULL)
|
||||||
|
print_export(env, fd);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i = -1;
|
||||||
|
while (args[++i])
|
||||||
|
if (add_export(env, args[i]) == 1)
|
||||||
|
err = 1;
|
||||||
|
}
|
||||||
|
return (err);
|
||||||
|
}
|
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/04/18 12:59:25 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./builtins_private.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", 2);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_pwd(void)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = ft_calloc(PATH_MAX, sizeof(char *));
|
||||||
|
if (getcwd(str, PATH_MAX) != NULL)
|
||||||
|
return (str);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_putendl_fd("Error getcwd", 2);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
}
|
35
builtins/unset.c
Normal file
35
builtins/unset.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* unset.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/22 13:28:27 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/07 12:46:28 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./builtins_private.h"
|
||||||
|
|
||||||
|
int error(char *str, int fd)
|
||||||
|
{
|
||||||
|
write(fd, "bozoshell: unset: `", 19);
|
||||||
|
write(fd, str, ft_strlen(str));
|
||||||
|
write(fd, "': not a valid identifier\n", 26);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int unset(t_list **env, char **args, int fd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
err = 0;
|
||||||
|
while (args[++i])
|
||||||
|
if (delete_by_key(args[i], env))
|
||||||
|
if (!possible_key(args[i]))
|
||||||
|
err = error(args[i], fd);
|
||||||
|
return (err);
|
||||||
|
}
|
73
cmd/cmd.c
Normal file
73
cmd/cmd.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cmd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/17 11:57:07 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "cmd.h"
|
||||||
|
#include "cmd_private.h"
|
||||||
|
#include "../signal/signal.h"
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
void ft_cmddel(void *ptr)
|
||||||
|
{
|
||||||
|
t_cmd *content;
|
||||||
|
|
||||||
|
content = (t_cmd *) ptr;
|
||||||
|
if (content->args != NULL)
|
||||||
|
ft_freer_tab_ultimate(1, content->args);
|
||||||
|
if (content->own_cmd == false && content->executable != NULL)
|
||||||
|
free(content->executable);
|
||||||
|
if (content->fd_in[0] > 2)
|
||||||
|
close(content->fd_in[0]);
|
||||||
|
if (content->fd_out[0] > 2)
|
||||||
|
close(content->fd_out[0]);
|
||||||
|
if (content->fd_in[1] > 2)
|
||||||
|
close(content->fd_in[1]);
|
||||||
|
if (content->fd_out[1] > 2)
|
||||||
|
close(content->fd_out[1]);
|
||||||
|
free(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_cmdcloser(void *ptr)
|
||||||
|
{
|
||||||
|
t_cmd *cmd;
|
||||||
|
|
||||||
|
cmd = ptr;
|
||||||
|
ft_closer(cmd->fd_in);
|
||||||
|
ft_closer(cmd->fd_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_cmdwaiter(void *ptr)
|
||||||
|
{
|
||||||
|
t_cmd *cmd;
|
||||||
|
int exit_status;
|
||||||
|
|
||||||
|
cmd = ptr;
|
||||||
|
if (cmd->executable != NULL && cmd->own_cmd == 0
|
||||||
|
&& cmd->pid != -1 && cmd->fd_in[0] != -2 && cmd->fd_out[0] != -2)
|
||||||
|
{
|
||||||
|
waitpid(cmd->pid, &exit_status, 0);
|
||||||
|
if (WIFSIGNALED(exit_status))
|
||||||
|
{
|
||||||
|
if (exit_status == 131)
|
||||||
|
{
|
||||||
|
ft_printf("Quit (core dumped)");
|
||||||
|
*ft_get_exit_code() = 131;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*ft_get_exit_code() = 130;
|
||||||
|
ft_printf("\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*ft_get_exit_code() = WEXITSTATUS(exit_status);
|
||||||
|
}
|
||||||
|
signal(SIGINT, ft_ctrlc);
|
||||||
|
signal(SIGQUIT, SIG_IGN);
|
||||||
|
}
|
32
cmd/cmd.h
Normal file
32
cmd/cmd.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cmd.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/28 15:47:01 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/07 15:03:53 by alouis-j ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef CMD_H
|
||||||
|
# define CMD_H
|
||||||
|
# include <stdbool.h>
|
||||||
|
# include "../data/data.h"
|
||||||
|
|
||||||
|
typedef struct s_cmd
|
||||||
|
{
|
||||||
|
int fd_in[2];
|
||||||
|
int fd_out[2];
|
||||||
|
int pid;
|
||||||
|
char *executable;
|
||||||
|
char **args;
|
||||||
|
bool own_cmd;
|
||||||
|
} t_cmd;
|
||||||
|
|
||||||
|
void ft_cmddel(void *content);
|
||||||
|
void ft_cmdwaiter(void *content);
|
||||||
|
void ft_cmdcloser(void *ptr);
|
||||||
|
|
||||||
|
#endif
|
21
cmd/cmd_private.h
Normal file
21
cmd/cmd_private.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cmd_private.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/28 15:50:23 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/31 16:33:24 by alouis-j ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef CMD_PRIVATE_H
|
||||||
|
# define CMD_PRIVATE_H
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <sys/wait.h>
|
||||||
|
# include "./cmd.h"
|
||||||
|
# include "../libftx/libftx.h"
|
||||||
|
# include "../data/data.h"
|
||||||
|
# include "../utils/utils.h"
|
||||||
|
#endif
|
21
data/data.c
Normal file
21
data/data.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* data.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/27 13:42:09 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/05 14:48:30 by alouis-j ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./data_private.h"
|
||||||
|
#include "data.h"
|
||||||
|
|
||||||
|
int *ft_get_exit_code(void)
|
||||||
|
{
|
||||||
|
static int exit_code;
|
||||||
|
|
||||||
|
return (&exit_code);
|
||||||
|
}
|
26
data/data.h
Normal file
26
data/data.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* data.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/27 13:43:39 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/05 14:45:23 by alouis-j ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef DATA_H
|
||||||
|
# define DATA_H
|
||||||
|
# include "../libftx/libft/list.h"
|
||||||
|
|
||||||
|
typedef struct s_data
|
||||||
|
{
|
||||||
|
t_list **env;
|
||||||
|
t_list **cmds;
|
||||||
|
int *exit_code;
|
||||||
|
} t_data;
|
||||||
|
|
||||||
|
int *ft_get_exit_code(void);
|
||||||
|
|
||||||
|
#endif
|
@ -1,21 +1,18 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* env.h :+: :+: :+: */
|
/* data_private.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/01/31 14:39:05 by erey-bet #+# #+# */
|
/* Created: 2023/03/27 13:42:21 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/01/31 14:56:16 by erey-bet ### ########.fr */
|
/* Updated: 2023/03/27 13:42:23 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef ENV_H
|
#ifndef DATA_PRIVATE_H
|
||||||
# define ENV_H
|
# define DATA_PRIVATE_H
|
||||||
|
# include "../libftx/libftx.h"
|
||||||
typedef struct s_env {
|
# include "./data.h"
|
||||||
void *ctn;
|
|
||||||
void *next;
|
|
||||||
} t_env;
|
|
||||||
|
|
||||||
#endif
|
#endif
|
139
env.c
139
env.c
@ -1,139 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* env.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
|
||||||
/* Updated: 2023/02/02 17:39:56 by erey-bet ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "env.h"
|
|
||||||
#include "libftx/libftx.h"
|
|
||||||
|
|
||||||
void print_export(t_list **head, int fd)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
char *ctn;
|
|
||||||
|
|
||||||
current = *head;
|
|
||||||
while (current != NULL)
|
|
||||||
{
|
|
||||||
ctn = current->content;
|
|
||||||
if (*(ft_strchr(ctn, '=') - 1) != '_')
|
|
||||||
{
|
|
||||||
write(fd, "declare -x ", 11);
|
|
||||||
ft_putstr_fd(ctn, fd);
|
|
||||||
write(fd, "\n", 1);
|
|
||||||
}
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_env(t_list **head, int fd)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
|
|
||||||
current = *head;
|
|
||||||
while (current != NULL)
|
|
||||||
{
|
|
||||||
ft_putstr_fd(current->content, 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(char **a, char **b)
|
|
||||||
{
|
|
||||||
void *c;
|
|
||||||
|
|
||||||
c = *a;
|
|
||||||
*a = *b;
|
|
||||||
*b = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
void exchange(char **a, char **b, char **c)
|
|
||||||
{
|
|
||||||
void *d;
|
|
||||||
|
|
||||||
d = *a;
|
|
||||||
*a = *b;
|
|
||||||
*b = *c;
|
|
||||||
*c = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_sort(t_list **head, char *str)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
char *last;
|
|
||||||
|
|
||||||
current = *head;
|
|
||||||
while (current->next != NULL && strcmp_alphabet(str, current->content) != 0)
|
|
||||||
current = current->next;
|
|
||||||
if (strcmp_alphabet(str, current->content) == 1)
|
|
||||||
last = str;
|
|
||||||
else
|
|
||||||
exchange(&last, (char **)(¤t->content), &str);
|
|
||||||
while (current != NULL)
|
|
||||||
{
|
|
||||||
if (current->next == NULL)
|
|
||||||
current->next = ft_calloc(1, sizeof(t_list));
|
|
||||||
if (current->next == NULL)
|
|
||||||
return ;
|
|
||||||
current = current->next;
|
|
||||||
if (current->content == NULL)
|
|
||||||
{
|
|
||||||
current->content = last;
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
ft_double_swap((char **)(¤t->content), &last);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t_list **init_env(char **env)
|
|
||||||
{
|
|
||||||
t_list **head;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
head = ft_calloc(1, sizeof(t_list *));
|
|
||||||
*head = ft_calloc(1, sizeof(t_list));
|
|
||||||
if (*head == NULL)
|
|
||||||
return (NULL);
|
|
||||||
i = -1;
|
|
||||||
while (env[++i])
|
|
||||||
{
|
|
||||||
if (ft_strnstr(env[i], "XMODIFIERS=@im=ibus", 200))
|
|
||||||
write(1, "", 0);
|
|
||||||
add_sort(head, env[i]);
|
|
||||||
}
|
|
||||||
return (head);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*int main(int argc, char *argv[], char **env)
|
|
||||||
{
|
|
||||||
(void)argc;
|
|
||||||
(void)argv;
|
|
||||||
print_export(init_env(env));
|
|
||||||
return (0);
|
|
||||||
}*/
|
|
37
env/env.h
vendored
Normal file
37
env/env.h
vendored
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/27 13:42:01 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/27 13:42:02 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef ENV_H
|
||||||
|
# define ENV_H
|
||||||
|
# include <stdbool.h>
|
||||||
|
# include "../libftx/libft/list.h"
|
||||||
|
# include "../data/data.h"
|
||||||
|
|
||||||
|
typedef struct s_env
|
||||||
|
{
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
} t_env;
|
||||||
|
|
||||||
|
char *ft_env_filler(t_data *data, const char *str);
|
||||||
|
void env_del(void *content);
|
||||||
|
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);
|
||||||
|
void env_del(void *ptr);
|
||||||
|
int delete_by_key(char *key, t_list **head);
|
||||||
|
int possible_key(char *key);
|
||||||
|
|
||||||
|
#endif
|
120
env/env1.c
vendored
Normal file
120
env/env1.c
vendored
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env1.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/18 13:57:38 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "env_private.h"
|
||||||
|
|
||||||
|
void add_sort(t_list **head, t_env *var)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
t_env *last;
|
||||||
|
|
||||||
|
current = *head;
|
||||||
|
while (current->next != NULL
|
||||||
|
&& ft_strcmp(var->key, ((t_env *)(current->content))->key) > 0)
|
||||||
|
current = current->next;
|
||||||
|
if (current->content == NULL)
|
||||||
|
current->content = var;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
last = NULL;
|
||||||
|
swap_env_3((void **)&last, ¤t->content, (void **)&var);
|
||||||
|
while (current->next != NULL)
|
||||||
|
{
|
||||||
|
current = current->next;
|
||||||
|
swap_env(¤t->content, (void **)&last);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (current->next == NULL)
|
||||||
|
{
|
||||||
|
current->next = ft_calloc(1, sizeof(t_list));
|
||||||
|
if (!current->next)
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_value_by_key(char *key, t_list **head)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
|
||||||
|
current = *head;
|
||||||
|
while (current->next != NULL)
|
||||||
|
{
|
||||||
|
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||||
|
return (((t_env *)current->content)->value);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int set_value_by_key(char *key, char *value, t_list **head)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
|
||||||
|
current = *head;
|
||||||
|
while (current->next != NULL)
|
||||||
|
{
|
||||||
|
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||||
|
{
|
||||||
|
free(((t_env *)current->content)->value);
|
||||||
|
((t_env *)current->content)->value = value;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int create_value_by_key(char *key, char *value, t_list **head)
|
||||||
|
{
|
||||||
|
t_env *content;
|
||||||
|
|
||||||
|
if (set_value_by_key(key, value, head) == 0)
|
||||||
|
{
|
||||||
|
free(key);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
content = ft_calloc(1, sizeof(t_env));
|
||||||
|
if (content == NULL)
|
||||||
|
return (1);
|
||||||
|
content->key = key;
|
||||||
|
content->value = value;
|
||||||
|
add_sort(head, content);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_list **init_env(char **env)
|
||||||
|
{
|
||||||
|
t_list **head;
|
||||||
|
int i;
|
||||||
|
t_env *var;
|
||||||
|
|
||||||
|
head = ft_calloc(1, sizeof(t_list *));
|
||||||
|
if (head == NULL)
|
||||||
|
return (NULL);
|
||||||
|
*head = ft_calloc(1, sizeof(t_list));
|
||||||
|
if (*head == NULL)
|
||||||
|
{
|
||||||
|
free(head);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
i = -1;
|
||||||
|
while (env[++i])
|
||||||
|
{
|
||||||
|
var = ft_calloc(1, sizeof(t_env));
|
||||||
|
if (var == NULL)
|
||||||
|
return (NULL);
|
||||||
|
var->key = get_key(env[i]);
|
||||||
|
var->value = get_value(env[i]);
|
||||||
|
add_sort(head, var);
|
||||||
|
}
|
||||||
|
return (head);
|
||||||
|
}
|
77
env/env2.c
vendored
Normal file
77
env/env2.c
vendored
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/09 19:59:03 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/18 13:26:31 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "env_private.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);
|
||||||
|
}
|
109
env/env3.c
vendored
Normal file
109
env/env3.c
vendored
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env3.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2023/04/07 13:21:54 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "env_private.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;
|
||||||
|
|
||||||
|
if (set_value_by_key(key, value, env) == 0)
|
||||||
|
return (0);
|
||||||
|
key_dup = ft_strdup(key);
|
||||||
|
if (key_dup == NULL)
|
||||||
|
return (1);
|
||||||
|
if (value != NULL)
|
||||||
|
{
|
||||||
|
value_dup = ft_strdup(value);
|
||||||
|
if (value_dup == NULL)
|
||||||
|
{
|
||||||
|
free(key);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
value_dup = value;
|
||||||
|
if (create_value_by_key(key_dup, value_dup, env))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int delete_by_key(char *key, t_list **head)
|
||||||
|
{
|
||||||
|
t_list *last;
|
||||||
|
t_list *current;
|
||||||
|
|
||||||
|
current = *head;
|
||||||
|
last = NULL;
|
||||||
|
while (current->next != NULL)
|
||||||
|
{
|
||||||
|
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||||
|
{
|
||||||
|
free(((t_env *)current->content)->key);
|
||||||
|
free(((t_env *)current->content)->value);
|
||||||
|
free(current->content);
|
||||||
|
if (last && last->next)
|
||||||
|
last->next = current->next;
|
||||||
|
else
|
||||||
|
*head = current->next;
|
||||||
|
free(current);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
last = current;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int possible_key(char *key)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
if (ft_isdigit(key[i + 1]))
|
||||||
|
return (0);
|
||||||
|
while (key[++i + 1])
|
||||||
|
if (!ft_isalnum(key[i]) && key[i] != '_')
|
||||||
|
return (0);
|
||||||
|
if (key[i] == '+')
|
||||||
|
return (2);
|
||||||
|
else if (!ft_isalnum(key[i]) && key[i] != '_')
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
117
env/env_fill.c
vendored
Normal file
117
env/env_fill.c
vendored
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env_fill.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/05 15:09:46 by alouis-j ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./env_private.h"
|
||||||
|
|
||||||
|
static char *ft_getkey(const char *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char *key;
|
||||||
|
|
||||||
|
if (ft_strncmp(str, "$$", 2) == 0)
|
||||||
|
key = ft_strdup("$");
|
||||||
|
else if (ft_strncmp(str, "$?", 2) == 0)
|
||||||
|
key = ft_strdup("?");
|
||||||
|
else if (str[1] == '\0')
|
||||||
|
key = ft_strdup("");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i = 1;
|
||||||
|
while (str[i] != '\0' && !ft_is_in("$?\'\" ", str[i]))
|
||||||
|
i++;
|
||||||
|
key = ft_strndup(str + 1, i - 1);
|
||||||
|
}
|
||||||
|
if (key == NULL)
|
||||||
|
ft_eprintf("bozoshell: malloc failed\n");
|
||||||
|
return (key);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ft_getvalue(t_data *data, char *key)
|
||||||
|
{
|
||||||
|
char *value;
|
||||||
|
|
||||||
|
if (ft_strcmp(key, "?") == 0)
|
||||||
|
value = ft_itoa(*data->exit_code);
|
||||||
|
else if (ft_strcmp(key, "$") == 0)
|
||||||
|
value = ft_strdup("PID");
|
||||||
|
else if (key[0] == '\0')
|
||||||
|
value = ft_strdup("$");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = get_value_by_key(key, data->env);
|
||||||
|
if (value == NULL)
|
||||||
|
value = ft_strdup("");
|
||||||
|
else
|
||||||
|
value = ft_strdup(value);
|
||||||
|
}
|
||||||
|
if (value == NULL)
|
||||||
|
ft_eprintf("bozoshell: malloc failed\n");
|
||||||
|
return (value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ft_getvalue_by_str(t_data *data, const char *str,
|
||||||
|
size_t *key_len)
|
||||||
|
{
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
|
||||||
|
key = ft_getkey(str);
|
||||||
|
if (key == NULL)
|
||||||
|
return (NULL);
|
||||||
|
*key_len = ft_strlen(key) + 1;
|
||||||
|
value = ft_getvalue(data, key);
|
||||||
|
free(key);
|
||||||
|
return (value);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_str_formator(t_data *data, char *str, size_t *i)
|
||||||
|
{
|
||||||
|
char *value;
|
||||||
|
size_t key_len;
|
||||||
|
char *out;
|
||||||
|
|
||||||
|
value = ft_getvalue_by_str(data, str + *i, &key_len);
|
||||||
|
if (value == NULL)
|
||||||
|
return (NULL);
|
||||||
|
out = ft_strreplace(str, value, *i, key_len + *i);
|
||||||
|
*i = *i + ft_strlen(value);
|
||||||
|
free(value);
|
||||||
|
return (out);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_env_filler(t_data *data, const char *str)
|
||||||
|
{
|
||||||
|
char *out;
|
||||||
|
char *temp;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
out = ft_strdup(str);
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (out[i] != '\0')
|
||||||
|
{
|
||||||
|
while (ft_is_in_quote(out, i) == 1)
|
||||||
|
i++;
|
||||||
|
while (out[i] == '$')
|
||||||
|
{
|
||||||
|
temp = ft_str_formator(data, out, &i);
|
||||||
|
if (temp == NULL)
|
||||||
|
return (NULL);
|
||||||
|
free(out);
|
||||||
|
out = temp;
|
||||||
|
}
|
||||||
|
if (out[i] != '\0')
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (out);
|
||||||
|
}
|
26
env/env_private.h
vendored
Normal file
26
env/env_private.h
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env_private.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/27 13:40:24 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/27 13:40:25 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef ENV_PRIVATE_H
|
||||||
|
# define ENV_PRIVATE_H
|
||||||
|
# include "./env.h"
|
||||||
|
# include "../libftx/libftx.h"
|
||||||
|
# include "../utils/utils.h"
|
||||||
|
|
||||||
|
void swap_env_3(void **a, void **b, void **c);
|
||||||
|
void swap_env(void **a, void **b);
|
||||||
|
char *get_value(char *str);
|
||||||
|
char *get_key(char *str);
|
||||||
|
int get_index(char *str, char c);
|
||||||
|
char *ft_env_filler(t_data *data, const char *str);
|
||||||
|
|
||||||
|
#endif
|
119
execution/execution.c
Normal file
119
execution/execution.c
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* execution.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/18 13:00:08 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "execution_private.h"
|
||||||
|
|
||||||
|
static int ft_execute_own_cmd(t_data *data, t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int return_code;
|
||||||
|
|
||||||
|
if (ft_strcmp(cmd->executable, "pwd") == 0)
|
||||||
|
return_code = pwd(cmd->fd_out[0]);
|
||||||
|
else if (ft_strcmp(cmd->executable, "env") == 0)
|
||||||
|
return_code = print_env(data->env, cmd->fd_out[0]);
|
||||||
|
else if (ft_strcmp(cmd->executable, "export") == 0)
|
||||||
|
return_code = ft_export(data->env, cmd->args + 1, cmd->fd_out[0]);
|
||||||
|
else if (ft_strcmp(cmd->executable, "cd") == 0)
|
||||||
|
return_code = (move_folder(cmd->args + 1, data->env, cmd->fd_out[0]));
|
||||||
|
else if (ft_strcmp(cmd->executable, "unset") == 0)
|
||||||
|
return_code = (unset(data->env, cmd->args, cmd->fd_out[0]));
|
||||||
|
else if (ft_strcmp(cmd->executable, "echo") == 0)
|
||||||
|
return_code = (echo(cmd->fd_out[0], cmd->args + 1));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return_code = ft_exit(cmd->args + 1, *data->exit_code);
|
||||||
|
if (return_code >= 0)
|
||||||
|
{
|
||||||
|
*data->exit_code = return_code;
|
||||||
|
return (-2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*data->exit_code = return_code;
|
||||||
|
return (return_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ft_executor(t_data *data, t_cmd *cmd, char **env)
|
||||||
|
{
|
||||||
|
if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL)
|
||||||
|
return (0);
|
||||||
|
cmd->pid = fork();
|
||||||
|
if (cmd->pid == -1)
|
||||||
|
return (1);
|
||||||
|
if (cmd->pid == 0)
|
||||||
|
{
|
||||||
|
signal(SIGQUIT, SIG_DFL);
|
||||||
|
signal(SIGINT, SIG_DFL);
|
||||||
|
dup2(cmd->fd_in[0], 0);
|
||||||
|
dup2(cmd->fd_out[0], 1);
|
||||||
|
ft_lstiter(*data->cmds, ft_cmdcloser);
|
||||||
|
execve(cmd->executable, cmd->args, env);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
signal(SIGQUIT, SIG_IGN);
|
||||||
|
signal(SIGINT, SIG_IGN);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ft_cmd_executor(t_data *data, t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int exit_code;
|
||||||
|
char **env;
|
||||||
|
|
||||||
|
if (cmd->own_cmd == 1)
|
||||||
|
{
|
||||||
|
exit_code = ft_execute_own_cmd(data, cmd);
|
||||||
|
ft_closer(cmd->fd_in);
|
||||||
|
ft_closer(cmd->fd_out);
|
||||||
|
if (exit_code == -2)
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
env = env_to_strs(data->env);
|
||||||
|
if (env == NULL)
|
||||||
|
return (1);
|
||||||
|
exit_code = ft_executor(data, cmd, env);
|
||||||
|
ft_closer(cmd->fd_in);
|
||||||
|
ft_closer(cmd->fd_out);
|
||||||
|
ft_freer_tab_ultimate(1, env);
|
||||||
|
if (exit_code == 1)
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_cmds_executor(t_data *data)
|
||||||
|
{
|
||||||
|
int fds[2];
|
||||||
|
t_list *current;
|
||||||
|
t_cmd *content;
|
||||||
|
|
||||||
|
current = *data->cmds;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
content = current->content;
|
||||||
|
fds[0] = -1;
|
||||||
|
if (current->next != NULL)
|
||||||
|
{
|
||||||
|
if (pipe(fds) == -1)
|
||||||
|
return (1);
|
||||||
|
ft_add_fd(content->fd_out, fds[1]);
|
||||||
|
ft_add_fd(((t_cmd *)(current->next->content))->fd_in, fds[0]);
|
||||||
|
}
|
||||||
|
if (content->fd_in[0] == -2 || content->fd_out[0] == -2)
|
||||||
|
ft_mega_closer(content->fd_in, content->fd_out);
|
||||||
|
else if (ft_cmd_executor(data, content))
|
||||||
|
return (1);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
19
execution/execution.h
Normal file
19
execution/execution.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* execution.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/28 15:46:52 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/28 15:46:53 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef EXECUTION_H
|
||||||
|
# define EXECUTION_H
|
||||||
|
# include "../data/data.h"
|
||||||
|
|
||||||
|
int ft_cmds_executor(t_data *data);
|
||||||
|
|
||||||
|
#endif
|
24
execution/execution_private.h
Normal file
24
execution/execution_private.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* execution_private.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/28 15:45:53 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/04/05 15:15:56 by alouis-j ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef EXECUTION_PRIVATE_H
|
||||||
|
# define EXECUTION_PRIVATE_H
|
||||||
|
# include <signal.h>
|
||||||
|
# include "../signal/signal.h"
|
||||||
|
# include "../data/data.h"
|
||||||
|
# include "../libftx/libftx.h"
|
||||||
|
# include "../cmd/cmd.h"
|
||||||
|
# include "../env/env.h"
|
||||||
|
# include "../builtins/builtins.h"
|
||||||
|
# include "../utils/utils.h"
|
||||||
|
|
||||||
|
#endif
|
49
file1
49
file1
@ -1,49 +0,0 @@
|
|||||||
COLORTERM=truecolor
|
|
||||||
DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/101231/bus,guid=2b19cb48c68a1a9a5df2465563da4d2b'
|
|
||||||
DBUS_STARTER_ADDRESS='unix:path=/run/user/101231/bus,guid=2b19cb48c68a1a9a5df2465563da4d2b'
|
|
||||||
DBUS_STARTER_BUS_TYPE=session
|
|
||||||
DESKTOP_SESSION=ubuntu
|
|
||||||
DISPLAY=:0
|
|
||||||
DOCKER_HOST=unix:///run/user/101231/docker.sock
|
|
||||||
GDMSESSION=ubuntu
|
|
||||||
GDM_LANG=en
|
|
||||||
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
|
|
||||||
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/dd4e2769_33dd_4e18_81b2_70c233dc78ce
|
|
||||||
GNOME_TERMINAL_SERVICE=:1.96
|
|
||||||
GPG_AGENT_INFO=/run/user/101231/gnupg/S.gpg-agent:0:1
|
|
||||||
GTK_MODULES=gail:atk-bridge
|
|
||||||
HOME=/nfs/homes/erey-bet
|
|
||||||
IM_CONFIG_PHASE=1
|
|
||||||
INVOCATION_ID=4f6816c8fd9446809f7d454fd8bdc56a
|
|
||||||
JOURNAL_STREAM=9:166232
|
|
||||||
KRB5CCNAME=FILE:/tmp/krb5cc_101231_TfpJ05
|
|
||||||
LANG=en_US.UTF-8
|
|
||||||
LANGUAGE=en
|
|
||||||
LOGNAME=erey-bet
|
|
||||||
MANAGERPID=38750
|
|
||||||
OLDPWD=/nfs/homes/erey-bet/travaux/Cursus42/minishell
|
|
||||||
PATH=/nfs/homes/erey-bet/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
|
|
||||||
PWD=/nfs/homes/erey-bet/travaux/Cursus42/minishell
|
|
||||||
QT_ACCESSIBILITY=1
|
|
||||||
QT_IM_MODULE=ibus
|
|
||||||
SESSION_MANAGER=local/1C2.42angouleme.fr:@/tmp/.ICE-unix/39041,unix/1C2.42angouleme.fr:/tmp/.ICE-unix/39041
|
|
||||||
SHELL=/bin/zsh
|
|
||||||
SHLVL=1
|
|
||||||
SSH_AGENT_PID=39003
|
|
||||||
SSH_AUTH_SOCK=/run/user/101231/keyring/ssh
|
|
||||||
TERM=xterm-256color
|
|
||||||
USER=erey-bet
|
|
||||||
VTE_VERSION=6003
|
|
||||||
XAUTHORITY=/nfs/homes/erey-bet/.Xauthority
|
|
||||||
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
|
|
||||||
XDG_CURRENT_DESKTOP=ubuntu:GNOME
|
|
||||||
XDG_DATA_DIRS=/usr/share/gnome:/usr/share/ubuntu:/nfs/homes/erey-bet/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share:/var/lib/snapd/desktop
|
|
||||||
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/erey-bet
|
|
||||||
XDG_MENU_PREFIX=gnome-
|
|
||||||
XDG_RUNTIME_DIR=/run/user/101231
|
|
||||||
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
|
|
||||||
XDG_SESSION_CLASS=user
|
|
||||||
XDG_SESSION_DESKTOP=ubuntu
|
|
||||||
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
|
|
||||||
XDG_SESSION_TYPE=x11
|
|
||||||
XMODIFIERS='@im=ibus'
|
|
50
file2
50
file2
@ -1,50 +0,0 @@
|
|||||||
COLORTERM=truecolor
|
|
||||||
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/101231/bus,guid=2b19cb48c68a1a9a5df2465563da4d2b
|
|
||||||
DBUS_STARTER_ADDRESS=unix:path=/run/user/101231/bus,guid=2b19cb48c68a1a9a5df2465563da4d2b
|
|
||||||
DBUS_STARTER_BUS_TYPE=session
|
|
||||||
DESKTOP_SESSION=ubuntu
|
|
||||||
DISPLAY=:0
|
|
||||||
DOCKER_HOST=unix:///run/user/101231/docker.sock
|
|
||||||
GDMSESSION=ubuntu
|
|
||||||
GDM_LANG=en
|
|
||||||
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
|
|
||||||
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/dd4e2769_33dd_4e18_81b2_70c233dc78ce
|
|
||||||
GNOME_TERMINAL_SERVICE=:1.96
|
|
||||||
GPG_AGENT_INFO=/run/user/101231/gnupg/S.gpg-agent:0:1
|
|
||||||
GTK_MODULES=gail:atk-bridge
|
|
||||||
HOME=/nfs/homes/erey-bet
|
|
||||||
IM_CONFIG_PHASE=1
|
|
||||||
INVOCATION_ID=4f6816c8fd9446809f7d454fd8bdc56a
|
|
||||||
JOURNAL_STREAM=9:166232
|
|
||||||
KRB5CCNAME=FILE:/tmp/krb5cc_101231_TfpJ05
|
|
||||||
LANG=en_US.UTF-8
|
|
||||||
LANGUAGE=en
|
|
||||||
LOGNAME=erey-bet
|
|
||||||
MANAGERPID=38750
|
|
||||||
OLDPWD=/nfs/homes/erey-bet/travaux/Cursus42/minishell
|
|
||||||
PATH=/nfs/homes/erey-bet/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
|
|
||||||
PWD=/nfs/homes/erey-bet/travaux/Cursus42/minishell
|
|
||||||
QT_ACCESSIBILITY=1
|
|
||||||
QT_IM_MODULE=ibus
|
|
||||||
SESSION_MANAGER=local/1C2.42angouleme.fr:@/tmp/.ICE-unix/39041,unix/1C2.42angouleme.fr:/tmp/.ICE-unix/39041
|
|
||||||
SHELL=/bin/zsh
|
|
||||||
SHLVL=1
|
|
||||||
SSH_AGENT_PID=39003
|
|
||||||
TERM=xterm-256color
|
|
||||||
USER=erey-bet
|
|
||||||
VTE_VERSION=6003
|
|
||||||
XAUTHORITY=/nfs/homes/erey-bet/.Xauthority
|
|
||||||
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
|
|
||||||
XDG_CURRENT_DESKTOP=ubuntu:GNOME
|
|
||||||
XDG_DATA_DIRS=/usr/share/gnome:/usr/share/ubuntu:/nfs/homes/erey-bet/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share:/var/lib/snapd/desktop
|
|
||||||
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/erey-bet
|
|
||||||
XDG_MENU_PREFIX=gnome-
|
|
||||||
XDG_RUNTIME_DIR=/run/user/101231
|
|
||||||
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
|
|
||||||
XDG_SESSION_CLASS=user
|
|
||||||
XDG_SESSION_DESKTOP=ubuntu
|
|
||||||
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
|
|
||||||
XDG_SESSION_TYPE=x11
|
|
||||||
XMODIFIERS=@im=ibus
|
|
||||||
_=/nfs/homes/erey-bet/travaux/Cursus42/minishell/./minishell
|
|
||||||
SSH_AUTH_SOCK=/run/user/101231/keyring/ssh
|
|
130
format/format.c
Normal file
130
format/format.c
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* format.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/02/15 13:35:50 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/31 15:22:21 by alouis-j ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./format_private.h"
|
||||||
|
|
||||||
|
static int ft_replace(char **str, size_t i)
|
||||||
|
{
|
||||||
|
char *temp;
|
||||||
|
|
||||||
|
temp = ft_strreplace(*str, " ", i, i);
|
||||||
|
free(*str);
|
||||||
|
if (temp == NULL)
|
||||||
|
{
|
||||||
|
ft_eprintf("bozoshell: malloc failed\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
*str = temp;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ft_spacer_after(const char *str)
|
||||||
|
{
|
||||||
|
char *out;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
out = ft_strdup(str);
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = 1;
|
||||||
|
while (out[i] != '\0')
|
||||||
|
{
|
||||||
|
while (ft_is_in_quote(out, i - 1))
|
||||||
|
i++;
|
||||||
|
if (out[i - 1] == '\0' || out[i] == '\0')
|
||||||
|
break ;
|
||||||
|
if (ft_is_in("><|", out[i - 1]))
|
||||||
|
{
|
||||||
|
while (out[i] == out[i - 1])
|
||||||
|
i++;
|
||||||
|
if (ft_replace(&out, i))
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
if (out[i] != '\0')
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ft_spacer_before(const char *str)
|
||||||
|
{
|
||||||
|
char *out;
|
||||||
|
ssize_t i;
|
||||||
|
|
||||||
|
out = ft_strdup(str);
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
i = -1;
|
||||||
|
while (out[++i] != '\0')
|
||||||
|
{
|
||||||
|
while (ft_is_in_quote(out, i + 1))
|
||||||
|
i++;
|
||||||
|
if (out[i] == '\0')
|
||||||
|
break ;
|
||||||
|
if (ft_is_in("><|", out[i + 1]))
|
||||||
|
{
|
||||||
|
while (out[i] == ' ')
|
||||||
|
i++;
|
||||||
|
while (out[i] == out[i + 1])
|
||||||
|
i++;
|
||||||
|
if (ft_replace(&out, i + 1))
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ft_space_simplifier(char *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
size_t y;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i] != '\0')
|
||||||
|
{
|
||||||
|
if (ft_is_in_quote(str, i))
|
||||||
|
i++;
|
||||||
|
if (str[i] != '\0')
|
||||||
|
break ;
|
||||||
|
y = 0;
|
||||||
|
while (str[y + i] == ' ')
|
||||||
|
y++;
|
||||||
|
if (y > 1)
|
||||||
|
{
|
||||||
|
ft_strshift(str + i, -y + 1);
|
||||||
|
y--;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_formater(t_data *data, const char *str)
|
||||||
|
{
|
||||||
|
char *out;
|
||||||
|
char *temp;
|
||||||
|
|
||||||
|
if (ft_str_is_empty(str))
|
||||||
|
return (ft_strdup(" "));
|
||||||
|
temp = ft_spacer_after(str);
|
||||||
|
if (temp == NULL)
|
||||||
|
return (NULL);
|
||||||
|
out = ft_spacer_before(temp);
|
||||||
|
free(temp);
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
ft_space_simplifier(out);
|
||||||
|
if (out[ft_strlen(out) - 1] == ' ')
|
||||||
|
out[ft_strlen(out) - 1] = '\0';
|
||||||
|
temp = ft_env_filler(data, out);
|
||||||
|
free(out);
|
||||||
|
return (temp);
|
||||||
|
}
|
19
format/format.h
Normal file
19
format/format.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* format.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/28 15:44:56 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/28 15:44:57 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FORMAT_H
|
||||||
|
# define FORMAT_H
|
||||||
|
# include "../data/data.h"
|
||||||
|
|
||||||
|
char *ft_formater(t_data *data, const char *str);
|
||||||
|
|
||||||
|
#endif
|
18
format/format_private.h
Normal file
18
format/format_private.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* format_private.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/28 15:44:59 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/28 15:45:00 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FORMAT_PRIVATE_H
|
||||||
|
# define FORMAT_PRIVATE_H
|
||||||
|
# include "../libftx/libftx.h"
|
||||||
|
# include "../utils/utils.h"
|
||||||
|
|
||||||
|
#endif
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
CC = clang
|
CC = clang
|
||||||
|
|
||||||
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c
|
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c ft_swap.c
|
||||||
|
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
|
Binary file not shown.
@ -28,9 +28,13 @@ char *ft_strfjoin(char *s1, char *s2);
|
|||||||
char *ft_strmerger(size_t arg_len, ...);
|
char *ft_strmerger(size_t arg_len, ...);
|
||||||
int ft_is_in(char *str, char c);
|
int ft_is_in(char *str, char c);
|
||||||
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
|
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
|
||||||
char *ft_strndup(char *src, size_t n);
|
char *ft_strndup(const char *src, size_t n);
|
||||||
ssize_t ft_strchri(char *str, char c);
|
ssize_t ft_strchri(char *str, char c);
|
||||||
int ft_contain_only_str(char *str, char *to_find);
|
int ft_contain_only_str(char *str, char *to_find);
|
||||||
int ft_contain_only(char *str, char c);
|
int ft_contain_only(char *str, char c);
|
||||||
int ft_strcmp(char *s1, char *s2);
|
int ft_strcmp(char *s1, char *s2);
|
||||||
|
void ft_swap(void *a, void *b);
|
||||||
|
void ft_swap_int(int *a, int *b);
|
||||||
|
void ft_swap_char(char *a, char *b);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "extra.h"
|
#include "extra.h"
|
||||||
|
|
||||||
char *ft_strndup(char *src, size_t n)
|
char *ft_strndup(const char *src, size_t n)
|
||||||
{
|
{
|
||||||
char *out;
|
char *out;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
Binary file not shown.
40
libftx/extra/ft_swap.c
Normal file
40
libftx/extra/ft_swap.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_swap.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/27 13:43:25 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/27 13:43:26 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "extra.h"
|
||||||
|
|
||||||
|
void ft_swap(void *a, void *b)
|
||||||
|
{
|
||||||
|
void *c;
|
||||||
|
|
||||||
|
c = a;
|
||||||
|
a = b;
|
||||||
|
b = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_swap_char(char *a, char *b)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
|
||||||
|
c = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_swap_int(int *a, int *b)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
|
||||||
|
c = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = c;
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -36,4 +36,4 @@ fclean: clean
|
|||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
||||||
.PHONY: all bonus clean fclean re
|
.PHONY: all clean fclean re
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -45,9 +45,8 @@ SRCS = ft_isalpha.c \
|
|||||||
ft_putchar_fd.c \
|
ft_putchar_fd.c \
|
||||||
ft_putstr_fd.c \
|
ft_putstr_fd.c \
|
||||||
ft_putendl_fd.c \
|
ft_putendl_fd.c \
|
||||||
ft_putnbr_fd.c
|
ft_putnbr_fd.c \
|
||||||
|
ft_lstnew.c \
|
||||||
BSRCS = ft_lstnew.c \
|
|
||||||
ft_lstadd_front.c \
|
ft_lstadd_front.c \
|
||||||
ft_lstsize.c \
|
ft_lstsize.c \
|
||||||
ft_lstlast.c \
|
ft_lstlast.c \
|
||||||
@ -59,8 +58,6 @@ BSRCS = ft_lstnew.c \
|
|||||||
|
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
BOBJS = $(BSRCS:.c=.o)
|
|
||||||
|
|
||||||
NAME = libft.a
|
NAME = libft.a
|
||||||
|
|
||||||
CFLAGS = -Wall -Werror -Wextra -g
|
CFLAGS = -Wall -Werror -Wextra -g
|
||||||
@ -73,15 +70,12 @@ all: $(NAME)
|
|||||||
$(NAME): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
ar -rc $(NAME) $(OBJS)
|
ar -rc $(NAME) $(OBJS)
|
||||||
|
|
||||||
bonus: $(OBJS) $(BOBJS)
|
|
||||||
ar -rc $(NAME) $(OBJS) $(BOBJS)
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS) $(BOBJS)
|
rm -f $(OBJS)
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
rm -f $(NAME)
|
rm -f $(NAME)
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
||||||
.PHONY: all bonus clean fclean re
|
.PHONY: all clean fclean re
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -6,13 +6,13 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
||||||
/* Updated: 2023/01/05 18:54:54 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:23:01 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void ft_putchar_fd(int fd, char c)
|
void ft_putchar_fd(char c, int fd)
|
||||||
{
|
{
|
||||||
write(fd, &c, 1);
|
write(fd, &c, 1);
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
|
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/09/29 22:43:19 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:15:07 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void *ft_cancel(char **tab, size_t len)
|
void *ft_cancel(void **tab, size_t len)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
|
|||||||
let_index++;
|
let_index++;
|
||||||
tab[tab_index] = ft_substr(s, start, let_index - start);
|
tab[tab_index] = ft_substr(s, start, let_index - start);
|
||||||
if (tab[tab_index] == NULL)
|
if (tab[tab_index] == NULL)
|
||||||
return (ft_cancel(tab, tab_index));
|
return (ft_cancel((void **)tab, tab_index));
|
||||||
tab_index++;
|
tab_index++;
|
||||||
}
|
}
|
||||||
return (tab);
|
return (tab);
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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/01/06 19:34:13 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/17 16:22:44 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,8 +14,9 @@
|
|||||||
# define LIBFT_H
|
# define LIBFT_H
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
|
# include "./list.h"
|
||||||
|
|
||||||
void *ft_cancel(char **tab, size_t len);
|
void *ft_cancel(void **tab, size_t len);
|
||||||
int ft_atoi(const char *nptr);
|
int ft_atoi(const char *nptr);
|
||||||
void ft_bzero(void *s, size_t n);
|
void ft_bzero(void *s, size_t n);
|
||||||
void *ft_calloc(size_t nmemb, size_t size);
|
void *ft_calloc(size_t nmemb, size_t size);
|
||||||
@ -47,25 +48,9 @@ char **ft_split(char const *s, char c);
|
|||||||
char *ft_itoa(int n);
|
char *ft_itoa(int n);
|
||||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
||||||
void ft_putchar_fd(int fd, char c);
|
void ft_putchar_fd(char c, int fd);
|
||||||
void ft_putstr_fd(char *s, int fd);
|
void ft_putstr_fd(char *s, int fd);
|
||||||
void ft_putendl_fd(char *s, int fd);
|
void ft_putendl_fd(char *s, int fd);
|
||||||
void ft_putnbr_fd(int n, int fd);
|
void ft_putnbr_fd(int n, int fd);
|
||||||
|
|
||||||
typedef struct s_list
|
|
||||||
{
|
|
||||||
void *content;
|
|
||||||
struct s_list *next;
|
|
||||||
} t_list;
|
|
||||||
|
|
||||||
t_list *ft_lstnew(void *content);
|
|
||||||
void ft_lstadd_front(t_list **lst, t_list *nouveau);
|
|
||||||
int ft_lstsize(t_list *lst);
|
|
||||||
t_list *ft_lstlast(t_list *lst);
|
|
||||||
void ft_lstadd_back(t_list **lst, t_list *nouveau);
|
|
||||||
void ft_lstdelone(t_list *lst, void (*del)(void *));
|
|
||||||
void ft_lstclear(t_list **lst, void (*del)(void *));
|
|
||||||
void ft_lstiter(t_list *lst, void (*f)(void *));
|
|
||||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
32
libftx/libft/list.h
Normal file
32
libftx/libft/list.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* list.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/03/27 13:43:53 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2023/03/27 13:43:54 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef LIST_H
|
||||||
|
# define LIST_H
|
||||||
|
|
||||||
|
typedef struct s_list
|
||||||
|
{
|
||||||
|
void *content;
|
||||||
|
struct s_list *next;
|
||||||
|
} t_list;
|
||||||
|
|
||||||
|
t_list *ft_lstnew(void *content);
|
||||||
|
void ft_lstadd_front(t_list **lst, t_list *nouveau);
|
||||||
|
int ft_lstsize(t_list *lst);
|
||||||
|
t_list *ft_lstlast(t_list *lst);
|
||||||
|
void ft_lstadd_back(t_list **lst, t_list *nouveau);
|
||||||
|
void ft_lstdelone(t_list *lst, void (*del)(void *));
|
||||||
|
void ft_lstclear(t_list **lst, void (*del)(void *));
|
||||||
|
void ft_lstiter(t_list *lst, void (*f)(void *));
|
||||||
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
||||||
|
|
||||||
|
#endif
|
BIN
libftx/libftx.a
BIN
libftx/libftx.a
Binary file not shown.
@ -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/01 16:20:05 by cchauvet ### ########.fr */
|
/* Updated: 2023/02/21 13:28:19 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -15,10 +15,13 @@
|
|||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <stdarg.h>
|
# include <stdarg.h>
|
||||||
|
# include "./libft/libft.h"
|
||||||
|
|
||||||
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, ...);
|
||||||
@ -32,59 +35,46 @@ char *ft_strfjoin(char *s1, char *s2);
|
|||||||
char *ft_strmerger(size_t arg_len, ...);
|
char *ft_strmerger(size_t arg_len, ...);
|
||||||
int ft_strcmp(char *s1, char *s2);
|
int ft_strcmp(char *s1, char *s2);
|
||||||
ssize_t ft_strchri(char *str, char c);
|
ssize_t ft_strchri(char *str, char c);
|
||||||
char *ft_strndup(char *src, size_t n);
|
char *ft_strndup(const char *src, size_t n);
|
||||||
|
void ft_swap(void *a, void *b);
|
||||||
|
void ft_swap_int(int *a, int *b);
|
||||||
|
void ft_swap_char(char *a, char *b);
|
||||||
|
|
||||||
void *ft_cancel(char **tab, size_t len);
|
/* void *ft_cancel(void **tab, size_t len); */
|
||||||
int ft_atoi(const char *nptr);
|
/* int ft_atoi(const char *nptr); */
|
||||||
void ft_bzero(void *s, size_t n);
|
/* void ft_bzero(void *s, size_t n); */
|
||||||
void *ft_calloc(size_t nmemb, size_t size);
|
/* void *ft_calloc(size_t nmemb, size_t size); */
|
||||||
int ft_isalnum(int c);
|
/* int ft_isalnum(int c); */
|
||||||
int ft_isalpha(int c);
|
/* int ft_isalpha(int c); */
|
||||||
int ft_isascii(int c);
|
/* int ft_isascii(int c); */
|
||||||
int ft_isdigit(int c);
|
/* int ft_isdigit(int c); */
|
||||||
int ft_isprint(int c);
|
/* int ft_isprint(int c); */
|
||||||
void *ft_memchr(const void *s, int c, size_t n);
|
/* void *ft_memchr(const void *s, int c, size_t n); */
|
||||||
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
/* int ft_memcmp(const void *s1, const void *s2, size_t n); */
|
||||||
void *ft_memcpy(void *dest, const void *src, size_t n);
|
/* void *ft_memcpy(void *dest, const void *src, size_t n); */
|
||||||
void *ft_memmove(void *dest, const void *src, size_t n);
|
/* void *ft_memmove(void *dest, const void *src, size_t n); */
|
||||||
void *ft_memset(void *s, int c, size_t n);
|
/* void *ft_memset(void *s, int c, size_t n); */
|
||||||
char *ft_strchr(const char *s, int c);
|
/* char *ft_strchr(const char *s, int c); */
|
||||||
char *ft_strdup(const char *s);
|
/* char *ft_strdup(const char *s); */
|
||||||
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
/* size_t ft_strlcat(char *dst, const char *src, size_t size); */
|
||||||
size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
/* size_t ft_strlcpy(char *dst, const char *src, size_t size); */
|
||||||
size_t ft_strlen(const char *s);
|
/* size_t ft_strlen(const char *s); */
|
||||||
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
/* int ft_strncmp(const char *s1, const char *s2, size_t n); */
|
||||||
char *ft_strnstr(const char *big, const char *little, size_t len);
|
/* char *ft_strnstr(const char *big, const char *little, size_t len); */
|
||||||
char *ft_strrchr(const char *s, int c);
|
/* char *ft_strrchr(const char *s, int c); */
|
||||||
int ft_tolower(int c);
|
/* int ft_tolower(int c); */
|
||||||
int ft_toupper(int c);
|
/* int ft_toupper(int c); */
|
||||||
|
|
||||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
/* char *ft_substr(char const *s, unsigned int start, size_t len); */
|
||||||
char *ft_strjoin(char const *s1, char const *s2);
|
/* char *ft_strjoin(char const *s1, char const *s2); */
|
||||||
char *ft_strtrim(char const *s1, char const *set);
|
/* char *ft_strtrim(char const *s1, char const *set); */
|
||||||
char **ft_split(char const *s, char c);
|
/* char **ft_split(char const *s, char c); */
|
||||||
char *ft_itoa(int n);
|
/* char *ft_itoa(int n); */
|
||||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
/* char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); */
|
||||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
/* void ft_striteri(char *s, void (*f)(unsigned int, char*)); */
|
||||||
void ft_putchar_fd(int fd, char c);
|
/* void ft_putchar_fd(char c, int fd); */
|
||||||
void ft_putstr_fd(int fd, char *s);
|
/* void ft_putstr_fd(char *s, int fd); */
|
||||||
void ft_putendl_fd(int fd, char *s);
|
/* void ft_putendl_fd(char *s, int fd); */
|
||||||
void ft_putnbr_fd(int fd, int n);
|
/* void ft_putnbr_fd(int n, int fd); */
|
||||||
|
|
||||||
typedef struct s_list
|
|
||||||
{
|
|
||||||
void *content;
|
|
||||||
struct s_list *next;
|
|
||||||
} t_list;
|
|
||||||
|
|
||||||
t_list *ft_lstnew(void *content);
|
|
||||||
void ft_lstadd_front(t_list **lst, t_list *nouveau);
|
|
||||||
int ft_lstsize(t_list *lst);
|
|
||||||
t_list *ft_lstlast(t_list *lst);
|
|
||||||
void ft_lstadd_back(t_list **lst, t_list *nouveau);
|
|
||||||
void ft_lstdelone(t_list *lst, void (*del)(void *));
|
|
||||||
void ft_lstclear(t_list **lst, void (*del)(void *));
|
|
||||||
void ft_lstiter(t_list *lst, void (*f)(void *));
|
|
||||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user