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 266971 2014-06-02 13:48:57Z 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 * POSIX says we shall return EINVAL if size is 0.
61 */
62 if (size == 0) {
63 errno = EINVAL;
64 return (NULL);
65 }
66
67 /*
68 * Retrieve the flags as used by open(2) from the mode argument, and
69 * validate them.
70 */
71 rc = __sflags(mode, &flags);
72 if (rc == 0) {
73 errno = EINVAL;
74 return (NULL);
75 }
--- 46 unchanged lines hidden (view full) ---
122 * for append (binary-mode), the size of the buffer
123 *
124 * for read, the size of the buffer
125 *
126 * for write, 0
127 */
128 switch (mode[0]) {
129 case 'a':
130 ck->off = ck->len = strnlen(ck->buf, ck->size);
131 break;
132 case 'r':
133 ck->len = size;
134 break;
135 case 'w':
136 ck->len = 0;
137 break;
138 }
--- 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 266971 2014-06-02 13:48:57Z 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 * POSIX says we shall return EINVAL if size is 0.
61 */
62 if (size == 0) {
63 errno = EINVAL;
64 return (NULL);
65 }
66
67 /*
68 * Retrieve the flags as used by open(2) from the mode argument, and
69 * validate them.
70 */
71 rc = __sflags(mode, &flags);
72 if (rc == 0) {
73 errno = EINVAL;
74 return (NULL);
75 }
--- 46 unchanged lines hidden (view full) ---
122 * for append (binary-mode), the size of the buffer
123 *
124 * for read, the size of the buffer
125 *
126 * for write, 0
127 */
128 switch (mode[0]) {
129 case 'a':
130 ck->off = ck->len = strnlen(ck->buf, ck->size);
131 break;
132 case 'r':
133 ck->len = size;
134 break;
135 case 'w':
136 ck->len = 0;
137 break;
138 }
--- 121 unchanged lines hidden ---