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