linux系统调用函数 lstat--获取文件属性

所需头文件:

#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>

函数功能:用来获取linux操作系统下文件的属性。
函数原型: int stat(const char *pathname,struct stat *buf);

参数:第一个参数为传入参数,pathname为文件的绝对路径相对路径。第二参数为传出参数,一个struct stat类型的结构体指针。传出参数可以采用下边两种方法,定义结构体变量struct stat st,或定义结构体指针变量strut stat *st = &st(注意这里一定要进行初始化,说明其为一块有效的内存空间),相对而言,使用结构体变量更为方便。

注意,在linux操作系统下,一切皆文件。文件共有七种类型,分别是普通文件、目录文件、管道文件、可执行文件、压缩文件、设备文件(字符、管道和块)和其他文件。

下面介绍一下struct stat结构体:

这里写图片描述

struct stat {
    dev_t     st_dev;      文件的设备编号
    ino_t     st_ino;      节点
    mode_t    st_mode;     文件的类型和权限
    nlink_t   st_nlink;    连到该文件的硬链接数目,刚建立的文件值为1
    uid_t     st_uid;      用户ID
    gid_t     st_gid;      组ID
    dev_t     st_rdev;     设备类型)若此文件为设备文件,则为其设备编号
    off_t     st_size;     文件字节数(文件大小)
    blksize_t st_blksize;  块大小(文件I/O缓冲区的大小)
    blkcnt_t  st_blocks;   块数
    time_t    st_atime;    最后一次访问时间
    time_t    st_mtime;    最后一次修改时间
    time_t    st_ctime;    最后一次改变时间(指属性)
};

下面举例使用stat函数获取文件的属性:
在linux的shell解释器中,输入这样的命令stat + 文件名即可获取文件的属性。如:
这里写图片描述
那么如何在函数中获取文件属性并将其打印出来呢?下面应用stat函数获取main.c文件的属性。

#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>

int main()
{
    struct stat st;//定义结构体变量,保存所获取的文件属性
    int res = stat("/home/lsc/Desktop/linux/k0512/test/main.c",&st);
    if(res == -1)//获取文件属性失败,errno设置为合适的值
    {
        perror("stat fail");
        exit(1);
    }
    printf("大小:%d    ",st.st_size);
    printf("设备:%d    ",st.st_dev);
    printf("块:%d   ",st_st_blocks);
    printf("Inode:%d   ",st.st_ino);
    printf("硬链接:%d   ",st.st_nlink);
    printf("uid:%d   ",st.st_uid);
    printf("gid:%   ",st.st_gid);
    return 0;
}

执行结果:
和我们用stat + 文件名命令获取到文件的属性信息是一致的。
这里写图片描述

大家可能注意到,在代码中并为涉及输出st_mode属性相关的信息。
重点:下面着重深入的研究一下st_mode这属性,可以通过它来获取文件的文件类型以及权限。

mode_t st_mode是一个16位的short类型,对应16个标志位,其组成如下:

这里写图片描述
而其,通常情况下特殊权限位不使用。
那么如何取出对应的位置的位获取相关的信息呢,Linux系统调用给我们提供了一系列的宏,可以查看man文档 man 2 stat

S_IFMT     0170000   //掩码,过滤st_mode中除文件类型以外的信息
S_IFSOCK   0140000   //套接字
S_IFLNK    0120000   //符号链接(软链接)
S_IFREG    0100000   //普通文件
S_IFBLK    0060000   //块设备
S_IFDIR    0040000   //目录文件
S_IFCHR    0020000   //字符设备
S_IFIFO    0010000   //管道
S_ISUID    0004000   //设置用户ID
S_ISGID    0002000   //设置组ID
S_ISVTX    0001000   //粘住位
S_IRWXU    00700     //掩码,过滤st_mode除文件所有者权限以外的信息
S_IRUSR    00400     //用户读权限
S_IWUSR    00200     //用户写权限
S_IXUSR    00100     //用户执行权限
S_IRWXG    00070     //掩码,过滤st_mode除所属组权限以外的信息
S_IRGRP    00040     //读权限
S_IWGRP    00020     //写权限
S_IXGRP    00010     //执行权限
S_IRWXO    00007     //掩码,过滤st_mode除其他人权限以外的信息
S_IROTH    00004     //读权限
S_IWOTH    00002     //写权限
S_IXOTH    00001     //执行权限

下面举一个简单例子,验证一个文件是否为普通文件。
在当前目录下有一个普通文件:
这里写图片描述

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#incude<sys/stat.h>
#include<sys/types.h>

int main()
{
    struct stat st;
    int res = stat("./test.c",&st);
    if(-1 == res)
    {
        perror("stat fail");
        exit(1);
    }
    if((st.st_mode & S_IFMT) == S_IFREG)
    {
        printf("regular file\n");
    }
    return 0;
}

这里写图片描述

相信完全理解了系统调用函数stat之后,实现一个ls -l的命令也会变得简单,大家可以试试。

  • 7
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现ls -l命令的关键是要读取当前目录下的文件和文件夹的信息,并将其格式化输出。下面是一个简单的C语言程序,实现了类似于ls -l的功能: ```c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> #include <pwd.h> #include <grp.h> #include <time.h> int main(int argc, char *argv[]) { DIR *dp; struct dirent *entry; struct stat statbuf; struct passwd *pwd; struct group *grp; char date[20]; if ((dp = opendir(".")) == NULL) { fprintf(stderr, "Cannot open directory.\n"); exit(1); } while ((entry = readdir(dp)) != NULL) { if (entry->d_name[0] == '.') continue; if (lstat(entry->d_name, &statbuf) < 0) continue; printf((S_ISDIR(statbuf.st_mode)) ? "d" : "-"); printf((statbuf.st_mode & S_IRUSR) ? "r" : "-"); printf((statbuf.st_mode & S_IWUSR) ? "w" : "-"); printf((statbuf.st_mode & S_IXUSR) ? "x" : "-"); printf((statbuf.st_mode & S_IRGRP) ? "r" : "-"); printf((statbuf.st_mode & S_IWGRP) ? "w" : "-"); printf((statbuf.st_mode & S_IXGRP) ? "x" : "-"); printf((statbuf.st_mode & S_IROTH) ? "r" : "-"); printf((statbuf.st_mode & S_IWOTH) ? "w" : "-"); printf((statbuf.st_mode & S_IXOTH) ? "x" : "-"); printf(" %2lu", statbuf.st_nlink); if ((pwd = getpwuid(statbuf.st_uid)) != NULL) { printf(" %s", pwd->pw_name); } else { printf(" %d", statbuf.st_uid); } if ((grp = getgrgid(statbuf.st_gid)) != NULL) { printf(" %s", grp->gr_name); } else { printf(" %d", statbuf.st_gid); } strftime(date, 20, "%b %d %H:%M", localtime(&statbuf.st_mtime)); printf(" %s", date); printf(" %s\n", entry->d_name); } closedir(dp); return 0; } ``` 这个程序使用了许多系统调用和库函数,比如opendir、readdir、lstat、getpwuid、getgrgid等等。它首先打开当前目录("."),然后循环读取目录下的每个文件和文件夹的信息,使用lstat函数获取文件的详细信息,再利用各种库函数将这些信息格式化输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值