tst-mmap-setvbuf.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Test setvbuf on readonly fopen (using mmap stdio).
  2. Copyright (C) 2002-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
  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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. int main (void)
  21. {
  22. char name[] = "/tmp/tst-mmap-setvbuf.XXXXXX";
  23. char buf[4096];
  24. const char * const test = "Let's see if mmap stdio works with setvbuf.\n";
  25. char temp[strlen (test) + 1];
  26. int fd = mkstemp (name);
  27. FILE *f;
  28. if (fd == -1)
  29. {
  30. printf ("%u: cannot open temporary file: %m\n", __LINE__);
  31. exit (1);
  32. }
  33. f = fdopen (fd, "w");
  34. if (f == NULL)
  35. {
  36. printf ("%u: cannot fdopen temporary file: %m\n", __LINE__);
  37. exit (1);
  38. }
  39. fputs (test, f);
  40. fclose (f);
  41. f = fopen (name, "rm");
  42. if (f == NULL)
  43. {
  44. printf ("%u: cannot fopen temporary file: %m\n", __LINE__);
  45. exit (1);
  46. }
  47. if (setvbuf (f, buf, _IOFBF, sizeof buf))
  48. {
  49. printf ("%u: setvbuf failed: %m\n", __LINE__);
  50. exit (1);
  51. }
  52. if (fread (temp, 1, strlen (test), f) != strlen (test))
  53. {
  54. printf ("%u: couldn't read the file back: %m\n", __LINE__);
  55. exit (1);
  56. }
  57. temp [strlen (test)] = '\0';
  58. if (strcmp (test, temp))
  59. {
  60. printf ("%u: read different string than was written:\n%s%s",
  61. __LINE__, test, temp);
  62. exit (1);
  63. }
  64. fclose (f);
  65. unlink (name);
  66. exit (0);
  67. }