tail -f를 구현하려고 합니다. 도와주세요~~

doogie의 이미지

tail -f 를 구현하려고 KLDP를 검색해보니까 ...
select 함수를 통해서 tail -f 식으로 실시간으로 들어온 파일의 내용을 캐치할 수 있다고 해서
지금 구현해봤습니다.
특별한 내용도 없고 간단한 소스인데도 select 함수에서 이벤트가 있다고 무조건 통과하네요...
그리고 read 함수에서는 읽은게 없다고 나옵니다...
원인을 잘 모르겠습니다. 가르쳐주세요 ^^
다음은 소스코드입니다...

  if (argc <= 1) {
    printf ("Usage: %s [tailable file]\n", argv [0]);
    exit (EXIT_FAILURE);
  }
 
  string file_name = argv[1];
  printf ("%s is started with [%s]\n", argv [0], file_name.c_str ());
 
  int fd = open (file_name.c_str (), O_RDONLY);
  if (fd < 0) {
    printf ("file [%s] open error : [%s]\n", file_name.c_str (), strerror (errno));
    exit (EXIT_FAILURE);
  }
 
  // select 관련 변수 선언
  struct timeval tv;
  fd_set read_fds;
  char buf [1024];
 
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  FD_ZERO (&read_fds);
  int res;
  while (true) {
    FD_SET (fd, &read_fds);
 
    if ((res = select (fd + 1, &read_fds, NULL, NULL, &tv)) <= 0) {
      printf ("select timeout found ...\n");
      continue;
    }
    printf ("fd : %d, select result : %d\n", fd, res);
 
    if (FD_ISSET (fd, &read_fds)) {
      memset (buf, 0, 1024);
      res = readline (fd, buf, 1024);
      if (res == 0) {
        printf ("EOF found in fd [%d][%s]\n", fd, file_name.c_str ());
      } else if (res < 0) {
        printf ("error found in fd [%d][%s]\n", fd, file_name.c_str ());
      // output file에다 write 한다.
      } else {
        printf ("readline : %s\n", buf);
      }
    }
  }
 
  exit (EXIT_SUCCESS);