forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfopencookie.c
311 lines (250 loc) · 7.31 KB
/
fopencookie.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/****************************************************************************
* apps/testing/fs/fopencookie/fopencookie.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BUF_SIZE 32
/****************************************************************************
* Private data
****************************************************************************/
struct my_cookie
{
char *buf;
size_t endpos;
size_t bufsize;
off_t offset;
};
static const char correctbuf[] = "abcdefghijklmnopqrstuvwxyz";
/****************************************************************************
* Private Functions
****************************************************************************/
ssize_t fopencookie_write_hook(void *c, const char *buf, size_t size)
{
char *new_buff;
off_t realloc_size;
struct my_cookie *cookie = (struct my_cookie *)c;
DEBUGASSERT(c != NULL);
if (size + cookie->offset > cookie->bufsize)
{
realloc_size = size + cookie->offset - cookie->bufsize + 1;
new_buff = realloc(cookie->buf, realloc_size);
if (new_buff == NULL)
{
fprintf(stderr, "ERROR: Cannot reallocate memory!\n");
return -1;
}
cookie->buf = new_buff;
cookie->bufsize = realloc_size;
}
memcpy(cookie->buf + cookie->offset, buf, size);
cookie->offset += size;
if (cookie->offset > cookie->endpos)
{
cookie->endpos = cookie->offset;
}
return size;
}
ssize_t fopencookie_read_hook(void *c, char *buf, size_t size)
{
struct my_cookie *cookie = (struct my_cookie *)c;
if (cookie->offset + size > cookie->endpos)
{
size = cookie->endpos - cookie->offset;
}
if (size < 0)
{
size = 0;
}
memcpy(buf, cookie->buf + cookie->offset, size);
cookie->offset += size;
return size;
}
off_t fopencookie_seek_hook(void *c, off_t *offset, int whence)
{
off_t new_offset;
struct my_cookie *cookie = (struct my_cookie *)c;
if (whence == SEEK_SET)
{
new_offset = *offset;
}
else if (whence == SEEK_END)
{
new_offset = cookie->endpos + *offset;
}
else if (whence == SEEK_CUR)
{
new_offset = cookie->offset + *offset;
}
else
{
return -1;
}
if (new_offset < 0)
{
return -1;
}
cookie->offset = new_offset;
*offset = new_offset;
return new_offset;
}
int fopencookie_close_hook(void *c)
{
struct my_cookie *cookie = (struct my_cookie *)c;
cookie->bufsize = 0;
return OK;
}
/****************************************************************************
* Name: fopencookie_write_test
****************************************************************************/
static int fopencookie_write_test(FILE *stream)
{
int n;
n = fputs(correctbuf, stream);
if (n < 0)
{
printf("fopencookie_write_test: fputs failed: %d\n", -errno);
return -1;
}
printf("fopencookie_write_test: written buffer: %.*s\n", n, correctbuf);
printf("fopencookie_write_test: succesfull\n");
return 0;
}
/****************************************************************************
* Name: fopencookie_write_test
****************************************************************************/
static int fopencookie_read_test(FILE *stream)
{
int n;
int endpos;
char buf[256];
fseek(stream, 0, SEEK_END);
endpos = ftell(stream);
fseek(stream, 0, SEEK_SET);
n = fread(buf, 1, endpos, stream);
for (int i = 0; i < n; i++)
{
if (correctbuf[i] != buf[i])
{
printf("fopencookie_write_test: error %d got %c, expected %c\n", i,
buf[i], correctbuf[i]);
return -1;
}
}
printf("fopencookie_read_test: read buffer: %.*s\n", n, correctbuf);
printf("fopencookie_read_test: succesfull\n");
return 0;
}
/****************************************************************************
* Name: fopencookie_seek_test
****************************************************************************/
static int fopencookie_seek_test(FILE *stream)
{
int endpos;
int i;
int n;
char buf[1];
fseek(stream, 0, SEEK_END);
endpos = ftell(stream);
fseek(stream, 0, SEEK_SET);
for (i = 0; i < endpos; i += 2)
{
fseek(stream, i, SEEK_SET);
n = fread(buf, 1, 1, stream);
if (n != 1)
{
printf("fopencookie_seek_test: read at %i failed\n", i);
}
if (correctbuf[i] != buf[0])
{
printf("fopencookie_seek_test: error %d got %c, expected %c\n", i,
buf[0], correctbuf[i]);
return -1;
}
printf("fopencookie_seek_test: pos %d, char %c\n", i, buf[i]);
}
printf("fopencookie_seek_test: succesfull\n");
fseek(stream, 0, SEEK_SET);
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, FAR char *argv[])
{
int ret;
FILE *stream;
struct my_cookie mycookie;
cookie_io_functions_t cookie_funcs =
{
.read = fopencookie_read_hook,
.write = fopencookie_write_hook,
.seek = fopencookie_seek_hook,
.close = fopencookie_close_hook
};
/* Set up the cookie before calling fopencookie(). */
mycookie.buf = malloc(BUF_SIZE);
if (mycookie.buf == NULL)
{
printf("Could not allocate memory for cookie.\n");
return -1;
}
mycookie.bufsize = BUF_SIZE;
mycookie.offset = 0;
mycookie.endpos = 0;
stream = fopencookie(&mycookie, "w+", cookie_funcs);
if (stream == NULL)
{
free(mycookie.buf);
return -1;
}
ret = fopencookie_write_test(stream);
if (ret < 0)
{
goto fopencookie_ret;
}
ret = fopencookie_read_test(stream);
if (ret < 0)
{
goto fopencookie_ret;
}
ret = fopencookie_seek_test(stream);
if (ret < 0)
{
goto fopencookie_ret;
}
printf("fopencokie tests were succesfull.\n");
fopencookie_ret:
fclose(stream);
free(mycookie.buf);
return ret;
}