preadwrite.c 3.9 KB

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