tst-preadwrite.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Tests for pread and pwrite.
  2. Copyright (C) 1998, 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <search.h>
  20. #include <errno.h>
  21. #include <error.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #define TESTFILE_NAME "CRAP.XXXXXX"
  25. #define STRINGIFY(s) STRINGIFY2 (s)
  26. #define STRINGIFY2(s) #s
  27. /* These are for the temporary file we generate. */
  28. char *name;
  29. int fd;
  30. /* Test the 32-bit versions first. */
  31. #define PREAD pread
  32. #define PWRITE pwrite
  33. int test(int argc, char *argv[])
  34. {
  35. char buf[1000];
  36. char res[1000];
  37. int i;
  38. memset (buf, '\0', sizeof (buf));
  39. memset (res, '\xff', sizeof (res));
  40. if (write (fd, buf, sizeof (buf)) != sizeof (buf))
  41. error (EXIT_FAILURE, errno, "during write");
  42. for (i = 100; i < 200; ++i)
  43. buf[i] = i;
  44. if (PWRITE (fd, buf + 100, 100, 100) != 100)
  45. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
  46. for (i = 450; i < 600; ++i)
  47. buf[i] = i;
  48. if (PWRITE (fd, buf + 450, 150, 450) != 150)
  49. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
  50. if (PREAD (fd, res, sizeof (buf) - 50, 50) != sizeof (buf) - 50)
  51. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PREAD));
  52. close (fd);
  53. unlink (name);
  54. return memcmp (buf + 50, res, sizeof (buf) - 50);
  55. }
  56. /* Test the 64-bit versions as well. */
  57. #if defined __UCLIBC_HAS_LFS__
  58. #undef PREAD
  59. #undef PWRITE
  60. #define PREAD pread64
  61. #define PWRITE pwrite64
  62. int test64(int argc, char *argv[])
  63. {
  64. char buf[1000];
  65. char res[1000];
  66. int i;
  67. memset (buf, '\0', sizeof (buf));
  68. memset (res, '\xff', sizeof (res));
  69. if (write (fd, buf, sizeof (buf)) != sizeof (buf))
  70. error (EXIT_FAILURE, errno, "during write");
  71. for (i = 100; i < 200; ++i)
  72. buf[i] = i;
  73. if (PWRITE (fd, buf + 100, 100, 100) != 100)
  74. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
  75. for (i = 450; i < 600; ++i)
  76. buf[i] = i;
  77. if (PWRITE (fd, buf + 450, 150, 450) != 150)
  78. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
  79. if (PREAD (fd, res, sizeof (buf) - 50, 50) != sizeof (buf) - 50)
  80. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PREAD));
  81. close (fd);
  82. unlink (name);
  83. return memcmp (buf + 50, res, sizeof (buf) - 50);
  84. }
  85. #endif
  86. void prepare(void)
  87. {
  88. if (!name) {
  89. name = malloc (BUFSIZ);
  90. if (name == NULL)
  91. error (EXIT_FAILURE, errno, "cannot allocate file name");
  92. }
  93. strncpy(name, TESTFILE_NAME, BUFSIZ);
  94. /* Open our test file. */
  95. fd = mkstemp (name);
  96. if (fd == -1)
  97. error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
  98. }
  99. int main (int argc, char **argv)
  100. {
  101. int result = 0;
  102. prepare();
  103. result+=test(argc, argv);
  104. if (result) {
  105. fprintf(stderr, "pread/pwrite test failed.\n");
  106. return(EXIT_FAILURE);
  107. }
  108. fprintf(stderr, "pread/pwrite test successful.\n");
  109. #if defined __UCLIBC_HAS_LFS__
  110. prepare();
  111. result+=test64(argc, argv);
  112. if (result) {
  113. fprintf(stderr, "pread64/pwrite64 test failed.\n");
  114. return(EXIT_FAILURE);
  115. }
  116. fprintf(stderr, "pread64/pwrite64 test successful.\n");
  117. #endif
  118. return(EXIT_SUCCESS);
  119. }