Lines Matching +full:max +full:- +full:size

8 <<fmemopen>>---open a stream around a fixed-length string
15 FILE *fmemopen(void *restrict <[buf]>, size_t <[size]>,
20 fixed-length buffer of <[size]> bytes starting at <[buf]>. The stream
23 <[size]> bytes are automatically provided as if by <<malloc>>, with
24 the initial size of 0, and <[mode]> must contain <<+>> so that data
29 The stream also maintains a current file size, which is never greater
30 than <[size]>. If <[mode]> starts with <<r>>, the position starts at
31 <<0>>, and file size starts at <[size]> if <[buf]> was provided. If
32 <[mode]> starts with <<w>>, the position and file size start at <<0>>,
34 <[mode]> starts with <<a>>, the position and file size start at the
35 location of the first NUL byte, or else <[size]> if <[buf]> was
39 the current file size. When writing, the file size can increase up to
40 <[size]> as needed, and NUL bytes may be embedded in the stream (see
43 changed the file size, a NUL byte is written at the current position
46 stream has exceeded <[size]>, so that a write-only <[buf]> is always
47 NUL-terminated when the stream is flushed or closed (and the initial
48 <[size]> should take this into account). It is not possible to seek
49 outside the bounds of <[size]>. A NUL byte written during a flush is
54 <<NULL>> is returned, and <<errno>> will be set to EINVAL if <[size]>
77 size_t eof; /* current file size */
78 size_t max; /* maximum file size */ member
80 char writeonly; /* 1 if write-only */
81 char saved; /* saved character that lived at pos before write-only NUL */
84 /* Read up to non-zero N bytes into BUF from stream described by
93 /* Can't read beyond current size, but EOF condition is not an error. */ in fmemreader()
94 if (c->pos > c->eof) in fmemreader()
96 if (n >= c->eof - c->pos) in fmemreader()
97 n = c->eof - c->pos; in fmemreader()
98 memcpy (buf, c->buf + c->pos, n); in fmemreader()
99 c->pos += n; in fmemreader()
103 /* Write up to non-zero N bytes of BUF into the stream described by COOKIE,
116 if (c->append) in fmemwriter()
117 c->pos = c->eof; in fmemwriter()
118 else if (c->pos > c->eof) in fmemwriter()
119 memset (c->buf + c->eof, '\0', c->pos - c->eof); in fmemwriter()
120 /* Do not write beyond EOF; saving room for NUL on write-only stream. */ in fmemwriter()
121 if (c->pos + n > c->max - c->writeonly) in fmemwriter()
123 adjust = c->writeonly; in fmemwriter()
124 n = c->max - c->pos; in fmemwriter()
128 write-only; or if read-write, eof changed, and there is still in fmemwriter()
131 if (c->pos + n > c->eof) in fmemwriter()
133 c->eof = c->pos + n; in fmemwriter()
134 if (c->eof - adjust < c->max) in fmemwriter()
135 c->saved = c->buf[c->eof - adjust] = '\0'; in fmemwriter()
137 else if (c->writeonly) in fmemwriter()
141 c->saved = c->buf[c->pos + n - adjust]; in fmemwriter()
142 c->buf[c->pos + n - adjust] = '\0'; in fmemwriter()
147 c->pos += n; in fmemwriter()
148 if (n - adjust) in fmemwriter()
149 memcpy (c->buf + c->pos - n, buf, n - adjust); in fmemwriter()
174 offset += c->pos; in fmemseeker()
176 offset += c->eof; in fmemseeker()
180 offset = -1; in fmemseeker()
182 else if (offset > (off_t) c->max) in fmemseeker()
185 offset = -1; in fmemseeker()
191 offset = -1; in fmemseeker()
196 if (c->writeonly && c->pos < c->eof) in fmemseeker()
198 c->buf[c->pos] = c->saved; in fmemseeker()
199 c->saved = '\0'; in fmemseeker()
201 c->pos = offset; in fmemseeker()
202 if (c->writeonly && c->pos < c->eof) in fmemseeker()
204 c->saved = c->buf[c->pos]; in fmemseeker()
205 c->buf[c->pos] = '\0'; in fmemseeker()
223 offset += c->pos; in fmemseeker64()
225 offset += c->eof; in fmemseeker64()
229 offset = -1; in fmemseeker64()
231 else if (offset > (_off64_t) c->max) in fmemseeker64()
234 offset = -1; in fmemseeker64()
238 if (c->writeonly && c->pos < c->eof) in fmemseeker64()
240 c->buf[c->pos] = c->saved; in fmemseeker64()
241 c->saved = '\0'; in fmemseeker64()
243 c->pos = offset; in fmemseeker64()
244 if (c->writeonly && c->pos < c->eof) in fmemseeker64()
246 c->saved = c->buf[c->pos]; in fmemseeker64()
247 c->buf[c->pos] = '\0'; in fmemseeker64()
260 free (c->storage); in fmemcloser()
264 /* Open a memstream around buffer BUF of SIZE bytes, using MODE.
269 size_t size, in fmemopen() argument
279 if (!size || !(buf || flags & __SRW)) in fmemopen()
286 if ((c = (fmemcookie *) malloc (sizeof *c + (buf ? 0 : size))) in fmemopen()
290 fp->_flags = 0; /* release */ in fmemopen()
292 __lock_close_recursive (fp->_lock); in fmemopen()
298 c->storage = c; in fmemopen()
299 c->max = size; in fmemopen()
302 c->writeonly = (flags & __SWR) != 0; in fmemopen()
303 c->saved = '\0'; in fmemopen()
307 c->buf = (char *) (c + 1); in fmemopen()
308 c->buf[0] = '\0'; in fmemopen()
309 c->pos = c->eof = 0; in fmemopen()
310 c->append = (flags & __SAPP) != 0; in fmemopen()
314 c->buf = (char *) buf; in fmemopen()
318 /* a/a+ and buf: position and size at first NUL. */ in fmemopen()
319 buf = memchr (c->buf, '\0', size); in fmemopen()
320 c->eof = c->pos = buf ? (size_t) ((char *) buf - c->buf) : size; in fmemopen()
321 if (!buf && c->writeonly) in fmemopen()
322 /* a: guarantee a NUL within size even if no writes. */ in fmemopen()
323 c->buf[size - 1] = '\0'; in fmemopen()
324 c->append = 1; in fmemopen()
327 /* r/r+ and buf: read at beginning, full size available. */ in fmemopen()
328 c->pos = c->append = 0; in fmemopen()
329 c->eof = size; in fmemopen()
333 c->pos = c->append = c->eof = 0; in fmemopen()
334 *c->buf = '\0'; in fmemopen()
342 fp->_file = -1; in fmemopen()
343 fp->_flags = flags; in fmemopen()
344 fp->_cookie = c; in fmemopen()
345 fp->_read = flags & (__SRD | __SRW) ? fmemreader : NULL; in fmemopen()
346 fp->_write = flags & (__SWR | __SRW) ? fmemwriter : NULL; in fmemopen()
347 fp->_seek = fmemseeker; in fmemopen()
349 fp->_seek64 = fmemseeker64; in fmemopen()
350 fp->_flags |= __SL64; in fmemopen()
352 fp->_close = fmemcloser; in fmemopen()