All pastes #375043 Raw Edit

Mine

public text v1 · immutable
#375043 ·published 2007-02-27 23:19 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

static struct stat st;
static time_t mod = 0;
static time_t local;

void
callback_review(char *path)
{
  FILE *file, *k;
  char buf[4096];
  char buf2[4096];
  char *p, *p2;

  file = fopen(path, "r");
  if (!file)
    {
      fprintf(stderr, "Unable to open %s\n", path);
      return 1;
    }

  fseek(file, -256L, SEEK_END);

  while (fgets(buf, sizeof(buf), file))
    {
      char line[4096];

      strcat(line, buf);

      p = buf;
      p2 = buf;

      while ((*p != 0) && (*p != '\n'))
        p++;

      strncpy(line, p2, (p - p2));
      line[p - p2] = 0;
      fprintf(stderr, "%s\n", line);

    }
  fflush(stderr);
  fclose(file);
}

void
timer (int betsec)
{
  time_t now;

  local = time(NULL);

  while (now-local < betsec) now = time(NULL);
}

int
main (int argc, char **argv)
{
  char *path;

  if (argc < 2)
    {
      fprintf(stderr, "Missing argument!\n");
      return 1;
    }

  path = argv[1];

  while (1)
    {
      timer(2);
      stat(path, &st);

      if (st.st_mtime != mod)
        {
          callback_review(path);
          mod = st.st_mtime;
        }
    }

  //test(path);                                                                                           

  return 0;
}