home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / OSKBox.lzh / MAILBOX / CC / tail.c < prev    next >
C/C++ Source or Header  |  1990-11-10  |  2KB  |  86 lines

  1. #include <stdio.h>
  2. #include <sgstat.h>
  3. #include <errno.h>
  4. #include <termcap.h>
  5.  
  6. #define NLINES 64
  7. int nlines;
  8.  
  9. main (argc, argv)
  10. char *argv[];
  11. {
  12.     int f;
  13.     char *term_type, *getenv();
  14.  
  15.     if (term_type = getenv ("TERM")) {
  16.         char tcapbuf[100], tcbuf[1024], *temp, *p = tcapbuf;
  17.         struct sgbuf opt;
  18.         
  19.         if (tgetent (tcbuf, term_type) > 0) {
  20.             nlines = min (tgetnum ("li"), NLINES);
  21.             }
  22.         else
  23.             term_type = NULL;
  24.         }
  25.     else
  26.         nlines = 24;
  27.     if (argc < 2)
  28.         tail (0);
  29.     else if ((f = open (argv[1], 1)) < 0)
  30.         exit (errno);
  31.     else
  32.         tail (f);
  33.     }
  34.  
  35. tail (path)
  36. {
  37.     struct sgbuf opt;
  38.  
  39.     _gs_opt (path, &opt);
  40.     if (opt.sg_class != 1)
  41.         scf_tail (path);
  42.     else
  43.         rbf_tail (path);
  44.     }
  45.  
  46. char lines[NLINES][256];
  47. scf_tail (f)
  48. {
  49.     int pos, count, i, len;
  50.  
  51.     for (count = pos = 0; (len = readln (f, lines[pos], 256)) > 0; count++) {
  52.         lines[pos][len] = '\0';
  53.         pos = (pos + 1) % nlines;
  54.         }
  55.     if (count < NLINES) pos = 0;
  56.     for (i = 0; i < min (count, nlines); i++) {
  57.         writeln (1, lines[pos], 256);
  58.         pos = (pos + 1) % nlines;
  59.         }
  60.     }
  61.  
  62. rbf_tail (f)
  63. {
  64.     int i;
  65.     long len, pos;
  66.     long lines[NLINES];
  67.     char buff[256];
  68.  
  69.     len = _gs_size (f);
  70.     if ((pos = len - (nlines+1)*80) < 0) pos = 0;
  71.     lseek (f, pos, 0);
  72.     for (i = 0; i < nlines; i++) lines[i] = -1;
  73.     for (i = 0; readln (f, buff, 256); i = (i + 1) % nlines)
  74.         lines[i] = _gs_pos (f);
  75.     if (i == 0 && lines[i] == -1) return;
  76.     do i = (i + 1) % nlines; while (lines[i] == -1);
  77.     lseek (f, lines[i], 0);
  78.     while (len = readln (f, buff, 256))
  79.         writeln (1, buff, len);
  80.     }
  81.  
  82. min (a, b)
  83. {
  84.     return ((a < b) ? a : b);
  85.     }
  86.