-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
121 lines (109 loc) · 2.57 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alajara- <alajara-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 21:59:00 by alajara- #+# #+# */
/* Updated: 2023/08/02 19:35:54 by alajara- ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static int ft_getlen(int sig, int len)
{
len <<= 1;
if (sig == SIGUSR2)
len += 1;
return (len);
}
static unsigned char *ft_setstr(int len)
{
unsigned char *s;
s = ft_calloc(sizeof(unsigned char) * len, sizeof(char));
if (!s)
exit(ft_error(3));
return (s);
}
void ft_constructbit(int sig, siginfo_t *info, void *context)
{
static int sig_cnt = 0;
static int len = 0;
static unsigned char *s;
(void)context;
if (++sig_cnt <= 32)
len = ft_getlen(sig, len);
if (sig_cnt == 32)
s = ft_setstr(++len);
if (sig_cnt > 32)
{
s[(sig_cnt - 33) / 8] <<= 1;
if (sig == SIGUSR2)
s[(sig_cnt - 33) / 8] += 1;
}
if (sig_cnt == 32 + (len * 8))
{
ft_printf("%s\n", s);
free(s);
sig_cnt = 0;
kill(info->si_pid, SIGUSR2);
}
}
int main(void)
{
struct sigaction act;
int id;
act.sa_sigaction = ft_constructbit;
id = getpid();
ft_printf("%d\n", id);
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
while (42)
{
sigaction(SIGUSR1, &act, NULL);
sigaction(SIGUSR2, &act, NULL);
pause();
}
return (0);
}
/*
void ft_constructbit(int sig)
{
static int sig_cnt = 0;
static int len = 0;
static unsigned char *s;
if (++sig_cnt <= 32)
len = ft_getlen(sig, len);
if (sig_cnt == 32)
{
s = ft_calloc(sizeof(unsigned char) * ++len, sizeof(char));
if (!s)
exit(ft_error(3));
}
if (sig_cnt > 32)
{
s[(sig_cnt - 33) / 8] <<= 1;
if (sig == SIGUSR2)
s[(sig_cnt - 33) / 8] += 1;
}
if (sig_cnt == 32 + (len * 8))
{
ft_printf("%s\n", s);
free(s);
sig_cnt = 0;
}
}
int main(void)
{
int id;
id = getpid();
ft_printf("%d\n", id);
while (42)
{
signal(SIGUSR1, ft_constructbit);
signal(SIGUSR2, ft_constructbit);
pause();
}
return (0);
}
*/