Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(546)

Side by Side Diff: fusl/src/stdio/fmemopen.c

Issue 1573973002: Add a "fork" of musl as //fusl. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fusl/src/stdio/flockfile.c ('k') | fusl/src/stdio/fopen.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "stdio_impl.h"
2 #include <errno.h>
3 #include <string.h>
4 #include <inttypes.h>
5
6 struct cookie {
7 size_t pos, len, size;
8 unsigned char *buf;
9 int mode;
10 };
11
12 static off_t mseek(FILE *f, off_t off, int whence)
13 {
14 ssize_t base;
15 struct cookie *c = f->cookie;
16 if (whence>2U) {
17 fail:
18 errno = EINVAL;
19 return -1;
20 }
21 base = (size_t [3]){0, c->pos, c->len}[whence];
22 if (off < -base || off > (ssize_t)c->size-base) goto fail;
23 return c->pos = base+off;
24 }
25
26 static size_t mread(FILE *f, unsigned char *buf, size_t len)
27 {
28 struct cookie *c = f->cookie;
29 size_t rem = c->len - c->pos;
30 if (c->pos > c->len) rem = 0;
31 if (len > rem) {
32 len = rem;
33 f->flags |= F_EOF;
34 }
35 memcpy(buf, c->buf+c->pos, len);
36 c->pos += len;
37 rem -= len;
38 if (rem > f->buf_size) rem = f->buf_size;
39 f->rpos = f->buf;
40 f->rend = f->buf + rem;
41 memcpy(f->rpos, c->buf+c->pos, rem);
42 c->pos += rem;
43 return len;
44 }
45
46 static size_t mwrite(FILE *f, const unsigned char *buf, size_t len)
47 {
48 struct cookie *c = f->cookie;
49 size_t rem;
50 size_t len2 = f->wpos - f->wbase;
51 if (len2) {
52 f->wpos = f->wbase;
53 if (mwrite(f, f->wpos, len2) < len2) return 0;
54 }
55 if (c->mode == 'a') c->pos = c->len;
56 rem = c->size - c->pos;
57 if (len > rem) len = rem;
58 memcpy(c->buf+c->pos, buf, len);
59 c->pos += len;
60 if (c->pos > c->len) {
61 c->len = c->pos;
62 if (c->len < c->size) c->buf[c->len] = 0;
63 else if ((f->flags&F_NORD) && c->size) c->buf[c->size-1] = 0;
64 }
65 return len;
66 }
67
68 static int mclose(FILE *m)
69 {
70 return 0;
71 }
72
73 FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
74 {
75 FILE *f;
76 struct cookie *c;
77 int plus = !!strchr(mode, '+');
78
79 if (!size || !strchr("rwa", *mode)) {
80 errno = EINVAL;
81 return 0;
82 }
83
84 if (!buf && size > SIZE_MAX-sizeof(FILE)-BUFSIZ-UNGET) {
85 errno = ENOMEM;
86 return 0;
87 }
88
89 f = calloc(sizeof *f + sizeof *c + UNGET + BUFSIZ + (buf?0:size), 1);
90 if (!f) return 0;
91 f->cookie = c = (void *)(f+1);
92 f->fd = -1;
93 f->lbf = EOF;
94 f->buf = (unsigned char *)(c+1) + UNGET;
95 f->buf_size = BUFSIZ;
96 if (!buf) buf = f->buf + BUFSIZ;
97
98 c->buf = buf;
99 c->size = size;
100 c->mode = *mode;
101
102 if (!plus) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
103 if (*mode == 'r') c->len = size;
104 else if (*mode == 'a') c->len = c->pos = strnlen(buf, size);
105
106 f->read = mread;
107 f->write = mwrite;
108 f->seek = mseek;
109 f->close = mclose;
110
111 if (!libc.threaded) f->lock = -1;
112
113 return __ofl_add(f);
114 }
OLDNEW
« no previous file with comments | « fusl/src/stdio/flockfile.c ('k') | fusl/src/stdio/fopen.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698