-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport_functions.c
77 lines (69 loc) · 1.85 KB
/
support_functions.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* support_functions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: llawrenc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/03 11:06:03 by llawrenc #+# #+# */
/* Updated: 2022/01/03 15:35:29 by llawrenc ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void output_pid(int pid)
{
ft_putstr_fd("The server is running. Its PID: ", 1);
ft_putnbr_fd(pid, 1);
ft_putstr_fd("\n", 1);
}
void output_sent_char(char *str)
{
ft_putstr_fd("\nSent:\t\t", 1);
ft_putnbr_fd(ft_strlen(str), 1);
ft_putstr_fd("\n", 1);
}
void send_signal(int pid, unsigned char ch)
{
uint8_t i;
i = 128;
while (i)
{
if (ch & i)
kill(pid, SIGUSR2);
else
kill(pid, SIGUSR1);
i >>= 1;
pause();
usleep(100);
}
}
static void final_signal(pid_t client_pid)
{
kill(client_pid, SIGUSR2);
write(1, "\n", 1);
return ;
}
void output_str(int signal, siginfo_t *siginfo, void *context)
{
static unsigned char ch;
static int i;
static pid_t client_pid;
(void)context;
if (client_pid != siginfo->si_pid)
{
client_pid = siginfo->si_pid;
i = 0;
}
ch <<= 1;
ch |= (signal == SIGUSR2);
if (++i == 8)
{
if (!ch)
final_signal(client_pid);
write(1, &ch, 1);
i = 0;
ch = 0;
}
usleep(100);
kill(client_pid, SIGUSR1);
}