Commit | Line | Data |
---|---|---|
762bce70 KM |
1 | /* |
2 | * Copyright (c) 1985 Regents of the University of California. | |
2c817985 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
4afb9d15 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
762bce70 KM |
16 | */ |
17 | ||
4afb9d15 KB |
18 | #if defined(LIBC_SCCS) && !defined(lint) |
19 | .asciz "@(#)gets.s 5.4 (Berkeley) %G%" | |
20 | #endif /* LIBC_SCCS and not lint */ | |
762bce70 KM |
21 | |
22 | /* | |
23 | * char *gets(s); | |
24 | * char *s; | |
25 | * | |
26 | * argument: a target string | |
27 | * side effects: reads bytes up to and including a newline from the | |
28 | * standard input into the target string and replaces the newline | |
29 | * with a null to null-terminate the string. | |
30 | * result: the target string if successful, 0 otherwise. | |
31 | */ | |
32 | ||
33 | #include "DEFS.h" | |
34 | ||
35 | #define NL 0xa | |
36 | ||
37 | ENTRY(gets, R11|R10) | |
38 | ||
39 | #define S r11 | |
40 | movl 4(ap),S | |
41 | #define IPTR r10 | |
42 | #define _CNT | |
43 | #define _PTR 4 | |
44 | #define _BASE 8 | |
45 | #define _BUFSIZ 12 | |
46 | #define _FLAG 16 | |
47 | movab __iob,IPTR | |
48 | ||
49 | #define OLD_S 4(ap) | |
50 | ||
51 | /* | |
52 | * If no characters, call _filbuf() to get some. | |
53 | */ | |
54 | tstl _CNT(IPTR) | |
55 | jgtr Lscan | |
56 | ||
57 | Lloop: | |
58 | pushl IPTR | |
59 | calls $1,__filbuf | |
60 | tstl r0 /* What did _filbuf() return? */ | |
61 | jlss Leof | |
62 | cmpb r0,$NL | |
63 | jneq 1f | |
64 | clrb (S) | |
65 | jbr Lret | |
66 | 1: | |
67 | movb r0,(S)+ /* Save the returned character */ | |
68 | tstl _BASE(IPTR) /* Is input buffered? */ | |
69 | jeql Lloop | |
70 | ||
71 | /* | |
72 | * Look for a newline in the buffer. | |
73 | */ | |
74 | Lscan: | |
75 | locc $NL,_CNT(IPTR),*_PTR(IPTR) | |
76 | jeql Lagain | |
77 | ||
78 | /* | |
79 | * Success -- copy the data and return. | |
80 | */ | |
81 | subl3 r0,_CNT(IPTR),r2 | |
82 | subl2 r2,_CNT(IPTR) | |
83 | movc3 r2,*_PTR(IPTR),(S) /* Copy the data */ | |
84 | clrb (r3) | |
85 | movl r1,_PTR(IPTR) | |
86 | decl _CNT(IPTR) /* Skip the newline */ | |
87 | incl _PTR(IPTR) | |
88 | ||
89 | /* | |
90 | * Normal return. | |
91 | */ | |
92 | Lret: | |
93 | movl OLD_S,r0 | |
94 | ret | |
95 | ||
96 | /* | |
97 | * If we run out of characters, copy the buffer and loop. | |
98 | */ | |
99 | Lagain: | |
100 | movc3 _CNT(IPTR),*_PTR(IPTR),(S) /* Copy the data */ | |
101 | movl r3,S | |
102 | movl _BASE(IPTR),_PTR(IPTR) /* Reset stdio */ | |
103 | clrl _CNT(IPTR) | |
104 | jbr Lloop | |
105 | ||
106 | /* | |
107 | * End of file? Check to see if we copied any data. | |
108 | */ | |
109 | Leof: | |
110 | cmpl S,OLD_S | |
111 | jeql Lerror | |
112 | clrb (S) | |
113 | jbr Lret | |
114 | ||
115 | /* | |
116 | * Error/eof return -- null pointer. | |
117 | */ | |
118 | Lerror: | |
119 | clrl r0 | |
120 | ret |