tst-preadwrite.c 2.9 KB

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