linux高级编程之IO操作(西红柿种子.五)

        本期分享的主要是关于获取文件属性的一些函数接口,由于这些接口涉及的结构体较多,所以不像前面几期分享的函数接口显得容易记忆,因此希望大家在遇到稍微复杂的函数接口时要有耐心,仔细分析,认清每个函数参数的具体意义和内涵:

文件属性的获取:
            1.stat
                      int stat(const char *pathname, struct stat *statbuf);
                      功能: 获得文件的属性
                      参数:
                                pathname:文件路径
                                statbuf:存放文件属性空间首地址
                      返回值:
                                成功返回0
                                失败返回-1     
        struct stat {
           dev_t     st_dev;         /* ID of device containing file */
           ino_t     st_ino;         /* Inode number */
           mode_t    st_mode;        /* File type and mode */
           nlink_t   st_nlink;       /* Number of hard links */
           uid_t     st_uid;         /* User ID of owner */
           gid_t     st_gid;         /* Group ID of owner */
           dev_t     st_rdev;        /* Device ID (if special file) */
           off_t     st_size;        /* Total size, in bytes */
           blksize_t st_blksize;     /* Block size for filesystem I/O */
           blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

           /* Since Linux 2.6, the kernel supports nanosecond
              precision for the following timestamp fields.
              For the details before Linux 2.6, see NOTES. */

           struct timespec st_atim;  /* Time of last access */
           struct timespec st_mtim;  /* Time of last modification */
           struct timespec st_ctim;  /* Time of last status change */

        #define st_atime st_atim.tv_sec      /* Backward compatibility */
        #define st_mtime st_mtim.tv_sec
        #define st_ctime st_ctim.tv_sec
        };
        stat是得到链接文件指向的文件的属性
        lstat是得到链接文件本身

            2.getpwuid
                      struct passwd *getpwuid(uid_t uid);
                      功能:
                                通过用户UID获得用户信息
                      返回值:
                                成功返回用户信息结构体指针
                                失败返回NULL        
        struct passwd {
           char   *pw_name;       /* username */
           char   *pw_passwd;     /* user password */
           uid_t   pw_uid;        /* user ID */
           gid_t   pw_gid;        /* group ID */
           char   *pw_gecos;      /* user information */
           char   *pw_dir;        /* home directory */
           char   *pw_shell;      /* shell program */
        };        
            3.getgrgid
                      struct group *getgrgid(gid_t gid); 
                      功能:获得组信息
                      参数:
                                gid:组ID号
                      返回值:
                                成功返回组结构体空间首地址
                                失败返回NULL    
    4.软连接、硬链接:
        软连接(符号链接)
        ln -s 要连接向的文件 软链接文件
        靠文件名链接
        
        硬链接(通过几个名字可以同时访问一个空间)
        ln 要连接向的文件 硬链接文件
        通过inode节点链接
        inode节点是一个标号,可以通过过这个节点找到文件的位置;

        本期的分享就到这里结束啦,这里基本上列举的是关于获取文件属性的函数接口,小伙伴们可以点赞评论私信交流哦!
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值