lstat函数和S_ISDIR 、S_ISLNK等几个常见的宏

一、lstat,是一种文件描述词。意思是获取一些文件相关的信息。

函数名: lstat

功 能: 获取一些文件相关的信息

用 法: int lstat(const char *path, struct stat *buf);

返回值:成功返回0。

参数:

path:文件路径名。

filedes:文件描述词

buf:是以下结构体指针

struct stat {

      dev_t st_dev; /* 文件所在设备的标识 */
 ino_t st_ino; /* 文件结点号 */
mode_t st_mode; /* 文件保护模式 */
nlink_t st_nlink; /* 硬连接数 */
uid_t st_uid; /* 文件用户标识 */
gid_t st_gid; /* 文件用户组标识 */
dev_t st_rdev; /* 文件所表示的特殊设备文件的设备标识 */
off_t st_size; /* 总大小,单位为字节*/
blksize_t st_blksize; /* 文件系统的块大小 */
blkcnt_t st_blocks; /* 分配给文件的块的数量,512字节为单元 */
time_t st_atime; /* 最后访问时间 */
time_t st_mtime; /* 最后修改时间 */
time_t st_ctime; /* 最后状态改变时间 */
};

二、S_ISDIR 、S_ISLNK等几个常见的宏

S_ISLNK(st_mode):是否是一个连接.
S_ISREG(st_mode):是否是一个常规文件.
S_ISDIR(st_mode):是否是一个目录
S_ISCHR(st_mode):是否是一个字符设备.
S_ISBLK(st_mode):是否是一个块设备
S_ISFIFO(st_mode):是否 是一个FIFO文件.
S_ISSOCK(st_mode):是否是一个SOCKET文件

我们使用最多的属性是st_mode,如果返回真,代表是目录或者连接等等


三、用法实例

#include <stdio.h>
#include <sys/stat.h>
int main(int argc,char** argv)  
{  
struct stat buf;
if( lstat("./Documents",&buf) < 0)
{
perror("lstat error\n");
return -1;
}
if(S_ISDIR(buf.st_mode))
{
printf("is dir\n");
}
if(S_ISREG(buf.st_mode))
{
printf("is normal file\n");
}
return 0;
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 以下是实现这个shell脚本的示例代码: ```bash #!/bin/bash # 显示当前登录用户名 echo "当前登录用户名为: $USER" # 显示最近10条命令历史 echo "最近10条命令历史为:" history | tail -n 10 # 打印当前日期 echo "当前日期为: $(date)" # 退出脚本,并置退出码为5 exit 5 ``` 2. 以下是将当前目录文件msg中的内容发给当前所有的登录用户的示例代码: ```bash #!/bin/bash # 获取当前所有登录用户的用户名 users=$(who | awk '{print $1}' | sort | uniq) # 遍历所有用户,将msg文件的内容发送给他们 for user in $users; do if [ -n "$(who | grep $user)" ]; then cat msg | write $user fi done ``` 3. 以下是使用系统调用完成指定目录中的所有文件类型统计的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <dirent.h> int main() { char path[100]; printf("请输入指定目录路径: "); scanf("%s", path); DIR *dir; struct dirent *entry; struct stat statbuf; int count_reg = 0, count_dir = 0, count_blk = 0, count_chr = 0, count_sock = 0, count_fifo = 0, count_lnk = 0; if ((dir = opendir(path)) == NULL) { perror("opendir error"); exit(1); } while ((entry = readdir(dir)) != NULL) { char filename[100]; sprintf(filename, "%s/%s", path, entry->d_name); if (lstat(filename, &statbuf) == -1) { perror("lstat error"); exit(1); } if (S_ISREG(statbuf.st_mode)) { count_reg++; } else if (S_ISDIR(statbuf.st_mode)) { count_dir++; } else if (S_ISBLK(statbuf.st_mode)) { count_blk++; } else if (S_ISCHR(statbuf.st_mode)) { count_chr++; } else if (S_ISSOCK(statbuf.st_mode)) { count_sock++; } else if (S_ISFIFO(statbuf.st_mode)) { count_fifo++; } else if (S_ISLNK(statbuf.st_mode)) { count_lnk++; } } printf("普通文件个数:%d\n", count_reg); printf("目录个数:%d\n", count_dir); printf("块设备个数:%d\n", count_blk); printf("字符设备个数:%d\n", count_chr); printf("套接字个数:%d\n", count_sock); printf("FIFO/pipe个数:%d\n", count_fifo); printf("符号链接个数:%d\n", count_lnk); closedir(dir); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值