-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibexso.c
More file actions
86 lines (75 loc) · 1.6 KB
/
libexso.c
File metadata and controls
86 lines (75 loc) · 1.6 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
#include "basic.h"
/*
* Reads all data in the buffer and examines them one byte at a time
*/
ssize_t exso_read(int fd, char *ptr) {
static int read_cnt = 0;
static char read_buf[MAXLINE];
static char *read_ptr;
if (read_cnt <= 0) {
while ((read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
if (errno != EINTR) {
return -1;
}
}
if (read_cnt == 0) {
return 0;
}
read_ptr = read_buf;
}
read_cnt--;
*ptr = *read_ptr++;
return 1;
}
/*
* Reads a line
*/
ssize_t exso_readln(int fd, void *vptr, size_t maxlen) {
size_t n;
int rc;
char c;
char *ptr;
ptr = vptr;
for (n = 1; n < maxlen; n++) {
if ((rc = exso_read(fd, &c)) == 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;
}
/*
* Writes n bytes
*/
ssize_t exso_writen(int fd, const void *vptr, size_t n) {
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ((nwritten = write(fd, ptr, nleft)) <= 0) {
if (errno == EINTR) {
nwritten = 0;
} else {
return -1;
}
}
nleft -= nwritten;
ptr += nwritten;
}
return n;
}