-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_bonus.c
77 lines (70 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gjakobss <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/11 17:15:56 by gjakobss #+# #+# */
/* Updated: 2021/09/11 17:15:57 by gjakobss ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
char *ft_strdup(const char *s1)
{
char *dest;
int len;
int i;
i = 0;
len = ft_strlen((char *)s1);
dest = (char *)malloc((len + 1) * sizeof(char));
if (!dest)
{
return (NULL);
}
while (s1[i] != '\0')
{
dest[i] = s1[i];
i++;
}
dest[i] = '\0';
return (dest);
}
void convert(unsigned char c, int pid)
{
unsigned char bit;
bit = 0b10000000;
while (bit != 0)
{
if ((bit & c))
kill(pid, SIGUSR1);
else
kill(pid, SIGUSR2);
bit = bit >> 1;
usleep(100);
}
}
int main(int argc, char **argv)
{
int pid;
char *msg;
int i;
if (argc != 3)
exit(0);
msg = ft_strdup(argv[2]);
i = 0;
pid = 0;
while (argv[1][i])
{
pid = (pid * 10) + argv[1][i] - '0';
i++;
}
while (*msg)
{
convert(*msg, pid);
msg++;
}
convert(0, pid);
ft_putstr("Message has been successfully sent");
return (0);
}