文件状态信息

本文通过三个C语言程序示例介绍了如何使用stat、chmod和chown系统调用获取文件的状态信息并修改文件的权限及所有者。具体包括:获取文件的详细信息如设备ID、inode号、权限模式等;通过chmod更改文件的读写权限;通过chown更改文件的所有者。

文件状态信息

例1:

mystat.c

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

#define ERROR(flag)                 \
     if(flag)                \
    {                    \
        printf("%d: ",__LINE__);    \
        fflush(stdout);            \
        perror("error");        \
        exit(errno);            \
    }
int main(int argc, char *argv[])
{
    struct stat buf;
    
    if (argc != 2)
    {
        printf("Usage: my_stat <filename>\n");
        exit(0);
    }
    
    int ret = stat(argv[1], &buf);
    ERROR(ret == -1);

    printf("device is:                 %d\n", buf.st_dev);
    printf("inode is:                 %d\n", buf.st_ino);
    printf("mode is:                 %o\n", buf.st_mode);
    printf("number of hard links  is:         %d\n", buf.st_nlink);
    printf("user ID of owner is:             %d\n", buf.st_uid);
    printf("group ID of owner is:             %d\n", buf.st_gid);
    printf("device type (if inode device) is:     %d\n", buf.st_rdev);
    
    printf("total size, in bytes is:         %d\n", buf.st_size);
    printf(" blocksize for filesystem I/O is:    %d\n", buf.st_blksize);
    printf("number of blocks allocated is:         %d\n", buf.st_blocks);
    
    printf("time of last access is:         %s", ctime(&buf.st_atime));
    printf("time of last modification is:         %s", ctime(&buf.st_mtime));
    printf("time of last change is:         %s", ctime(&buf.st_ctime));
    
    return 0;
}

编译/链接/执行后,  输出结果如下:

观察输出结果, 与前面的命令的输出结果一致

 

例2: chmod接口API

mychmod.c

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

#define ERROR(flag)                 \
     if(flag)                \
    {                    \
        printf("%d: ",__LINE__);    \
        fflush(stdout);            \
        perror("error");        \
        exit(errno);            \
    }

int main(int argc, char ** argv)
{
    int    mode;    
    int    mode_u;    
    int    mode_g;    
    int    mode_o;    

    mode = atoi(argv[1]);

    mode_u = mode / 100;
    mode_g = (mode - (mode_u*100)) / 10;
    mode_o = mode - (mode_u*100) - (mode_g*10);

    mode = (mode_u * 8 * 8) + (mode_g * 8) + mode_o;    

    int ret = chmod(argv[2], mode);

    ERROR(ret == -1);

    return 0;
}

编译链接执行, 输出结果如下:

文件的读写等属性发生改变

 

例3: chown接口API

mychown.c

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

#define ERROR(flag)                 \
     if(flag)                \
    {                    \
        printf("%d: ",__LINE__);    \
        fflush(stdout);            \
        perror("error");        \
        exit(errno);            \
    }

int main(int argc,char *argv[])
{
    
    //int ret = chown(argv[1],500,500);
    int ret = chown(argv[1],7,7);

    ERROR(ret == -1);

    return 0;
}

编译链接执行, 结果输出如下:

文件的拥有者属性发生变化

 

转载于:https://www.cnblogs.com/zhanglong71/p/5119965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值