Repository navigation
Expand file tree
/
Copy pathclient.c
More file actions
106 lines (98 loc) · 1.63 KB
/
Copy pathclient.c
File metadata and controls
106 lines (98 loc) · 1.63 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
#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]))
{
ft_printf("PID must be only numbers.\n");
return (0);
}
}
*pid = ft_atoi(str);
if (*pid < 2)
{
ft_printf("PID isn't a valid process.\n");
return (0);
}
return (1);
}
/* Sends a int via SIGUSR1 (bit 0) and SIGUSR2 (bit 1). */
static int send_int(int pid, int str_size)
{
int i;
i = 32;
while (--i >= 0)
{
if (!((str_size >> i) & 1))
{
if ((kill(pid, SIGUSR1)) == -1)
{
ft_printf("Kill function returned an error.\n");
return (0);
}
}
else
{
if ((kill(pid, SIGUSR2)) == -1)
{
ft_printf("Kill function returned an error.\n");
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);
}
int main(int argc, char **argv)
{
int pid;
int str_size;
if (argc != 3)
{
ft_printf("Invalid number of arguments.\n");
exit (1);
}
if (!check_pid(argv[1], &pid))
exit (1);
str_size = ft_strlen(argv[2]);
if (!send_int(pid, str_size))
exit (1);
if (!send_str(pid, argv[2]))
{
ft_printf("Kill function returned an error.\n");
exit (1);
}
exit (0);
}