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