|
|
1.1 root 1: /*
2: * Copyright (c) 1985 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
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.
16: */
17:
18: #if defined(LIBC_SCCS) && !defined(lint)
19: .asciz "@(#)fgets.s 5.5 (Berkeley) 6/27/88"
20: #endif /* LIBC_SCCS and not lint */
21:
22: /*
23: * char *fgets(s, n, iptr);
24: * char *s;
25: * int n;
26: * FILE *iptr;
27: *
28: * arguments: a target string, a length, and a file pointer.
29: * side effects: reads up to and including a newline, or up to n-1 bytes,
30: * whichever is less, from the file indicated by iptr into the target
31: * string and null terminates.
32: * result: the target string if successful, 0 otherwise.
33: */
34:
35: #include "DEFS.h"
36:
37: #define NL 0xa
38:
39: ENTRY(fgets, R11|R10|R9)
40:
41: #define OLD_S 4(ap)
42: #define S r11
43: movl OLD_S,S
44:
45: #define N 8(ap)
46:
47: #define IPTR r10
48: #define _CNT
49: #define _PTR 4
50: #define _BASE 8
51: movl 12(ap),IPTR
52:
53: #define COUNT r9
54:
55: /*
56: * Sanity check -- is the buffer big enough?
57: */
58: cmpl N,$1
59: jleq Lerror
60:
61: subl3 $1,N,COUNT /* We scan at most COUNT chars */
62:
63: /*
64: * If no characters, call _filbuf() to get some.
65: */
66: tstl _CNT(IPTR)
67: jgtr Lscan
68:
69: Lloop:
70: pushl IPTR
71: calls $1,__filbuf
72: tstl r0
73: jlss Leof
74: movb r0,(S)+ /* Save the returned character */
75: decl N
76: decl COUNT
77: jleq 1f
78: cmpb r0,$NL /* If it was a newline, we're done */
79: jneq 2f
80: 1:
81: clrb (S)
82: jbr Lret
83: 2:
84: tstl _BASE(IPTR) /* Is the input buffered? */
85: jeql Lloop /* If not, loop inefficiently */
86:
87: /*
88: * Look for a newline in the buffer.
89: */
90: Lscan:
91: cmpl _CNT(IPTR),COUNT /* Is buffer bigger than N-1? */
92: jgeq 1f
93: movl _CNT(IPTR),COUNT /* If not, don't read off the end */
94: 1:
95: locc $NL,COUNT,*_PTR(IPTR) /* Scan the buffer */
96: jeql Lagain
97:
98: /*
99: * Success -- copy the data and return.
100: */
101: decl r0 /* How many characters did we read? */
102: subl2 r0,COUNT
103: movc3 COUNT,*_PTR(IPTR),(S) /* Copy the data */
104: clrb (r3)
105: subl2 COUNT,_CNT(IPTR) /* Fix up the I/O buffer */
106: movl r1,_PTR(IPTR)
107:
108: Lret:
109: movl OLD_S,r0
110: ret
111:
112: /*
113: * If we run out of characters, copy the buffer and loop if needed.
114: */
115: Lagain:
116: movc3 COUNT,*_PTR(IPTR),(S) /* Copy the data */
117: subl2 COUNT,_CNT(IPTR) /* Adjust the buffers and counts */
118: movl r1,_PTR(IPTR)
119: subl2 COUNT,N
120: movl r3,S
121: subl3 $1,N,COUNT
122: jgtr Lloop
123:
124: /*
125: * End of file? Check to see if we copied any data.
126: */
127: Leof:
128: cmpl S,OLD_S
129: jeql Lerror
130: clrb (S)
131: jbr Lret
132:
133: /*
134: * Error return -- null pointer.
135: */
136: Lerror:
137: clrl r0
138: ret
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.