summaryrefslogtreecommitdiffstats
path: root/basic/test6.c
blob: 1d64c882bf5855c5034f6261dc8c4f26b2ae451e (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 *	@(#)test6.c	1.5	99/08/29 Connectathon Testsuite
 *	1.4 Lachman ONC Test Suite source
 *
 * Test readdir
 *
 * Uses the following important system/library calls against the server:
 *
 *	chdir()
 *	mkdir()		(for initial directory creation if not -m)
 *	creat()
 *	unlink()
 *	opendir(), rewinddir(), readdir(), closedir()
 */

#if defined (DOS) || defined (WIN32)
/* If Dos, Windows or Win32 */
#define DOSorWIN32
#endif

#ifndef DOSorWIN32
#include <sys/param.h>
#include <unistd.h>
#include <string.h>
#ifdef use_directs
#include <sys/dir.h>
#else
#include <dirent.h>
#endif
#endif /* DOSorWIN32 */

#include <sys/types.h>
#include <sys/stat.h>
#ifdef DOSorWIN32
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <stdio.h>
#include <stdlib.h>

#include "../tests.h"

static int Tflag = 0;	/* print timing */
static int Fflag = 0;	/* test function only;  set count to 1, negate -t */
static int Nflag = 0;	/* Suppress directory operations */
static int Iflag = 0;	/* Ignore non-test files dir entries */

#define MAXFILES 512		/* maximum files allowed for this test */
#define BITMOD	 8		/* bits per u_char */
static unsigned char bitmap[MAXFILES / BITMOD];

#define BIT(x)    (bitmap[(x) / BITMOD] &   (1 << ((x) % BITMOD)) )
#define SETBIT(x) (bitmap[(x) / BITMOD] |=  (1 << ((x) % BITMOD)) )
#define CLRBIT(x) (bitmap[(x) / BITMOD] &= ~(1 << ((x) % BITMOD)) )

static void
usage()
{
	fprintf(stdout, "usage: %s [-htfni] [files count fname]\n", Myname);
	fprintf(stdout, "  Flags:  h    Help - print this usage info\n");
	fprintf(stdout, "          t    Print execution time statistics\n");
	fprintf(stdout, "          f    Test function only (negate -t)\n");
	fprintf(stdout, "          n    Suppress test directory create operations\n");
	fprintf(stdout, "          i    Ignore non-test files dir entries\n");
}

main(argc, argv)
	int argc;
	char *argv[];
{
#ifdef use_directs
	struct direct *dp;
#else
	struct dirent *dp;
#endif
	char *fname = FNAME;
	int files = 200;	/* number of files in each dir */
	int fi;
	int count = 200;	/* times to read dir */
	int ct;
	int entries = 0;
	int totfiles = 0;
	int totdirs = 0;
	DIR *dir;
	struct timeval time;
	char *p, str[MAXPATHLEN];
	char *opts;
	int err, i, dot, dotdot;
	int nmoffset;

	umask(0);
	setbuf(stdout, NULL);
	Myname = *argv++;
	argc--;
	while (argc && **argv == '-') {
		for (opts = &argv[0][1]; *opts; opts++) {
			switch (*opts) {
				case 'h':	/* help */
					usage();
					exit(1);
					break;

				case 't':	/* time */
					Tflag++;
					break;
				
				case 'f':	/* funtionality */
					Fflag++;
					break;
				
				case 'n':	/* No Test Directory create */
					Nflag++;
					break;

				case 'i':	/* ignore spurious files */
					Iflag++;
					break;

				default:
					error("unknown option '%c'", *opts);
					usage();
					exit(1);
			}
		}
		argc--;
		argv++;
	}

	if (argc) {
		files = getparm(*argv, 1, "files");
		argv++;
		argc--;
	}
	if (argc) {
		count = getparm(*argv, 1, "count");
		argv++;
		argc--;
	}
	if (argc) {
		fname = *argv;
		argv++;
		argc--;
	}
	if (argc) {
		usage();
		exit(1);
	}

	nmoffset = strlen(fname);

	if (Fflag) {
		Tflag = 0;
		count = 1;
	}

	if (count > files) {
		error("count (%d) can't be greater than files (%d)",
			count, files);
		exit(1);
	}

	if (files > MAXFILES) {
		error("too many files requested (max is %d)", MAXFILES);
		exit(1);
	}

	fprintf(stdout, "%s: readdir\n", Myname);

	if (!Nflag)
		testdir(NULL);
	else
		mtestdir(NULL);

	dirtree(1, files, 0, fname, DNAME, &totfiles, &totdirs);

	if (Tflag) {
		starttime();
	}

	if ((dir = opendir(".")) == NULL) {
		error("can't opendir %s", ".");
		exit(1);
	}

	for (ct = 0; ct < count; ct++) {
		rewinddir(dir);
		dot = 0;
		dotdot = 0;
		err = 0;
		for (i = 0; i < sizeof(bitmap); i++)
			bitmap[i] = 0;
		while ((dp = readdir(dir)) != NULL) {
			entries++;
			if (strcmp(".", dp->d_name) == 0) {
				if (dot) {
					/* already read dot */
					error("'.' dir entry read twice");
					exit(1);
				}
				dot++;
				continue;
			} else if (strcmp("..", dp->d_name) == 0) {
				if (dotdot) {
					/* already read dotdot */
					error("'..' dir entry read twice");
					exit(1);
				}
				dotdot++;
				continue;
			}

			/*
			 * at this point, should have entry of the form
			 *  fname%d
			 */
			/* If we don't have our own directory, ignore
			   such errors (if Iflag set). */
			if (strncmp(dp->d_name, fname, nmoffset)) {
				if (Iflag)
					continue;
				else {
					error("unexpected dir entry '%s'",
						dp->d_name);
					exit(1);
				}
			}

			/* get ptr to numeric part of name */
			p = dp->d_name + nmoffset;
			fi = atoi(p);
			if (fi < 0 || fi >= MAXFILES) {
				error("unexpected dir entry '%s'",
					dp->d_name);
				exit(1);
			}
			if (BIT(fi)) {
				error("duplicate '%s' dir entry read",
					dp->d_name);
				err++;
			} else
				SETBIT(fi);
		}	/* end readdir loop */
		if (!dot) {
			error("didn't read '.' dir entry, pass %d", ct);
			err++;
		}
		if (!dotdot) {
			error("didn't read '..' dir entry, pass %d", ct);
			err++;
		}
		for (fi = 0; fi < ct; fi++) {
			if (BIT(fi)) {
				sprintf(str, "%s%d", fname, fi);
				error("unlinked '%s' dir entry read pass %d",
					str, ct);
				err++;
			}
		}
		for (fi = ct; fi < files; fi++) {
			if (!BIT(fi)) {
				sprintf(str, "%s%d", fname, fi);
				error("\
didn't read expected '%s' dir entry, pass %d", str, ct);
				err++;
			}
		}
		if (err) {
			error("Test failed with %d errors", err);
			exit(1);
		}
		sprintf(str, "%s%d", fname, ct);
		if (unlink(str) < 0) {
			error("can't unlink %s", str);
			exit(1);
		}
	}

	closedir(dir);

	if (Tflag) {
		endtime(&time);
	}
	fprintf(stdout, "\t%d entries read, %d files",
		entries, files);
	if (Tflag) {
		fprintf(stdout, " in %ld.%-2ld seconds",
		    (long)time.tv_sec, (long)time.tv_usec / 10000);
	}
	fprintf(stdout, "\n");
	rmdirtree(1, files, 0, fname, DNAME, &totfiles, &totdirs, 1);
	complete();
}