1 /* basic fmemopen interface testing.
2 Copyright (C) 2014 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/>. */
27 #include <sys/types.h>
29 static char *test_file
;
32 do_prepare (int argc
, char *argv
[])
34 /* Construct the test file name based on ARGV[0], which will be
35 an absolute file name in the build directory. Don't touch the
36 source directory, which might be read-only. */
37 if (asprintf (&test_file
, "%s.test", argv
[0]) < 0)
39 puts ("asprintf failed\n");
47 const char blah
[] = "BLAH";
54 /* setup the physical file, and use it */
55 if ((fp
= fopen (test_file
, "w+")) == NULL
)
57 if (fwrite (blah
, 1, strlen (blah
), fp
) != strlen (blah
))
66 while ((ch
= getc (fp
)) != EOF
)
71 printf ("\ncharacter %td: '%c' instead of '%c'\n",
95 /* Now, mmap the file into a buffer, and do that too */
96 if ((fd
= open (test_file
, O_RDONLY
)) == -1)
98 printf ("open (%s, O_RDONLY) failed\n", test_file
);
101 if (fstat (fd
, &fs
) == -1)
103 printf ("stat (%i)\n", fd
);
107 if ((mmap_data
= (char *) mmap (NULL
, fs
.st_size
, PROT_READ
,
108 MAP_SHARED
, fd
, 0)) == MAP_FAILED
)
110 printf ("mmap (NULL, %zu, PROT_READ, MAP_SHARED, %i, 0) failed\n",
115 if ((fp
= fmemopen (mmap_data
, fs
.st_size
, "r")) == NULL
)
117 printf ("fmemopen (%p, %zu) failed\n", mmap_data
, fs
.st_size
);
123 while ((ch
= getc (fp
)) != EOF
)
128 printf ("%td character: '%c' instead of '%c'\n",
136 fputc ('\n', stdout
);
145 printf ("fp: EOF\n");
155 munmap (mmap_data
, fs
.st_size
);
163 #define PREPARE(argc, argv) do_prepare (argc, argv)
164 #define TEST_FUNCTION do_test ()
165 #include "../test-skeleton.c"