133 lines
1.9 KiB
C
133 lines
1.9 KiB
C
#include "../includes/philo_header.h"
|
|
|
|
t_all *structalloc_and_bzero(void)
|
|
{
|
|
t_all *all;
|
|
|
|
all = ft_calloc(sizeof(t_all), 1);
|
|
all->number_of_philosophers = -1;
|
|
all->time_to_die = -1;
|
|
all->time_to_eat = -1;
|
|
all->time_to_sleep = -1;
|
|
all->number_of_times_each_philosopher_must_eat = -1;
|
|
return(all);
|
|
}
|
|
|
|
size_t ft_strlcpy(char *dst, const char *src, size_t size)
|
|
{
|
|
unsigned int i;
|
|
unsigned int count;
|
|
|
|
count = 0;
|
|
if (!dst && !src)
|
|
return (0);
|
|
while (src[count] != '\0')
|
|
{
|
|
++count;
|
|
}
|
|
i = 0;
|
|
if (size > 0)
|
|
{
|
|
while (src[i] != '\0' && i < (size - 1) && src[i])
|
|
{
|
|
dst[i] = src[i];
|
|
++i;
|
|
}
|
|
dst[i] = '\0';
|
|
}
|
|
return (count);
|
|
}
|
|
|
|
size_t ft_strlcat(char *dst, const char *src, size_t size)
|
|
{
|
|
unsigned int i;
|
|
unsigned int len;
|
|
|
|
len = 0;
|
|
if (!dst && !src)
|
|
return (0);
|
|
len = ft_strlen(dst);
|
|
i = 0;
|
|
if (size < len)
|
|
return (ft_strlen((char *)src) + size);
|
|
if (size)
|
|
{
|
|
while (src[i] && len + 1 < size)
|
|
dst[len++] = src[i++];
|
|
dst[len] = '\0';
|
|
}
|
|
while (src[i])
|
|
{
|
|
i++;
|
|
len++;
|
|
}
|
|
return (len);
|
|
}
|
|
|
|
|
|
int ft_strlen(char *str)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (str[i])
|
|
i++;
|
|
return(i);
|
|
}
|
|
|
|
int return_big(int a, int b)
|
|
{
|
|
if (a > b)
|
|
{
|
|
return (a);
|
|
}
|
|
return (b);
|
|
}
|
|
|
|
int return_less(int a, int b)
|
|
{
|
|
if (a < b)
|
|
{
|
|
return (a);
|
|
}
|
|
return (b);
|
|
}
|
|
|
|
void ft_exit(t_all *all)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
all->smert = 42;
|
|
ft_msleep(200);
|
|
while (i <= all->number_of_philosophers)
|
|
{
|
|
// pthread_mutex_destroy(&all->data_philo->philors[i].eat_mutex);
|
|
//pthread_mutex_destroy(&all->data_philo->mutex[i]); // TODO fix this
|
|
i++;
|
|
}
|
|
i = 0;
|
|
while (i < all->number_of_philosophers)
|
|
{
|
|
free(&all->data_philo->philors[i]);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
char *sem_set_name(char *str, int i)
|
|
{
|
|
char *temp_nbr;
|
|
char *returnstr;
|
|
|
|
temp_nbr = ft_itoa(i);
|
|
returnstr = ft_strjoin(str, temp_nbr);
|
|
free(temp_nbr);
|
|
return(returnstr);
|
|
}
|
|
|
|
sem_t *reopen_sem(const char *name, int value)
|
|
{
|
|
sem_unlink(name);
|
|
return (sem_open(name, O_RDWR | O_CREAT, S_IRWXU, value));
|
|
}
|