-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocketFunctions.h
More file actions
82 lines (72 loc) · 1.37 KB
/
socketFunctions.h
File metadata and controls
82 lines (72 loc) · 1.37 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
int read_line (int fd, char *ptr, int line_size)
{
int n;
int rc;
char c;
for (n = 1; n < line_size; n++)
{
if ((rc = read_n (fd, &c, 1)) == 1)
{
*ptr++ = c;
if (c == '\n')
{
break;
}
}
else if (rc == 0)
{
if (n == 1)
{
return (0);
}
else
{
break;
}
}
else
{
return (-1);
}
}
*ptr = 0;
return (n);
}
int write_n (int fd, char *ptr, int n_bytes)
{
int n_left;
int n_written;
n_left = n_bytes;
while (n_left > 0)
{
n_written = write (fd, ptr, n_left);
if (n_written <= 0)
{
return (n_written);
}
n_left = n_left - n_written;
ptr = ptr + n_written;
}
return (n_bytes - n_left);
}
int read_n (int fd, char *ptr, int n_bytes)
{
int n_left;
int n_read;
n_left = n_bytes;
while (n_left > 0)
{
n_read = read (fd, ptr, n_left);
if (n_read < 0)
{
return (n_read);
}
else if (n_read == 0)
{
break;
}
n_left = n_left - n_read;
ptr = ptr + n_read;
}
return (n_bytes - n_left);
}