Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- flog - "follow log"
- Copyright (C) Marcus Ranum, 1992 All rights reserved.
- You may freely use this software provided you do not alter
- this copyright statement.
- This little utility does the equivalent of "tail -f" but is smart
- enough to handle changing files if a log rotater moves the file out
- from under the user while we're running. Also, unlike "tail -f" this
- program is smart enough to exit when the user logs out(!) and will
- not spew logs to the pty forever. No effort is made at elegance or
- performance.
- */
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- extern int errno;
- #include <fcntl.h>
- #include <sys/file.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #define DEFAULT_FILE "/usr/spool/mqueue/syslog"
- #ifdef hpux
- #include <unistd.h>
- int getdtablesize()
- {
- return(sysconf(_SC_OPEN_MAX));
- }
- #endif
- main(ac,av)
- int ac;
- char **av;
- {
- char rbuf[BUFSIZ];
- struct stat sbuf;
- int red;
- int *tb;
- int *sb;
- char **nb;
- off_t *ob;
- int max = 0;
- int xx;
- int siz;
- /* file descriptor table */
- siz = getdtablesize();
- tb = (int *)malloc(sizeof(int) * siz);
- sb = (int *)malloc(sizeof(int) * siz);
- ob = (off_t *)malloc(sizeof(off_t) * siz);
- nb = (char **)malloc(sizeof(char *) * siz);
- if(nb == (char **)0 || ob == (off_t *)0 || sb == (int *)0 || tb == (int *)0) {
- perror("cannot malloc file descriptor table");
- exit(1);
- } else {
- for(xx = 0; xx < siz; xx++) {
- tb[xx] = -1;
- sb[xx] = -1;
- ob[xx] = 0;
- nb[xx] = (char *)0;
- }
- }
- /* open all the files */
- if(ac > 1) {
- for(xx = 1; xx < ac; xx++)
- if(openfile(av[xx],tb,sb,ob,nb,max) == 0)
- max++;
- } else {
- if(openfile(DEFAULT_FILE,tb,sb,ob,nb,max) == 0)
- max++;
- }
- looptop:
- if(getppid() == 1 || max == 0)
- exit(0);
- for(xx = 0; xx < max; xx++) {
- if(stat(nb[xx],&sbuf)) {
- if(tb[xx] != -1)
- (void)close(tb[xx]);
- tb[xx] = -1;
- (void)openfile(nb[xx],tb,sb,ob,nb,xx);
- continue;
- }
- if(sb[xx] != sbuf.st_ino) {
- (void)close(tb[xx]);
- tb[xx] = -1;
- (void)openfile(nb[xx],tb,sb,ob,nb,xx);
- }
- if(tb[xx] == -1)
- continue;
- while(ob[xx] < sbuf.st_size) {
- int t;
- t = sbuf.st_size - ob[xx] > sizeof(rbuf) ? sizeof(rbuf) : sbuf.st_size - ob[xx];
- red = read(tb[xx],rbuf,t);
- if(red < 0) {
- (void)close(tb[xx]);
- tb[xx] = -1;
- continue;
- }
- if(red > 0) {
- if(write(1,rbuf,red) != red)
- exit(1);
- }
- ob[xx] += red;
- }
- }
- sleep(3);
- goto looptop;
- }
- openfile(path,tb,sb,ob,nb,max)
- char *path;
- int *tb;
- int *sb;
- off_t *ob;
- char **nb;
- int max;
- {
- struct stat sbuf;
- if(stat(path,&sbuf)) {
- perror(path);
- return(1);
- }
- if((tb[max] = open(path,O_RDONLY,0)) < 0) {
- perror(path);
- return(1);
- }
- (void)lseek(tb[max],0L,L_XTND);
- sb[max] = sbuf.st_ino;
- ob[max] = sbuf.st_size;
- nb[max] = path;
- fprintf(stderr,"flog: opened %s\n",nb[max]);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement