본문 바로가기
호기심_메모

지명된 화일의 내용이 변경되는 시간을 주기적으로 모니터하는 프로그램 slowwatch

by 겸손, 빚진자, 늘 배우는 사람, 배운것을 실습해보는 사람 2021. 8. 13.
반응형

화일을 출력하기 전에 화일이 완전히 갱신되었다는 것은 st_mtime 바뀐 것을 확인하고 출력함

 

#include <stdio.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <fcntl.h>

 

void cmp( const char *, time_t );

struct stat sb;

 

main( int argc, char* argv[])

{

int i=1;

time_t last_time;

 

if( argc != 2 )

{

fprintf( stderr, "usage: slowwatch filename\n");

exit(1);

}

 

while(i)

{

if( stat( argv[1], &sb ) != -1)

i = 0;

}

 

last_time = sb.st_mtime;

 

while(1)

{

cmp( argv[1], last_time );

sleep(10);

}

}

 

void cmp( const char *name, time_t last )

{

int fd;

ssize_t nread;

char buf[BUFSIZ];

 

if( stat(name, &sb) == -1 || sb.st_mtime != last )

{

if( (fd = open(name, O_RDONLY)) == -1 )

{

printf("%s removed\n", name);

exit(0);

}

 

while( (nread = read(fd, buf, BUFSIZ)) > 0)

write(1, buf, nread);

exit(0);

}

}

728x90