Bell 32V release
[unix-history] / usr / src / cmd / tail.c
index 974824c..b5e5aa9 100644 (file)
@@ -6,8 +6,6 @@
  *     + means nth line from beginning
  *     type 'b' means tail n blocks, not lines
  *     type 'c' means tail n characters
  *     + means nth line from beginning
  *     type 'b' means tail n blocks, not lines
  *     type 'c' means tail n characters
- *     Type 'r' means in lines in reverse order from end
- *      (for -r, default is entire buffer )
 */
 #include       <sys/types.h>
 #include       <sys/stat.h>
 */
 #include       <sys/types.h>
 #include       <sys/stat.h>
@@ -21,9 +19,10 @@ main(argc,argv)
 char **argv;
 {
        long n,di;
 char **argv;
 {
        long n,di;
+       int fromend;
        register i,j,k;
        char *p;
        register i,j,k;
        char *p;
-       int partial,piped,bylines,bkwds,fromend,lastnl;
+       int partial,piped,bylines;
        char *arg;
        lseek(0,(long)0,1);
        piped = errno==ESPIPE;
        char *arg;
        lseek(0,(long)0,1);
        piped = errno==ESPIPE;
@@ -35,6 +34,8 @@ char **argv;
        }
        fromend = *arg=='-';
        arg++;
        }
        fromend = *arg=='-';
        arg++;
+       if(!digit(*arg))
+               goto errcom;
        n = 0;
        while(digit(*arg))
                n = n*10 + *arg++ - '0';
        n = 0;
        while(digit(*arg))
                n = n*10 + *arg++ - '0';
@@ -49,17 +50,13 @@ char **argv;
                        exit(1);
                }
        }
                        exit(1);
                }
        }
-       bylines = 0; bkwds = 0;
+       bylines = 0;
        switch(*arg) {
        case 'b':
                n <<= 9;
                break;
        case 'c':
                break;
        switch(*arg) {
        case 'b':
                n <<= 9;
                break;
        case 'c':
                break;
-       case 'r':
-               if(n==0) n = LBIN;
-               bkwds = 1; fromend = 1; bylines = 1;
-               break;
        case '\0':
        case 'l':
                bylines = 1;
        case '\0':
        case 'l':
                bylines = 1;
@@ -97,7 +94,6 @@ char **argv;
                else
                        lseek(0,n,0);
        }
                else
                        lseek(0,n,0);
        }
-copy:
        while((i=read(0,bin,512))>0)
                write(1,bin,i);
        exit(0);
        while((i=read(0,bin,512))>0)
                write(1,bin,i);
        exit(0);
@@ -108,11 +104,9 @@ keep:
        if(n<=0) exit(0);
        if(!piped) {
                fstat(0,&statb);
        if(n<=0) exit(0);
        if(!piped) {
                fstat(0,&statb);
-               di = !bylines? n: LBIN-1;
+               di = !bylines&&n<LBIN?n:LBIN-1;
                if(statb.st_size > di)
                        lseek(0,-di,2);
                if(statb.st_size > di)
                        lseek(0,-di,2);
-               if(!bylines)
-                       goto copy;
        }
        partial = 1;
        for(;;) {
        }
        partial = 1;
        for(;;) {
@@ -134,33 +128,18 @@ brka:
                    i-n+LBIN;
                k--;
        } else {
                    i-n+LBIN;
                k--;
        } else {
-               if(bkwds && bin[i==0?LBIN-1:i-1]!='\n'){        /* force trailing newline */
-                       bin[i]='\n';
-                       if(++i>=LBIN) {i = 0; partial = 0;}
-               }
                k = i;
                j = 0;
                do {
                k = i;
                j = 0;
                do {
-                       lastnl = k;
                        do {
                                if(--k<0) {
                        do {
                                if(--k<0) {
-                                       if(partial) {
-                                               if(bkwds) write(1,bin,lastnl+1);
+                                       if(partial)
                                                goto brkb;
                                                goto brkb;
-                                       }
                                        k = LBIN -1;
                                }
                        } while(bin[k]!='\n'&&k!=i);
                                        k = LBIN -1;
                                }
                        } while(bin[k]!='\n'&&k!=i);
-                       if(bkwds && j>0){
-                               if(k<lastnl) write(1,&bin[k+1],lastnl-k);
-                               else {
-                                       write(1,&bin[k+1],LBIN-k-1);
-                                       write(1,bin,lastnl+1);
-                               }
-                       }
                } while(j++<n&&k!=i);
 brkb:
                } while(j++<n&&k!=i);
 brkb:
-               if(bkwds) exit(0);
                if(k==i) do {
                        if(++k>=LBIN)
                                k = 0;
                if(k==i) do {
                        if(++k>=LBIN)
                                k = 0;
@@ -174,7 +153,7 @@ brkb:
        }
        exit(0);
 errcom:
        }
        exit(0);
 errcom:
-       write(2,"usage: tail +\b_n[lbcr] [file]\n",30);
+       write(2,"usage: tail +\b_n[lbc] [file]\n",29);
        exit(1);
 }
 
        exit(1);
 }