tst-preadwrite.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <error.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. /* Allow testing of the 64-bit versions as well. */
  21. #ifndef PREAD
  22. # define PREAD pread
  23. # define PWRITE pwrite
  24. #endif
  25. #define STRINGIFY(s) STRINGIFY2 (s)
  26. #define STRINGIFY2(s) #s
  27. /* Prototype for our test function. */
  28. extern void do_prepare (int argc, char *argv[]);
  29. extern int do_test (int argc, char *argv[]);
  30. /* We have a preparation function. */
  31. #define PREPARE do_prepare
  32. /* We might need a bit longer timeout. */
  33. #define TIMEOUT 20 /* sec */
  34. /* This defines the `main' function and some more. */
  35. #include "../test-skeleton.c"
  36. /* These are for the temporary file we generate. */
  37. char *name;
  38. int fd;
  39. void
  40. do_prepare (int argc, char *argv[])
  41. {
  42. char name_len;
  43. #define FNAME FNAME2(TRUNCATE)
  44. #define FNAME2(s) "/" STRINGIFY(s) "XXXXXX"
  45. name_len = strlen (test_dir);
  46. name = malloc (name_len + sizeof (FNAME));
  47. if (name == NULL)
  48. error (EXIT_FAILURE, errno, "cannot allocate file name");
  49. mempcpy (mempcpy (name, test_dir, name_len), FNAME, sizeof (FNAME));
  50. add_temp_file (name);
  51. /* Open our test file. */
  52. fd = mkstemp (name);
  53. if (fd == -1)
  54. error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
  55. }
  56. int
  57. do_test (int argc, char *argv[])
  58. {
  59. char buf[1000];
  60. char res[1000];
  61. int i;
  62. memset (buf, '\0', sizeof (buf));
  63. memset (res, '\xff', sizeof (res));
  64. if (write (fd, buf, sizeof (buf)) != sizeof (buf))
  65. error (EXIT_FAILURE, errno, "during write");
  66. for (i = 100; i < 200; ++i)
  67. buf[i] = i;
  68. if (PWRITE (fd, buf + 100, 100, 100) != 100)
  69. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
  70. for (i = 450; i < 600; ++i)
  71. buf[i] = i;
  72. if (PWRITE (fd, buf + 450, 150, 450) != 150)
  73. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
  74. if (PREAD (fd, res, sizeof (buf) - 50, 50) != sizeof (buf) - 50)
  75. error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PREAD));
  76. close (fd);
  77. unlink (name);
  78. return memcmp (buf + 50, res, sizeof (buf) - 50);
  79. }