基本信息
源码名称:linux系统监控文件变化状态
源码大小:2.14KB
文件格式:.c
开发语言:C/C++
更新时间:2021-09-08
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  
#include <sys/inotify.h>  
#include <unistd.h>  

#define EVENT_NUM 12  

char *event_str[EVENT_NUM] =   
{  
    "IN_ACCESS",  
    "IN_MODIFY",  
    "IN_ATTRIB",  
    "IN_CLOSE_WRITE",  
    "IN_CLOSE_NOWRITE",  
    "IN_OPEN",  
    "IN_MOVED_FROM",  
    "IN_MOVED_TO",  
    "IN_CREATE",  
    "IN_DELETE",  
    "IN_DELETE_SELF",  
    "IN_MOVE_SELF"
};  

int main(int argc, char *argv[])  
{  
    int fd;  
    int wd;  
    int len;  
    int nread;  
    char buf[BUFSIZ];  
    struct inotify_event *event;  
    int i;  

    if(argc < 2)  
    {  
        fprintf(stderr, "%s path\n", argv[0]);  
        return -1;  
    }  

    fd = inotify_init();  
    if( fd < 0 )  
    {  
        fprintf(stderr, "inotify_init failed\n");  
        return -1;  
    }  

    wd = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS);  
    if(wd < 0)  
    {  
        fprintf(stderr, "inotify_add_watch %s failed\n", argv[1]);  
        return -1;  
    }  

    buf[sizeof(buf) - 1] = 0;  
    while( (len = read(fd, buf, sizeof(buf) - 1)) > 0 )  
    {  
        nread = 0;  
        while( len > 0 )  
        {  
            event = (struct inotify_event *)&buf[nread];  
            for(i=0; i<EVENT_NUM; i )  
            {  
                if((event->mask >> i) & 1)  
                {  
                    if(event->len > 0)  
                        fprintf(stdout, "%s --- %s\n", event->name, event_str[i]);  
                    else  
                        fprintf(stdout, "%s --- %s\n", " ", event_str[i]);  
                }  
            }  
            nread = nread sizeof(struct inotify_event) event->len;  
            len = len - sizeof(struct inotify_event) - event->len;  
        }  
    }  

    return 0;  
}