Compare commits

..

No commits in common. "8371b013907104bafacc88d8565fc63af843a9b8" and "958448ca4e8db37d4ba6e93629ce5d0c1aa3b253" have entirely different histories.

6 changed files with 120 additions and 4 deletions

View File

@ -1,5 +1,8 @@
SRCS = parsing.c \ SRCS = parsing.c \
utils/ft_isnum.c \ utils/ft_isnum.c \
utils/putchar.c \
utils/putnum.c \
utils/putstr.c \
utils/ft_atoi.c \ utils/ft_atoi.c \
main.c \ main.c \
time.c \ time.c \

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */ /* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */
/* Updated: 2023/05/17 13:22:19 by cchauvet ### ########.fr */ /* Updated: 2023/04/27 11:43:42 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,7 +30,12 @@ static void print(t_data *data, size_t id, char *str)
if (stop == false) if (stop == false)
{ {
time = get_time(); time = get_time();
printf("%zu %zu %s\n", time, id, str); ft_putnum(time, 7);
ft_putchar(' ');
ft_putnum(id + 1, 3);
ft_putchar(' ');
ft_putstr(str);
ft_putchar('\n');
} }
pthread_mutex_unlock(&data->print_mutex); pthread_mutex_unlock(&data->print_mutex);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:41:14 by cchauvet #+# #+# */ /* Created: 2023/04/27 11:41:14 by cchauvet #+# #+# */
/* Updated: 2023/05/17 13:21:33 by cchauvet ### ########.fr */ /* Updated: 2023/04/27 11:41:46 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,6 +22,11 @@ void print_died(t_philo *philo)
data = philo->data; data = philo->data;
time = get_time(); time = get_time();
pthread_mutex_lock(&data->print_mutex); pthread_mutex_lock(&data->print_mutex);
printf("%7zu %3zu %s\n", time, philo->id, "died"); ft_putnum(time, 7);
ft_putchar(' ');
ft_putnum(philo->id + 1, 3);
ft_putchar(' ');
ft_putstr("died");
ft_putchar('\n');
pthread_mutex_unlock(&data->print_mutex); pthread_mutex_unlock(&data->print_mutex);
} }

18
utils/putchar.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:28:09 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:28:10 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}

59
utils/putnum.c Normal file
View File

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:28:00 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:28:01 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include "utils.h"
static size_t get_size(size_t num)
{
size_t nb_digit;
size_t rest;
nb_digit = 0;
if (num == 0)
nb_digit++;
rest = num;
while (rest != 0)
{
nb_digit++;
rest = rest / 10;
}
return (nb_digit);
}
static void ft_putdigit(size_t num)
{
if (num > 9)
{
ft_putdigit(num / 10);
ft_putchar(num % 10 + 48);
}
else
ft_putchar(num + 48);
}
void ft_putnum(size_t num, size_t padding)
{
size_t size;
size_t i;
i = 0;
size = get_size(num);
while (i < padding - size)
{
ft_putchar('0');
i++;
}
ft_putdigit(num);
}

26
utils/putstr.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:28:26 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:28:27 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
#include <stddef.h>
void ft_putstr(char *str)
{
size_t i;
i = 0;
while (str[i] != 0)
{
ft_putchar(str[i]);
i++;
}
}