嵌入式学习Day20 Linux高级编程 --- stat、getpwuid、getgrgid、chdir、getcwd、access函数

本文介绍了Linux系统中的几个关键函数,如stat用于获取文件信息,getpwuid和getgrgid用于获取用户和组信息,chdir和getcwd用于路径切换,access用于检查权限。还讨论了软链接和硬链接的区别。
摘要由CSDN通过智能技术生成


    1.stat

      格式:
      int stat(const char *pathname, struct stat *statbuf);
      功能:
        将pathname对应的文件信息放入statbuf中
      参数:
        pathname:文件路径字符串的首地址
        statbuf:存放文件信息空间的首地址
      返回值:
        成功返回0 
        失败返回-1 

      注意:
            stat操作链接文件指向的文件
            lstat操作链接文件
    

    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
    };

    2.getpwuid 

      格式:
        struct passwd *getpwuid(uid_t uid);
      功能:
        通过UID获得对应的用户信息
      参数:
        uid:用户的ID号
      返回值:
        成功返回包含用户信息的结构体
        失败返回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);
      功能:
        通过组ID获得组信息
      参数:
        gid:组的ID号
      返回值:
        成功返回包含组信息的结构体
        失败返回NULL
    
    struct group {
        char   *gr_name;        /* group name */
        char   *gr_passwd;      /* group password */
        gid_t   gr_gid;         /* group ID */
        char  **gr_mem;         /* NULL-terminated array of pointers
                                    to names of group members */
    };

    4.chdir

      格式:
        int chdir(const char *path);
      功能:
        切换当前代码的工作路径

    5.getcwd

      格式:
        char *getcwd(char *buf, size_t size);
      功能:
        获得当前目录的绝对路径
        

#include "head.h"

int main(void)
{
    char tmpbuff[4096] = {0};

    getcwd(tmpbuff, sizeof(tmpbuff));
    printf("tmpbuff = %s\n", tmpbuff);

    chdir("..");
    getcwd(tmpbuff, sizeof(tmpbuff));
    printf("tmpbuff = %s\n", tmpbuff);

    return 0;
}

    6.access 

      格式:
        int access(const char *pathname, int mode);
      功能:
        检测调用函数的程序对文件是否拥有指定权限
      参数:
        pathname:文件路径
        mode:
            R_OK    检测是否拥有读权限
            W_OK    检测是否拥有写权限
            X_OK    检测是否拥有执行权限
            F_OK    检测文件是否存在
      返回值:
        有该权限返回0
        出错返回-1         

#include "head.h"

int main(int argc, const char *argv[])
{
    int ret = 0;

    if (argc != 2)
    {
        fprintf(stderr, "Usage:./a.out filename\n");
        return -1;
    }

    ret = access(argv[1], F_OK);
    if (0 == ret)
    {
        printf("该文件存在!\n");
    }
    else 
    {
        printf("该文件不存在!\n");
    }

    return 0;
}  


7.软连接和硬链接


    软连接(符号链接)
                通过文件名链接,所有能够看到的连接文件均为软连接文件

                ln -s file.txt a.txt 
        
                将file.txt连接到a.txt
                symlink(const char* oldpath,const char* newpath)   //软连接函数

    硬链接
                通过文件对应的inode节点链接     

                ln file.txt b.txt 

  • 27
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值