1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 CTERA Networks. All Rights Reserved.
4 * Author: Amir Goldstein <amir73il@gmail.com>
10 * print directory entries and their file type, optionally filtered by d_type
13 * ./t_dir_type <path> [u|f|d|c|b|l|p|s|w|<ino>]
23 #include <sys/syscall.h>
25 struct linux_dirent64 {
28 unsigned short d_reclen;
35 unsigned char type_to_char[DT_MAX] = {
47 #define DT_CHAR(t) type_to_char[(t)&DT_MASK]
52 main(int argc, char *argv[])
56 struct linux_dirent64 *d;
58 int type = -1; /* -1 means all types */
62 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
68 if (argc > 2 && argv[2][0]) {
71 for (type = DT_MAX-1; type >= 0; type--)
72 if (DT_CHAR(type) == t)
74 /* no match ends up with type = -1 */
76 ino = strtoul(argv[2], NULL, 10);
80 nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
89 for (bpos = 0; bpos < nread;) {
90 d = (struct linux_dirent64 *) (buf + bpos);
91 if ((type < 0 || type == (int)d->d_type) &&
92 (!ino || ino == d->d_ino)) {
94 printf("%s %c\n", d->d_name, DT_CHAR(d->d_type));