TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: all
from: Bill Birrell
date: 2004-06-27 08:50:00
subject: Tail

This looks weird with all the casts and everything, but it gets through the
latest lint without warnings, so maybe it's right. TAILBUF can be any
convenient size.

TAIL.C

#include 
#include 
#include 
#define DEBUG 0
#define TAILBUF 640

static char tailbuf[TAILBUF];

static int tail(char *fname)
{
    FILE *fp;
    long length, f, start, offset;

    length = f = start = offset = 0l;

    if((fp=/*_fs*/fopen(fname,"r"/*,_SH_DENYWR*/))==NULL) {
        printf("tail: error can't open %s\n",fname);
        return(-1);
        }
    (void)fseek(fp,0l,SEEK_END);
    length = ftell(fp);
    rewind(fp);
    if (DEBUG) {
    start = ftell(fp);
    printf("\nstart = %ld",start);
    printf("\nlength = %ld", length);
    printf("\nbuffer length = %d", TAILBUF);
    }
    if(length >= TAILBUF) (void)fseek(fp, -(long)TAILBUF, SEEK_END);
    if(DEBUG) {
    offset = ftell(fp);
    printf("\noffset = %ld",offset);
    printf("\npress return to continue");
    (void)getchar();
    }
    (void)fread((void *)tailbuf,TAILBUF,1,fp);
        (void)fclose(fp);
    if(length >= TAILBUF) {
        for(f=0; f < (TAILBUF-1); f++)
            (void)putc((char)tailbuf[f],stdout);
    }
    else for (f=0; f < (length-1); f++) (void)putc((char)tailbuf[f],stdout);
    return 0;
}


int main(int argc, char *argv[])
{

    if(argc < 2) {
        printf("need file name\n");
                exit(EXIT_FAILURE);
        }

    memset(tailbuf,0,TAILBUF);

    while(--argc != 0) {
        ++argv;
                (void)tail(*argv);
        }

    return 0;
}

Best Wishes,
Bill.

---
* Origin: Escan BBS (2:25/200)
SEEN-BY: 633/267 270
@PATH: 25/200 108 252/110 250/501 140/1 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.