Push project
This commit is contained in:
commit
aee974b462
61
Makefile
Normal file
61
Makefile
Normal file
|
@ -0,0 +1,61 @@
|
|||
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
|
||||
|
||||
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}
|
||||
|
||||
NAME = minishell
|
||||
|
||||
CC = clang
|
||||
|
||||
CFLAGS = -Werror -Wextra -Wall -g
|
||||
|
||||
LIBS = libftx/libftx.a
|
||||
|
||||
%.o: %.c
|
||||
${CC} ${CFLAGS} -c -o $@ $<
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
make -C libftx all
|
||||
${CC} ${OBJS} -o ${NAME} ${LIBS} -lreadline
|
||||
|
||||
clean:
|
||||
make -C libftx clean
|
||||
rm -f ${OBJS}
|
||||
|
||||
fclean: clean
|
||||
make -C libftx fclean
|
||||
rm -f ${NAME}
|
||||
|
||||
re: fclean
|
||||
make all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
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
|
BIN
builtins/.nfs000000000ab804b60000004a
Normal file
BIN
builtins/.nfs000000000ab804b60000004a
Normal file
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
|
18
data/data_private.h
Normal file
18
data/data_private.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* data_private.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/27 13:42:21 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/03/27 13:42:23 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef DATA_PRIVATE_H
|
||||
# define DATA_PRIVATE_H
|
||||
# include "../libftx/libftx.h"
|
||||
# include "./data.h"
|
||||
|
||||
#endif
|
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
|
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
|
35
libftx/Makefile
Normal file
35
libftx/Makefile
Normal file
|
@ -0,0 +1,35 @@
|
|||
OBJ = ${SRC:.c=.o}
|
||||
|
||||
NAME = libftx.a
|
||||
|
||||
LIBS = libft/libft.a gnl/get_next_line.a printf/ft_printf.a extra/extra.a
|
||||
|
||||
CC = clang
|
||||
|
||||
FLAG = -Wall -Wextra -Werror
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
${NAME}: ${OBJ}
|
||||
make -C extra
|
||||
make -C libft
|
||||
make -C gnl
|
||||
make -C printf
|
||||
ar -rcT $(NAME) $(LIBS)
|
||||
|
||||
clean:
|
||||
make -C extra clean
|
||||
make -C libft clean
|
||||
make -C gnl clean
|
||||
make -C printf clean
|
||||
|
||||
fclean: clean
|
||||
rm -f ${NAME}
|
||||
make -C extra fclean
|
||||
make -C libft fclean
|
||||
make -C printf fclean
|
||||
make -C gnl fclean
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
39
libftx/extra/Makefile
Normal file
39
libftx/extra/Makefile
Normal file
|
@ -0,0 +1,39 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
|
||||
# Updated: 2023/01/19 12:55:43 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
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 ft_swap.c
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
NAME = extra.a
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g
|
||||
|
||||
%.o: %.c extra.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
ar -rc $(NAME) $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(BOBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all bonus clean fclean re
|
40
libftx/extra/extra.h
Normal file
40
libftx/extra/extra.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* extra.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 14:03:10 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/19 17:35:09 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef EXTRA_H
|
||||
# define EXTRA_H
|
||||
# include <stdarg.h>
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include "../libft/libft.h"
|
||||
|
||||
char *ft_ultoa_base(unsigned long long n, char *base);
|
||||
char *get_next_line(int fd);
|
||||
size_t ft_random_generator(size_t start, size_t stop);
|
||||
void ft_freer_tab_ultimate(size_t len, ...);
|
||||
void ft_freer_ultimate(size_t len, ...);
|
||||
char *ft_strgen(char c, size_t len);
|
||||
char *ft_strfjoin(char *s1, char *s2);
|
||||
char *ft_strmerger(size_t arg_len, ...);
|
||||
int ft_is_in(char *str, char c);
|
||||
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
|
||||
char *ft_strndup(const char *src, size_t n);
|
||||
ssize_t ft_strchri(char *str, char c);
|
||||
int ft_contain_only_str(char *str, char *to_find);
|
||||
int ft_contain_only(char *str, char c);
|
||||
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
|
41
libftx/extra/ft_contain_only.c
Normal file
41
libftx/extra/ft_contain_only.c
Normal file
|
@ -0,0 +1,41 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_contain_only.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/12 16:28:20 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/12 16:31:22 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
int ft_contain_only(char *str, char c)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if (str[i] != c)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_contain_only_str(char *str, char *to_find)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if (!ft_is_in(to_find, str[i]))
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
58
libftx/extra/ft_freer.c
Normal file
58
libftx/extra/ft_freer.c
Normal file
|
@ -0,0 +1,58 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_freer.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/08 15:01:40 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/09 17:57:52 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
void ft_freer_ultimate(size_t len, ...)
|
||||
{
|
||||
va_list va;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
va_start(va, len);
|
||||
while (i < len)
|
||||
{
|
||||
free(va_arg(va, char *));
|
||||
i++;
|
||||
}
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
void ft_freer_tab(char **tab)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (tab == NULL)
|
||||
return ;
|
||||
i = 0;
|
||||
while (tab[i] != NULL)
|
||||
{
|
||||
free(tab[i]);
|
||||
i++;
|
||||
}
|
||||
free(tab);
|
||||
}
|
||||
|
||||
void ft_freer_tab_ultimate(size_t len, ...)
|
||||
{
|
||||
va_list va;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
va_start(va, len);
|
||||
while (i < len)
|
||||
{
|
||||
ft_freer_tab(va_arg(va, char **));
|
||||
i++;
|
||||
}
|
||||
va_end(va);
|
||||
}
|
27
libftx/extra/ft_is_in.c
Normal file
27
libftx/extra/ft_is_in.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_is_in.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 20:29:53 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/08 12:15:08 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
int ft_is_in(char *str, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if (str[i] == c)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
38
libftx/extra/ft_random_generator.c
Normal file
38
libftx/extra/ft_random_generator.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_random_generator.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/09 18:43:27 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/09 20:09:33 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
size_t ft_random_generator(size_t start, size_t stop)
|
||||
{
|
||||
int fd;
|
||||
char *line;
|
||||
int sum;
|
||||
size_t i;
|
||||
|
||||
fd = open("/dev/random", O_RDONLY);
|
||||
if (fd == -1)
|
||||
return (0);
|
||||
line = ft_calloc(sizeof(char), 10001);
|
||||
if (line == NULL)
|
||||
return (0);
|
||||
read(fd, line, 10000);
|
||||
sum = 0;
|
||||
i = 0;
|
||||
while (i < ft_strlen(line))
|
||||
{
|
||||
sum += (unsigned int) line[i];
|
||||
i++;
|
||||
}
|
||||
free(line);
|
||||
return (sum % (stop - start));
|
||||
}
|
27
libftx/extra/ft_strchri.c
Normal file
27
libftx/extra/ft_strchri.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strchri.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 19:22:58 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/10 18:30:35 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
ssize_t ft_strchri(char *str, char c)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (str == NULL)
|
||||
return (-1);
|
||||
i = 0;
|
||||
while (str[i] != c && str[i] != '\0')
|
||||
i++;
|
||||
if (str[i] == '\0')
|
||||
return (-1);
|
||||
return (i);
|
||||
}
|
23
libftx/extra/ft_strcmp.c
Normal file
23
libftx/extra/ft_strcmp.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strcmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 19:20:47 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/05 13:20:20 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
int ft_strcmp(char *s1, char *s2)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s1[i] == s2[i] && s1[i] != '\0')
|
||||
i++;
|
||||
return (s1[i] - s2[i]);
|
||||
}
|
42
libftx/extra/ft_strfjoin.c
Normal file
42
libftx/extra/ft_strfjoin.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strfjoin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 14:06:04 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/10 18:29:59 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
char *ft_strfjoin(char *s1, char *s2)
|
||||
{
|
||||
ssize_t i;
|
||||
ssize_t y;
|
||||
char *out;
|
||||
|
||||
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
if (s1 != NULL)
|
||||
{
|
||||
i = -1;
|
||||
while (s1[++i] != '\0')
|
||||
out[i] = s1[i];
|
||||
}
|
||||
free(s1);
|
||||
y = 0;
|
||||
if (s2 != NULL)
|
||||
{
|
||||
y = -1;
|
||||
while (s2[++y] != '\0')
|
||||
out[i + y] = s2[y];
|
||||
}
|
||||
free(s2);
|
||||
out[i + y] = '\0';
|
||||
return (out);
|
||||
}
|
31
libftx/extra/ft_strgen.c
Normal file
31
libftx/extra/ft_strgen.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strgen.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/08 12:32:52 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/09 18:04:31 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
char *ft_strgen(char c, size_t len)
|
||||
{
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
out = ft_calloc((len + 1), sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
out[i] = c;
|
||||
i++;
|
||||
}
|
||||
out[len] = '\0';
|
||||
return (out);
|
||||
}
|
33
libftx/extra/ft_strmerger.c
Normal file
33
libftx/extra/ft_strmerger.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strmerger.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/04 15:09:15 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/04 15:24:17 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
char *ft_strmerger(size_t arg_len, ...)
|
||||
{
|
||||
va_list va;
|
||||
char *out;
|
||||
char *temp;
|
||||
|
||||
va_start(va, arg_len);
|
||||
out = ft_strjoin(va_arg(va, char *), va_arg(va, char *));
|
||||
while (arg_len > 2)
|
||||
{
|
||||
temp = ft_strjoin(out, va_arg(va, char *));
|
||||
free(out);
|
||||
if (temp == NULL)
|
||||
return (NULL);
|
||||
out = temp;
|
||||
arg_len--;
|
||||
}
|
||||
return (out);
|
||||
}
|
32
libftx/extra/ft_strndup.c
Normal file
32
libftx/extra/ft_strndup.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strndup.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/05 19:27:03 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
char *ft_strndup(const char *src, size_t n)
|
||||
{
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
if (src[0] == '\0')
|
||||
return (NULL);
|
||||
out = ft_calloc(n + 1, sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (src[i] != '\0' && i < n)
|
||||
{
|
||||
out[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
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;
|
||||
}
|
31
libftx/extra/ft_tabrealloc.c
Normal file
31
libftx/extra/ft_tabrealloc.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_tabrealloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/05 18:58:48 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/10 18:30:21 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size)
|
||||
{
|
||||
char **new;
|
||||
size_t i;
|
||||
|
||||
new = ft_calloc(new_size, sizeof(char *));
|
||||
if (new == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i < current_size)
|
||||
{
|
||||
new[i] = tab[i];
|
||||
i++;
|
||||
}
|
||||
free(tab);
|
||||
return (new);
|
||||
}
|
89
libftx/extra/ft_ultoa_base.c
Normal file
89
libftx/extra/ft_ultoa_base.c
Normal file
|
@ -0,0 +1,89 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ultoa_base.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/19 13:01:09 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "extra.h"
|
||||
|
||||
static size_t ft_str_size(unsigned long long n, size_t base_size)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
size = 1;
|
||||
if (n == 0)
|
||||
return (2);
|
||||
while (n != 0)
|
||||
{
|
||||
n = n / base_size;
|
||||
size++;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
static int ft_isdup(char *str)
|
||||
{
|
||||
char c;
|
||||
size_t i;
|
||||
|
||||
while (*str != 0)
|
||||
{
|
||||
c = *str;
|
||||
i = 1;
|
||||
while (str[i] != 0)
|
||||
{
|
||||
if (str[i] == c)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static size_t ft_base_size(char *base)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
if (ft_isdup(base))
|
||||
return (0);
|
||||
len = ft_strlen(base);
|
||||
if (len < 2)
|
||||
return (0);
|
||||
return (len);
|
||||
}
|
||||
|
||||
char *ft_ultoa_base(unsigned long long n, char *base)
|
||||
{
|
||||
size_t base_size;
|
||||
int str_size;
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
if (base == NULL)
|
||||
return (NULL);
|
||||
base_size = ft_base_size(base);
|
||||
if (base_size == 0)
|
||||
return (NULL);
|
||||
str_size = ft_str_size(n, base_size);
|
||||
out = ft_calloc(str_size + 1, sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
if (n == 0)
|
||||
out[0] = base[0];
|
||||
while (n != 0)
|
||||
{
|
||||
out[str_size - 2 - i] = base[n % base_size];
|
||||
n /= base_size;
|
||||
i++;
|
||||
}
|
||||
out[str_size - 1] = '\0';
|
||||
return (out);
|
||||
}
|
39
libftx/gnl/Makefile
Normal file
39
libftx/gnl/Makefile
Normal file
|
@ -0,0 +1,39 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/12/14 15:49:18 by cchauvet #+# #+# #
|
||||
# Updated: 2023/01/05 19:22:40 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SRCS = get_next_line.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
NAME = get_next_line.a
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g
|
||||
|
||||
CC = clang
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
ar -rc $(NAME) $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(BOBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
104
libftx/gnl/get_next_line.c
Normal file
104
libftx/gnl/get_next_line.c
Normal file
|
@ -0,0 +1,104 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/05 13:07:10 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
char *ft_getstash(int fd)
|
||||
{
|
||||
char *str;
|
||||
int readed;
|
||||
|
||||
str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
readed = read(fd, str, BUFFER_SIZE);
|
||||
if (readed < 1)
|
||||
{
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_getline(int fd)
|
||||
{
|
||||
char *stash;
|
||||
char *buf;
|
||||
|
||||
stash = NULL;
|
||||
buf = NULL;
|
||||
while (ft_strchri(stash, '\n') == -1)
|
||||
{
|
||||
buf = ft_getstash(fd);
|
||||
if (buf == NULL)
|
||||
return (stash);
|
||||
stash = ft_strfjoin(stash, buf);
|
||||
if (stash == NULL)
|
||||
return (NULL);
|
||||
}
|
||||
return (stash);
|
||||
}
|
||||
|
||||
char *ft_getreturn(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
i = ft_strchri(str, '\n') + 1;
|
||||
if (i == 0)
|
||||
i = ft_strlen(str);
|
||||
return (ft_strndup(str, i));
|
||||
}
|
||||
|
||||
char *ft_getextra(char *str)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
i = ft_strchri(str, '\n') + 1;
|
||||
if (i == 0)
|
||||
return (NULL);
|
||||
j = ft_strlen(str + i);
|
||||
return (ft_strndup(str + i, j));
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char *stash = NULL;
|
||||
char *buf1;
|
||||
char *buf2;
|
||||
|
||||
buf2 = stash;
|
||||
if (ft_strchri(stash, '\n') == -1)
|
||||
{
|
||||
buf1 = ft_getline(fd);
|
||||
buf2 = ft_strfjoin(stash, buf1);
|
||||
if (buf2 == NULL)
|
||||
{
|
||||
free(buf1);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
buf1 = ft_getreturn(buf2);
|
||||
stash = ft_getextra(buf2);
|
||||
free(buf2);
|
||||
if (buf1 == NULL)
|
||||
{
|
||||
free(stash);
|
||||
free(buf1);
|
||||
return (NULL);
|
||||
}
|
||||
return (buf1);
|
||||
}
|
25
libftx/gnl/get_next_line.h
Normal file
25
libftx/gnl/get_next_line.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/10 18:26:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 42
|
||||
# endif
|
||||
# include "../libft/libft.h"
|
||||
# include "../extra/extra.h"
|
||||
|
||||
char *get_next_line(int fd);
|
||||
|
||||
#endif
|
81
libftx/libft/Makefile
Normal file
81
libftx/libft/Makefile
Normal file
|
@ -0,0 +1,81 @@
|
|||
:# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
|
||||
# Updated: 2023/01/05 17:44:37 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
CC = clang
|
||||
|
||||
SRCS = ft_isalpha.c \
|
||||
ft_isdigit.c \
|
||||
ft_isalnum.c \
|
||||
ft_isascii.c \
|
||||
ft_isprint.c \
|
||||
ft_strlen.c \
|
||||
ft_memset.c \
|
||||
ft_bzero.c \
|
||||
ft_memcpy.c \
|
||||
ft_memmove.c \
|
||||
ft_strlcpy.c \
|
||||
ft_strlcat.c \
|
||||
ft_toupper.c \
|
||||
ft_tolower.c \
|
||||
ft_strchr.c \
|
||||
ft_strrchr.c \
|
||||
ft_strncmp.c \
|
||||
ft_memchr.c \
|
||||
ft_memcmp.c \
|
||||
ft_strnstr.c \
|
||||
ft_atoi.c \
|
||||
ft_calloc.c \
|
||||
ft_strdup.c \
|
||||
ft_substr.c \
|
||||
ft_strjoin.c \
|
||||
ft_strtrim.c \
|
||||
ft_split.c \
|
||||
ft_itoa.c \
|
||||
ft_strmapi.c \
|
||||
ft_striteri.c \
|
||||
ft_putchar_fd.c \
|
||||
ft_putstr_fd.c \
|
||||
ft_putendl_fd.c \
|
||||
ft_putnbr_fd.c \
|
||||
ft_lstnew.c \
|
||||
ft_lstadd_front.c \
|
||||
ft_lstsize.c \
|
||||
ft_lstlast.c \
|
||||
ft_lstadd_back.c \
|
||||
ft_lstdelone.c \
|
||||
ft_lstclear.c \
|
||||
ft_lstiter.c \
|
||||
ft_lstmap.c
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
NAME = libft.a
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g
|
||||
|
||||
%.o: %.c libft.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
ar -rc $(NAME) $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
39
libftx/libft/ft_atoi.c
Normal file
39
libftx/libft/ft_atoi.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/28 18:15:04 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_atoi(const char *nptr)
|
||||
{
|
||||
char *str;
|
||||
int out;
|
||||
int sign;
|
||||
|
||||
str = (char *) nptr;
|
||||
out = 0;
|
||||
while (*str == ' ' || *str == '\f' || *str == '\v' || *str == '\t'
|
||||
|| *str == '\r' || *str == '\n')
|
||||
str++;
|
||||
sign = 1;
|
||||
if (*str == '-' || *str == '+')
|
||||
{
|
||||
if (*str == '-')
|
||||
sign = -1;
|
||||
str++;
|
||||
}
|
||||
while (ft_isdigit(*str))
|
||||
{
|
||||
out = 10 * out + *str - 48;
|
||||
str++;
|
||||
}
|
||||
return (out * sign);
|
||||
}
|
18
libftx/libft/ft_bzero.c
Normal file
18
libftx/libft/ft_bzero.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_bzero.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 09:40:13 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/27 09:41:02 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_bzero(void *s, size_t n)
|
||||
{
|
||||
ft_memset(s, 0, n);
|
||||
}
|
26
libftx/libft/ft_calloc.c
Normal file
26
libftx/libft/ft_calloc.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_calloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 16:09:01 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/28 17:31:12 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
void *tab;
|
||||
|
||||
if (nmemb != 0 && (size_t)(nmemb * size) / nmemb != size)
|
||||
return (NULL);
|
||||
tab = malloc(nmemb * size);
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
ft_bzero(tab, nmemb * size);
|
||||
return (tab);
|
||||
}
|
22
libftx/libft/ft_isalnum.c
Normal file
22
libftx/libft/ft_isalnum.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isalnum.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 11:15:04 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/28 17:31:44 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isalnum(int c)
|
||||
{
|
||||
if (('9' >= c && c >= '0')
|
||||
|| ('z' >= c && c >= 'a')
|
||||
|| ('Z' >= c && c >= 'A'))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
20
libftx/libft/ft_isalpha.c
Normal file
20
libftx/libft/ft_isalpha.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isalpha.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 11:15:04 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/27 11:04:58 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isalpha(int c)
|
||||
{
|
||||
if (('z' >= c && c >= 'a') || ('Z' >= c && c >= 'A'))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
18
libftx/libft/ft_isascii.c
Normal file
18
libftx/libft/ft_isascii.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isascii.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 11:57:55 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/26 13:33:50 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_isascii(int c)
|
||||
{
|
||||
if (c >= 0 && 127 >= c)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
20
libftx/libft/ft_isdigit.c
Normal file
20
libftx/libft/ft_isdigit.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isdigit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 11:15:04 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/27 11:09:19 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isdigit(int c)
|
||||
{
|
||||
if ('9' >= c && c >= '0')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
18
libftx/libft/ft_isprint.c
Normal file
18
libftx/libft/ft_isprint.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isprint.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 11:52:31 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/28 15:05:17 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isprint(int c)
|
||||
{
|
||||
return (32 <= c && c < 127);
|
||||
}
|
55
libftx/libft/ft_itoa.c
Normal file
55
libftx/libft/ft_itoa.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/09 13:51:09 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int ft_nb_digit(int n)
|
||||
{
|
||||
int out;
|
||||
|
||||
out = 0;
|
||||
while (n)
|
||||
{
|
||||
n /= 10;
|
||||
out++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
char *out;
|
||||
unsigned int nb[2];
|
||||
|
||||
if (!n)
|
||||
return (ft_strdup("0"));
|
||||
nb[0] = ft_nb_digit(n);
|
||||
if (n < 0)
|
||||
{
|
||||
nb[1] = -n;
|
||||
nb[0]++;
|
||||
}
|
||||
else
|
||||
nb[1] = n;
|
||||
out = malloc(sizeof(char) * (nb[0] + 1));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
out[nb[0]--] = 0;
|
||||
if (n < 0)
|
||||
out[0] = '-';
|
||||
while (nb[1])
|
||||
{
|
||||
out[nb[0]--] = nb[1] % 10 + 48;
|
||||
nb[1] /= 10;
|
||||
}
|
||||
return (out);
|
||||
}
|
26
libftx/libft/ft_lstadd_back.c
Normal file
26
libftx/libft/ft_lstadd_back.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstadd_back.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 23:58:34 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/26 18:06:00 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstadd_back(t_list **lst, t_list *new)
|
||||
{
|
||||
t_list *last;
|
||||
|
||||
if (lst == NULL || new == NULL)
|
||||
return ;
|
||||
last = ft_lstlast(*lst);
|
||||
if (last == NULL)
|
||||
*lst = new;
|
||||
else
|
||||
last->next = new;
|
||||
}
|
21
libftx/libft/ft_lstadd_front.c
Normal file
21
libftx/libft/ft_lstadd_front.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstadd_front.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 23:50:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/30 01:08:27 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstadd_front(t_list **lst, t_list *new)
|
||||
{
|
||||
if (new == NULL)
|
||||
return ;
|
||||
new->next = *lst;
|
||||
*lst = new;
|
||||
}
|
27
libftx/libft/ft_lstclear.c
Normal file
27
libftx/libft/ft_lstclear.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstclear.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 00:01:05 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/21 14:55:37 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstclear(t_list **lst, void (*del)(void *))
|
||||
{
|
||||
t_list *next;
|
||||
|
||||
if (lst == NULL || del == NULL)
|
||||
return ;
|
||||
while (*lst != NULL)
|
||||
{
|
||||
next = (*lst)->next;
|
||||
ft_lstdelone(*lst, del);
|
||||
*lst = next;
|
||||
}
|
||||
}
|
25
libftx/libft/ft_lstdelone.c
Normal file
25
libftx/libft/ft_lstdelone.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstdelone.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 00:02:55 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/05 21:01:41 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstdelone(t_list *lst, void (*del)(void *))
|
||||
{
|
||||
if (del == NULL || lst == NULL)
|
||||
return ;
|
||||
if (lst != NULL)
|
||||
{
|
||||
if (lst->content != NULL)
|
||||
del(lst->content);
|
||||
free(lst);
|
||||
}
|
||||
}
|
24
libftx/libft/ft_lstiter.c
Normal file
24
libftx/libft/ft_lstiter.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstiter.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 00:05:43 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/05 20:58:18 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstiter(t_list *lst, void (*f)(void *))
|
||||
{
|
||||
if (f == NULL)
|
||||
return ;
|
||||
while (lst != NULL)
|
||||
{
|
||||
f(lst->content);
|
||||
lst = lst->next;
|
||||
}
|
||||
}
|
22
libftx/libft/ft_lstlast.c
Normal file
22
libftx/libft/ft_lstlast.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstlast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 23:55:52 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/30 01:20:18 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
t_list *ft_lstlast(t_list *lst)
|
||||
{
|
||||
if (lst == NULL)
|
||||
return (NULL);
|
||||
while (lst->next != NULL)
|
||||
lst = lst->next;
|
||||
return (lst);
|
||||
}
|
39
libftx/libft/ft_lstmap.c
Normal file
39
libftx/libft/ft_lstmap.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstmap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 00:05:05 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/05 00:08:45 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
||||
{
|
||||
t_list *root;
|
||||
t_list *last;
|
||||
|
||||
if (f == NULL || del == NULL)
|
||||
return (NULL);
|
||||
root = ft_lstnew(f(lst->content));
|
||||
if (root == NULL)
|
||||
return (NULL);
|
||||
last = root;
|
||||
lst = lst->next;
|
||||
while (lst != NULL)
|
||||
{
|
||||
last->next = ft_lstnew(f(lst->content));
|
||||
if (last->next == NULL)
|
||||
{
|
||||
ft_lstclear(&root, del);
|
||||
return (NULL);
|
||||
}
|
||||
lst = lst->next;
|
||||
last = last->next;
|
||||
}
|
||||
return (root);
|
||||
}
|
25
libftx/libft/ft_lstnew.c
Normal file
25
libftx/libft/ft_lstnew.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstnew.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 23:41:40 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 23:47:50 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
t_list *ft_lstnew(void *content)
|
||||
{
|
||||
t_list *new;
|
||||
|
||||
new = malloc(sizeof(t_list));
|
||||
if (new == NULL)
|
||||
return (NULL);
|
||||
new->next = NULL;
|
||||
new->content = content;
|
||||
return (new);
|
||||
}
|
26
libftx/libft/ft_lstsize.c
Normal file
26
libftx/libft/ft_lstsize.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstsize.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/30 01:18:08 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/30 01:18:15 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_lstsize(t_list *lst)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
while (lst != NULL)
|
||||
{
|
||||
count++;
|
||||
lst = lst->next;
|
||||
}
|
||||
return (count);
|
||||
}
|
31
libftx/libft/ft_memchr.c
Normal file
31
libftx/libft/ft_memchr.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 13:40:09 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/27 13:59:03 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memchr(const void *s, int c, size_t n)
|
||||
{
|
||||
unsigned char *tab;
|
||||
size_t i;
|
||||
unsigned char tofind;
|
||||
|
||||
tab = (unsigned char *) s;
|
||||
tofind = (unsigned char) c;
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
if (tab[i] == tofind)
|
||||
return (tab + i);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
29
libftx/libft/ft_memcmp.c
Normal file
29
libftx/libft/ft_memcmp.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memcmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 14:15:37 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/27 15:10:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_memcmp(const void *s1, const void *s2, size_t n)
|
||||
{
|
||||
unsigned char *str1;
|
||||
unsigned char *str2;
|
||||
size_t i;
|
||||
|
||||
if (n == 0)
|
||||
return (0);
|
||||
str1 = (unsigned char *) s1;
|
||||
str2 = (unsigned char *) s2;
|
||||
i = 0;
|
||||
while (str1[i] == str2[i] && i < n - 1)
|
||||
i++;
|
||||
return (str1[i] - str2[i]);
|
||||
}
|
32
libftx/libft/ft_memcpy.c
Normal file
32
libftx/libft/ft_memcpy.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 09:42:11 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 11:50:17 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memcpy(void *dest, const void *src, size_t n)
|
||||
{
|
||||
char *tab1;
|
||||
char *tab2;
|
||||
size_t i;
|
||||
|
||||
if (dest == NULL && src == NULL)
|
||||
return (dest);
|
||||
tab1 = dest;
|
||||
tab2 = (void *) src;
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
tab1[i] = tab2[i];
|
||||
i++;
|
||||
}
|
||||
return (tab1);
|
||||
}
|
37
libftx/libft/ft_memmove.c
Normal file
37
libftx/libft/ft_memmove.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memmove.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 11:44:12 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/28 16:40:24 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memmove(void *dest, const void *src, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
char *tab1;
|
||||
char *tab2;
|
||||
|
||||
if (dest == NULL && src == NULL)
|
||||
return (dest);
|
||||
tab1 = dest;
|
||||
tab2 = (void *) src;
|
||||
if (tab1 < tab2)
|
||||
ft_memcpy(dest, src, n);
|
||||
else
|
||||
{
|
||||
i = n;
|
||||
while (0 < i)
|
||||
{
|
||||
i--;
|
||||
tab1[i] = tab2[i];
|
||||
}
|
||||
}
|
||||
return (dest);
|
||||
}
|
28
libftx/libft/ft_memset.c
Normal file
28
libftx/libft/ft_memset.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 09:29:51 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/27 09:48:23 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memset(void *s, int c, size_t n)
|
||||
{
|
||||
char *tab;
|
||||
size_t i;
|
||||
|
||||
tab = s;
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
tab[i] = c;
|
||||
i++;
|
||||
}
|
||||
return (s);
|
||||
}
|
18
libftx/libft/ft_putchar_fd.c
Normal file
18
libftx/libft/ft_putchar_fd.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 16:23:01 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putchar_fd(char c, int fd)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
21
libftx/libft/ft_putendl_fd.c
Normal file
21
libftx/libft/ft_putendl_fd.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putendl_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 16:15:07 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putendl_fd(char *s, int fd)
|
||||
{
|
||||
if (s == NULL)
|
||||
return ;
|
||||
ft_putstr_fd(s, fd);
|
||||
ft_putchar_fd('\n', fd);
|
||||
}
|
34
libftx/libft/ft_putnbr_fd.c
Normal file
34
libftx/libft/ft_putnbr_fd.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:28:27 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 22:39:25 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putnbr_fd(int n, int fd)
|
||||
{
|
||||
if (n == -2147483648)
|
||||
{
|
||||
ft_putnbr_fd(-2, fd);
|
||||
ft_putnbr_fd(147483648, fd);
|
||||
}
|
||||
else if (n < 0)
|
||||
{
|
||||
ft_putchar_fd('-', fd);
|
||||
ft_putnbr_fd(-n, fd);
|
||||
}
|
||||
else if (n > 9)
|
||||
{
|
||||
ft_putnbr_fd(n / 10, fd);
|
||||
ft_putnbr_fd(n % 10, fd);
|
||||
}
|
||||
else
|
||||
ft_putchar_fd(n + 48, fd);
|
||||
}
|
21
libftx/libft/ft_putstr_fd.c
Normal file
21
libftx/libft/ft_putstr_fd.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putstr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 22:40:34 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putstr_fd(char *s, int fd)
|
||||
{
|
||||
if (s == NULL)
|
||||
return ;
|
||||
while (*s)
|
||||
write(fd, s++, 1);
|
||||
}
|
92
libftx/libft/ft_split.c
Normal file
92
libftx/libft/ft_split.c
Normal file
|
@ -0,0 +1,92 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/06 19:33:51 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_cancel(void **tab, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (tab != NULL)
|
||||
{
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
free(tab[i]);
|
||||
i++;
|
||||
}
|
||||
free(tab);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static size_t ft_seglen(const char *s, char c)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
if (s == NULL)
|
||||
return (0);
|
||||
len = 0;
|
||||
while (*s != 0)
|
||||
{
|
||||
while (*s == c && *s != 0)
|
||||
s++;
|
||||
if (*s != 0)
|
||||
len++;
|
||||
while (*s != c && *s != 0)
|
||||
s++;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
|
||||
{
|
||||
size_t tab_index;
|
||||
size_t let_index;
|
||||
size_t start;
|
||||
|
||||
tab_index = 0;
|
||||
let_index = 0;
|
||||
start = 0;
|
||||
if (tab == NULL || s == NULL)
|
||||
return (NULL);
|
||||
while (tab_index < len)
|
||||
{
|
||||
while (s[let_index] == c && s[let_index] != 0)
|
||||
let_index++;
|
||||
start = let_index;
|
||||
while (s[let_index] != c && s[let_index] != 0)
|
||||
let_index++;
|
||||
tab[tab_index] = ft_substr(s, start, let_index - start);
|
||||
if (tab[tab_index] == NULL)
|
||||
return (ft_cancel((void **)tab, tab_index));
|
||||
tab_index++;
|
||||
}
|
||||
return (tab);
|
||||
}
|
||||
|
||||
char **ft_split(const char *s, char c)
|
||||
{
|
||||
size_t len;
|
||||
char **tab;
|
||||
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
len = ft_seglen(s, c);
|
||||
tab = malloc((len + 1) * sizeof(char *));
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
tab[len] = NULL;
|
||||
if (ft_segsplitter(tab, len, s, c) == NULL)
|
||||
return (NULL);
|
||||
return (tab);
|
||||
}
|
24
libftx/libft/ft_strchr.c
Normal file
24
libftx/libft/ft_strchr.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/05 20:56:37 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
while (*s != (char) c)
|
||||
{
|
||||
if (*s == 0)
|
||||
return (NULL);
|
||||
s++;
|
||||
}
|
||||
return ((char *) s);
|
||||
}
|
31
libftx/libft/ft_strdup.c
Normal file
31
libftx/libft/ft_strdup.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strdup.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 16:00:49 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 23:24:11 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strdup(const char *s)
|
||||
{
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
out = ft_calloc((ft_strlen(s) + 1), sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
out[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
out[i] = 0;
|
||||
return (out);
|
||||
}
|
27
libftx/libft/ft_striteri.c
Normal file
27
libftx/libft/ft_striteri.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_striteri.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:21:01 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/04 15:01:00 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char *))
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (s == NULL)
|
||||
return ;
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
f(i, s + i);
|
||||
i++;
|
||||
}
|
||||
}
|
38
libftx/libft/ft_strjoin.c
Normal file
38
libftx/libft/ft_strjoin.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strjoin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 11:20:27 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 11:30:50 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strjoin(const char *s1, const char *s2)
|
||||
{
|
||||
char *out;
|
||||
ssize_t i;
|
||||
ssize_t j;
|
||||
size_t len_s1;
|
||||
size_t len_s2;
|
||||
|
||||
if (s1 == NULL || s2 == NULL)
|
||||
return (NULL);
|
||||
len_s1 = ft_strlen(s1);
|
||||
len_s2 = ft_strlen(s2);
|
||||
out = malloc(sizeof(char) * (len_s1 + len_s2 + 1));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
while (++i < (ssize_t) len_s1)
|
||||
out[i] = s1[i];
|
||||
j = -1;
|
||||
while (++j < (ssize_t) len_s2)
|
||||
out[j + i] = s2[j];
|
||||
out[i + j] = 0;
|
||||
return (out);
|
||||
}
|
25
libftx/libft/ft_strlcat.c
Normal file
25
libftx/libft/ft_strlcat.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcat.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 17:02:13 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/28 17:11:29 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
size_t ft_strlcat(char *dest, const char *src, size_t size)
|
||||
{
|
||||
size_t len_dest;
|
||||
|
||||
if (size == 0)
|
||||
return (ft_strlen(src));
|
||||
len_dest = ft_strlen(dest);
|
||||
if (len_dest >= size)
|
||||
return (ft_strlen(src) + (size));
|
||||
return (len_dest + ft_strlcpy(dest + len_dest, src, size - len_dest));
|
||||
}
|
28
libftx/libft/ft_strlcpy.c
Normal file
28
libftx/libft/ft_strlcpy.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 16:24:15 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/28 17:23:27 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
size_t ft_strlcpy(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i + 1 < size && src[i])
|
||||
{
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
if (size)
|
||||
dst[i] = 0;
|
||||
return (ft_strlen(src));
|
||||
}
|
25
libftx/libft/ft_strlen.c
Normal file
25
libftx/libft/ft_strlen.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlen.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 12:03:59 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/01/05 17:46:12 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
size_t ft_strlen(const char *s)
|
||||
{
|
||||
size_t length;
|
||||
|
||||
if (s == NULL)
|
||||
return (0);
|
||||
length = 0;
|
||||
while (s[length])
|
||||
length++;
|
||||
return (length);
|
||||
}
|
33
libftx/libft/ft_strmapi.c
Normal file
33
libftx/libft/ft_strmapi.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strmapi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:01:18 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/30 00:53:55 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
|
||||
{
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
out = malloc((ft_strlen(s) + 1) * sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
out[i] = f(i, s[i]);
|
||||
i++;
|
||||
}
|
||||
out[i] = 0;
|
||||
return (out);
|
||||
}
|
25
libftx/libft/ft_strncmp.c
Normal file
25
libftx/libft/ft_strncmp.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strncmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 08:30:26 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/28 17:29:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_strncmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (n == 0)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (s1[i] == s2[i] && i < n - 1 && s1[i] && s2[i])
|
||||
i++;
|
||||
return ((unsigned char) s1[i] - (unsigned char) s2[i]);
|
||||
}
|
35
libftx/libft/ft_strnstr.c
Normal file
35
libftx/libft/ft_strnstr.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strnstr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 13:39:33 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 21:10:21 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strnstr(const char *big, const char *little, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
if (!*little || (little == big && ft_strlen(little) <= len))
|
||||
return ((char *) big);
|
||||
i = 0;
|
||||
while (i < len && big[i])
|
||||
{
|
||||
j = 0;
|
||||
while (big[i + j] == little[j] && i + j < len)
|
||||
{
|
||||
if (!little[j + 1])
|
||||
return ((char *) big + i);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
24
libftx/libft/ft_strrchr.c
Normal file
24
libftx/libft/ft_strrchr.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strrchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 08:25:33 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/05 20:50:49 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strrchr(const char *s, int c)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = ft_strlen(s) + 1;
|
||||
while (i-- > 0)
|
||||
if (s[i] == (char) c)
|
||||
return ((char *) s + i);
|
||||
return (NULL);
|
||||
}
|
44
libftx/libft/ft_strtrim.c
Normal file
44
libftx/libft/ft_strtrim.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strtrim.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 11:33:53 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 18:03:20 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int ft_is_in(const char c, const char *charset)
|
||||
{
|
||||
while (*charset)
|
||||
{
|
||||
if (c == *charset)
|
||||
return (1);
|
||||
charset++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *ft_strtrim(const char *s1, const char *set)
|
||||
{
|
||||
size_t start;
|
||||
size_t stop;
|
||||
char *str;
|
||||
|
||||
if (s1 == NULL || set == NULL)
|
||||
return (NULL);
|
||||
if (!*s1 || !*s1)
|
||||
return (ft_strdup(""));
|
||||
start = 0;
|
||||
while (ft_is_in(s1[start], set))
|
||||
start++;
|
||||
stop = ft_strlen(s1) - 1;
|
||||
while (ft_is_in(s1[stop], set))
|
||||
stop--;
|
||||
str = ft_substr(s1, start, stop - start + 1);
|
||||
return (str);
|
||||
}
|
35
libftx/libft/ft_substr.c
Normal file
35
libftx/libft/ft_substr.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_substr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/28 18:53:44 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/05 20:53:19 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
ssize_t size;
|
||||
char *ptr;
|
||||
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
size = ft_strlen(s);
|
||||
size -= start;
|
||||
if (size < 0)
|
||||
size = 0;
|
||||
if ((size_t)size > len)
|
||||
size = len;
|
||||
ptr = malloc((size + 1) * sizeof(char));
|
||||
if (ptr == NULL)
|
||||
return (NULL);
|
||||
ptr[size] = '\0';
|
||||
while (size-- > 0)
|
||||
ptr[size] = s[start + size];
|
||||
return (ptr);
|
||||
}
|
18
libftx/libft/ft_tolower.c
Normal file
18
libftx/libft/ft_tolower.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_tolower.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 12:38:07 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/28 16:41:24 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_tolower(int c)
|
||||
{
|
||||
return (c - ('A' - 'a') * (c >= 'A' && 'Z' >= c));
|
||||
}
|
18
libftx/libft/ft_toupper.c
Normal file
18
libftx/libft/ft_toupper.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_toupper.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 12:38:07 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/28 15:08:58 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_toupper(int c)
|
||||
{
|
||||
return (c + ('A' - 'a') * (c >= 'a' && 'z' >= c));
|
||||
}
|
56
libftx/libft/libft.h
Normal file
56
libftx/libft/libft.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* libft.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 16:22:44 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBFT_H
|
||||
# define LIBFT_H
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include "./list.h"
|
||||
|
||||
void *ft_cancel(void **tab, size_t len);
|
||||
int ft_atoi(const char *nptr);
|
||||
void ft_bzero(void *s, size_t n);
|
||||
void *ft_calloc(size_t nmemb, size_t size);
|
||||
int ft_isalnum(int c);
|
||||
int ft_isalpha(int c);
|
||||
int ft_isascii(int c);
|
||||
int ft_isdigit(int c);
|
||||
int ft_isprint(int c);
|
||||
void *ft_memchr(const void *s, int c, 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_memmove(void *dest, const void *src, size_t n);
|
||||
void *ft_memset(void *s, int c, size_t n);
|
||||
char *ft_strchr(const char *s, int c);
|
||||
char *ft_strdup(const char *s);
|
||||
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_strlen(const char *s);
|
||||
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_strrchr(const char *s, int c);
|
||||
int ft_tolower(int c);
|
||||
int ft_toupper(int c);
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||
char *ft_strjoin(char const *s1, char const *s2);
|
||||
char *ft_strtrim(char const *s1, char const *set);
|
||||
char **ft_split(char const *s, char c);
|
||||
char *ft_itoa(int n);
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putendl_fd(char *s, int fd);
|
||||
void ft_putnbr_fd(int n, int fd);
|
||||
|
||||
#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
|
80
libftx/libftx.h
Normal file
80
libftx/libftx.h
Normal file
|
@ -0,0 +1,80 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* libftx.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/21 13:28:19 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBFTX_H
|
||||
# define LIBFTX_H
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <stdarg.h>
|
||||
# include "./libft/libft.h"
|
||||
|
||||
char *ft_ultoa_base(unsigned long long n, char *base);
|
||||
int ft_printf(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);
|
||||
size_t ft_random_generator(size_t start, size_t stop);
|
||||
void ft_freer_tab_ultimate(size_t len, ...);
|
||||
void ft_freer_ultimate(size_t len, ...);
|
||||
char *ft_strgen(char c, size_t len);
|
||||
int ft_is_in(char *str, char c);
|
||||
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
|
||||
int ft_contain_only_str(char *str, char *to_find);
|
||||
int ft_contain_only(char *str, char c);
|
||||
char *ft_strfjoin(char *s1, char *s2);
|
||||
char *ft_strmerger(size_t arg_len, ...);
|
||||
int ft_strcmp(char *s1, char *s2);
|
||||
ssize_t ft_strchri(char *str, char c);
|
||||
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(void **tab, size_t len); */
|
||||
/* int ft_atoi(const char *nptr); */
|
||||
/* void ft_bzero(void *s, size_t n); */
|
||||
/* void *ft_calloc(size_t nmemb, size_t size); */
|
||||
/* int ft_isalnum(int c); */
|
||||
/* int ft_isalpha(int c); */
|
||||
/* int ft_isascii(int c); */
|
||||
/* int ft_isdigit(int c); */
|
||||
/* int ft_isprint(int c); */
|
||||
/* void *ft_memchr(const void *s, int c, 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_memmove(void *dest, const void *src, size_t n); */
|
||||
/* void *ft_memset(void *s, int c, size_t n); */
|
||||
/* char *ft_strchr(const char *s, int c); */
|
||||
/* char *ft_strdup(const char *s); */
|
||||
/* 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_strlen(const char *s); */
|
||||
/* 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_strrchr(const char *s, int c); */
|
||||
/* int ft_tolower(int c); */
|
||||
/* int ft_toupper(int c); */
|
||||
|
||||
/* char *ft_substr(char const *s, unsigned int start, size_t len); */
|
||||
/* char *ft_strjoin(char const *s1, char const *s2); */
|
||||
/* char *ft_strtrim(char const *s1, char const *set); */
|
||||
/* char **ft_split(char const *s, char c); */
|
||||
/* char *ft_itoa(int n); */
|
||||
/* char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); */
|
||||
/* void ft_striteri(char *s, void (*f)(unsigned int, char*)); */
|
||||
/* void ft_putchar_fd(char c, int fd); */
|
||||
/* void ft_putstr_fd(char *s, int fd); */
|
||||
/* void ft_putendl_fd(char *s, int fd); */
|
||||
/* void ft_putnbr_fd(int n, int fd); */
|
||||
|
||||
#endif
|
39
libftx/printf/Makefile
Normal file
39
libftx/printf/Makefile
Normal file
|
@ -0,0 +1,39 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
|
||||
# Updated: 2023/02/01 16:19:19 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
CC = clang
|
||||
|
||||
SRCS = ft_dprintarg.c ft_dprintflag.c ft_dprintl_base.c ft_dprintptr.c ft_dprintstrtab.c ft_dprintul_base.c ft_dprintul.c ft_dprintx.c ft_dprintX.c ft_isarg.c ft_isdigit.c ft_printf.c ft_putchar_fd.c ft_putstr_fd.c ft_skipflag.c ft_strlen.c ft_vdprintf.c ft_eprintf.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
NAME = ft_printf.a
|
||||
|
||||
CFLAG = -Wall -Werror -Wextra -g
|
||||
|
||||
%.o: %.c
|
||||
${CC} ${CFLAG} -c -o $@ $<
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
ar -rc ${NAME} ${OBJS}
|
||||
|
||||
clean:
|
||||
rm -f ${OBJS} ${BOBJS}
|
||||
|
||||
fclean: clean
|
||||
rm -f ${NAME}
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
18
libftx/printf/ft_dprintX.c
Normal file
18
libftx/printf/ft_dprintX.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_dprintX.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/23 17:44:38 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/23 18:22:43 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
int ft_dprint_upperx(int fd, unsigned int n)
|
||||
{
|
||||
return (ft_dprintul_base(fd, n, "0123456789ABCDEF"));
|
||||
}
|
36
libftx/printf/ft_dprintarg.c
Normal file
36
libftx/printf/ft_dprintarg.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_dprintarg.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 16:29:56 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
int ft_dprintarg(int fd, int arg, va_list args)
|
||||
{
|
||||
if (arg == 'i' || arg == 'd')
|
||||
return (ft_dprintl_base(fd, va_arg(args, int), "0123456789"));
|
||||
if (arg == 'X')
|
||||
return (ft_dprint_upperx(fd, va_arg(args, unsigned int)));
|
||||
if (arg == 'x')
|
||||
return (ft_dprintx(fd, va_arg(args, unsigned int)));
|
||||
if (arg == 'u')
|
||||
return (ft_dprintul(fd, va_arg(args, unsigned int)));
|
||||
if (arg == 'c')
|
||||
return (ft_putchar_fd_p(fd, va_arg(args, int)));
|
||||
if (arg == 'S')
|
||||
return (ft_dprintstrtab(fd, va_arg(args, char **)));
|
||||
if (arg == 's')
|
||||
return (ft_putstr_fd_p(fd, va_arg(args, char *)));
|
||||
if (arg == '%')
|
||||
return (ft_putchar_fd_p(fd, '%'));
|
||||
if (arg == 'p')
|
||||
return (ft_dprintptr(fd, va_arg(args, void *)));
|
||||
return (0);
|
||||
}
|
23
libftx/printf/ft_dprintflag.c
Normal file
23
libftx/printf/ft_dprintflag.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_dprintflag.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/11 16:30:39 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/23 18:59:55 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
int ft_dprintflag(int fd, const char *flag, va_list va)
|
||||
{
|
||||
if (ft_skipflag(flag) == 0)
|
||||
return (-1);
|
||||
flag++;
|
||||
while (ft_isdigit(flag[0]))
|
||||
flag += 1;
|
||||
return (ft_dprintarg(fd, flag[0], va));
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue