-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_bonus.c
78 lines (71 loc) · 1.87 KB
/
client_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ialdidi <ialdidi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 14:43:17 by ialdidi #+# #+# */
/* Updated: 2024/01/23 08:35:45 by ialdidi ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk_bonus.h"
void signal_handler(int sig);
int send_message(pid_t pid, char *str);
int main(int ac, char **av)
{
int sid;
char *msg;
if (ac != 3)
{
ft_putstr("The format must be as follows : ./program [pid] [message]");
}
else
{
sid = ft_atoi(av[1]);
msg = av[2];
if (sid > 0 && msg[0])
{
signal(SIGUSR2, signal_handler);
sid = send_message(sid, msg);
if (sid == 0)
return (EXIT_SUCCESS);
ft_putstr("No such process");
}
else
{
ft_putstr("Invalid process id or An empty message provided");
}
}
return (EXIT_FAILURE);
}
void signal_handler(int sig)
{
(void)sig;
ft_putstr("Message sent successfully \xE2\x9C\x85\n");
}
int send_message(pid_t pid, char *str)
{
int i;
unsigned char mask;
while (*str)
{
i = -1;
mask = 1;
while (++i < 8)
{
if (kill(pid, SIGUSR1 + !(mask & *str)) < 0)
return (-1);
usleep(500);
mask <<= 1;
}
str++;
}
i = 0;
while (i++ < 8)
{
kill(pid, SIGUSR2);
usleep(500);
}
return (0);
}