文件类型 c、b、l、-、d

下面是使用C语言实现`ls -l`命令的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <time.h> void print_permissions(mode_t mode) { printf((S_ISDIR(mode)) ? "d" : "-"); printf((mode & S_IRUSR) ? "r" : "-"); printf((mode & S_IWUSR) ? "w" : "-"); printf((mode & S_IXUSR) ? "x" : "-"); printf((mode & S_IRGRP) ? "r" : "-"); printf((mode & S_IWGRP) ? "w" : "-"); printf((mode & S_IXGRP) ? "x" : "-"); printf((mode & S_IROTH) ? "r" : "-"); printf((mode & S_IWOTH) ? "w" : "-"); printf((mode & S_IXOTH) ? "x" : "-"); } int main() { DIR *dir; struct dirent *entry; struct stat file_stat; dir = opendir("."); if (dir == NULL) { printf("Cannot open directory.\n"); exit(1); } while ((entry = readdir(dir)) != NULL) { if (stat(entry->d_name, &file_stat) == -1) continue; print_permissions(file_stat.st_mode); printf(" %ld ", file_stat.st_nlink); struct passwd *pw = getpwuid(file_stat.st_uid); if (pw != NULL) printf("%s ", pw->pw_name); struct group *gr = getgrgid(file_stat.st_gid); if (gr != NULL) printf("%s ", gr->gr_name); printf("%lld ", (long long)file_stat.st_size); char time_buffer[80]; strftime(time_buffer, 80, "%b %d %H:%M", localtime(&(file_stat.st_mtime))); printf("%s ", time_buffer); printf("%s\n", entry->d_name); } closedir(dir); return 0; } ``` 这段代码使用了`dirent.h`、`sys/stat.h`、`pwd.h`和`grp.h`头文件来获取目录和文件的相关信息,并使用了`time.h`头文件来格式化时间。它遍历当前目录下的文件和子目录,并打印出每个文件的相关信息,包括文件类型、权限、链接数、所有者、所属组、文件大小、修改时间和文件名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值