Fix intermittent failure in tst-getpid2.
[glibc.git] / dirent / tst-seekdir.c
blob43808fecb58a48d41bae0464293cae0a677e8c1b
1 #include <stdio.h>
2 #include <dirent.h>
3 #include <stdlib.h>
5 int
6 main (int argc, char *argv[])
8 DIR * dirp;
9 long int save3 = 0;
10 long int cur;
11 int i = 0;
12 int result = 0;
13 struct dirent *dp;
14 long int save0;
15 long int rewind;
17 dirp = opendir (".");
18 if (dirp == NULL)
20 printf ("opendir failed: %m\n");
21 return 1;
24 save0 = telldir (dirp);
25 if (save0 == -1)
27 printf ("telldir failed: %m\n");
28 result = 1;
31 for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
33 /* save position 3 (after fourth entry) */
34 if (i++ == 3)
35 save3 = telldir (dirp);
37 printf ("%s\n", dp->d_name);
39 /* stop at 400 (just to make sure dirp->__offset and dirp->__size are
40 scrambled */
41 if (i == 400)
42 break;
45 printf ("going back past 4-th entry...\n");
47 /* go back to saved entry */
48 seekdir (dirp, save3);
50 /* Check whether telldir equals to save3 now. */
51 cur = telldir (dirp);
52 if (cur != save3)
54 printf ("seekdir (d, %ld); telldir (d) == %ld\n", save3, cur);
55 result = 1;
58 /* print remaining files (3-last) */
59 for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
60 printf ("%s\n", dp->d_name);
62 /* Check rewinddir */
63 rewinddir (dirp);
64 rewind = telldir (dirp);
65 if (rewind == -1)
67 printf ("telldir failed: %m\n");
68 result = 1;
70 else if (save0 != rewind)
72 printf ("rewinddir didn't reset directory stream\n");
73 result = 1;
76 closedir (dirp);
77 return result;