tail.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  secwatch - Copyright (C) 2006 Nic Stevens -- See COPYING for license details
00003 ------------------------------------------------------------------------------
00004   tail.c contains the code for the tail functions.
00005   Tail Functions:
00006         These are functions which manipulate and maintain tail_t objects. Each
00007         object is associated with a system log file name, open descriptor, and
00008         current offset. This allows for a simple function call to retrieve the
00009         next available log file line along with the log file name. 
00010 -----------------------------------------------------------------------------*/
00011 #include <stdio.h>
00012 #include <sys/types.h>
00013 #include <sys/stat.h>
00014 #include <fcntl.h>
00015 #include <string.h>
00016 #include <stdlib.h>
00017 #include <tail.h>
00018 
00019 typedef struct _tail {
00020      char filename[FILENAME_MAX+1];
00021      int fd;
00022      off_t pos;
00023 } tail_t;
00024 
00025 static tail_t * tails = NULL;
00026 static int tcnt = 0;
00027 static int tp = 0;
00028 
00029 static int _tail(tail_t *t, char *buf, size_t bufsz);
00030 
00036 int addtailsource(char *filename) {
00037      int tp;
00038      tail_t _t = {"",-1,-1}, *t = &_t, *a;
00039      strcpy(t->filename,filename);
00040      if(_tail(t,NULL,-1)) 
00041           return -1;
00042      tp = tcnt ++;
00043      if((tails = realloc(tails,(sizeof(tail_t)*(tcnt)))) == NULL) {
00044           close(t->fd);
00045           return -1;
00046      }
00047      tails[tp] = *t;
00048      return 0;
00049 }
00054 int taildata(char **filename, char *buf, size_t bufsz) {
00055      int cnt;
00056      tail_t * t = &tails[tp++]; 
00057      if(tp >= tcnt)
00058           tp = 0;
00059      cnt = _tail(t,buf,bufsz);
00060      *filename = &t->filename[0];
00061      return cnt;
00062 }
00067 static int _tail(tail_t *t, char *buf, size_t bufsz) {
00068      char c;
00069      int i;
00070      if(t->fd == -1) {
00071           struct stat _st, *st = &_st;
00072           if((t->fd = open(t->filename,O_RDONLY)) == -1) 
00073                return -1;
00074 
00075           if(fstat(t->fd,st) == -1) {
00076                close(t->fd);
00077                return -1;
00078           }
00079           t->pos = st->st_size;
00080           lseek(t->fd,0,SEEK_END);
00081           return 0;
00082      }
00083      for(i = 0, c=-1; c != '\0';) {
00084           if(read(t->fd, &c,1)>0)  {
00085                buf[i++] = c;
00086                buf[i] = '\0';
00087                if(c == '\n' || i >= bufsz)
00088                     break;
00089           } else break;
00090      }
00091      return i;
00092 }
00096 void tailsleep(unsigned long sleep) {
00097      struct timespec ts = {0,(sleep*1000000)};
00098      nanosleep(&ts,NULL);
00099 }

Generated on Tue Oct 31 10:17:23 2006 for secwatch by  doxygen 1.4.6