1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/syscall.h> #include <dirent.h> #include <string.h> #include <sys/stat.h>
struct linux_dirent64 { ino64_t d_ino; off64_t d_off; unsigned short d_reclen; unsigned char d_type; char d_name[]; };
struct dir_stats { int total_files; int directories; int regular_files; int symlinks; int devices; int fifos; int sockets; int unknown; long long total_size; };
void update_stats(struct dir_stats *stats, unsigned char d_type) { stats->total_files++; switch (d_type) { case DT_DIR: stats->directories++; break; case DT_REG: stats->regular_files++; break; case DT_LNK: stats->symlinks++; break; case DT_CHR: case DT_BLK: stats->devices++; break; case DT_FIFO: stats->fifos++; break; case DT_SOCK: stats->sockets++; break; case DT_UNKNOWN: default: stats->unknown++; break; } }
void print_stats(const struct dir_stats *stats) { printf("\n=== 统计信息 ===\n"); printf("总计文件数: %d\n", stats->total_files); printf("目录数: %d\n", stats->directories); printf("普通文件: %d\n", stats->regular_files); printf("符号链接: %d\n", stats->symlinks); printf("设备文件: %d\n", stats->devices); printf("命名管道: %d\n", stats->fifos); printf("套接字: %d\n", stats->sockets); printf("未知类型: %d\n", stats->unknown); }
int filter_by_type(unsigned char d_type, unsigned char filter_type) { if (filter_type == 0) return 1; return d_type == filter_type; }
int main(int argc, char *argv[]) { int fd; char buf[8192]; int nread; char *dir_path; unsigned char filter_type = 0; struct dir_stats stats = {0}; if (argc < 2) { printf("用法: %s <目录路径> [过滤类型]\n", argv[0]); printf("过滤类型: d(目录), f(文件), l(链接), 其他类型字符\n"); dir_path = "."; } else { dir_path = argv[1]; } if (argc > 2) { switch (argv[2][0]) { case 'd': filter_type = DT_DIR; break; case 'f': filter_type = DT_REG; break; case 'l': filter_type = DT_LNK; break; case 'c': filter_type = DT_CHR; break; case 'b': filter_type = DT_BLK; break; case 'p': filter_type = DT_FIFO; break; case 's': filter_type = DT_SOCK; break; } if (filter_type != 0) { printf("过滤类型: %c\n", argv[2][0]); } } fd = open(dir_path, O_RDONLY | O_DIRECTORY); if (fd == -1) { perror("open"); return 1; } printf("=== 目录 '%s' 的内容 ===\n", dir_path); if (filter_type != 0) { printf("(已过滤)\n"); } printf("%c %-20s %12s %10s\n", 'T', "名称", "INODE", "大小(字节)"); printf("%c %-20s %12s %10s\n", '-', "----", "-----", "----------"); while (1) { nread = syscall(SYS_getdents64, fd, buf, sizeof(buf)); if (nread == -1) { perror("getdents64"); close(fd); return 1; } if (nread == 0) { break; } for (int bpos = 0; bpos < nread;) { struct linux_dirent64 *d; d = (struct linux_dirent64 *)(buf + bpos); if (filter_by_type(d->d_type, filter_type)) { long long file_size = 0; if (d->d_type == DT_REG) { char full_path[1024]; struct stat st; snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, d->d_name); if (stat(full_path, &st) == 0) { file_size = st.st_size; stats.total_size += file_size; } } printf("%c %-20s %12llu %10lld\n", get_file_type_char(d->d_type), d->d_name, (unsigned long long)d->d_ino, file_size); } update_stats(&stats, d->d_type); bpos += d->d_reclen; } } close(fd); print_stats(&stats); return 0; }
|