1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "tests.h"
29 #include <asm/unistd.h>
30 
31 #ifdef __NR_readdir
32 
33 # include <assert.h>
34 # include <dirent.h>
35 # include <fcntl.h>
36 # include <stdio.h>
37 # include <string.h>
38 # include <sys/stat.h>
39 # include <unistd.h>
40 
41 static const char fname[] =
42 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
43 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
44 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
45 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
46 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
47 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
48 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
49 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nZ";
50 static const char qname[] =
51 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
52 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
53 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
54 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
55 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
56 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
57 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
58 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nZ";
59 
60 int
main(int ac,const char ** av)61 main(int ac, const char **av)
62 {
63 	char *dname;
64 	struct {
65 		unsigned long   d_ino;
66 		unsigned long   d_off;
67 		unsigned short  d_reclen;
68 		char d_name[1024];
69 	} e;
70 	int rc;
71 
72 	assert(ac == 1);
73 	assert(asprintf(&dname, "%s.test.tmp.dir", av[0]) > 0);
74 	assert(!mkdir(dname, 0700));
75 	assert(!chdir(dname));
76 	(void) close(0);
77 	assert(!creat(fname, 0600));
78 	assert(!close(0));
79 	assert(!open(".", O_RDONLY | O_DIRECTORY));
80 	while ((rc = syscall(__NR_readdir, 0, &e, 1))) {
81 		if (rc < 0)
82 			perror_msg_and_skip("readdir");
83 		e.d_name[e.d_reclen] = '\0';
84 		printf("readdir(0, {d_ino=%lu, d_off=%lu, d_reclen=%u"
85 		       ", d_name=\"%s\"}) = %d\n",
86 		       e.d_ino, e.d_off, e.d_reclen,
87 		       e.d_name[0] == '.' ? e.d_name : qname, rc);
88 	}
89 	printf("readdir(0, %p) = 0\n", &e);
90 	puts("+++ exited with 0 +++");
91 	assert(!unlink(fname));
92 	assert(!chdir(".."));
93 	assert(!rmdir(dname));
94 
95 	return 0;
96 }
97 
98 #else
99 
100 SKIP_MAIN_UNDEFINED("__NR_readdir")
101 
102 #endif
103