Repository navigation
Expand file tree
/
Copy pathclient_bonus.c
More file actions
120 lines (111 loc) · 1.91 KB
/
Copy pathclient_bonus.c
File metadata and controls
120 lines (111 loc) · 1.91 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "minitalk.h"
/* Return TRUE if PID is all digits or FALSE if anything else is found. */
static int check_pid(char *str, int *pid)
{
int i;
i = -1;
while (str[++i])
{
if (!ft_isdigit((int)str[i]))
{
write(1, "PID must be only numbers.\n", 26);
return (0);
}
}
*pid = ft_atoi(str);
if (*pid < 2)
{
write(1, "PID isn't a valid process.\n", 27);
return (0);
}
return (1);
}
/* Sends a int via SIGUSR1 (bit 0) and SIGUSR2 (bit 1). */
static int send_int(int pid, int nbr)
{
int i;
i = 32;
while (--i >= 0)
{
if (!((nbr >> i) & 1))
{
if ((kill(pid, SIGUSR1)) == -1)
{
write(1, "Kill function returned an error.\n", 33);
return (0);
}
}
else
{
if ((kill(pid, SIGUSR2)) == -1)
{
write(1, "Kill function returned an error.\n", 33);
return (0);
}
}
usleep(100);
}
return (1);
}
/* Sends a string via SIGUSR1 (bit 0) and SIGUSR2 (bit 1). */
static int send_str(int pid, char *str)
{
unsigned char c;
int i;
int j;
j = -1;
while (str[++j])
{
i = 8;
c = (unsigned char)str[j];
while (--i >= 0)
{
if (!((c >> i) & 1))
{
if ((kill(pid, SIGUSR1)) == -1)
return (0);
}
else
{
if ((kill(pid, SIGUSR2)) == -1)
return (0);
}
usleep(100);
}
}
return (1);
}
/* Print msg received and close client. */
static void msg_received(int x)
{
if (x == SIGUSR1)
{
write(1, "Server received your message!\n", 30);
exit (0);
}
}
int main(int argc, char **argv)
{
int pid;
struct sigaction sas;
sas.sa_handler = &msg_received;
if (argc != 3)
{
write(1, "Invalid number of arguments.\n", 29);
exit (1);
}
if (!check_pid(argv[1], &pid))
exit (1);
if (!send_int(pid, ft_strlen(argv[2])))
exit (1);
if (!send_int(pid, getpid()))
exit (1);
if (!send_str(pid, argv[2]))
{
write(1, "Kill function returned an error.\n", 33);
exit (1);
}
while (1)
sigaction(SIGUSR1, &sas, NULL);
exit (0);
}