Merge branch 'mylan2' into mylan
This commit is contained in:
commit
24d8b38510
43 changed files with 1395 additions and 17 deletions
8
Makefile
8
Makefile
|
@ -3,13 +3,13 @@ NAME = minishell
|
|||
# src directory
|
||||
SRCS = src/
|
||||
# include directory
|
||||
INCLUDES = include/ libtf/
|
||||
INCLUDES = include/ libtf/ libft/
|
||||
# .c files in src/ without the extension
|
||||
CODE = main ask_command error
|
||||
CODE = main ask_command error path
|
||||
# directories to 'make'
|
||||
LIBRARIES = libtf
|
||||
LIBRARIES = libtf libft
|
||||
# .a files to include
|
||||
LIBRARIES_FILES = libtf/libtf.a
|
||||
LIBRARIES_FILES = libtf/libtf.a libft/libft.a
|
||||
# to use with the flags -L and -l
|
||||
LIBRARIES_LINK =
|
||||
# to use with the flag -l
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/04/23 16:01:17 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/04/24 13:19:28 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -17,6 +17,12 @@
|
|||
# include <stdio.h>
|
||||
# include <errno.h>
|
||||
# include <string.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include <stdlib.h>
|
||||
# include <strings.h>
|
||||
# include "libft.h"
|
||||
# include "libtf.h"
|
||||
|
||||
///// ASK COMMAND /////
|
||||
|
@ -30,7 +36,7 @@ const char *ask_command(t_memclass mc);
|
|||
// Represents a call to a program (the program name and its arguments)
|
||||
typedef struct s_call
|
||||
{
|
||||
char *program; // path of the program to call.
|
||||
const char *program; // path of the program to call.
|
||||
int argc; // number of arguments given
|
||||
const char *const *argv; // arguments given
|
||||
// (argc and argv must include the program name)
|
||||
|
@ -39,11 +45,11 @@ typedef struct s_call
|
|||
// Represents a command given by the user.
|
||||
typedef struct s_command
|
||||
{
|
||||
bool error; // true means an error occured in interpret_command().
|
||||
t_call *calls; // all calls to programs.
|
||||
int input_fd; // fd to use with '<' redirection (0 by default)
|
||||
int output_fd; // fd to use with '>' redirection (1 by default)
|
||||
} t_command;
|
||||
bool error; // true means an error occured in interpret_command().
|
||||
const t_call *calls; // all calls to programs.
|
||||
int input_fd; // fd to use with '<' redirection (0 by default)
|
||||
int output_fd; // fd to use with '>' redirection (1 by default)
|
||||
} t_command;
|
||||
|
||||
// Return the t_command representing the command given by the user.
|
||||
// If error, return a t_command wth the value .error = true.
|
||||
|
@ -60,4 +66,10 @@ int execute_command(t_command command);
|
|||
// If msg == "errno", use strerror(errno)
|
||||
void minishell_error(const char *msg);
|
||||
|
||||
///// PATH /////
|
||||
|
||||
// Get the PATH values.
|
||||
// Return a list of strings ended by NULL.
|
||||
const char **get_path(const char **envp);
|
||||
|
||||
#endif
|
||||
|
|
38
libft/Makefile
Normal file
38
libft/Makefile
Normal file
|
@ -0,0 +1,38 @@
|
|||
# The interesting part
|
||||
NAME = libft.a
|
||||
SRCS = ./
|
||||
INCLUDES = ./
|
||||
FUNCTIONS = \
|
||||
isalpha isdigit isalnum isascii isprint \
|
||||
toupper tolower \
|
||||
strncmp strlen strlcpy strlcat strchr strrchr strnstr strdup \
|
||||
strmapi striteri substr strjoin strtrim split \
|
||||
memset bzero memcpy memmove memchr memcmp calloc \
|
||||
atoi itoa \
|
||||
putchar_fd putstr_fd putendl_fd putnbr_fd
|
||||
CODE = $(addprefix ft_, $(FUNCTIONS))
|
||||
|
||||
# It works and I probably won't change it for the rest of my life
|
||||
C_FILES = $(addsuffix .c,$(addprefix $(SRCS),$(CODE)))
|
||||
O_FILES = $(addsuffix .o,$(addprefix $(SRCS),$(CODE)))
|
||||
CC = cc -Wall -Wextra -Werror
|
||||
AR = ar rcs
|
||||
MAKE = make
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(O_FILES)
|
||||
$(AR) $(NAME) $(O_FILES)
|
||||
|
||||
%.o : %.c
|
||||
$(CC) -I $(INCLUDES) -c $< -o $@
|
||||
|
||||
clean :
|
||||
rm -f $(O_FILES)
|
||||
|
||||
fclean : clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
50
libft/ft_atoi.c
Normal file
50
libft/ft_atoi.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/28 17:45:41 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/09 12:03:52 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
static int is_in(char c, char *chars)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (chars[i])
|
||||
{
|
||||
if (c == chars[i])
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_atoi(const char *nptr)
|
||||
{
|
||||
int sign;
|
||||
int i;
|
||||
int r;
|
||||
|
||||
sign = 1;
|
||||
i = 0;
|
||||
r = 0;
|
||||
while (is_in(nptr[i], " \f\n\r\t\v"))
|
||||
i++;
|
||||
if (is_in(nptr[i], "+-"))
|
||||
{
|
||||
if (nptr[i] == '-')
|
||||
sign *= -1;
|
||||
i++;
|
||||
}
|
||||
while (is_in(nptr[i], "0123456789"))
|
||||
{
|
||||
r = r * 10 + (nptr[i] - '0');
|
||||
i++;
|
||||
}
|
||||
return (r * sign);
|
||||
}
|
18
libft/ft_bzero.c
Normal file
18
libft/ft_bzero.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_bzero.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 13:10:31 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/03 13:12:35 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_bzero(void *s, size_t n)
|
||||
{
|
||||
ft_memset(s, '\0', n);
|
||||
}
|
29
libft/ft_calloc.c
Normal file
29
libft/ft_calloc.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_calloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 13:41:02 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/09 15:58:33 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
void *dest;
|
||||
int len;
|
||||
|
||||
if ((long)nmemb * (long)size > (long)INT_MAX)
|
||||
return (NULL);
|
||||
len = size * nmemb;
|
||||
dest = malloc(len);
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
ft_bzero(dest, len);
|
||||
return (dest);
|
||||
}
|
18
libft/ft_isalnum.c
Normal file
18
libft/ft_isalnum.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isalnum.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/02 16:44:20 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/03 15:02:39 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_isalnum(int c)
|
||||
{
|
||||
return (ft_isdigit(c) || ft_isalpha(c));
|
||||
}
|
16
libft/ft_isalpha.c
Normal file
16
libft/ft_isalpha.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isalpha.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/02 16:44:20 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/02 16:46:27 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_isalpha(int c)
|
||||
{
|
||||
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
|
||||
}
|
16
libft/ft_isascii.c
Normal file
16
libft/ft_isascii.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isascii.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/02 16:44:20 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/02 16:50:28 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_isascii(int c)
|
||||
{
|
||||
return ((c >= 0 && c <= 127));
|
||||
}
|
16
libft/ft_isdigit.c
Normal file
16
libft/ft_isdigit.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isdigit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/02 16:44:20 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/02 16:47:07 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_isdigit(int c)
|
||||
{
|
||||
return (c >= '0' && c <= '9');
|
||||
}
|
16
libft/ft_isprint.c
Normal file
16
libft/ft_isprint.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isprint.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/02 16:44:20 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/02 16:51:08 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_isprint(int c)
|
||||
{
|
||||
return (c >= 32 && c <= 126);
|
||||
}
|
67
libft/ft_itoa.c
Normal file
67
libft/ft_itoa.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 13:51:10 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/04 16:49:44 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "libft.h"
|
||||
|
||||
static char get_digit_at(long pos_n, int i, int nb_len)
|
||||
{
|
||||
int w;
|
||||
long ten_power;
|
||||
|
||||
w = nb_len - i - 1;
|
||||
ten_power = 1;
|
||||
while (w--)
|
||||
ten_power *= 10;
|
||||
return ('0' + pos_n / ten_power % 10);
|
||||
}
|
||||
|
||||
static int get_nb_len(long pos_n)
|
||||
{
|
||||
int r;
|
||||
long ten_powers;
|
||||
|
||||
r = 1;
|
||||
ten_powers = 10;
|
||||
while (pos_n >= ten_powers)
|
||||
{
|
||||
r++;
|
||||
ten_powers *= 10;
|
||||
}
|
||||
return (r);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
long pos_n;
|
||||
int sign;
|
||||
int nb_len;
|
||||
char *dest;
|
||||
int i;
|
||||
|
||||
sign = (n >= 0) - (n < 0);
|
||||
pos_n = (long)n * sign;
|
||||
nb_len = get_nb_len(pos_n);
|
||||
dest = malloc(((sign == -1) + nb_len + 1) * sizeof(char));
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
if (sign == -1)
|
||||
dest[i++] = '-';
|
||||
while (i - (sign == -1) < nb_len)
|
||||
{
|
||||
dest[i] = get_digit_at(pos_n, i - (sign == -1), nb_len);
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
28
libft/ft_memchr.c
Normal file
28
libft/ft_memchr.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 13:28:00 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/03 15:31:16 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memchr(const void *s, int c, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
const char *s2 = s;
|
||||
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
if (s2[i] == (char) c)
|
||||
return ((void *) s + i);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
25
libft/ft_memcmp.c
Normal file
25
libft/ft_memcmp.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memcmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 13:37:06 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/09 12:00:22 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_memcmp(const void *s1, const void *s2, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (n == 0)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (i < n - 1 && ((unsigned char *)s1)[i] == ((unsigned char *)s2)[i])
|
||||
i++;
|
||||
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
|
||||
}
|
30
libft/ft_memcpy.c
Normal file
30
libft/ft_memcpy.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 13:12:53 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/16 13:40:19 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memcpy(void *dest, const void *src, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
char *dest2;
|
||||
char *src2;
|
||||
|
||||
dest2 = (char *)dest;
|
||||
src2 = (char *)src;
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
dest2[i] = src2[i];
|
||||
i++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
31
libft/ft_memmove.c
Normal file
31
libft/ft_memmove.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memmove.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 13:17:41 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/12 10:44:37 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memmove(void *dest, const void *src, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (!dest || !src)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
if (dest < src)
|
||||
((char *)dest)[i] = ((char *)src)[i];
|
||||
else
|
||||
((char *)dest)[n - i - 1] = ((char *)src)[n - i - 1];
|
||||
i++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
28
libft/ft_memset.c
Normal file
28
libft/ft_memset.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 13:05:27 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/03 15:27:09 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void *ft_memset(void *s, int c, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
char *dest;
|
||||
|
||||
dest = (char *)s;
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
dest[i] = (char)c;
|
||||
i++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
18
libft/ft_putchar_fd.c
Normal file
18
libft/ft_putchar_fd.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 14:25:02 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/03 14:29:09 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_putchar_fd(char c, int fd)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
20
libft/ft_putendl_fd.c
Normal file
20
libft/ft_putendl_fd.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putendl_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 14:25:02 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/04 14:17:01 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putendl_fd(char *s, int fd)
|
||||
{
|
||||
write(fd, s, ft_strlen(s));
|
||||
write(fd, "\n", 1);
|
||||
}
|
40
libft/ft_putnbr_fd.c
Normal file
40
libft/ft_putnbr_fd.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 14:32:29 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/09 17:23:02 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include "libft.h"
|
||||
|
||||
static void putunsigned(long nb, int fd)
|
||||
{
|
||||
if (nb == 0)
|
||||
return ;
|
||||
putunsigned(nb / 10, fd);
|
||||
ft_putchar_fd((char)(nb % 10) + '0', fd);
|
||||
}
|
||||
|
||||
void ft_putnbr_fd(int n, int fd)
|
||||
{
|
||||
long nb;
|
||||
|
||||
if (n < 0)
|
||||
{
|
||||
ft_putchar_fd('-', fd);
|
||||
nb = -(long)n;
|
||||
}
|
||||
else
|
||||
nb = n;
|
||||
if (n == 0)
|
||||
ft_putchar_fd('0', fd);
|
||||
else
|
||||
putunsigned(nb, fd);
|
||||
}
|
19
libft/ft_putstr_fd.c
Normal file
19
libft/ft_putstr_fd.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putstr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 14:25:02 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/03 14:34:35 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
#include "libft.h"
|
||||
|
||||
void ft_putstr_fd(char *s, int fd)
|
||||
{
|
||||
write(fd, s, ft_strlen(s));
|
||||
}
|
98
libft/ft_split.c
Normal file
98
libft/ft_split.c
Normal file
|
@ -0,0 +1,98 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 11:47:18 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/12 13:24:04 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "libft.h"
|
||||
|
||||
static int count_until(char const *s, char const c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i] && s[i] != c)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
static int count_words(char const *s, char c)
|
||||
{
|
||||
int i;
|
||||
int r;
|
||||
int in_a_word;
|
||||
|
||||
i = 0;
|
||||
r = 0;
|
||||
in_a_word = 0;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == c)
|
||||
{
|
||||
in_a_word = 0;
|
||||
}
|
||||
else if (!in_a_word)
|
||||
{
|
||||
in_a_word = 1;
|
||||
r++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (r);
|
||||
}
|
||||
|
||||
static char **free_array(char ***array, int size)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
free((*array)[i]);
|
||||
(*array)[i] = NULL;
|
||||
i++;
|
||||
}
|
||||
free(*array);
|
||||
*array = NULL;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static void ignore_sep(char const *s, char c, int *i)
|
||||
{
|
||||
while (s[*i] == c)
|
||||
(*i)++;
|
||||
}
|
||||
|
||||
char **ft_split(char const *s, char c)
|
||||
{
|
||||
int size;
|
||||
char **dest;
|
||||
int i;
|
||||
int str_i;
|
||||
int len_until;
|
||||
|
||||
size = count_words(s, c);
|
||||
dest = malloc((size + 1) * sizeof(char *));
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
str_i = 0;
|
||||
while (++i < size)
|
||||
{
|
||||
ignore_sep(s, c, &str_i);
|
||||
len_until = count_until(s + str_i, c);
|
||||
dest[i] = ft_substr(s, str_i, len_until);
|
||||
if (!dest[i])
|
||||
return (free_array(&dest, i));
|
||||
str_i += len_until;
|
||||
}
|
||||
dest[i] = NULL;
|
||||
return (dest);
|
||||
}
|
29
libft/ft_strchr.c
Normal file
29
libft/ft_strchr.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 10:43:17 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/10 13:17:35 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == c)
|
||||
return ((char *)s + i);
|
||||
i++;
|
||||
}
|
||||
if (s[i] == c)
|
||||
return ((char *)s + i);
|
||||
return (NULL);
|
||||
}
|
35
libft/ft_strdup.c
Normal file
35
libft/ft_strdup.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strdup.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/30 09:18:39 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/03 11:27:46 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
char *ft_strdup(const char *s)
|
||||
{
|
||||
int len;
|
||||
int i;
|
||||
char *dest;
|
||||
|
||||
len = 0;
|
||||
while (s[len])
|
||||
len++;
|
||||
dest = malloc((len + 1) * sizeof(char));
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
dest[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
25
libft/ft_striteri.c
Normal file
25
libft/ft_striteri.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_striteri.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 11:08:18 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/04 11:10:28 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char *))
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
(*f)(i, s + i);
|
||||
i++;
|
||||
}
|
||||
}
|
42
libft/ft_strjoin.c
Normal file
42
libft/ft_strjoin.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strjoin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 11:31:45 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/04 11:25:38 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strjoin(char const *s1, char const *s2)
|
||||
{
|
||||
int len;
|
||||
char *dest;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
len = ft_strlen(s1) + ft_strlen(s2);
|
||||
dest = malloc((len + 1) * sizeof(char));
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s1[i])
|
||||
{
|
||||
dest[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
j = 0;
|
||||
while (s2[j])
|
||||
{
|
||||
dest[i] = s2[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
44
libft/ft_strlcat.c
Normal file
44
libft/ft_strlcat.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcat.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/28 10:40:23 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/09 16:20:20 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static size_t min(size_t a, size_t b)
|
||||
{
|
||||
if (a < b)
|
||||
return (a);
|
||||
else
|
||||
return (b);
|
||||
}
|
||||
|
||||
size_t ft_strlcat(char *dest, const char *src, size_t size)
|
||||
{
|
||||
size_t dest_len;
|
||||
size_t dest_i;
|
||||
size_t src_i;
|
||||
|
||||
dest_len = min(ft_strlen(dest), size);
|
||||
if (dest_len < size - 1)
|
||||
{
|
||||
dest_i = dest_len;
|
||||
src_i = 0;
|
||||
while (src[src_i] && size > 0 && dest_i < size - 1)
|
||||
{
|
||||
dest[dest_i] = src[src_i];
|
||||
src_i++;
|
||||
dest_i++;
|
||||
}
|
||||
if (size > 0)
|
||||
dest[dest_i] = '\0';
|
||||
}
|
||||
return (dest_len + ft_strlen(src));
|
||||
}
|
30
libft/ft_strlcpy.c
Normal file
30
libft/ft_strlcpy.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/25 20:39:39 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/09 11:44:13 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
size_t ft_strlcpy(char *dest, const char *src, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (src[i] && size > 0 && i < size - 1)
|
||||
{
|
||||
dest[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
if (i < size)
|
||||
dest[i] = '\0';
|
||||
while (src[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
23
libft/ft_strlen.c
Normal file
23
libft/ft_strlen.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlen.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/24 13:10:16 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/10 14:09:44 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
size_t ft_strlen(const char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
32
libft/ft_strmapi.c
Normal file
32
libft/ft_strmapi.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strmapi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 11:08:18 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/03 15:20:49 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
|
||||
{
|
||||
char *dest;
|
||||
unsigned int i;
|
||||
|
||||
dest = malloc((ft_strlen(s) + 1) * sizeof(char));
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
dest[i] = (*f)(i, s[i]);
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
25
libft/ft_strncmp.c
Normal file
25
libft/ft_strncmp.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strncmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/10 12:21:33 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/10 14:39:03 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_strncmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
if (n == 0)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (i < n - 1 && s1[i] && s1[i] == s2[i])
|
||||
i++;
|
||||
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
|
||||
}
|
47
libft/ft_strnstr.c
Normal file
47
libft/ft_strnstr.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strnstr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 10:50:23 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/09 16:09:21 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
// Return 1 if there is 'little' at the pos 'i' of 'big', or else 0.
|
||||
static int match(const char *big, const char *little, size_t len, size_t i)
|
||||
{
|
||||
int j;
|
||||
|
||||
j = 0;
|
||||
while (little[j])
|
||||
{
|
||||
if (len <= i || big[i] != little[j])
|
||||
return (0);
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
char *ft_strnstr(const char *big, const char *little, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!little[0])
|
||||
return ((char *)big);
|
||||
if (!big[i] || i >= len)
|
||||
return (NULL);
|
||||
while (!match(big, little, len, i))
|
||||
{
|
||||
i++;
|
||||
if (!big[i] || i >= len)
|
||||
return (NULL);
|
||||
}
|
||||
return ((char *)big + i);
|
||||
}
|
31
libft/ft_strrchr.c
Normal file
31
libft/ft_strrchr.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strrchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 10:43:17 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/10 13:16:58 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
char *ft_strrchr(const char *s, int c)
|
||||
{
|
||||
char *r;
|
||||
int i;
|
||||
|
||||
r = NULL;
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == c)
|
||||
r = (char *)s + i;
|
||||
i++;
|
||||
}
|
||||
if (s[i] == c)
|
||||
r = (char *)s + i;
|
||||
return (r);
|
||||
}
|
80
libft/ft_strtrim.c
Normal file
80
libft/ft_strtrim.c
Normal file
|
@ -0,0 +1,80 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strtrim.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 11:39:33 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/04 13:07:54 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "libft.h"
|
||||
|
||||
static int is_in(char const c, char const *set)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (set[i])
|
||||
{
|
||||
if (set[i] == c)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static size_t get_length(char const *s1, char const *set)
|
||||
{
|
||||
size_t i;
|
||||
size_t r;
|
||||
size_t tmp;
|
||||
|
||||
i = 0;
|
||||
while (is_in(s1[i], set))
|
||||
i++;
|
||||
r = 0;
|
||||
tmp = 0;
|
||||
while (s1[i])
|
||||
{
|
||||
if (is_in(s1[i], set))
|
||||
{
|
||||
tmp++;
|
||||
}
|
||||
else
|
||||
{
|
||||
r += tmp;
|
||||
tmp = 0;
|
||||
r++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (r);
|
||||
}
|
||||
|
||||
char *ft_strtrim(char const *s1, char const *set)
|
||||
{
|
||||
size_t len;
|
||||
char *dest;
|
||||
size_t start;
|
||||
size_t i;
|
||||
|
||||
len = get_length(s1, set);
|
||||
dest = malloc((len + 1) * sizeof(char));
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
start = 0;
|
||||
while (is_in(s1[start], set))
|
||||
start++;
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
dest[i] = s1[start + i];
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
41
libft/ft_substr.c
Normal file
41
libft/ft_substr.c
Normal file
|
@ -0,0 +1,41 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_substr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/03 11:17:12 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/09 13:27:30 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
char *dest;
|
||||
size_t s_len;
|
||||
size_t dest_len;
|
||||
size_t i;
|
||||
|
||||
s_len = ft_strlen(s);
|
||||
if (s_len < start)
|
||||
dest_len = 0;
|
||||
else if (s_len < start + len)
|
||||
dest_len = s_len - start;
|
||||
else
|
||||
dest_len = len;
|
||||
dest = malloc((dest_len + 1) * sizeof(char));
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i < dest_len)
|
||||
{
|
||||
dest[i] = s[start + i];
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
18
libft/ft_tolower.c
Normal file
18
libft/ft_tolower.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_tolower.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/02 16:53:06 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/02 16:58:02 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_tolower(int c)
|
||||
{
|
||||
if (c >= 'A' && c <= 'Z')
|
||||
return (c - 'A' + 'a');
|
||||
return (c);
|
||||
}
|
18
libft/ft_toupper.c
Normal file
18
libft/ft_toupper.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_toupper.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/02 16:53:06 by mcolonna #+# #+# */
|
||||
/* Updated: 2023/10/02 16:56:52 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_toupper(int c)
|
||||
{
|
||||
if (c >= 'a' && c <= 'z')
|
||||
return (c - 'a' + 'A');
|
||||
return (c);
|
||||
}
|
153
libft/libft.h
Normal file
153
libft/libft.h
Normal file
|
@ -0,0 +1,153 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* libft.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/02 11:46:15 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/04/24 13:16:41 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBFT_H
|
||||
# define LIBFT_H
|
||||
|
||||
# include <stddef.h>
|
||||
# include <limits.h>
|
||||
|
||||
///// CHAR /////
|
||||
|
||||
// (In libc) Return 1 if the char 'c' is a specific type of char, or else 0.
|
||||
int ft_isalpha(int c); // A-Za-z
|
||||
int ft_isdigit(int c); // 0-9
|
||||
int ft_isalnum(int c); // A-Za-z0-9
|
||||
int ft_isascii(int c); // (0)-(127)
|
||||
int ft_isprint(int c); // (32)-(126)
|
||||
|
||||
// (In libc) Return the uppercase/lowercase version of the letter,
|
||||
// or the char itself if it's not a letter.
|
||||
int ft_toupper(int c);
|
||||
int ft_tolower(int c);
|
||||
|
||||
///// STR /////
|
||||
|
||||
// (In libc) Compare 2 strings until n
|
||||
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
||||
|
||||
// (In libc) Return the length of the string 's'.
|
||||
size_t ft_strlen(const char *s);
|
||||
|
||||
// (In libc) Copy/Append src on dst.
|
||||
// 'dst' will be 'size' chars max, including \0,
|
||||
// except if 'dst' is already longer for strlcat().
|
||||
// 1. Never do more than size.
|
||||
// 2. Always end by \0, except if it doesn't respect 1.
|
||||
size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
||||
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
||||
|
||||
// (In libc) Return a pointer to the first of last char 'c' in 's',
|
||||
// or return NULL if there aren't any.
|
||||
char *ft_strchr(const char *s, int c); // First char.
|
||||
char *ft_strrchr(const char *s, int c); // Last char.
|
||||
|
||||
// (In libc) Return a pointer to the first occurence
|
||||
// of string 'little' in string 'big'.
|
||||
// Don't look chars after 'size'.
|
||||
char *ft_strnstr(const char *big, const char *little, size_t len);
|
||||
|
||||
// (In libc) Return an malloced duplicate of 's'.
|
||||
// Uses: malloc
|
||||
// Error: return NULL
|
||||
char *ft_strdup(const char *s);
|
||||
|
||||
// (Perso) Apply 'f' on each char of the string 's' and returns the result.
|
||||
// Uses: malloc
|
||||
// Error: return NULL
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
|
||||
// (Perso) Call 'f' for each char of 's'.
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char *));
|
||||
|
||||
// (Perso) Return the substring of 's' which starts at 'start'
|
||||
// and is of length 'len'.
|
||||
// Don't take chars after the end of the string.
|
||||
// Uses: malloc
|
||||
// Error: return NULL
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||
|
||||
// (Perso) Concatenate the two strings.
|
||||
// Uses: malloc
|
||||
// Error: return NULL
|
||||
char *ft_strjoin(char const *s1, char const *s2);
|
||||
|
||||
// (Perso) Remove the characters 'set' from the beginning
|
||||
// and the end of the string.
|
||||
// Uses: malloc
|
||||
// Error: return NULL
|
||||
char *ft_strtrim(char const *s1, char const *set);
|
||||
|
||||
// (Perso) Split 's' by the chars 'c'.
|
||||
// Return an array ended by NULL.
|
||||
// Uses: malloc
|
||||
// Error: return NULL
|
||||
char **ft_split(char const *s, char c);
|
||||
|
||||
///// MEM /////
|
||||
|
||||
// (In libc) Fills 's' with 'n' times the char 'c'.
|
||||
// Return 'c'.
|
||||
void *ft_memset(void *s, int c, size_t n);
|
||||
|
||||
// (In libc) Just ft_memset(s, '\0', n) lol.
|
||||
void ft_bzero(void *s, size_t n);
|
||||
|
||||
// (In libc) Copy 'n' bytes from 'src' to 'dest'.
|
||||
// The 'src' and 'dest' areas mustn't overlap.
|
||||
// Return 'dest'.
|
||||
void *ft_memcpy(void *dest, const void *src, size_t n);
|
||||
|
||||
// (In libc) Same, but the areas can overlap.
|
||||
// Return 'dest'.
|
||||
void *ft_memmove(void *dest, const void *src, size_t n);
|
||||
|
||||
// (In libc) Find the character 'c' in 's', 's' being of length 'n'.
|
||||
// NULL if none.
|
||||
void *ft_memchr(const void *s, int c, size_t n);
|
||||
|
||||
// (In libc) Compare 'n' bytes of 's1' and 's2'.
|
||||
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
||||
|
||||
// (In libc) Alloc an array of 'size' elements with each element
|
||||
// being of size 'size'.
|
||||
// Uses: malloc
|
||||
// Error: return NULL
|
||||
void *ft_calloc(size_t nmemb, size_t size);
|
||||
|
||||
///// CAST /////
|
||||
|
||||
// (In libc) Cast a string to an int.
|
||||
// /[ \f\n\r\t\v]*[-+]?[0-9]*.*/
|
||||
int ft_atoi(const char *nptr);
|
||||
|
||||
// (Perso) Cast an int to a string.
|
||||
// Uses: malloc
|
||||
// Error: return NULL
|
||||
char *ft_itoa(int n);
|
||||
|
||||
///// WRITE /////
|
||||
|
||||
// (Perso) Write the char/string on the file descriptor 'fd'.
|
||||
// Uses: write
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
|
||||
// (Perso) Write the string 's' followed by a newline.
|
||||
// Uses: write
|
||||
void ft_putendl_fd(char *s, int fd);
|
||||
|
||||
// (Perso) Write the number 'n'.
|
||||
// Uses: write
|
||||
void ft_putnbr_fd(int n, int fd);
|
||||
|
||||
#endif
|
BIN
minishell
Executable file
BIN
minishell
Executable file
Binary file not shown.
|
@ -6,13 +6,13 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/23 15:03:23 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/04/23 15:56:21 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/04/24 13:19:42 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "include.h"
|
||||
|
||||
const char *ask_command(t_memclass mc)
|
||||
const char *ask_command(const t_memclass mc)
|
||||
{
|
||||
const char *r;
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/23 15:51:56 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/04/23 15:56:49 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/04/24 13:20:31 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
50
src/main.c
50
src/main.c
|
@ -6,27 +6,71 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/04/23 16:00:53 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/04/24 13:24:10 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "include.h"
|
||||
|
||||
int main(void)
|
||||
// Clean the screen (TODO)
|
||||
static void execclear(void)
|
||||
{
|
||||
}
|
||||
|
||||
// Print hi :D
|
||||
static void print_hi(void)
|
||||
{
|
||||
printf("Welcome to____________________________________\n");
|
||||
printf("|| ___ ___ ||\n");
|
||||
printf("|| | \\ / | ||\n");
|
||||
printf("|| | |\\ \\ / /| | __ __ __ __ ||\n");
|
||||
printf("|| | | \\ \\ / / | | |__| ||\\ || |__| ||\n");
|
||||
printf("|| | | \\ \\/ / | | __ || \\ || __ ||\n");
|
||||
printf("|| | | \\ / | | || || \\ || || ||\n");
|
||||
printf("|| | | \\/ | | || || \\ || || ||\n");
|
||||
printf("|| | | | | || || \\ || || ||\n");
|
||||
printf("|| |_| |_| || || \\|| || ||\n");
|
||||
printf("|| _____ ||\n");
|
||||
printf("|| / ___| By: Mylan COLONNA ||\n");
|
||||
printf("|| \\ \\ And: Joris SCHAFT ||\n");
|
||||
printf("|| \\ \\ ||\n");
|
||||
printf("|| \\ \\ __ __ __ __ __ ||\n");
|
||||
printf("|| \\ \\ || || ||=== || || ||\n");
|
||||
printf("|| \\ \\ || || || || || ||\n");
|
||||
printf("|| / / ||===|| ||=== || || ||\n");
|
||||
printf("|| ___/ / || || || || || ||\n");
|
||||
printf("|| |____/ || || ||=== ||=== ||=== ||\n");
|
||||
printf("|_____________________________________________|\n\n");
|
||||
}
|
||||
|
||||
int main(const int argc, const char *argv[], const char *envp[])
|
||||
{
|
||||
const char **path = get_path(envp);
|
||||
t_memclass mc;
|
||||
const char *command_str;
|
||||
t_command command;
|
||||
int errorstatus;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
(void)path;
|
||||
(void)command;
|
||||
execclear();
|
||||
print_hi();
|
||||
errorstatus = 0;
|
||||
while (true)
|
||||
{
|
||||
mc = mem_newclass(minishell_error);
|
||||
command = parse_command(ask_command(mc));
|
||||
command_str = ask_command(mc);
|
||||
print_str(minishell_error, 1, "command: ");
|
||||
print_line(minishell_error, 1, command_str);
|
||||
/*
|
||||
command = parse_command(command_str);
|
||||
if (command.error)
|
||||
continue ;
|
||||
errorstatus = execute_command(command);
|
||||
mem_freeall(mc);
|
||||
*/
|
||||
}
|
||||
return (errorstatus);
|
||||
}
|
||||
|
|
28
src/path.c
Normal file
28
src/path.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* path.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
|
||||
/* Updated: 2024/04/24 13:26:41 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "include.h"
|
||||
|
||||
const char **get_path(const char **envp)
|
||||
{
|
||||
int i;
|
||||
char **re;
|
||||
char *path;
|
||||
|
||||
i = 0;
|
||||
while (ft_strncmp(envp[i], "PATH=", 5) != 0)
|
||||
i++;
|
||||
path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5);
|
||||
re = ft_split(path, ':');
|
||||
free(path);
|
||||
return ((const char **)re);
|
||||
}
|
Loading…
Add table
Reference in a new issue