1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
6 * Mountain View, CA 94043, or:
7 *
8 * http://www.sgi.com
9 *
10 * For further information regarding this notice, see:
11 *
12 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
13 *
14 */
15
16 /*\
17 * [Description]
18 *
19 * The test for the readdir(2) system call.
20 * Create n files and check that readdir() finds n files
21 */
22 #include <stdio.h>
23 #include "tst_test.h"
24
25 static const char prefix[] = "readdirfile";
26 static int nfiles = 10;
27
setup(void)28 static void setup(void)
29 {
30 char fname[255];
31 int i;
32 int fd;
33
34 for (i = 0; i < nfiles; i++) {
35 sprintf(fname, "%s_%d", prefix, i);
36 fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
37 SAFE_WRITE(1, fd, "hello\n", 6);
38 SAFE_CLOSE(fd);
39 }
40 }
41
verify_readdir(void)42 static void verify_readdir(void)
43 {
44 int cnt = 0;
45 DIR *test_dir;
46 struct dirent *ent;
47
48 test_dir = SAFE_OPENDIR(".");
49 while ((ent = SAFE_READDIR(test_dir))) {
50 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
51 continue;
52 if (!strncmp(ent->d_name, prefix, sizeof(prefix) - 1))
53 cnt++;
54 }
55
56 if (cnt == nfiles) {
57 tst_res(TPASS, "found all %d that were created", nfiles);
58 } else {
59 tst_res(TFAIL, "found %s files than were created, created: %d, found: %d",
60 cnt > nfiles ? "more" : "less", nfiles, cnt);
61 }
62 }
63
64 static struct tst_test test = {
65 .setup = setup,
66 .test_all = verify_readdir,
67 .needs_tmpdir = 1,
68 };
69