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.s 5.2 (Berkeley) 03/09/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 26ENTRY(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 46Lloop: 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 551: 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 */ 63Lscan: 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 */ 81Lret: 82 movl OLD_S,r0 83 ret 84 85 /* 86 * If we run out of characters, copy the buffer and loop. 87 */ 88Lagain: 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 */ 98Leof: 99 cmpl S,OLD_S 100 jeql Lerror 101 clrb (S) 102 jbr Lret 103 104 /* 105 * Error/eof return -- null pointer. 106 */ 107Lerror: 108 clrl r0 109 ret 110