-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
72 lines (66 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhagon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 08:42:35 by juhagon #+# #+# */
/* Updated: 2021/12/15 14:39:47 by juhagon ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
t_sig g_global;
void handler1(int sig)
{
(void)sig;
if (g_global.toggle == 0)
g_global.len++;
else
{
if (g_global.toggle == 1)
g_global.toggle = 2;
else
{
g_global.c++;
}
}
if (g_global.len <= 0 && g_global.toggle != 0)
{
g_global.message[g_global.i] = '\0';
printf("%s\n", g_global.message);
free(g_global.message);
g_global.toggle = 0;
g_global.len = 0;
g_global.i = 0;
g_global.c = 0;
}
}
void handler2(int sig)
{
(void)sig;
if (g_global.toggle == 0)
{
g_global.message = malloc(sizeof(char) * g_global.len + 2);
g_global.toggle = 2;
g_global.toggle = 1;
}
else
{
g_global.len--;
g_global.message[g_global.i] = g_global.c;
g_global.i++;
g_global.c = 0;
}
}
int main(void)
{
pid_t pid;
pid = getpid();
printf("PID %d\n", pid);
while (1)
{
signal(SIGUSR1, handler1);
signal(SIGUSR2, handler2);
}
}