/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#define N 5120

static char retbuf[1024];

char *get_last_line(char *fnm)
{
   FILE *fp;
   struct stat st;
   char tmpbuf[N];
   int ix,len,bufix;
   stat(fnm, &st);
   len=st.st_size<N?st.st_size:N;
   ix=st.st_size-len;
   fp=fopen(fnm,"rb");
   fseek(fp,ix,SEEK_SET);
   fread(tmpbuf,len,1,fp);
   fclose(fp);
   bufix=len-1;
   while((tmpbuf[bufix]!='\n')&&(bufix>0)) bufix--;
   memcpy(retbuf,tmpbuf+bufix,len-bufix);
   retbuf[len-bufix]='\0';
   return retbuf;
}

int main(int argc,char *argv[])
{
   printf("last number = %d\n", atoi(get_last_line("z.z")));
   exit(0);
}
