2 * Copyright (C) 2016 CTERA Networks. All Rights Reserved.
3 * Author: Amir Goldstein <amir73il@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * print directory entries, optionally filtered by d_type
24 * ./t_dir_type <path> [u|f|d|c|b|l|p|s|w]
34 #include <sys/syscall.h>
36 struct linux_dirent64 {
39 unsigned short d_reclen;
46 unsigned char type_to_char[DT_MAX] = {
58 #define DT_CHAR(t) type_to_char[(t)&DT_MASK]
63 main(int argc, char *argv[])
67 struct linux_dirent64 *d;
69 int type = -1; /* -1 means all types */
72 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
78 if (argc > 2 && argv[2][0]) {
81 for (type = DT_MAX-1; type >= 0; type--)
82 if (DT_CHAR(type) == t)
84 /* no match ends up with type = -1 */
88 nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
97 for (bpos = 0; bpos < nread;) {
98 d = (struct linux_dirent64 *) (buf + bpos);
99 if (type < 0 || type == (int)d->d_type) {
101 printf("%s %c\n", d->d_name, DT_CHAR(d->d_type));