首页 > 其他 > 详细

文件状态信息

时间:2016-01-12 21:03:19      阅读:242      评论:0      收藏:0      [点我收藏+]

文件状态信息

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

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

技术分享

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

 

文件状态信息

原文:http://www.cnblogs.com/zhanglong71/p/5119965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!