-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
109 lines (100 loc) · 2.39 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
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-hask <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/14 19:56:31 by ael-hask #+# #+# */
/* Updated: 2021/12/14 19:56:32 by ael-hask ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static size_t check(size_t n, const char *nptr, int sign)
{
while (*nptr >= '0' && *nptr <= '9')
{
n = *nptr - '0' + (n * 10);
nptr++;
}
return (n * sign);
}
int ft_atoi(const char *nptr)
{
size_t n;
int sign;
int d;
d = 0;
sign = 1;
n = 0;
while (*nptr == ' ' || (*nptr >= 9 && *nptr <= 13))
nptr++;
while (*nptr == '+' || *nptr == '-')
{
if (*nptr == '-')
sign = -1;
d++;
nptr++;
}
if (d > 1)
return (n);
n = check(n, nptr, sign);
return (n);
}
void send_signal(int bit, int pid)
{
if (bit == 0)
{
if (kill(pid, SIGUSR1) == -1)
{
write(1, "Problem sending the signal!\n", 29);
exit(1);
}
usleep(150);
}
if (bit == 1)
{
if (kill(pid, SIGUSR2) == -1)
{
write(1, "Problem sending the signal!\n", 29);
exit(1);
}
usleep(150);
}
}
void convert(int c, int pid)
{
t_byte_struct byte_char;
*(unsigned char *)&byte_char = (unsigned char)c;
send_signal(byte_char.b1, pid);
send_signal(byte_char.b2, pid);
send_signal(byte_char.b3, pid);
send_signal(byte_char.b4, pid);
send_signal(byte_char.b5, pid);
send_signal(byte_char.b6, pid);
send_signal(byte_char.b7, pid);
send_signal(byte_char.b8, pid);
}
int main(int argc, char **argv)
{
int pid;
int i;
i = 0;
if (argc == 3)
{
pid = ft_atoi((const char *)argv[1]);
if (pid == 0 || pid < 0)
{
write(1, "Invalid PID!\n", 13);
return (0);
}
while (argv[2][i])
{
convert((int)argv[2][i], pid);
i++;
}
}
else
write(1, "Usage: ./client [PID] [STRING_TO_PASS]\n", 40);
return (0);
}