Fix build issue with SINGLE_THREAD_P
[glibc.git] / libio / fmemopen.c
blob0b45f9c32374d165d2b98fc8fcf558d2f530682a
1 /* fmemopen implementation.
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* fmemopen() from 2.22 and forward works as defined by POSIX. It also
20 provides an older symbol, version 2.2.5, that behaves different regarding
21 SEEK_END (libio/oldfmemopen.c). */
24 #include <errno.h>
25 #include <libio.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include "libioP.h"
34 typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
35 struct fmemopen_cookie_struct
37 char *buffer; /* memory buffer. */
38 int mybuffer; /* allocated my buffer? */
39 int append; /* buffer open for append? */
40 size_t size; /* buffer length in bytes. */
41 _IO_off64_t pos; /* current position at the buffer. */
42 size_t maxpos; /* max position in buffer. */
46 static ssize_t
47 fmemopen_read (void *cookie, char *b, size_t s)
49 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
51 if (c->pos + s > c->maxpos)
53 s = c->maxpos - c->pos;
54 if ((size_t) c->pos > c->maxpos)
55 s = 0;
58 memcpy (b, &(c->buffer[c->pos]), s);
60 c->pos += s;
62 return s;
66 static ssize_t
67 fmemopen_write (void *cookie, const char *b, size_t s)
69 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;;
70 _IO_off64_t pos = c->append ? c->maxpos : c->pos;
71 int addnullc = (s == 0 || b[s - 1] != '\0');
73 if (pos + s > c->size)
75 if ((size_t) (c->pos + addnullc) >= c->size)
77 __set_errno (ENOSPC);
78 return 0;
80 s = c->size - pos;
83 memcpy (&(c->buffer[pos]), b, s);
85 c->pos = pos + s;
86 if ((size_t) c->pos > c->maxpos)
88 c->maxpos = c->pos;
89 if (c->maxpos < c->size && addnullc)
90 c->buffer[c->maxpos] = '\0';
91 /* A null byte is written in a stream open for update iff it fits. */
92 else if (c->append == 0 && addnullc != 0)
93 c->buffer[c->size-1] = '\0';
96 return s;
100 static int
101 fmemopen_seek (void *cookie, _IO_off64_t *p, int w)
103 _IO_off64_t np;
104 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
106 switch (w)
108 case SEEK_SET:
109 np = *p;
110 break;
112 case SEEK_CUR:
113 np = c->pos + *p;
114 break;
116 case SEEK_END:
117 np = c->maxpos + *p;
118 break;
120 default:
121 return -1;
124 if (np < 0 || (size_t) np > c->size)
126 __set_errno (EINVAL);
127 return -1;
130 *p = c->pos = np;
132 return 0;
136 static int
137 fmemopen_close (void *cookie)
139 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
141 if (c->mybuffer)
142 free (c->buffer);
143 free (c);
145 return 0;
149 FILE *
150 __fmemopen (void *buf, size_t len, const char *mode)
152 cookie_io_functions_t iof;
153 fmemopen_cookie_t *c;
154 FILE *result;
156 c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1);
157 if (c == NULL)
158 return NULL;
160 c->mybuffer = (buf == NULL);
162 if (c->mybuffer)
164 c->buffer = (char *) malloc (len);
165 if (c->buffer == NULL)
167 free (c);
168 return NULL;
170 c->buffer[0] = '\0';
172 else
174 if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf))
176 free (c);
177 __set_errno (EINVAL);
178 return NULL;
181 c->buffer = buf;
183 /* POSIX states that w+ mode should truncate the buffer. */
184 if (mode[0] == 'w' && mode[1] == '+')
185 c->buffer[0] = '\0';
187 if (mode[0] == 'a')
188 c->maxpos = strnlen (c->buffer, len);
192 /* Mode | starting position (cookie::pos) | size (cookie::size)
193 ------ |----------------------------------|-----------------------------
194 read | beginning of the buffer | size argument
195 write | beginning of the buffer | zero
196 append | first null or size buffer + 1 | first null or size argument
199 c->size = len;
201 if (mode[0] == 'r')
202 c->maxpos = len;
204 c->append = mode[0] == 'a';
205 if (c->append)
206 c->pos = c->maxpos;
207 else
208 c->pos = 0;
210 iof.read = fmemopen_read;
211 iof.write = fmemopen_write;
212 iof.seek = fmemopen_seek;
213 iof.close = fmemopen_close;
215 result = _IO_fopencookie (c, mode, iof);
216 if (__glibc_unlikely (result == NULL))
218 if (c->mybuffer)
219 free (c->buffer);
221 free (c);
224 return result;
226 libc_hidden_def (__fmemopen)
227 versioned_symbol (libc, __fmemopen, fmemopen, GLIBC_2_22);