124423Smckusick/* 224423Smckusick * Copyright (c) 1985 Regents of the University of California. 334435Sbostic * All rights reserved. 434435Sbostic * 534435Sbostic * Redistribution and use in source and binary forms are permitted 6*34826Sbostic * provided that the above copyright notice and this paragraph are 7*34826Sbostic * duplicated in all such forms and that any documentation, 8*34826Sbostic * advertising materials, and other materials related to such 9*34826Sbostic * distribution and use acknowledge that the software was developed 10*34826Sbostic * by the University of California, Berkeley. The name of the 11*34826Sbostic * University may not be used to endorse or promote products derived 12*34826Sbostic * from this software without specific prior written permission. 13*34826Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34826Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34826Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1624423Smckusick */ 1724423Smckusick 18*34826Sbostic#if defined(LIBC_SCCS) && !defined(lint) 19*34826Sbostic .asciz "@(#)gets.s 5.4 (Berkeley) 06/27/88" 20*34826Sbostic#endif /* LIBC_SCCS and not lint */ 2124423Smckusick 2224423Smckusick/* 2324423Smckusick * char *gets(s); 2424423Smckusick * char *s; 2524423Smckusick * 2624423Smckusick * argument: a target string 2724423Smckusick * side effects: reads bytes up to and including a newline from the 2824423Smckusick * standard input into the target string and replaces the newline 2924423Smckusick * with a null to null-terminate the string. 3024423Smckusick * result: the target string if successful, 0 otherwise. 3124423Smckusick */ 3224423Smckusick 3324423Smckusick#include "DEFS.h" 3424423Smckusick 3524423Smckusick#define NL 0xa 3624423Smckusick 3724423SmckusickENTRY(gets, R11|R10) 3824423Smckusick 3924423Smckusick#define S r11 4024423Smckusick movl 4(ap),S 4124423Smckusick#define IPTR r10 4224423Smckusick#define _CNT 4324423Smckusick#define _PTR 4 4424423Smckusick#define _BASE 8 4524423Smckusick#define _BUFSIZ 12 4624423Smckusick#define _FLAG 16 4724423Smckusick movab __iob,IPTR 4824423Smckusick 4924423Smckusick#define OLD_S 4(ap) 5024423Smckusick 5124423Smckusick /* 5224423Smckusick * If no characters, call _filbuf() to get some. 5324423Smckusick */ 5424423Smckusick tstl _CNT(IPTR) 5524423Smckusick jgtr Lscan 5624423Smckusick 5724423SmckusickLloop: 5824423Smckusick pushl IPTR 5924423Smckusick calls $1,__filbuf 6024423Smckusick tstl r0 /* What did _filbuf() return? */ 6124423Smckusick jlss Leof 6224423Smckusick cmpb r0,$NL 6324423Smckusick jneq 1f 6424423Smckusick clrb (S) 6524423Smckusick jbr Lret 6624423Smckusick1: 6724423Smckusick movb r0,(S)+ /* Save the returned character */ 6824423Smckusick tstl _BASE(IPTR) /* Is input buffered? */ 6924423Smckusick jeql Lloop 7024423Smckusick 7124423Smckusick /* 7224423Smckusick * Look for a newline in the buffer. 7324423Smckusick */ 7424423SmckusickLscan: 7524423Smckusick locc $NL,_CNT(IPTR),*_PTR(IPTR) 7624423Smckusick jeql Lagain 7724423Smckusick 7824423Smckusick /* 7924423Smckusick * Success -- copy the data and return. 8024423Smckusick */ 8124423Smckusick subl3 r0,_CNT(IPTR),r2 8224423Smckusick subl2 r2,_CNT(IPTR) 8324423Smckusick movc3 r2,*_PTR(IPTR),(S) /* Copy the data */ 8424423Smckusick clrb (r3) 8524423Smckusick movl r1,_PTR(IPTR) 8624423Smckusick decl _CNT(IPTR) /* Skip the newline */ 8724423Smckusick incl _PTR(IPTR) 8824423Smckusick 8924423Smckusick /* 9024423Smckusick * Normal return. 9124423Smckusick */ 9224423SmckusickLret: 9324423Smckusick movl OLD_S,r0 9424423Smckusick ret 9524423Smckusick 9624423Smckusick /* 9724423Smckusick * If we run out of characters, copy the buffer and loop. 9824423Smckusick */ 9924423SmckusickLagain: 10024423Smckusick movc3 _CNT(IPTR),*_PTR(IPTR),(S) /* Copy the data */ 10124423Smckusick movl r3,S 10224423Smckusick movl _BASE(IPTR),_PTR(IPTR) /* Reset stdio */ 10324423Smckusick clrl _CNT(IPTR) 10424423Smckusick jbr Lloop 10524423Smckusick 10624423Smckusick /* 10724423Smckusick * End of file? Check to see if we copied any data. 10824423Smckusick */ 10924423SmckusickLeof: 11024423Smckusick cmpl S,OLD_S 11124423Smckusick jeql Lerror 11224423Smckusick clrb (S) 11324423Smckusick jbr Lret 11424423Smckusick 11524423Smckusick /* 11624423Smckusick * Error/eof return -- null pointer. 11724423Smckusick */ 11824423SmckusickLerror: 11924423Smckusick clrl r0 12024423Smckusick ret 121