-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
80 lines (72 loc) · 1.76 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oqaroot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 17:20:20 by oqaroot #+# #+# */
/* Updated: 2024/10/17 20:11:53 by oqaroot ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void ft_putstr_fd(char *s, int fd)
{
write(fd, s, ft_strlen(s));
}
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
void signal_action(int pid, char *str)
{
int i;
int c;
i = 0;
while (*str)
{
c = *(str);
while (i < 8)
{
if (c << i & 0b10000000)
kill(pid, SIGUSR1);
else
kill(pid, SIGUSR2);
i++;
usleep(850);
}
str++;
i = 0;
}
}
int main(int argc, char **argv)
{
int i;
if (argc != 3)
{
ft_putstr("Wrong args\nFormat: [./client <(PID)> <STRING>]\n");
exit(EXIT_FAILURE);
}
if (argv[1] != NULL)
{
i = 0;
while ((argv[1][i]))
{
if (ft_isdigit(argv[1][i]) == 0)
{
ft_putstr("Pid is dangerous\n");
exit(EXIT_FAILURE);
}
i++;
}
}
if (ft_atoi(argv[1]) <= 0)
return (write(1, "Pid is dangerous\n", 16), 1);
else
signal_action(ft_atoi(argv[1]), argv[2]);
return (0);
}