-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
73 lines (65 loc) · 1.89 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vsimeono <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 20:33:40 by vsimeono #+# #+# */
/* Updated: 2022/05/25 15:17:17 by vsimeono ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
long ft_atoi(const char *str)
{
int i;
long num;
int sign;
i = 0;
sign = 1;
num = 0;
while ((str[i] >= 9 && str[i] <= 13) || (str[i] == ' '))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign = sign * (-1);
i++;
}
while ((str[i] >= 48 && str[i] <= 57))
{
num = (num * 10) + (str[i] - '0');
i++;
}
return (num * sign);
}
int ft_sum(int *arr, int len)
{
int i;
int sum;
i = 0;
sum = 0;
while (i < len)
{
sum += arr[i];
i++;
}
return (sum);
}
void print(t_philosopher *philosopher, int msec, char *str)
{
pthread_mutex_lock(&(philosopher->to_game->m_print));
if (philosopher->to_game->end == -1 || ft_strncmp(str, "died", 4) == 0)
printf("%i %i %s\n", msec, philosopher->id, str);
pthread_mutex_unlock(&(philosopher->to_game->m_print));
}
int ft_strncmp(char *s1, char *s2, size_t n)
{
size_t i;
i = 0;
if (n == 0)
return (0);
while (i < n - 1 && (s1[i] == s2[i]) && s1[i] != '\0')
i++;
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}