      SUBROUTINE MAIN
      LOGICAL*1 ARG(512)
      INTEGER N, I, FD
      INTEGER CTOI, GETARG, OPEN
      CALL QUERY(28Husage:  tail [-n] [file] ...)
      N = 23
      FD = -3
      I=1
23000 IF (.NOT.(GETARG(I, ARG, 512) .NE. -1))GOTO 23002
      IF (.NOT.(ARG(1) .EQ. 45 .AND. ARG(2) .NE. 0))GOTO 23003
      J = 2
      N = CTOI(ARG, J)
      IF (.NOT.(N .LE. 0))GOTO 23005
      CALL ERROR (13Hinvalid size.)
23005 CONTINUE
      GOTO 23004
23003 CONTINUE
      IF (.NOT.(ARG(1) .EQ. 45 .AND. ARG(2) .EQ. 0))GOTO 23007
      FD = 1
      CALL PTAIL (N, FD)
      GOTO 23008
23007 CONTINUE
      FD = OPEN(ARG, 1)
      IF (.NOT.(FD .EQ. -3))GOTO 23009
      CALL CANT(ARG)
23009 CONTINUE
      CALL PTAIL (N, FD)
      CALL CLOSE(FD)
23008 CONTINUE
23004 CONTINUE
23001 I=I+1
      GOTO 23000
23002 CONTINUE
      IF (.NOT.(FD .EQ. -3))GOTO 23011
      CALL PTAIL (N, 1)
23011 CONTINUE
      RETURN
      END
      SUBROUTINE PTAIL (NLINS, FD)
      INTEGER N, FD, NLINS
      LOGICAL*1 BUF(3000)
      LOGICAL*1 GETCH
      INTEGER HEAD, TAIL, I
      HEAD = 1
      TAIL = 1
      N = NLINS
23013 IF (.NOT.(GETCH(BUF(TAIL), FD) .NE. -1))GOTO 23014
      TAIL = MOD(TAIL, 3000) + 1
      IF (.NOT.(TAIL .EQ. HEAD))GOTO 23015
      HEAD = MOD(HEAD, 3000) + 1
23015 CONTINUE
      GOTO 23013
23014 CONTINUE
      I = TAIL
23017 IF (.NOT.(I .NE. HEAD))GOTO 23019
      I = I - 1
      IF (.NOT.(I .EQ. 0))GOTO 23020
      I = 3000
23020 CONTINUE
      IF (.NOT.(BUF(I) .EQ. 10))GOTO 23022
      N = N - 1
      IF (.NOT.(N .LT. 0))GOTO 23024
      I = MOD(I, 3000) + 1
      GOTO 23019
23024 CONTINUE
23022 CONTINUE
23018 GOTO 23017
23019 CONTINUE
      HEAD = I
23026 IF (.NOT.(HEAD .NE. TAIL))GOTO 23028
      CALL PUTCH(BUF(HEAD), 2)
23027 HEAD = MOD(HEAD, 3000) + 1
      GOTO 23026
23028 CONTINUE
      RETURN
      END
