-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_bonus.c
73 lines (67 loc) · 1.77 KB
/
client_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gvardaki <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/02 09:23:02 by gvardaki #+# #+# */
/* Updated: 2023/10/02 10:39:27 by gvardaki ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void ft_send_sig(int pid, char c)
{
int ascii;
int bin[8];
int i;
ascii = (int)c;
i = 7;
while (i >= 0)
{
bin[i] = (ascii >> i) & 1;
i--;
}
i = 8;
while (--i >= 0)
{
if (bin[i] == 0)
kill(pid, SIGUSR1);
else if (bin[i] == 1)
kill(pid, SIGUSR2);
usleep(50);
}
}
void handler(int num, siginfo_t *info, void *context)
{
(void)info;
(void)context;
if (num == SIGUSR2)
write(1, "Message fully received\n", 23);
exit(1);
}
int main(int ac, char **av)
{
int pid;
struct sigaction sa;
sa.sa_sigaction = handler;
sa.sa_flags = SA_SIGINFO;
if (ac != 3)
write(1, "Arguments error\n", 16);
else
{
pid = ft_atoi(av[1]);
if (pid < 0 || ft_strlen(av[1]) > 5)
{
write(1, "Wrong PID\n", 10);
exit(1);
}
sigaction(SIGUSR2, &sa, NULL);
while (*av[2])
ft_send_sig(pid, *av[2]++);
ft_send_sig(pid, '\0');
}
while (1)
pause();
return (0);
}