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