-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
87 lines (80 loc) · 1.99 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sojammal <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/30 23:22:17 by sojammal #+# #+# */
/* Updated: 2025/01/22 17:38:26 by sojammal ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void ft_send_signal(int pid, char h)
{
int signal;
int bit;
bit = 0;
while (bit < 8)
{
if (h & (1 << bit))
signal = SIGUSR1;
else
signal = SIGUSR2;
if (kill(pid, signal) == -1)
{
ft_putstr_fd(RED "Invalid PID. Signal failed.\n" "\e[0m", 2);
exit(1);
}
usleep(600);
bit++;
}
}
int validate_pid(char *pid_str)
{
int i;
int pid;
i = 0;
while (pid_str[i])
{
if (!ft_isdigit(pid_str[i]))
{
ft_putstr_fd(RED "PID has non-digit chars.\n" "\e[0m", 2);
return (0);
}
i++;
}
pid = ft_atoi(pid_str);
if (pid <= 0)
{
ft_putstr_fd(RED "PID must be positive.\n" "\e[0m", 2);
return (0);
}
return (pid);
}
int main(int ac, char **av)
{
int pid;
int i;
i = 0;
if (ac == 3 && av[2][0] != '\0')
{
pid = validate_pid(av[1]);
if (pid != 0)
{
ft_putstr_fd(GRN "Valid PID. Sending...\n" "\e[0m", 1);
while (av[2][i])
{
ft_send_signal(pid, av[2][i]);
i++;
}
ft_send_signal(pid, '\0');
}
}
else
{
ft_putstr_fd(RED "Invalid args or empty msg.\n" "\e[0m", 2);
ft_putstr_fd(PUR "Usage: ./client [PID] [MSG]\n" "\e[0m", 2);
}
return (0);
}