tst-preadwrite.c 2.9 KB

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