文件状态信息

文件状态信息

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

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

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

 

posted @ 2016-01-12 19:52  zhanglong71  阅读(236)  评论(0编辑  收藏  举报