-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathio.c
More file actions
42 lines (37 loc) · 651 Bytes
/
io.c
File metadata and controls
42 lines (37 loc) · 651 Bytes
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
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include "ftpscan.h"
int
drain_all(int fd)
{
char recv_buffer[1024];
int n;
size_t total = 0;
set_blocking(fd);
while(1) {
if((n = recv(fd, recv_buffer, sizeof(recv_buffer), 0)) < 0) {
error("recv() failed while draining data");
close(fd);
return -1;
}
if(n == 0) {
debug("Drained %d bytes.", total);
close(fd);
return 0;
}
total += n;
}
}
void
write_all(int fd, const void *buf, size_t count)
{
int n;
const char *p = buf;
while(count) {
if((n = write(fd, p, count)) < 0)
fatal("write() failed.");
count -= n;
p += n;
}
}