added depend label
[unix-history] / usr / src / usr.bin / tail / tail.c
index 4e9476e..17c70d9 100644 (file)
@@ -1,8 +1,23 @@
-static char *sccsid = "@(#)tail.c      4.1 (Berkeley) %G%";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)tail.c     5.3 (Berkeley) %G%";
+#endif not lint
+
 /* tail command 
  *
  *     tail where [file]
 /* tail command 
  *
  *     tail where [file]
- *     where is +\b_n[type]
+ *     where is +/-n[type]
  *     - means n lines before end
  *     + means nth line from beginning
  *     type 'b' means tail n blocks, not lines
  *     - means n lines before end
  *     + means nth line from beginning
  *     type 'b' means tail n blocks, not lines
@@ -18,9 +33,12 @@ static char *sccsid = "@(#)tail.c    4.1 (Berkeley) %G%";
 #include       <ctype.h>
 #include       <sys/types.h>
 #include       <sys/stat.h>
 #include       <ctype.h>
 #include       <sys/types.h>
 #include       <sys/stat.h>
+#include       <sys/file.h>
 #include       <errno.h>
 
 #include       <errno.h>
 
-#define LBIN 4097
+#define LBIN 32769
+#undef BUFSIZ
+#define        BUFSIZ  8192
 struct stat    statb;
 int    follow;
 int    piped;
 struct stat    statb;
 int    follow;
 int    piped;
@@ -36,8 +54,6 @@ char **argv;
        int partial,bylines,bkwds,fromend,lastnl;
        char *p;
 
        int partial,bylines,bkwds,fromend,lastnl;
        char *p;
 
-       lseek(0,(long)0,1);
-       piped = errno==ESPIPE;
        arg = argv[1];
        if(argc<=1 || *arg!='-'&&*arg!='+') {
                arg = "-10l";
        arg = argv[1];
        if(argc<=1 || *arg!='-'&&*arg!='+') {
                arg = "-10l";
@@ -46,23 +62,29 @@ char **argv;
        }
        fromend = *arg=='-';
        arg++;
        }
        fromend = *arg=='-';
        arg++;
-       n = 0;
-       while(isdigit(*arg))
-               n = n*10 + *arg++ - '0';
+       if (isdigit(*arg)) {
+               n = 0;
+               while(isdigit(*arg))
+                       n = n*10 + *arg++ - '0';
+       } else
+               n = -1;
        if(!fromend&&n>0)
                n--;
        if(argc>2) {
        if(!fromend&&n>0)
                n--;
        if(argc>2) {
-               close(0);
+               (void)close(0);
                if(open(argv[2],0)!=0) {
                        perror(argv[2]);
                        exit(1);
                }
        }
                if(open(argv[2],0)!=0) {
                        perror(argv[2]);
                        exit(1);
                }
        }
+       (void)lseek(0,(off_t)0,L_INCR);
+       piped = errno==ESPIPE;
        bylines = -1; bkwds = 0;
        while(*arg)
        switch(*arg++) {
 
        case 'b':
        bylines = -1; bkwds = 0;
        while(*arg)
        switch(*arg++) {
 
        case 'b':
+               if (n == -1) n = 1;
                n <<= 9;
                if(bylines!=-1) goto errcom;
                bylines=0;
                n <<= 9;
                if(bylines!=-1) goto errcom;
                bylines=0;
@@ -75,7 +97,7 @@ char **argv;
                follow = 1;
                break;
        case 'r':
                follow = 1;
                break;
        case 'r':
-               if(n==0) n = LBIN;
+               if(n==-1) n = LBIN;
                bkwds = 1; fromend = 1; bylines = 1;
                break;
        case 'l':
                bkwds = 1; fromend = 1; bylines = 1;
                break;
        case 'l':
@@ -85,7 +107,7 @@ char **argv;
        default:
                goto errcom;
        }
        default:
                goto errcom;
        }
-       if (n==0) n = 10;
+       if (n==-1) n = 10;
        if(bylines==-1) bylines = 1;
        if(bkwds) follow=0;
        if(fromend)
        if(bylines==-1) bylines = 1;
        if(bkwds) follow=0;
        if(fromend)
@@ -105,10 +127,10 @@ char **argv;
                                }
                        } while(*p++ != '\n');
                }
                                }
                        } while(*p++ != '\n');
                }
-               write(1,p,j);
+               (void)write(1,p,j);
        } else  if(n>0) {
                if(!piped)
        } else  if(n>0) {
                if(!piped)
-                       fstat(0,&statb);
+                       (void)fstat(0,&statb);
                if(piped||(statb.st_mode&S_IFMT)==S_IFCHR)
                        while(n>0) {
                                i = n>BUFSIZ?BUFSIZ:n;
                if(piped||(statb.st_mode&S_IFMT)==S_IFCHR)
                        while(n>0) {
                                i = n>BUFSIZ?BUFSIZ:n;
@@ -118,11 +140,11 @@ char **argv;
                                n -= i;
                        }
                else
                                n -= i;
                        }
                else
-                       lseek(0,n,0);
+                       (void)lseek(0,(off_t)n,L_SET);
        }
 copy:
        while((i=read(0,bin,BUFSIZ))>0)
        }
 copy:
        while((i=read(0,bin,BUFSIZ))>0)
-               write(1,bin,i);
+               (void)write(1,bin,i);
        fexit();
 
                        /*seek from end*/
        fexit();
 
                        /*seek from end*/
