-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
73 lines (66 loc) · 1.77 KB
/
Copy pathserver.c
File metadata and controls
73 lines (66 loc) · 1.77 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybounite <ybounite@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 15:49:31 by ybounite #+# #+# */
/* Updated: 2025/02/18 09:54:08 by ybounite ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
pid_t g_client;
void write_message(char c, int *bit)
{
*bit = 0;
write(1, &c, 1);
}
void handler_signal(int sigum, siginfo_t *siginfo, void *walo)
{
static char c;
static int bit;
(void)walo;
if (siginfo->si_pid != g_client)
{
c = 0;
bit = 0;
}
if (sigum == SIGUSR1)
{
bit++;
c = c << 1 | 1;
}
else if (sigum == SIGUSR2)
{
bit++;
c = c << 1;
}
if (bit == 8)
{
write_message(c, &bit);
c = 0;
}
g_client = siginfo->si_pid;
}
int main(int ac, char **av)
{
struct sigaction sa;
pid_t server_pid;
(void)av;
g_client = 0;
if (ac != 1)
return (1);
server_pid = getpid();
sa.sa_sigaction = handler_signal;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
ft_printf("%sSERVER PID: %s%d\n", GREEN, WHITE, server_pid);
if (sigaction(SIGUSR1, &sa, NULL) == -1
|| sigaction(SIGUSR2, &sa, NULL) == -1)
return (1);
while (1)
{
}
return (0);
}