protection segfault creation zero philosophs + deplacement fichiers dans philo + ajout exeption philo folder dans gitignore
This commit is contained in:
62
philo/Makefile
Normal file
62
philo/Makefile
Normal file
@@ -0,0 +1,62 @@
|
||||
NAME = philo
|
||||
|
||||
CC = clang
|
||||
|
||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3 # del g3
|
||||
|
||||
VPATH = $(DIR_SRCS)
|
||||
DIR_SRCS = srcs
|
||||
|
||||
INCLUDES = -I$(HEADERS_D)
|
||||
|
||||
HEADERS_D = ./headers
|
||||
HEADERS = philo.h \
|
||||
philo_struct.h \
|
||||
philo_proto.h \
|
||||
philo_macro.h
|
||||
|
||||
LIBS = -lpthread
|
||||
|
||||
SRCS = main.c \
|
||||
init.c \
|
||||
launch.c \
|
||||
exec.c \
|
||||
utils.c \
|
||||
generic.c
|
||||
|
||||
DIR_OBJS = builds
|
||||
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||
|
||||
# --------------------
|
||||
# ------ RULES -------
|
||||
# --------------------
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(DIR_OBJS)/%.o: %.c | $(DIR_OBJS)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(DIR_OBJS):
|
||||
mkdir $@
|
||||
|
||||
$(OBJS): $(HEADERS:%=$(HEADERS_D)/%)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
$(CC) $(OBJS) -o $(NAME) $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
valgrind: $(NAME)
|
||||
valgrind --leak-check=full --leak-resolution=low --show-reachable=yes ./$(NAME)
|
||||
|
||||
run: $(NAME)
|
||||
./$(NAME)
|
||||
|
||||
.PHONY : all clean fclean re bonus subsystem run valgrind
|
||||
|
||||
27
philo/headers/philo.h
Normal file
27
philo/headers/philo.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:27:01 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:27:02 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
# include <sys/time.h>
|
||||
|
||||
# include "philo_struct.h"
|
||||
# include "philo_proto.h"
|
||||
# include "philo_macro.h"
|
||||
|
||||
#endif
|
||||
36
philo/headers/philo_macro.h
Normal file
36
philo/headers/philo_macro.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo_macro.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:27:08 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:27:09 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_MACRO_H
|
||||
# define PHILO_MACRO_H
|
||||
|
||||
# define GRAY "\e[0;30m"
|
||||
# define RED "\e[0;31m"
|
||||
# define GREEN "\e[0;32m"
|
||||
# define YELLOW "\e[0;33m"
|
||||
# define BLUE "\e[0;34m"
|
||||
# define PURPLE "\e[0;35m"
|
||||
# define CYAN "\e[0;36m"
|
||||
# define WHITE "\e[0;37m"
|
||||
|
||||
# define B_GRAY "\e[1;30m"
|
||||
# define B_RED "\e[1;31m"
|
||||
# define B_GREEN "\e[1;32m"
|
||||
# define B_YELLOW "\e[1;33m"
|
||||
# define B_BLUE "\e[1;34m"
|
||||
# define B_PURPLE "\e[1;35m"
|
||||
# define B_CYAN "\e[1;36m"
|
||||
# define B_WHITE "\e[1;37m"
|
||||
|
||||
# define RESET "\e[0m"
|
||||
|
||||
#endif
|
||||
38
philo/headers/philo_proto.h
Normal file
38
philo/headers/philo_proto.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo_proto.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:27:13 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 17:50:11 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_PROTO_H
|
||||
# define PHILO_PROTO_H
|
||||
|
||||
// init.c
|
||||
t_philo *init(int ac, char **av, pthread_t **id);
|
||||
|
||||
// launch.c
|
||||
void launch(t_philo *philo, pthread_t *id);
|
||||
|
||||
// exec.c
|
||||
void *philo_exec(void *arg);
|
||||
|
||||
// generic.c
|
||||
void init_time(t_philo *philo);
|
||||
void update_time(t_philo *philo);
|
||||
int diff_time(t_time *old, struct timeval *new);
|
||||
int print_message(t_philo *philo, char *clr, char *msg);
|
||||
|
||||
// utils.c
|
||||
int ft_isdigit_2d_arr(char **str);
|
||||
size_t ft_strlen(char *str);
|
||||
int ft_strncmp(char *s1, char *s2, size_t n);
|
||||
int ft_int_overflow(char *str);
|
||||
int ft_atoi(const char *str);
|
||||
|
||||
#endif
|
||||
52
philo/headers/philo_struct.h
Normal file
52
philo/headers/philo_struct.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo_struct.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:27:23 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:29:09 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_STRUCT_H
|
||||
# define PHILO_STRUCT_H
|
||||
|
||||
typedef pthread_mutex_t t_mtx;
|
||||
|
||||
typedef struct s_time
|
||||
{
|
||||
long int ts;
|
||||
long int tu;
|
||||
} t_time;
|
||||
|
||||
typedef struct s_params
|
||||
{
|
||||
int n_phi;
|
||||
int t_die;
|
||||
int t_eat;
|
||||
int t_slp;
|
||||
int n_eat;
|
||||
} t_params;
|
||||
|
||||
typedef struct s_global
|
||||
{
|
||||
int stop;
|
||||
int satiated_count;
|
||||
t_time t_start;
|
||||
t_mtx m_print;
|
||||
} t_global;
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
t_params *params;
|
||||
t_global *global;
|
||||
int p_nbr;
|
||||
t_mtx m_fork;
|
||||
t_time t_last_meal;
|
||||
int eat_count;
|
||||
struct s_philo *next;
|
||||
} t_philo;
|
||||
|
||||
#endif
|
||||
73
philo/srcs/exec.c
Normal file
73
philo/srcs/exec.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exec.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 18:51:44 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
static void action_delay(t_philo *philo, int action_time)
|
||||
{
|
||||
struct timeval stime;
|
||||
int death_time;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
death_time = philo->params->t_die - diff_time(&philo->t_last_meal, &stime);
|
||||
if (death_time > action_time)
|
||||
usleep(action_time * 1000);
|
||||
else if (death_time > 0)
|
||||
usleep(philo->params->t_die * 1000);
|
||||
}
|
||||
|
||||
static int ret_err_unlock(t_philo *philo, int nbr_fork)
|
||||
{
|
||||
pthread_mutex_unlock(&philo->m_fork);
|
||||
if (nbr_fork == 2)
|
||||
pthread_mutex_unlock(&philo->next->m_fork);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int eat(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_fork));
|
||||
if (print_message(philo, WHITE, "has taken a fork"))
|
||||
return (ret_err_unlock(philo, 1));
|
||||
pthread_mutex_lock(&(philo->next->m_fork));
|
||||
if (print_message(philo, WHITE, "has taken a fork"))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
update_time(philo);
|
||||
if (print_message(philo, B_YELLOW, "is eating"))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
action_delay(philo, philo->params->t_eat);
|
||||
philo->eat_count++;
|
||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||
pthread_mutex_unlock(&(philo->m_fork));
|
||||
return (0);
|
||||
}
|
||||
|
||||
void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo *)arg;
|
||||
init_time(philo);
|
||||
if (philo->p_nbr % 2 == 0)
|
||||
usleep(10 * 1000);
|
||||
while (1)
|
||||
{
|
||||
if (eat(philo) != 0)
|
||||
break ;
|
||||
if (print_message(philo, B_BLUE, "is sleeping"))
|
||||
break ;
|
||||
action_delay(philo, philo->params->t_slp);
|
||||
if (print_message(philo, B_GREEN, "is thinking"))
|
||||
break ;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
60
philo/srcs/generic.c
Normal file
60
philo/srcs/generic.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* generic.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 16:29:10 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
void init_time(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
|
||||
if (philo->global->t_start.ts == 0)
|
||||
{
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->global->t_start.ts = stime.tv_sec;
|
||||
philo->global->t_start.tu = stime.tv_usec;
|
||||
}
|
||||
philo->t_last_meal.ts = philo->global->t_start.ts;
|
||||
philo->t_last_meal.tu = philo->global->t_start.tu;
|
||||
}
|
||||
|
||||
void update_time(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->t_last_meal.ts = stime.tv_sec;
|
||||
philo->t_last_meal.tu = stime.tv_usec;
|
||||
}
|
||||
|
||||
int diff_time(t_time *old, struct timeval *new)
|
||||
{
|
||||
return ((new->tv_sec - old->ts) * 1000 + (new->tv_usec - old->tu) / 1000);
|
||||
}
|
||||
|
||||
int print_message(t_philo *philo, char *clr, char *msg)
|
||||
{
|
||||
long int time_stamp;
|
||||
struct timeval stime;
|
||||
|
||||
pthread_mutex_lock(&(philo->global->m_print));
|
||||
if (philo->global->stop)
|
||||
{
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
}
|
||||
gettimeofday(&stime, NULL);
|
||||
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
|
||||
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
|
||||
printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (0);
|
||||
}
|
||||
122
philo/srcs/init.c
Normal file
122
philo/srcs/init.c
Normal file
@@ -0,0 +1,122 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* init.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/27 15:01:30 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
static t_philo *lst_add_philo(t_params *params, t_global *global, int i)
|
||||
{
|
||||
t_philo *new;
|
||||
|
||||
new = malloc(sizeof(t_philo) * 1);
|
||||
if (new == NULL)
|
||||
return (NULL);
|
||||
new->params = params;
|
||||
new->global = global;
|
||||
new->p_nbr = i + 1;
|
||||
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||
return (NULL);
|
||||
new->t_last_meal.ts = 0;
|
||||
new->t_last_meal.tu = 0;
|
||||
new->eat_count = 0;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
||||
static t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_philo *tmp;
|
||||
t_philo *start;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
philo = NULL;
|
||||
while (i < params->n_phi)
|
||||
{
|
||||
tmp = lst_add_philo(params, global, i);
|
||||
if (philo)
|
||||
philo->next = tmp;
|
||||
else
|
||||
start = tmp;
|
||||
philo = tmp;
|
||||
i++;
|
||||
}
|
||||
if (philo)
|
||||
philo->next = start;
|
||||
return (philo);
|
||||
}
|
||||
|
||||
static t_params *init_params(int ac, char **av)
|
||||
{
|
||||
t_params *params;
|
||||
int i;
|
||||
|
||||
if (ac < 5 || ac > 6)
|
||||
return (NULL);
|
||||
if (!ft_isdigit_2d_arr(av + 1))
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (av[i])
|
||||
{
|
||||
if (ft_int_overflow(av[i]))
|
||||
return (NULL);
|
||||
i++;
|
||||
}
|
||||
params = malloc(sizeof(t_params));
|
||||
if (!params)
|
||||
return (NULL);
|
||||
params->n_phi = ft_atoi(av[1]);
|
||||
params->t_die = ft_atoi(av[2]);
|
||||
params->t_eat = ft_atoi(av[3]);
|
||||
params->t_slp = ft_atoi(av[4]);
|
||||
params->n_eat = -1;
|
||||
if (ac == 6)
|
||||
params->n_eat = ft_atoi(av[5]);
|
||||
return (params);
|
||||
}
|
||||
|
||||
static t_global *init_global(void)
|
||||
{
|
||||
t_global *global;
|
||||
|
||||
global = malloc(sizeof(t_global));
|
||||
if (!global)
|
||||
return (NULL);
|
||||
global->stop = 0;
|
||||
global->satiated_count = 0;
|
||||
global->t_start.ts = 0;
|
||||
global->t_start.tu = 0;
|
||||
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
||||
return (NULL);
|
||||
return (global);
|
||||
}
|
||||
|
||||
t_philo *init(int ac, char **av, pthread_t **id)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_params *params;
|
||||
t_global *global;
|
||||
|
||||
params = init_params(ac, av);
|
||||
if (params == NULL)
|
||||
return (NULL);
|
||||
global = init_global();
|
||||
if (params == NULL)
|
||||
return (NULL);
|
||||
*id = malloc(sizeof(pthread_t) * params->n_phi);
|
||||
if (*id == NULL)
|
||||
return (NULL);
|
||||
philo = init_chain_philo(params, global);
|
||||
if (philo == NULL)
|
||||
return (NULL);
|
||||
return (philo);
|
||||
}
|
||||
92
philo/srcs/launch.c
Normal file
92
philo/srcs/launch.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* launch.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 18:52:32 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
static int is_finished(t_philo *philo, struct timeval *stime)
|
||||
{
|
||||
long int time_stamp;
|
||||
int time;
|
||||
|
||||
if (philo->global->satiated_count == philo->params->n_phi)
|
||||
{
|
||||
philo->global->stop = 1;
|
||||
return (1);
|
||||
}
|
||||
time = diff_time(&philo->t_last_meal, stime);
|
||||
if (time >= philo->params->t_die)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->global->m_print));
|
||||
philo->global->stop = 1;
|
||||
time_stamp = (stime->tv_sec - philo->global->t_start.ts) * 1000;
|
||||
time_stamp += (stime->tv_usec - philo->global->t_start.tu) / 1000;
|
||||
printf("%s%li %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET);
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int is_satiated(t_philo *philo)
|
||||
{
|
||||
if (philo->eat_count == philo->params->n_eat)
|
||||
{
|
||||
philo->global->satiated_count++;
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void pere_fouettard(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
int i;
|
||||
int satiated;
|
||||
|
||||
while (philo->global->stop == 0)
|
||||
{
|
||||
i = 0;
|
||||
satiated = 1;
|
||||
gettimeofday(&stime, NULL);
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
if (satiated == 1 && is_satiated(philo) == 0)
|
||||
satiated = 0;
|
||||
if (is_finished(philo, &stime))
|
||||
break ;
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
usleep(1 * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
void launch(t_philo *philo, pthread_t *id)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
pere_fouettard(philo);
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_mutex_unlock(&(philo->m_fork));
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
32
philo/srcs/main.c
Normal file
32
philo/srcs/main.c
Normal file
@@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:40 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:32:30 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
pthread_t *id;
|
||||
t_philo *philo;
|
||||
int i;
|
||||
|
||||
philo = init(ac, av, &id);
|
||||
if (philo == NULL)
|
||||
return (0);
|
||||
launch(philo, id);
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_join(id[i], NULL);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
103
philo/srcs/utils.c
Normal file
103
philo/srcs/utils.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 18:04:08 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
int ft_isdigit_2d_arr(char **str)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
j = 0;
|
||||
while (str[i][j])
|
||||
{
|
||||
if (str[i][j] < '0' || str[i][j] > '9')
|
||||
return (0);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
size_t ft_strlen(char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_strncmp(char *s1, char *s2, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
int res;
|
||||
|
||||
i = 0;
|
||||
res = 0;
|
||||
while (s1[i] && s1[i] == s2[i] && i < n - 1)
|
||||
i++;
|
||||
if (n != 0)
|
||||
res = (unsigned char)s1[i] - (unsigned char)s2[i];
|
||||
return (res);
|
||||
}
|
||||
|
||||
int ft_int_overflow(char *str)
|
||||
{
|
||||
size_t len;
|
||||
char *int_xtrem;
|
||||
|
||||
int_xtrem = "2147483647";
|
||||
if (str[0] == '-')
|
||||
int_xtrem = "2147483648";
|
||||
if (str[0] == '+' || str[0] == '-')
|
||||
str++;
|
||||
len = ft_strlen(str);
|
||||
if (len < 10)
|
||||
return (0);
|
||||
else if (len > 10 || ft_strncmp(str, int_xtrem, len) > 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int i;
|
||||
int result;
|
||||
int is_negative;
|
||||
|
||||
is_negative = 0;
|
||||
result = 0;
|
||||
i = 0;
|
||||
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
|
||||
i++;
|
||||
if (str[i] == '+')
|
||||
i++;
|
||||
else if (str[i] == '-')
|
||||
{
|
||||
is_negative = 1;
|
||||
i++;
|
||||
}
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
result = (result * 10) + (str[i] - '0');
|
||||
i++;
|
||||
}
|
||||
if (is_negative)
|
||||
result = result * -1;
|
||||
return (result);
|
||||
}
|
||||
Reference in New Issue
Block a user