| 1 | #include <errno.h>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 | #include <unistd.h>
|
|---|
| 5 |
|
|---|
| 6 | #ifndef HAVE_64BIT_FILEIO_TYPES
|
|---|
| 7 | #define fopen64 fopen
|
|---|
| 8 | #endif
|
|---|
| 9 |
|
|---|
| 10 | static char fname[] = "/tmp/rndseek.XXXXXX";
|
|---|
| 11 | static char tempdata[65 * 1024];
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | static int do_test (void);
|
|---|
| 15 | #define TEST_FUNCTION do_test ()
|
|---|
| 16 | #define TIMEOUT 10
|
|---|
| 17 |
|
|---|
| 18 | #include "../test-skeleton.c"
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | static int
|
|---|
| 22 | fp_test (const char *name, FILE *fp)
|
|---|
| 23 | {
|
|---|
| 24 | int result = 0;
|
|---|
| 25 | int rounds = 10000;
|
|---|
| 26 |
|
|---|
| 27 | do
|
|---|
| 28 | {
|
|---|
| 29 | int idx = random () % (sizeof (tempdata) - 2);
|
|---|
| 30 | char ch1;
|
|---|
| 31 | char ch2;
|
|---|
| 32 |
|
|---|
| 33 | if (fseek (fp, idx, SEEK_SET) != 0)
|
|---|
| 34 | {
|
|---|
| 35 | printf ("%s: %d: fseek failed: %m\n", name, rounds);
|
|---|
| 36 | result = 1;
|
|---|
| 37 | break;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | ch1 = fgetc_unlocked (fp);
|
|---|
| 41 | ch2 = tempdata[idx];
|
|---|
| 42 | if (ch1 != ch2)
|
|---|
| 43 | {
|
|---|
| 44 | printf ("%s: %d: character at index %d not what is expected ('%c' vs '%c')\n",
|
|---|
| 45 | name, rounds, idx, ch1, ch2);
|
|---|
| 46 | result = 1;
|
|---|
| 47 | break;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | ch1 = fgetc (fp);
|
|---|
| 51 | ch2 = tempdata[idx + 1];
|
|---|
| 52 | if (ch1 != ch2)
|
|---|
| 53 | {
|
|---|
| 54 | printf ("%s: %d: character at index %d not what is expected ('%c' vs '%c')\n",
|
|---|
| 55 | name, rounds, idx + 1, ch1, ch2);
|
|---|
| 56 | result = 1;
|
|---|
| 57 | break;
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | while (--rounds > 0);
|
|---|
| 61 |
|
|---|
| 62 | fclose (fp);
|
|---|
| 63 |
|
|---|
| 64 | return result;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 | static int
|
|---|
| 69 | do_test (void)
|
|---|
| 70 | {
|
|---|
| 71 | int fd;
|
|---|
| 72 | FILE *fp;
|
|---|
| 73 | size_t i;
|
|---|
| 74 | int result;
|
|---|
| 75 |
|
|---|
| 76 | fd = mkstemp (fname);
|
|---|
| 77 | if (fd == -1)
|
|---|
| 78 | {
|
|---|
| 79 | printf ("cannot open temporary file: %m\n");
|
|---|
| 80 | return 1;
|
|---|
| 81 | }
|
|---|
| 82 | /* Make sure the file gets removed. */
|
|---|
| 83 | add_temp_file (fname);
|
|---|
| 84 |
|
|---|
| 85 | /* Repeatability demands this. */
|
|---|
| 86 | srandom (42);
|
|---|
| 87 |
|
|---|
| 88 | /* First create some temporary data. */
|
|---|
| 89 | for (i = 0; i < sizeof (tempdata); ++i)
|
|---|
| 90 | tempdata[i] = 'a' + random () % 26;
|
|---|
| 91 |
|
|---|
| 92 | /* Write this data to a file. */
|
|---|
| 93 | if (TEMP_FAILURE_RETRY (write (fd, tempdata, sizeof (tempdata)))
|
|---|
| 94 | != sizeof (tempdata))
|
|---|
| 95 | {
|
|---|
| 96 | printf ("cannot wrote data to temporary file: %m\n");
|
|---|
| 97 | return 1;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /* Now try reading the data. */
|
|---|
| 101 | fp = fdopen (dup (fd), "r");
|
|---|
| 102 | if (fp == NULL)
|
|---|
| 103 | {
|
|---|
| 104 | printf ("cannot duplicate temporary file descriptor: %m\n");
|
|---|
| 105 | return 1;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | rewind (fp);
|
|---|
| 109 | for (i = 0; i < sizeof (tempdata); ++i)
|
|---|
| 110 | {
|
|---|
| 111 | int ch0 = fgetc (fp);
|
|---|
| 112 | char ch1 = ch0;
|
|---|
| 113 | char ch2 = tempdata[i];
|
|---|
| 114 |
|
|---|
| 115 | if (ch0 == EOF)
|
|---|
| 116 | {
|
|---|
| 117 | puts ("premature end of file while reading data");
|
|---|
| 118 | return 1;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | if (ch1 != ch2)
|
|---|
| 122 | {
|
|---|
| 123 | printf ("%zd: '%c' vs '%c'\n", i, ch1, ch2);
|
|---|
| 124 | return 1;
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | result = fp_test ("fdopen(\"r\")", fp);
|
|---|
| 129 |
|
|---|
| 130 | fp = fopen (fname, "r");
|
|---|
| 131 | result |= fp_test ("fopen(\"r\")", fp);
|
|---|
| 132 |
|
|---|
| 133 | fp = fopen64 (fname, "r");
|
|---|
| 134 | result |= fp_test ("fopen64(\"r\")", fp);
|
|---|
| 135 |
|
|---|
| 136 | /* The "rw" mode will prevent the mmap-using code from being used. */
|
|---|
| 137 | fp = fdopen (fd, "rw");
|
|---|
| 138 | result = fp_test ("fdopen(\"rw\")", fp);
|
|---|
| 139 |
|
|---|
| 140 | fp = fopen (fname, "rw");
|
|---|
| 141 | result |= fp_test ("fopen(\"rw\")", fp);
|
|---|
| 142 |
|
|---|
| 143 | fp = fopen64 (fname, "rw");
|
|---|
| 144 | result |= fp_test ("fopen64(\"rw\")", fp);
|
|---|
| 145 |
|
|---|
| 146 | return result;
|
|---|
| 147 | }
|
|---|