7 static char fname
[] = "/tmp/rndseek.XXXXXX";
8 static char tempdata
[65 * 1024];
11 static int do_test (void);
12 #define TEST_FUNCTION do_test ()
14 #include "../test-skeleton.c"
18 fp_test (const char *name
, FILE *fp
)
25 int idx
= random () % (sizeof (tempdata
) - 2);
29 if (fseek (fp
, idx
, SEEK_SET
) != 0)
31 printf ("%s: %d: fseek failed: %m\n", name
, rounds
);
36 ch1
= fgetc_unlocked (fp
);
40 printf ("%s: %d: character at index %d not what is expected ('%c' vs '%c')\n",
41 name
, rounds
, idx
, ch1
, ch2
);
47 ch2
= tempdata
[idx
+ 1];
50 printf ("%s: %d: character at index %d not what is expected ('%c' vs '%c')\n",
51 name
, rounds
, idx
+ 1, ch1
, ch2
);
75 printf ("cannot open temporary file: %m\n");
78 /* Make sure the file gets removed. */
79 add_temp_file (fname
);
81 /* Repeatability demands this. */
84 /* First create some temporary data. */
85 for (i
= 0; i
< sizeof (tempdata
); ++i
)
86 tempdata
[i
] = 'a' + random () % 26;
88 /* Write this data to a file. */
89 if (TEMP_FAILURE_RETRY (write (fd
, tempdata
, sizeof (tempdata
)))
92 printf ("cannot wrote data to temporary file: %m\n");
96 /* Now try reading the data. */
97 fp
= fdopen (dup (fd
), "r");
100 printf ("cannot duplicate temporary file descriptor: %m\n");
105 for (i
= 0; i
< sizeof (tempdata
); ++i
)
107 int ch0
= fgetc (fp
);
109 char ch2
= tempdata
[i
];
113 puts ("premature end of file while reading data");
119 printf ("%zd: '%c' vs '%c'\n", i
, ch1
, ch2
);
124 result
= fp_test ("fdopen(\"r\")", fp
);
126 fp
= fopen (fname
, "r");
127 result
|= fp_test ("fopen(\"r\")", fp
);
129 fp
= fopen64 (fname
, "r");
130 result
|= fp_test ("fopen64(\"r\")", fp
);
132 /* The "rw" mode will prevent the mmap-using code from being used. */
133 fp
= fdopen (fd
, "rw");
134 result
= fp_test ("fdopen(\"rw\")", fp
);
136 fp
= fopen (fname
, "rw");
137 result
|= fp_test ("fopen(\"rw\")", fp
);
139 fp
= fopen64 (fname
, "rw");
140 result
|= fp_test ("fopen64(\"rw\")", fp
);