-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
105 lines (95 loc) · 2.32 KB
/
client.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sleleu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/20 20:05:57 by sleleu #+# #+# */
/* Updated: 2022/06/23 03:47:07 by sleleu ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
unsigned int g_bits = 0;
int ft_atoi(char *str)
{
int result;
int sign;
result = 0;
sign = 1;
while ((*str >= 9 && *str <= 13) || *str == 32)
str++;
if (*str == '+' || *str == '-')
{
if (*str == '-')
sign *= -1;
str++;
}
while (*str >= '0' && *str <= '9')
{
result = result * 10 + (*str - '0');
str++;
}
return (result * sign);
}
void ft_client_error(int i)
{
if (i == 1)
ft_printf(RED"❌ ERROR : Invalid argument ❌\n"END);
else if (i == 2)
ft_printf(RED"❌ Sigaction Error ❌\n"END);
else if (i == 3)
ft_printf(RED"❌ ERROR : Bad PID ❌\n"END);
exit(EXIT_FAILURE);
}
void ft_end_signal(int signum)
{
if (signum == SIGUSR1)
{
ft_printf(GREEN"⚡ %d bits "END, g_bits);
ft_printf(YELLOW"successfully sent ! ⚡\n"END);
}
}
void ft_send_signal(int pid, char octet)
{
int i;
if (pid <= 0)
ft_client_error(3);
i = 7;
while (i >= 0)
{
if (octet & 1 << i)
{
if (kill(pid, SIGUSR1) != 0)
ft_client_error(3);
}
else
{
if (kill(pid, SIGUSR2) != 0)
ft_client_error(3);
}
usleep(400);
i--;
}
}
int main(int argc, char **argv)
{
int i;
pid_t pid;
struct sigaction sa;
if (argc != 3)
ft_client_error(1);
i = 0;
sa.sa_handler = ft_end_signal;
pid = ft_atoi(argv[1]);
if (sigaction(SIGUSR1, &sa, NULL) == -1)
ft_client_error(2);
while (argv[2][i])
{
ft_send_signal(pid, argv[2][i]);
i++;
g_bits += 8;
}
ft_send_signal(pid, '\0');
return (0);
}