Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (c) 2015 Dmitry V. Levin <ldv (at) 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 #ifdef HAVE_CONFIG_H
     29 # include "config.h"
     30 #endif
     31 
     32 #include <sys/syscall.h>
     33 
     34 #ifdef __NR_readdir
     35 
     36 #include <assert.h>
     37 #include <dirent.h>
     38 #include <fcntl.h>
     39 #include <stdio.h>
     40 #include <string.h>
     41 #include <sys/stat.h>
     42 #include <unistd.h>
     43 
     44 static const char fname[] =
     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\nA\n"
     50 	"A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
     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\nZ";
     53 static const char qname[] =
     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\\nA\\n"
     59 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
     60 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
     61 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nZ";
     62 
     63 int
     64 main(int ac, const char **av)
     65 {
     66 	char *dname;
     67 	struct {
     68 		unsigned long   d_ino;
     69 		unsigned long   d_off;
     70 		unsigned short  d_reclen;
     71 		char d_name[1024];
     72 	} e;
     73 	int rc;
     74 
     75 	assert(ac == 1);
     76 	assert(asprintf(&dname, "%s.test.tmp.dir", av[0]) > 0);
     77 	assert(!mkdir(dname, 0700));
     78 	assert(!chdir(dname));
     79 	(void) close(0);
     80 	assert(!creat(fname, 0600));
     81 	assert(!close(0));
     82 	assert(!open(".", O_RDONLY | O_DIRECTORY));
     83 	while ((rc = syscall(__NR_readdir, 0, &e, 1))) {
     84 		if (rc < 0)
     85 			return 77;
     86 		e.d_name[e.d_reclen] = '\0';
     87 		printf("readdir(0, {d_ino=%lu, d_off=%lu, d_reclen=%u"
     88 		       ", d_name=\"%s\"}) = %d\n",
     89 		       e.d_ino, e.d_off, e.d_reclen,
     90 		       e.d_name[0] == '.' ? e.d_name : qname, rc);
     91 	}
     92 	printf("readdir(0, %p) = 0\n", &e);
     93 	puts("+++ exited with 0 +++");
     94 	assert(!unlink(fname));
     95 	assert(!chdir(".."));
     96 	assert(!rmdir(dname));
     97 
     98 	return 0;
     99 }
    100 
    101 #else
    102 
    103 int
    104 main(void)
    105 {
    106 	return 77;
    107 }
    108 
    109 #endif
    110