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 "@(#)puts.s 5.6 (Berkeley) 06/27/88" 20#endif /* LIBC_SCCS and not lint */ 21 22/* 23 * puts(s); 24 * char *s; 25 * 26 * argument: a source string. 27 * side effects: writes to the standard output using the data in 28 * the null-terminated source string; a newline is appended. 29 * result: technically void; for compatibility we return 0 for the null 30 * string, non-zero otherwise. We return zero for errors too. 31 */ 32 33#include "DEFS.h" 34 35#define NBF 04 36#define LBF 0200 37 38#define NL 012 39 40ENTRY(puts, R11|R10|R9) 41 42#define S r11 43 movl 4(ap),S 44#define IOP r10 45#define _CNT 46#define _PTR 4 47#define _BASE 8 48#define _BUFSIZ 12 49#define _FLAG 16 50 movab __iob+20,IOP 51 52#define UNBUF -4(fp) 53 54#define COUNT r9 55 56 /* 57 * For unbuffered I/O, line buffer the output line. 58 * Ugly but fast -- and doesn't CURRENTLY break anything (sigh). 59 */ 60 movab -1028(sp),sp 61 bicw3 $~NBF,_FLAG(IOP),UNBUF 62 jeql 1f 63 64 bicw2 $NBF,_FLAG(IOP) /* Clear no-buffering flag */ 65 movl sp,_BASE(IOP) /* Create a buffer */ 66 movl sp,_PTR(IOP) 67 cvtwl $1024,_BUFSIZ(IOP) 68 jbr 2f 69 701: 71 tstl _CNT(IOP) /* Has a buffer been allocated? */ 72 jgtr 2f 73 pushl IOP /* Get _flsbuf() to make one */ 74 pushl $0 75 calls $2,__flsbuf 76 tstl r0 77 jlss Lerror 78 incl _CNT(IOP) /* Unput the char we sent */ 79 decl _PTR(IOP) 802: 81 82 /* 83 * Search for the terminating null. 84 */ 85Lloop: 86 addl3 _BASE(IOP),_BUFSIZ(IOP),COUNT /* How many bytes? */ 87 subl2 _PTR(IOP),COUNT 88 locc $0,COUNT,(S) /* Look for a null */ 89 jeql Lagain 90 91 subl2 r0,COUNT /* Copy the data */ 92 movc3 COUNT,(S),*_PTR(IOP) 93 movl r3,_PTR(IOP) /* Fix up IOP */ 94 subl2 COUNT,_CNT(IOP) 95 96Lnl: 97 movb $NL,*_PTR(IOP) /* Append a newline */ 98 incl _PTR(IOP) 99 decl _CNT(IOP) 100 101 bitw $LBF,_FLAG(IOP) /* If line buffered... */ 102 jneq 1f 103 tstw UNBUF /* or unbuffered... */ 104 jneq 1f 105 tstl _CNT(IOP) /* or a full buffer... */ 106 jgtr 2f 1071: 108 pushl IOP /* ... flush the buffer */ 109 calls $1,_fflush 110 tstl r0 111 jlss Lerror 1122: 113 114 /* 115 * Fix up buffering again. 116 */ 117 tstw UNBUF 118 jeql 1f 119 bisw2 $NBF,_FLAG(IOP) /* Reset flag */ 120 clrl _BASE(IOP) /* Clear data structure */ 121 clrl _BUFSIZ(IOP) 122 clrl _CNT(IOP) 1231: 124 cvtbl $NL,r0 /* Compatibility hack */ 125 ret 126 127 /* 128 * We didn't find the null -- loop. 129 */ 130Lagain: 131 movc3 COUNT,(S),*_PTR(IOP) /* Copy the data */ 132 movl r1,S 133 movl r3,_PTR(IOP) /* Fix up IOP */ 134 subl2 COUNT,_CNT(IOP) 135 pushl IOP /* The buffer is full -- flush it */ 136 calls $1,_fflush 137 tstl r0 138 jlss Lerror 139 tstb (S) /* More data? */ 140 jneq Lloop 141 jbr Lnl 142 143 /* 144 * Bomb out. Return 0 (why not? that's what the old one did). 145 */ 146Lerror: 147 clrl r0 148 ret 149