1: /*
2: * Copyright (c) 1985 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: _sccsid:.asciz "@(#)gets.c 5.2 (Berkeley) 3/9/86"
9: #endif LIBC_SCCS
10:
11: /*
12: * char *gets(s);
13: * char *s;
14: *
15: * argument: a target string
16: * side effects: reads bytes up to and including a newline from the
17: * standard input into the target string and replaces the newline
18: * with a null to null-terminate the string.
19: * result: the target string if successful, 0 otherwise.
20: */
21:
22: #include "DEFS.h"
23:
24: #define NL 0xa
25:
26: ENTRY(gets, R11|R10)
27:
28: #define S r11
29: movl 4(ap),S
30: #define IPTR r10
31: #define _CNT
32: #define _PTR 4
33: #define _BASE 8
34: #define _BUFSIZ 12
35: #define _FLAG 16
36: movab __iob,IPTR
37:
38: #define OLD_S 4(ap)
39:
40: /*
41: * If no characters, call _filbuf() to get some.
42: */
43: tstl _CNT(IPTR)
44: jgtr Lscan
45:
46: Lloop:
47: pushl IPTR
48: calls $1,__filbuf
49: tstl r0 /* What did _filbuf() return? */
50: jlss Leof
51: cmpb r0,$NL
52: jneq 1f
53: clrb (S)
54: jbr Lret
55: 1:
56: movb r0,(S)+ /* Save the returned character */
57: tstl _BASE(IPTR) /* Is input buffered? */
58: jeql Lloop
59:
60: /*
61: * Look for a newline in the buffer.
62: */
63: Lscan:
64: locc $NL,_CNT(IPTR),*_PTR(IPTR)
65: jeql Lagain
66:
67: /*
68: * Success -- copy the data and return.
69: */
70: subl3 r0,_CNT(IPTR),r2
71: subl2 r2,_CNT(IPTR)
72: movc3 r2,*_PTR(IPTR),(S) /* Copy the data */
73: clrb (r3)
74: movl r1,_PTR(IPTR)
75: decl _CNT(IPTR) /* Skip the newline */
76: incl _PTR(IPTR)
77:
78: /*
79: * Normal return.
80: */
81: Lret:
82: movl OLD_S,r0
83: ret
84:
85: /*
86: * If we run out of characters, copy the buffer and loop.
87: */
88: Lagain:
89: movc3 _CNT(IPTR),*_PTR(IPTR),(S) /* Copy the data */
90: movl r3,S
91: movl _BASE(IPTR),_PTR(IPTR) /* Reset stdio */
92: clrl _CNT(IPTR)
93: jbr Lloop
94:
95: /*
96: * End of file? Check to see if we copied any data.
97: */
98: Leof:
99: cmpl S,OLD_S
100: jeql Lerror
101: clrb (S)
102: jbr Lret
103:
104: /*
105: * Error/eof return -- null pointer.
106: */
107: Lerror:
108: clrl r0
109: ret
Defined functions
ENTRY
defined in line
8;
never used
_BASE
defined in line
8;
never used
_CNT
defined in line
8;
never used
_PTR
defined in line
8;
never used
clrb
defined in line
8;
never used
Defined macros
IPTR
defined in line
30; used 17 times
NL
defined in line
24;
never used
OLD_S
defined in line
38; used 2 times
S
defined in line
28; used 8 times
_BASE
defined in line
33; used 1 times
_CNT
defined in line
31; used 3 times
_FLAG
defined in line
35;
never used
_PTR
defined in line
32; used 2 times