@@ -131,10 +153,11 @@ keep:
        if(n <= 0)
                fexit();
        if(!piped) {
        if(n <= 0)
                fexit();
        if(!piped) {
-               fstat(0,&statb);
-               di = !bylines&&n<LBIN?n:LBIN-1;
+               (void)fstat(0,&statb);
+               /* If by lines, back up 1 buffer: else back up as needed */
+               di = bylines?LBIN-1:n;
                if(statb.st_size > di)
                if(statb.st_size > di)
-                       lseek(0,-di,2);
+                       (void)lseek(0,(off_t)-di,L_XTND);
                if(!bylines)
                        goto copy;
        }
                if(!bylines)
                        goto copy;
        }
@@ -169,17 +192,18 @@ brka:
                        do {
                                if(--k<0) {
                                        if(partial) {
                        do {
                                if(--k<0) {
                                        if(partial) {
-                                               if(bkwds) write(1,bin,lastnl+1);
+                                               if(bkwds) 
+                                                   (void)write(1,bin,lastnl+1);
                                                goto brkb;
                                        }
                                        k = LBIN -1;
                                }
                        } while(bin[k]!='\n'&&k!=i);
                        if(bkwds && j>0){
                                                goto brkb;
                                        }
                                        k = LBIN -1;
                                }
                        } while(bin[k]!='\n'&&k!=i);
                        if(bkwds && j>0){
-                               if(k<lastnl) write(1,&bin[k+1],lastnl-k);
+                               if(k<lastnl) (void)write(1,&bin[k+1],lastnl-k);
                                else {
                                else {
-                                       write(1,&bin[k+1],LBIN-k-1);
-                                       write(1,bin,lastnl+1);
+                                       (void)write(1,&bin[k+1],LBIN-k-1);
+                                       (void)write(1,bin,lastnl+1);
                                }
                        }
                } while(j++<n&&k!=i);
                                }
                        }
                } while(j++<n&&k!=i);
@@ -191,10 +215,10 @@ brkb:
                } while(bin[k]!='\n'&&k!=i);
        }
        if(k<i)
                } while(bin[k]!='\n'&&k!=i);
        }
        if(k<i)
-               write(1,&bin[k+1],i-k-1);
+               (void)write(1,&bin[k+1],i-k-1);
        else {
        else {
-               write(1,&bin[k+1],LBIN-k-1);
-               write(1,bin,i);
+               (void)write(1,&bin[k+1],LBIN-k-1);
+               (void)write(1,bin,i);
        }
        fexit();
 errcom:
        }
        fexit();
 errcom:
@@ -208,6 +232,7 @@ fexit()
        for (;;)
        {       sleep(1);
                while ((n = read (0, bin, BUFSIZ)) > 0)
        for (;;)
        {       sleep(1);
                while ((n = read (0, bin, BUFSIZ)) > 0)
-                       write (1, bin, n);
+                       if (write (1, bin, n) < 0)
+                               exit(1);
        }
 }
        }
 }