10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*722Smuffin * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
24*722Smuffin * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*LINTLIBRARY*/
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * This version writes directly to the buffer rather than looping on putc.
360Sstevel@tonic-gate * Ptr args aren't checked for NULL because the program would be a
370Sstevel@tonic-gate * catastrophic mess anyway. Better to abort than just to return NULL.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include "stdiom.h"
410Sstevel@tonic-gate #include <errno.h>
42*722Smuffin #include <memory.h>
430Sstevel@tonic-gate
44*722Smuffin static char *memnulccpy(char *, char *, int, int);
450Sstevel@tonic-gate
460Sstevel@tonic-gate int
fputs(char * ptr,FILE * iop)47*722Smuffin fputs(char *ptr, FILE *iop)
480Sstevel@tonic-gate {
49*722Smuffin int ndone = 0, n;
50*722Smuffin unsigned char *cptr, *bufend;
51*722Smuffin char *p;
52*722Smuffin char c;
530Sstevel@tonic-gate
540Sstevel@tonic-gate if (_WRTCHK(iop)) {
550Sstevel@tonic-gate iop->_flag |= _IOERR;
560Sstevel@tonic-gate #ifdef POSIX
570Sstevel@tonic-gate errno = EBADF;
58*722Smuffin #endif /* POSIX */
590Sstevel@tonic-gate return (EOF);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate bufend = iop->_base + iop->_bufsiz;
620Sstevel@tonic-gate
630Sstevel@tonic-gate if ((iop->_flag & _IONBF) == 0) {
640Sstevel@tonic-gate if (iop->_flag & _IOLBF) {
650Sstevel@tonic-gate for ( ; ; ptr += n) {
660Sstevel@tonic-gate while ((n = bufend - (cptr = iop->_ptr)) <= 0)
670Sstevel@tonic-gate /* full buf */
680Sstevel@tonic-gate if (_xflsbuf(iop) == EOF)
690Sstevel@tonic-gate return(EOF);
700Sstevel@tonic-gate if ((p = memnulccpy((char *) cptr, ptr, '\n', n)) != NULL) {
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * Copy terminated either because we
730Sstevel@tonic-gate * saw a newline or we saw a NUL (end
740Sstevel@tonic-gate * of string).
750Sstevel@tonic-gate */
760Sstevel@tonic-gate c = *(p - 1); /* last character moved */
770Sstevel@tonic-gate if (c == '\0')
780Sstevel@tonic-gate p--; /* didn't write '\0' */
790Sstevel@tonic-gate n = p - (char *) cptr;
800Sstevel@tonic-gate }
810Sstevel@tonic-gate iop->_cnt -= n;
820Sstevel@tonic-gate iop->_ptr += n;
830Sstevel@tonic-gate _BUFSYNC(iop);
840Sstevel@tonic-gate ndone += n;
850Sstevel@tonic-gate if (p != NULL) {
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate * We found either a newline or a NUL.
880Sstevel@tonic-gate * If we found a newline, flush the
890Sstevel@tonic-gate * buffer.
900Sstevel@tonic-gate * If we found a NUL, we're done.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate if (c == '\n') {
930Sstevel@tonic-gate if (_xflsbuf(iop) == EOF)
940Sstevel@tonic-gate return(EOF);
950Sstevel@tonic-gate } else {
960Sstevel@tonic-gate /* done */
970Sstevel@tonic-gate return(ndone);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate } else {
1020Sstevel@tonic-gate for ( ; ; ptr += n) {
1030Sstevel@tonic-gate while ((n = bufend - (cptr = iop->_ptr)) <= 0)
1040Sstevel@tonic-gate /* full buf */
1050Sstevel@tonic-gate if (_xflsbuf(iop) == EOF)
1060Sstevel@tonic-gate return(EOF);
1070Sstevel@tonic-gate if ((p = memccpy((char *) cptr, ptr, '\0', n)) != NULL)
1080Sstevel@tonic-gate n = (p - (char *) cptr) - 1;
1090Sstevel@tonic-gate iop->_cnt -= n;
1100Sstevel@tonic-gate iop->_ptr += n;
1110Sstevel@tonic-gate _BUFSYNC(iop);
1120Sstevel@tonic-gate ndone += n;
1130Sstevel@tonic-gate if (p != NULL) {
1140Sstevel@tonic-gate /* done */
1150Sstevel@tonic-gate return(ndone);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate } else {
1200Sstevel@tonic-gate /* write out to an unbuffered file */
1210Sstevel@tonic-gate return (write(iop->_file, ptr, strlen(ptr)));
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate * Copy s2 to s1, stopping if character c or a NUL is copied.
1270Sstevel@tonic-gate * Copy no more than n bytes.
1280Sstevel@tonic-gate * Return a pointer to the byte after character c or NUL in the copy,
1290Sstevel@tonic-gate * or NULL if c or NUL is not found in the first n bytes.
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate static char *
memnulccpy(char * s1,char * s2,int c,int n)132*722Smuffin memnulccpy(char *s1, char *s2, int c, int n)
1330Sstevel@tonic-gate {
134*722Smuffin int cmoved;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate while (--n >= 0) {
1370Sstevel@tonic-gate cmoved = *s2++;
1380Sstevel@tonic-gate if ((*s1++ = cmoved) == '\0' || cmoved == c)
1390Sstevel@tonic-gate return (s1);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate return (0);
1420Sstevel@tonic-gate }
143