-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
94 lines (86 loc) · 1.9 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: belguabd <belguabd@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/13 22:21:17 by belguabd #+# #+# */
/* Updated: 2024/01/16 17:07:03 by belguabd ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include "minitalk.h"
static int validate_input(int ac, char *av[])
{
if (ac != 3)
{
ft_putstr_fd("Invalid input : ./client [server_pid] [message]", 1);
exit(1);
}
if (ft_atoi(av[1]) <= 0)
{
ft_putstr_fd("pid incorrect", 1);
exit(1);
}
return (1);
}
static void __pass(int pid_proc, char c)
{
int i;
int bit;
i = 7;
bit = 0;
while (i >= 0)
{
bit = (c >> i) & 1;
if (bit)
{
if (kill(pid_proc, SIGUSR1) < 0)
exit(1);
}
else
{
if (kill(pid_proc, SIGUSR2) < 0)
exit(0);
}
usleep(100);
usleep(100);
i--;
}
}
static int check_pid(char const *av)
{
int i;
i = 0;
while (av[i])
{
if (!ft_isdigit(av[i]))
return (0);
i++;
}
return (1);
}
int main(int ac, char *av[])
{
int pid_pro;
int i;
if (validate_input(ac, av) == 1)
{
i = 0;
if (!check_pid(av[1]))
{
ft_putstr_fd("pid incorrect", 1);
exit(1);
}
pid_pro = ft_atoi(av[1]);
while (av[2][i])
{
__pass(pid_pro, av[2][i]);
i++;
}
}
return (0);
}