-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_bonus.c
91 lines (82 loc) · 1.94 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
79
80
81
82
83
84
85
86
87
88
89
90
91
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybourais <ybourais@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 16:51:57 by ybourais #+# #+# */
/* Updated: 2023/01/07 15:23:32 by ybourais ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk_bonus.h"
char *ft_bin(int a)
{
static char bin[9];
int i;
i = 7;
while (a != 0 && i >= 0)
{
bin[i] = (a % 2) + '0';
a = a / 2;
i--;
}
bin[8] = '\0';
while (i >= 0)
{
bin[i] = '0';
i--;
}
return (bin);
}
void ft_send_signal(char *pid, char *s)
{
int i;
i = 0;
while (s[i] != '\0')
{
if (s[i] == '0')
kill(ft_atoi(pid), SIGUSR1);
else if (s[i] == '1')
kill(ft_atoi(pid), SIGUSR2);
usleep(150);
i++;
}
}
void ft_hand(int sig)
{
if (sig == SIGUSR1)
write(1, "message rah wssal kaml\n", 23);
}
void ft_sender(char *p, char *string)
{
ft_send_signal(p, string);
ft_send_signal(p, ft_bin('\0'));
signal(SIGUSR1, ft_hand);
while (1)
;
pause();
}
int main(int argc, char *argv[])
{
char *str;
int i;
int j;
int k;
if (argc != 3)
exit(1);
str = (char *)malloc((ft_strlen(argv[2]) * 8) + 1);
k = 0;
i = 0;
while ((unsigned char)argv[2][i] != '\0')
{
j = 0;
while (j < 8)
str[k++] = ft_bin((unsigned char)argv[2][i])[j++];
i++;
}
str[k] = '\0';
ft_sender(argv[1], str);
free(str);
return (0);
}