1 #include <stdio.h>
2 #include <dirent.h>
3 #include <stdlib.h>
4 
5 static int
do_test(void)6 do_test (void)
7 {
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;
16 
17   dirp = opendir (".");
18   if (dirp == NULL)
19     {
20       printf ("opendir failed: %m\n");
21       return 1;
22     }
23 
24   save0 = telldir (dirp);
25   if (save0 == -1)
26     {
27       printf ("telldir failed: %m\n");
28       result = 1;
29     }
30 
31   for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
32     {
33       /* save position 3 (after fourth entry) */
34       if (i++ == 3)
35 	save3 = telldir (dirp);
36 
37       printf ("%s\n", dp->d_name);
38 
39       /* stop at 400 (just to make sure dirp->__offset and dirp->__size are
40 	 scrambled */
41       if (i == 400)
42 	break;
43     }
44 
45   printf ("going back past 4-th entry...\n");
46 
47   /* go back to saved entry */
48   seekdir (dirp, save3);
49 
50   /* Check whether telldir equals to save3 now.  */
51   cur = telldir (dirp);
52   if (cur != save3)
53     {
54       printf ("seekdir (d, %ld); telldir (d) == %ld\n", save3, cur);
55       result = 1;
56     }
57 
58   /* print remaining files (3-last) */
59   for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
60     printf ("%s\n", dp->d_name);
61 
62   /* Check rewinddir */
63   rewinddir (dirp);
64   rewind = telldir (dirp);
65   if (rewind == -1)
66     {
67       printf ("telldir failed: %m\n");
68       result = 1;
69     }
70   else if (save0 != rewind)
71     {
72       printf ("rewinddir didn't reset directory stream\n");
73       result = 1;
74     }
75 
76   closedir (dirp);
77   return result;
78 }
79 
80 #define TEST_FUNCTION do_test ()
81 #include "../test-skeleton.c"
82