-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
87 lines (72 loc) · 1.44 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
# include "ipc.h"
int recursive_power(int nb, int power)
{
int res;
if (power == 0)
return (1);
else if (power < 0)
return (0);
else
{
res = nb * recursive_power(nb, power - 1);
return (res);
}
}
char *letter_to_string(char const *string, char const letter)
{
int i;
int j;
char *temp;
i = 0;
j = 0;
temp = malloc((strlen(string) + 2) * sizeof(char));
if (!temp)
return (NULL);
while (string[i])
temp[j++] = string[i++];
i = 0;
temp[j++] = letter;
temp[j] = 0;
free ((void *)(string));
return (temp);
}
void signal_handler(int signum)
{
static int bits_counter = 0;
static int char_result = 0;
static int str_len = 0;
static char *final;
if (!final)
final = strdup("");
if (signum == SIGUSR1)
char_result = char_result + 0;
else if (signum == SIGUSR2)
char_result = char_result + (1 * recursive_power(2, 7 - bits_counter));
bits_counter++;
if (bits_counter == 8)
{
final = letter_to_string(final, char_result);
if (char_result == '\0')
{
printf("%s\n", final);
final = NULL;
}
bits_counter = 0;
char_result = 0;
str_len += 1;
}
}
int main(void)
{
struct sigaction action;
printf("Welcome to the Creators Area Server\n");
printf("PID: %d\n", getpid());
action.sa_handler = signal_handler;
action.sa_flags = 0;
sigemptyset(&action.sa_mask);
sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL);
while (1)
pause();
return 0;
}