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 and their file type, optionally filtered by d_type
25 * ./t_dir_type <path> [u|f|d|c|b|l|p|s|w|<ino>]
35 #include <sys/syscall.h>
37 struct linux_dirent64 {
40 unsigned short d_reclen;
47 unsigned char type_to_char[DT_MAX] = {
59 #define DT_CHAR(t) type_to_char[(t)&DT_MASK]
64 main(int argc, char *argv[])
68 struct linux_dirent64 *d;
70 int type = -1; /* -1 means all types */
74 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
80 if (argc > 2 && argv[2][0]) {
83 for (type = DT_MAX-1; type >= 0; type--)
84 if (DT_CHAR(type) == t)
86 /* no match ends up with type = -1 */
88 ino = strtoul(argv[2], NULL, 10);
92 nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
101 for (bpos = 0; bpos < nread;) {
102 d = (struct linux_dirent64 *) (buf + bpos);
103 if ((type < 0 || type == (int)d->d_type) &&
104 (!ino || ino == d->d_ino)) {
106 printf("%s %c\n", d->d_name, DT_CHAR(d->d_type));