1: /*
   2:  * Copyright (c) 1987 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifdef LIBC_SCCS
   8:         <@(#)gets.s	5.6 (Berkeley) 9/2/88\0>
   9:         .even
  10: #endif LIBC_SCCS
  11: 
  12: #include "DEFS.h"
  13: #include "STDIO.h"
  14: 
  15: .globl  __filbuf
  16: 
  17: #define         S       r3
  18: #define         IOP     r2
  19: #define         P       r1
  20: #define         C       r0
  21: /*
  22:  * P & C get trounced when we call someone else ...
  23:  */
  24: 
  25: /*
  26:  * char *gets(s);
  27:  * char *s;
  28:  *
  29:  * argument: a target string
  30:  * side effects: reads bytes up to and including a newline from the
  31:  *	standard input into the target string and replaces the newline
  32:  *	with a null to null-terminate the string.
  33:  * result: the target string if successful, 0 otherwise.
  34:  */
  35: ENTRY(gets)
  36:         mov     r2,-(sp)                / need a few registers
  37:         mov     r3,-(sp)
  38: 
  39: #	define        OLD_S   6.(sp)
  40: 
  41:         mov     OLD_S,S                 / grab string pointer
  42:         mov     $STDIN,IOP              / in from stdin
  43: 
  44:         /*
  45: 	 * If no characters, call _filbuf() to get some.
  46: 	 */
  47:         tst     _CNT(IOP)
  48:         bgt     Lscan
  49: 
  50: Lloop:
  51:         mov     IOP,-(sp)               / _filbuf(stdin)
  52:         jsr     pc,__filbuf
  53:         tst     (sp)+
  54:         tst     r0                      / _filbuf return EOF?
  55:         blt     Leof
  56:         cmpb    r0,$NL                  / a newline?
  57:         bne     1f
  58:         clrb    (S)                     / yes, terminate the string and return
  59:         br      Lret                    /   with a pointer to it
  60: 1:
  61:         movb    r0,(S)+                 / save the returned character
  62:         tst     _BASE(IOP)              / is input buffered?
  63:         beq     Lloop                   / no, have to do it the hard way ...
  64:         tst     _CNT(IOP)               / did __filbuf leave us anything
  65:         beq     Lloop                   /   to work with??
  66: 
  67: Lscan:
  68:         /*
  69: 	 * Copy till terminating newline found or end of buffer.
  70: 	 */
  71:         mov     _PTR(IOP),P             / grab pointer into I/O buffer
  72:         mov     _CNT(IOP),C             /   and how many characters in it
  73: 1:
  74:         movb    (P),(S)+                / copy from buffer to string
  75:         cmpb    (P)+,$NL                / was it a newline?
  76:         beq     2f
  77:         sob     C,1b                    / repeat till we hit the end of the
  78:         br      Lloop                   / buffer, and back for another
  79: 2:
  80:         clrb    -(S)                    / overwrite the newline with a null
  81:         sub     _PTR(IOP),P             / figure out how much we took from
  82:         add     P,_PTR(IOP)             / the buffer and update IOP
  83:         sub     P,_CNT(IOP)
  84: 
  85: Lret:
  86:         mov     OLD_S,r0                / return pointer to string
  87: Lexit:
  88:         mov     (sp)+,r3                / restore registers
  89:         mov     (sp)+,r2
  90:         rts     pc                      / and return
  91: 
  92:         /*
  93: 	 * End of file?  Check to see if we copied any data.
  94: 	 */
  95: Leof:
  96:         cmp     S,OLD_S                 / did we copy anything?
  97:         beq     Lerror                  / nope, return null
  98:         clrb    (S)                     / yes, terminate string
  99:         br      Lret                    /   and return a pointer to it
 100: 
 101:         /*
 102: 	 * Error/eof return -- null pointer.
 103: 	 */
 104: Lerror:
 105:         clr     r0
 106:         br      Lexit

Defined functions

Leof defined in line 95; used 1 times
  • in line 55
Lerror defined in line 104; used 1 times
  • in line 97
Lexit defined in line 87; used 1 times
Lloop defined in line 50; used 3 times
Lret defined in line 85; used 2 times
Lscan defined in line 67; used 1 times
  • in line 48
_gets defined in line 35; used 120 times

Defined macros

C defined in line 20; used 2 times
IOP defined in line 18; used 10 times
OLD_S defined in line 39; used 3 times
P defined in line 19; used 6 times
S defined in line 17; used 7 times
Last modified: 1988-12-26
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3046
Valid CSS Valid XHTML 1.0 Strict