forked from MeiK2333/apue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp7.c
228 lines (202 loc) · 4.34 KB
/
p7.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct memstream {
char *buf;
size_t rsize;
size_t vsize;
size_t curpos;
int flags;
};
#define MS_READ 0x01
#define MS_WRITE 0x02
#define MS_APPEND 0x04
#define MS_TRUNCATE 0x08
#define MS_MYBUF 0x10
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#define off_t int
#define fpos_t int
static int mstream_read(void *, char *, int);
static int mstream_write(void *, const char *, int);
static fpos_t mstream_seek(void *, fpos_t, int);
static int mstream_close(void *);
static int type_to_flags(const char *__restrict type);
static off_t find_end(char *buf, size_t len);
FILE *fmemopen(void *__restrict buf, size_t size, const char *__restrict type) {
struct memstream *ms;
FILE *fp;
if (size == 0) {
errno = EINVAL;
return (NULL);
}
if ((ms = malloc(sizeof(struct memstream))) == NULL) {
errno = ENOMEM;
return (NULL);
}
if ((ms->flags = type_to_flags(type)) == 0) {
errno = EINVAL;
free(ms);
return (NULL);
}
if (buf == NULL) {
if ((ms->flags & (MS_READ | MS_WRITE)) != (MS_READ | MS_WRITE)) {
errno = EINVAL;
free(ms);
return (NULL);
}
if ((ms->buf = malloc(size)) == NULL) {
errno = ENOMEM;
free(ms);
return (NULL);
}
ms->rsize = size;
ms->flags |= MS_MYBUF;
ms->curpos = 0;
} else {
ms->buf = buf;
ms->rsize = size;
if (ms->flags & MS_APPEND) {
ms->curpos = find_end(ms->buf, ms->rsize);
} else {
ms->curpos = 0;
}
}
if (ms->flags & MS_APPEND) {
ms->vsize = ms->curpos;
} else if (ms->flags & MS_TRUNCATE) {
ms->vsize = 0;
} else {
ms->vsize = size;
}
fp = funopen(ms, mstream_read, mstream_write, mstream_seek, mstream_close);
if (fp == NULL) {
if (ms->flags & MS_MYBUF) {
free(ms->buf);
}
free(ms);
}
return (fp);
}
static int type_to_flags(const char *__restrict type) {
const char *cp;
int flags = 0;
for (cp = type; *cp != 0; cp++) {
switch (*cp) {
case 'r':
if (flags != 0) {
return 0;
}
flags |= MS_READ;
break;
case 'w':
if (flags != 0) {
return 0;
}
flags |= MS_WRITE | MS_TRUNCATE;
break;
case 'a':
if (flags != 0) {
return 0;
}
flags |= MS_APPEND;
break;
case '+':
if (flags == 0) {
return 0;
}
flags |= MS_READ | MS_WRITE;
break;
case 'b':
if (flags == 0) {
return 0;
}
break;
default:
return 0;
}
}
return flags;
}
static off_t find_end(char *buf, size_t len) {
off_t off = 0;
while (off < len) {
if (buf[off] == 0) {
break;
}
off++;
}
return off;
}
static int mstream_read(void *cookie, char *buf, int len) {
int nr;
struct memstream *ms = cookie;
if (!(ms->flags & MS_READ)) {
errno = EBADF;
return -1;
}
if (ms->curpos >= ms->vsize) {
return 0;
}
nr = MIN(len, ms->vsize - ms->curpos);
memcpy(buf, ms->buf + ms->curpos, nr);
ms->curpos += nr;
return nr;
}
static int mstream_write(void *cookie, const char *buf, int len) {
int nw, off;
struct memstream *ms = cookie;
if (!(ms->flags & (MS_APPEND | MS_WRITE))) {
errno = EBADF;
return -1;
}
if (ms->flags & MS_APPEND) {
off = ms->vsize;
} else {
off = ms->curpos;
}
nw = MIN(len, ms->rsize - off);
memcpy(ms->buf + off, buf, nw);
ms->curpos = off + nw;
if (ms->curpos > ms->vsize) {
ms->vsize = ms->curpos;
if (((ms->flags & (MS_READ | MS_WRITE)) == (MS_READ | MS_WRITE)) &&
(ms->vsize < ms->rsize)) {
*(ms->buf + ms->curpos) = 0;
} else {
*(ms->buf + ms->rsize - 1) = 0;
}
}
return nw;
}
static fpos_t mstream_seek(void *cookie, fpos_t pos, int whence) {
int off;
struct memstream *ms = cookie;
switch (whence) {
case SEEK_SET:
off = pos;
break;
case SEEK_END:
off = ms->vsize + pos;
break;
case SEEK_CUR:
off = ms->curpos + pos;
break;
}
if (off < 0 || off > ms->vsize) {
errno = EINVAL;
return -1;
}
ms->curpos = off;
return off;
}
static int mstream_close(void *cookie) {
struct memstream *ms = cookie;
if (ms->flags & MS_MYBUF) {
free(ms->buf);
}
free(ms);
return 0;
}