1/*-
2 * Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
--- 10 unchanged lines hidden (view full) ---
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/lib/libc/stdio/fmemopen.c 246206 2013-02-01 13:04:06Z gahr $");
28
29#include <fcntl.h>
30#include <stdbool.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <errno.h>
35#include "local.h"
--- 16 unchanged lines hidden (view full) ---
52FILE *
53fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
54{
55 struct fmemopen_cookie *ck;
56 FILE *f;
57 int flags, rc;
58
59 /*
60 * Retrieve the flags as used by open(2) from the mode argument, and
61 * validate them.
62 */
63 rc = __sflags(mode, &flags);
64 if (rc == 0) {
65 errno = EINVAL;
66 return (NULL);
67 }
--- 46 unchanged lines hidden (view full) ---
114 * for append (binary-mode), the size of the buffer
115 *
116 * for read, the size of the buffer
117 *
118 * for write, 0
119 */
120 switch (mode[0]) {
121 case 'a':
122 if (ck->bin) {
123 /*
124 * This isn't useful, since the buffer isn't allowed
125 * to grow.
126 */
127 ck->off = ck->len = size;
128 } else
129 ck->off = ck->len = strnlen(ck->buf, ck->size);
130 break;
131 case 'r':
132 ck->len = size;
133 break;
134 case 'w':
135 ck->len = 0;
136 break;
137 }
--- 121 unchanged lines hidden ---
2 * Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
--- 10 unchanged lines hidden (view full) ---
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/lib/libc/stdio/fmemopen.c 246206 2013-02-01 13:04:06Z gahr $");
28
29#include <fcntl.h>
30#include <stdbool.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <errno.h>
35#include "local.h"
--- 16 unchanged lines hidden (view full) ---
52FILE *
53fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
54{
55 struct fmemopen_cookie *ck;
56 FILE *f;
57 int flags, rc;
58
59 /*
60 * Retrieve the flags as used by open(2) from the mode argument, and
61 * validate them.
62 */
63 rc = __sflags(mode, &flags);
64 if (rc == 0) {
65 errno = EINVAL;
66 return (NULL);
67 }
--- 46 unchanged lines hidden (view full) ---
114 * for append (binary-mode), the size of the buffer
115 *
116 * for read, the size of the buffer
117 *
118 * for write, 0
119 */
120 switch (mode[0]) {
121 case 'a':
122 if (ck->bin) {
123 /*
124 * This isn't useful, since the buffer isn't allowed
125 * to grow.
126 */
127 ck->off = ck->len = size;
128 } else
129 ck->off = ck->len = strnlen(ck->buf, ck->size);
130 break;
131 case 'r':
132 ck->len = size;
133 break;
134 case 'w':
135 ck->len = 0;
136 break;
137 }
--- 121 unchanged lines hidden ---