added -W
[tridge/junkcode.git] / bsd_telldir.c
1 /*
2   test readdir/unlink pattern that OS/2 uses
3   tridge@samba.org July 2005
4 */
5
6 #define _GNU_SOURCE
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <dirent.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <fcntl.h>
16
17 #define NUM_FILES 29
18
19 #define TESTDIR "test.dir"
20
21 #define FAILED() (fprintf(stderr, "Failed at %s:%d - %s\n", __FUNCTION__, __LINE__, strerror(errno)), exit(1), 1)
22
23 static void cleanup(void)
24 {
25         /* I'm a lazy bastard */
26         system("rm -rf " TESTDIR);
27         mkdir(TESTDIR, 0700) == 0 || FAILED();
28 }
29
30 static void create_files()
31 {
32         int i;
33         for (i=0;i<NUM_FILES;i++) {
34                 char fname[40];
35                 snprintf(fname, sizeof(fname), TESTDIR "/test%u.txt", i);
36                 close(open(fname, O_CREAT|O_RDWR, 0600)) == 0 || FAILED();
37         }
38 }
39
40 int main(void)
41 {
42         int total_deleted = 0;
43         DIR *d;
44         struct dirent *de;
45
46         cleanup();
47         create_files();
48         
49         d = opendir(TESTDIR);
50
51         chdir(TESTDIR) == 0 || FAILED();
52
53         /* skip past . and .. */
54         de = readdir(d);
55         strcmp(de->d_name, ".") == 0 || FAILED();
56         de = readdir(d);
57         strcmp(de->d_name, "..") == 0 || FAILED();
58
59         while ((de = readdir(d))) {
60                 off_t ofs = telldir(d);
61                 unlink(de->d_name) == 0 || FAILED();
62
63                 /* move one more position on */
64                 readdir(d);
65
66                 /* seek to just after the first readdir() */
67                 seekdir(d, ofs);
68                 total_deleted++;
69         }
70         closedir(d);
71
72         printf("Deleted %d files of %d\n", total_deleted, NUM_FILES);
73
74         chdir("..") == 0 || FAILED();
75
76         rmdir(TESTDIR) == 0 || FAILED();
77
78         return 0;
79 }