-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
77 lines (69 loc) · 1.64 KB
/
server.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yamzil <yamzil@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/28 17:14:41 by yamzil #+# #+# */
/* Updated: 2022/01/17 20:18:24 by yamzil ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
int ft_power(int nb, int power)
{
int d;
d = nb;
if (power < 0)
return (0);
if (power == 0)
return (1);
if (power == 1)
return (nb);
power = power - 2;
while (power >= 0)
{
nb = nb * d;
power--;
}
return (nb);
}
void ft_binary(char *str)
{
int sum;
int exp;
int i;
exp = 0;
sum = 0;
i = 8;
while (i-- > 0)
{
if (str[i] == '1')
sum = sum + ft_power(2, exp);
exp += 1;
}
ft_putchar(sum);
i = 0;
}
void ft_get_bits(int signum)
{
char byte [9];
static int i = 0;
if (signum == SIGUSR1)
byte[i++] = '1';
else
byte[i++] = '0';
if (i == 8)
{
ft_binary(byte);
i = 0;
}
}
int main(void)
{
printf("SERVER'S PID: %d\n", getpid());
signal(SIGUSR1, ft_get_bits);
signal(SIGUSR2, ft_get_bits);
while (1)
pause();
}