-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
140 lines (132 loc) · 4.86 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gudaniel <gudaniel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/29 08:45:33 by gudaniel #+# #+# */
/* Updated: 2024/07/30 12:12:07 by gudaniel ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void server_art(void)
{
ft_printf(RED " \t\t\t\t ▄████ █ ██ ██████ \n");
ft_printf(RED " \t\t\t\t ██▒ ▀█▒ ██ ▓██▒▒██ ▒ \n");
ft_printf(RED " \t\t\t\t ▒██░▄▄▄░▓██ ▒██░░ ▓██▄ \n");
ft_printf(RED " \t\t\t\t ░▓█ ██▓▓▓█ ░██░ ▒ ██▒ \n");
ft_printf(RED " \t\t\t\t ░▒▓███▀▒▒▒█████▓ ▒██████▒▒ \n");
ft_printf(RED " \t\t\t\t ░▒ ▒ ░▒▓▒ ▒ ▒ ▒ ▒▓▒ ▒ ░ \n");
ft_printf(RED " \t\t\t\t ░ ░ ░░▒░ ░ ░ ░ ░▒ ░ ░ \n");
ft_printf(RED " \t\t\t\t\t ░ ░ ░ ░░░ ░ ░ ░ ░ ░ \n");
ft_printf(RED " \t\t\t\t\t ░ ░ ░ \n");
ft_printf(" \n");
ft_printf(RED "\t\t\t ██████ ▓█████ ██▀███ ██▒ █▓▓█████ ██▀███ \n");
ft_printf(RED "\t\t\t▒██ ▒ ▓█ ▀ ▓██ ▒ ██▒▓██░ █▒▓█ ▀ ▓██ ▒ ██▒\n");
ft_printf(RED "\t\t\t░ ▓██▄ ▒███ ▓██ ░▄█ ▒ ▓██ █▒░▒███ ▓██ ░▄█ ▒\n");
ft_printf(RED "\t\t\t ▒ ██▒▒▓█ ▄ ▒██▀▀█▄ ▒██ █░░▒▓█ ▄ ▒██▀▀█▄ \n");
ft_printf(RED "\t\t\t▒██████▒▒░▒████▒░██▓ ▒██▒ ▒▀█░ ░▒████▒░██▓ ▒██▒\n");
ft_printf(RED "\t\t\t▒ ▒▓▒ ▒ ░░░ ▒░ ░░ ▒▓ ░▒▓░ ░ ▐░ ░░ ▒░ ░░ ▒▓ ░▒▓░\n");
ft_printf(RED "\t\t\t░ ░▒ ░ ░ ░ ░ ░ ░▒ ░ ▒░ ░ ░░ ░ ░ ░ ░▒ ░ ▒░\n");
ft_printf(RED "\t\t\t░ ░ ░ ░ ░░ ░ ░░ ░ ░░ ░ \n");
ft_printf(RED "\t\t\t ░ ░ ░ ░ ░ ░ ░ ░ \n");
ft_printf(RED "\t\t ░ \n");
ft_printf("\n");
}
/**
* @brief Join a string with a character
* @param s1 The string to join
* @param c The character to join
* @param string The new string
* @param size The size of the string
* @param i The index
* @return char*
*/
char *ft_strcjoin(char const *s1, char c)
{
int size;
int i;
char *string;
if (s1)
size = ft_strlen(s1);
else
size = 0;
string = (char *)malloc(sizeof(char) * (size + 2));
if (!string)
{
free(string);
return (NULL);
}
i = -1;
while (s1[++i])
string[i] = s1[i];
string[i++] = c;
string[i] = '\0';
return (string);
}
/**
* @brief Convert the bits to ascii
* @param sig The signal
* @param bits The number of bits
* @param i The index
* @param str The string
* @param temp The temporary string
* @return void
*/
void bit_ascii(int sig)
{
static int bits;
static int i;
static char *str;
char *temp;
if (!str)
str = ft_calloc(1, 1);
if (sig == SIGUSR1)
i |= (1 << bits);
bits++;
if (bits == 8)
{
temp = ft_strcjoin(str, (char)i);
free(str);
str = temp;
if (i == 0)
{
ft_printf("%s\n", str);
free(str);
str = NULL;
}
bits = 0;
i = 0;
}
}
/**
* @brief The core of the server side, where we receive
* the string from the client
* @param argc The number of arguments
* @param argv The arguments
* @param pid The process id
* @return int
*/
int main(int argc, char **argv)
{
int pid;
(void)argv;
if (argc != 1)
{
ft_printf("%s", "Error\n");
return (1);
}
pid = getpid();
server_art();
ft_printf(WHT "The server is open\n");
ft_printf(WHT "$ [PID]: %d \n", pid);
ft_printf(WHT "$ %s \n", "Output:");
while (argc == 1)
{
signal(SIGUSR1, bit_ascii);
signal(SIGUSR2, bit_ascii);
pause();
}
return (0);
}