ftp.nice.ch/NiCE/PowerKey/Listings/Terminal.tar.gz#/Terminal/mtail/mtail.c

This is mtail.c in view mode; [Download] [Up]

#include <libc.h>
#include <stdio.h>

/*
 * mtail - simple multi tail
 *
 * wgb: 1994.8.5
 * this program reads a list of growing logfiles
 * and displays the incoming data to stdout
 */

#define	MAX_FILES	10

static char *file_name[MAX_FILES];

void fput_tail(FILE *, int);			// display the growing tail of a file


void main(int argc, char *argv[])
{
    int i, n;
    FILE *fps[MAX_FILES];

    if (argc == 1) {				// no arguments is an error!
	fprintf(stderr, "usage: `%s growing_files_to_list`\n", argv[0]);
	exit(1);
    }
    for (i=0; (i <= MAX_FILES)&&(argc > 1); ) {	// open the files
	file_name[i] = argv[--argc];		// save filename for later use
        if ((fps[i] = fopen(argv[argc], "r")) == NULL) {
	    fprintf(stderr, "%s: can't open file: %s\n", argv[0], argv[argc]);
	    continue;
	}
	fseek(fps[i], 0, SEEK_END);		// position them to the end
	++i;
    }
    n = i-1;
    if (!n) {					// if no open files then no work
	fprintf(stderr, "%s: no file to list!\n", argv[0]);
	exit(2);
    }
    while (1) {					// do not exit
        for (i=0; i < n; ++i)			// for all open files
	    fput_tail(fps[i], i);		// display growing tail
	sleep(3);				// suspend 3 sec to save processor time
    }
}


void fput_tail(FILE *fp, int fi)
{
    static int old_fi=-1;
    int c, nl_cnt=0;

    while ((c = getc(fp)) != EOF) {		// check for additional bytes
	if (fi != old_fi) {			// give out name if file has changed
	    old_fi = fi;
	    printf("\n========== %s ==========\n", file_name[fi]);
	}
	nl_cnt = (c == '\n') ? nl_cnt+1 : 0;
	if (nl_cnt <= 2)			// omit multiple newlines
	    putchar(c);				// and display them
    }
    clearerr(fp);				// reset eof condition
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.