OSDN Git Service

Last portion of libc_hidden_proto removal.
[uclinux-h8/uClibc.git] / libc / stdio / fmemopen.c
1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7
8 #include <features.h>
9
10 #ifdef __USE_GNU
11 #include "_stdio.h"
12
13 /* Experimentally off - libc_hidden_proto(memcpy) */
14 /* libc_hidden_proto(fopencookie) */
15
16 #ifndef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
17 #error no custom streams!
18 #endif
19
20 typedef struct {
21         size_t pos;
22         size_t len;
23         size_t eof;
24         int dynbuf;
25         unsigned char *buf;
26         FILE *fp;
27 } __fmo_cookie;
28
29 #define COOKIE ((__fmo_cookie *) cookie)
30
31 static ssize_t fmo_read(register void *cookie, char *buf, size_t bufsize)
32 {
33         size_t count = COOKIE->len - COOKIE->pos;
34
35         /* Note: 0 < bufsize < SSIZE_MAX because of _stdio_READ. */
36         if (!count) {                           /* EOF! */
37                 return 0;
38         }
39
40         if (bufsize > count) {
41                 bufsize = count;
42         }
43
44         memcpy(buf, COOKIE->buf + COOKIE->pos, bufsize);
45         COOKIE->pos += bufsize;
46
47         return bufsize;
48 }
49
50 static ssize_t fmo_write(register void *cookie, const char *buf, size_t bufsize)
51 {
52         size_t count;
53
54         /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
55
56         /* If appending, need to seek to end of file!!!! */
57         if (COOKIE->fp->__modeflags & __FLAG_APPEND) {
58                 COOKIE->pos = COOKIE->eof;
59         }
60
61         count = COOKIE->len - COOKIE->pos;
62
63         if (bufsize > count) {
64                 bufsize = count;
65                 if (count == 0) {               /* We're at the end of the buffer... */
66                         __set_errno(EFBIG);
67                         return -1;
68                 }
69         }
70
71         memcpy(COOKIE->buf + COOKIE->pos, buf, bufsize);
72         COOKIE->pos += bufsize;
73
74         if (COOKIE->pos > COOKIE->eof) {
75                 COOKIE->eof = COOKIE->pos;
76                 if (bufsize < count) {  /* New eof and still room in buffer? */
77                         *(COOKIE->buf + COOKIE->pos) = 0;
78                 }
79         }
80
81         return bufsize;
82 }
83
84 /* glibc doesn't allow seeking, but it has in-buffer seeks... we don't. */
85 static int fmo_seek(register void *cookie, __offmax_t *pos, int whence)
86 {
87         __offmax_t p = *pos;
88
89         /* Note: fseek already checks that whence is legal, so don't check here
90          * unless debugging. */
91         assert(((unsigned int) whence) <= 2);
92
93         if (whence != SEEK_SET) {
94                 p += (whence == SEEK_CUR) ? COOKIE->pos : /* SEEK_END */ COOKIE->eof;
95         }
96
97         /* Note: glibc only allows seeking in the buffer.  We'll actually restrict
98          * to the data. */
99         /* Check for offset < 0, offset > eof, or offset overflow... */
100         if (((uintmax_t) p) > COOKIE->eof) {
101                 return -1;
102         }
103
104         COOKIE->pos = *pos = p;
105         return 0;
106 }
107
108 static int fmo_close(register void *cookie)
109 {
110         if (COOKIE->dynbuf) {
111                 free(COOKIE->buf);
112         }
113         free(cookie);
114         return 0;
115 }
116
117 #undef COOKIE
118
119 static const cookie_io_functions_t _fmo_io_funcs = {
120         fmo_read, fmo_write, fmo_seek, fmo_close
121 };
122
123 /* TODO: If we have buffers enabled, it might be worthwile to add a pointer
124  * to the FILE in the cookie and have read, write, and seek operate directly
125  * on the buffer itself (ie replace the FILE buffer with the cookie buffer
126  * and update FILE bufstart, etc. whenever we seek). */
127
128 FILE *fmemopen(void *s, size_t len, const char *modes)
129 {
130         FILE *fp;
131         register __fmo_cookie *cookie;
132         size_t i;
133
134         if ((cookie = malloc(sizeof(__fmo_cookie))) != NULL) {
135                 cookie->len = len;
136                 cookie->eof = cookie->pos = 0; /* pos and eof adjusted below. */
137                 cookie->dynbuf = 0;
138                 if (((cookie->buf = s) == NULL) && (len > 0)) {
139                         if ((cookie->buf = malloc(len)) == NULL) {
140                                 goto EXIT_cookie;
141                         }
142                         cookie->dynbuf = 1;
143                         *cookie->buf = 0;       /* If we're appending, treat as empty file. */
144                 }
145
146 #ifndef __BCC__
147                 fp = fopencookie(cookie, modes, _fmo_io_funcs);
148 #else
149                 fp = fopencookie(cookie, modes, &_fmo_io_funcs);
150 #endif
151                 /* Note: We don't need to worry about locking fp in the thread case
152                  * as the only possible access would be a close or flush with
153                  * nothing currently in the FILE's write buffer. */
154
155                 if (fp != NULL) {
156                         cookie->fp = fp;
157                         if (fp->__modeflags & __FLAG_READONLY) {
158                                 cookie->eof = len;
159                         }
160                         if ((fp->__modeflags & __FLAG_APPEND) && (len > 0)) {
161                                 for (i = 0 ; i < len ; i++) {
162                                         if (cookie->buf[i] == 0) {
163                                                 break;
164                                         }
165                                 }
166                                 cookie->eof = cookie->pos = i; /* Adjust eof and pos. */
167                         }
168
169                         __STDIO_STREAM_VALIDATE(fp);
170
171                         return fp;
172                 }
173         }
174
175         if (!s) {
176                 free(cookie->buf);
177         }
178  EXIT_cookie:
179         free(cookie);
180
181         return NULL;
182 }
183 #endif