-
Notifications
You must be signed in to change notification settings - Fork 0
/
minitalk_utils_bonus.c
91 lines (82 loc) · 1.75 KB
/
minitalk_utils_bonus.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minitalk_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amait-ou <amait-ou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/31 22:45:52 by amait-ou #+# #+# */
/* Updated: 2022/12/31 22:51:01 by amait-ou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *s)
{
while (*s)
{
ft_putchar(*s);
++s;
}
}
int ft_power(int nb, int power)
{
int r;
if (power < 0)
return (0);
r = 1;
while (power-- > 0)
r *= nb;
return (r);
}
void ft_putnbr(int n)
{
char a;
char b;
if (n >= 0 && n <= 9)
{
a = n + 48 ;
write(1, &a, 1);
}
else if (n == -2147483648)
{
write(1, "-2147483648", 11);
}
else if (n < 0)
{
b = '-';
write(1, &b, 1);
ft_putnbr(n * (-1));
}
else
{
ft_putnbr(n / 10);
ft_putnbr(n % 10);
}
}
void ft_sender(pid_t pid, char c)
{
int bits;
int kill_status;
bits = 0b10000000;
while (bits)
{
if (c & bits)
{
kill_status = kill(pid, SIGUSR2);
if (kill_status < 0)
return ;
}
else
{
kill_status = kill(pid, SIGUSR1);
if (kill_status < 0)
return ;
}
bits >>= 1;
usleep(250);
}
}