1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/* $OpenBSD: telldir.c,v 1.3 2013/08/20 01:13:54 guenther Exp $ */
/* Written by Otto Moerbeek, 2006, Public domain. */
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <err.h>
#include <limits.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define NFILES 1000
void
createfiles(void)
{
int i, fd;
char file[PATH_MAX];
mkdir("d", 0755);
for (i = 0; i < NFILES; i++) {
snprintf(file, sizeof file, "d/%d", i);
if ((fd = open(file, O_CREAT | O_WRONLY, 0600)) == -1)
err(1, "open %s", file);
close(fd);
}
}
void
delfiles(void)
{
DIR *dp;
struct dirent *f;
char file[PATH_MAX];
dp = opendir("d");
if (dp == NULL)
err(1, "opendir");
while (f = readdir(dp)) {
if (strcmp(f->d_name, ".") == 0 ||
strcmp(f->d_name, "..") == 0)
continue;
snprintf(file, sizeof file, "d/%s", f->d_name);
if (unlink(file) == -1)
err(1, "unlink %s", f->d_name);
}
closedir(dp);
if (rmdir("d") == -1)
err(1, "rmdir");
}
void
loop(DIR *dp, int i)
{
struct dirent *f;
char file[PATH_MAX];
long pos, t, remember = -1;
rewinddir(dp);
snprintf(file, sizeof file, "%d", i);
for (;;) {
pos = telldir(dp);
f = readdir(dp);
if (f == NULL)
break;
if (strcmp(file, f->d_name) == 0)
remember = pos;
}
if (remember == -1)
errx(1, "remember %s", file);
seekdir(dp, remember);
if ((t = telldir(dp)) != remember)
errx(1, "tell after seek %s %ld != %ld", file, t, pos);
if ((t = telldir(dp)) != remember)
errx(1, "tell after tell %s %ld != %ld", file, t, pos);
f = readdir(dp);
if (f == NULL)
err(1, "seek to %s %ld", file, remember);
if (strcmp(f->d_name, file) != 0)
err(1, "name mismatch: %s != %s\n", f->d_name, file);
}
int
main(void)
{
DIR *dp;
int i;
createfiles();
dp = opendir("d");
if (dp == NULL)
err(1, "opendir");
for (i = 0; i < NFILES; i++)
loop(dp, (i + NFILES/2) % NFILES);
closedir(dp);
delfiles();
return 0;
}
|