tail.c File Reference

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <tail.h>

Include dependency graph for tail.c:

Go to the source code of this file.

Data Structures

struct  _tail

Typedefs

typedef _tail tail_t

Functions

static int _tail (tail_t *t, char *buf, size_t bufsz)
int addtailsource (char *filename)
int taildata (char **filename, char *buf, size_t bufsz)
void tailsleep (unsigned long sleep)

Variables

static tail_ttails = NULL
static int tcnt = 0
static int tp = 0


Typedef Documentation

typedef struct _tail tail_t
 


Function Documentation

static int _tail tail_t t,
char *  buf,
size_t  bufsz
[static]
 

_tail does the real work. If the tail pointer's fd is -1, the file is opened and seeked to EOF and the EOF pos is stored. On error -1 is returned and errno is set.

Definition at line 67 of file tail.c.

References _tail::fd, _tail::filename, and _tail::pos.

Referenced by addtailsource(), and taildata().

00067                                                      {
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 }

int addtailsource char *  filename  ) 
 

addtailsource opens and seeks to EOF the specified file. The file descriptor and position offset is saved. If an error occurs errno is set and addtailsource returns -1 otherwise it returns 0.

Definition at line 36 of file tail.c.

References _tail(), tails, tcnt, and tp.

Referenced by main().

00036                                   {
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 }

Here is the call graph for this function:

int taildata char **  filename,
char *  buf,
size_t  bufsz
 

taildata returns the next available line from a tail-file. The filename read from is stored in filename. buf will contain the data read upto bufsz bytes.

Definition at line 54 of file tail.c.

References _tail(), _tail::filename, tails, tcnt, and tp.

Referenced by secwatch().

00054                                                        {
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 }

Here is the call graph for this function:

void tailsleep unsigned long  sleep  ) 
 

tailsleep suspends execution for sleep number of milliseconds.

Definition at line 96 of file tail.c.

Referenced by secwatch().

00096                                     {
00097      struct timespec ts = {0,(sleep*1000000)};
00098      nanosleep(&ts,NULL);
00099 }


Variable Documentation

tail_t* tails = NULL [static]
 

Definition at line 25 of file tail.c.

Referenced by addtailsource(), and taildata().

int tcnt = 0 [static]
 

Definition at line 26 of file tail.c.

Referenced by addtailsource(), and taildata().

int tp = 0 [static]
 

Definition at line 27 of file tail.c.

Referenced by addtailsource(), and taildata().


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