1 /* Test for fmemopen implementation.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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 <https://www.gnu.org/licenses/>. */
18
19 static char buffer[] = "foobar";
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <mcheck.h>
26
27 static int
do_bz18820(void)28 do_bz18820 (void)
29 {
30 char ch;
31 FILE *stream;
32
33 errno = 0;
34 stream = fmemopen (&ch, 1, "?");
35 if (stream)
36 {
37 printf ("fmemopen: expected NULL, got %p\n", stream);
38 fclose (stream);
39 return 1;
40 }
41 if (errno != EINVAL)
42 {
43 printf ("fmemopen: got %i, expected EINVAL (%i)\n", errno, EINVAL);
44 return 10;
45 }
46
47 stream = fmemopen (NULL, 42, "?");
48 if (stream)
49 {
50 printf ("fmemopen: expected NULL, got %p\n", stream);
51 fclose (stream);
52 return 2;
53 }
54
55 errno = 0;
56 stream = fmemopen (NULL, ~0, "w");
57 if (stream)
58 {
59 printf ("fmemopen: expected NULL, got %p\n", stream);
60 fclose (stream);
61 return 3;
62 }
63 if (errno != ENOMEM)
64 {
65 printf ("fmemopen: got %i, expected ENOMEM (%i)\n", errno, ENOMEM);
66 return 20;
67 }
68
69 return 0;
70 }
71
72 static int
do_test(void)73 do_test (void)
74 {
75 int ch;
76 FILE *stream;
77 int ret = 0;
78
79 mtrace ();
80
81 stream = fmemopen (buffer, strlen (buffer), "r+");
82
83 while ((ch = fgetc (stream)) != EOF)
84 printf ("Got %c\n", ch);
85
86 fputc ('1', stream);
87 if (fflush (stream) != EOF || errno != ENOSPC)
88 {
89 printf ("fflush didn't fail with ENOSPC\n");
90 ret = 1;
91 }
92
93 fclose (stream);
94
95 return ret + do_bz18820 ();
96 }
97
98 #define TEST_FUNCTION do_test ()
99 #include "../test-skeleton.c"
100