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