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
51846Scraigm * Common Development and Distribution License (the "License").
61846Scraigm * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211846Scraigm
220Sstevel@tonic-gate /*
235891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
301846Scraigm #pragma ident "%Z%%M% %I% %E% SMI"
311846Scraigm
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * Ptr args aren't checked for NULL because the program would be a
350Sstevel@tonic-gate * catastrophic mess anyway. Better to abort than just to return NULL.
360Sstevel@tonic-gate */
37*6812Sraf #include "lint.h"
380Sstevel@tonic-gate #include "file64.h"
390Sstevel@tonic-gate #include "mtlib.h"
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate #include <thread.h>
430Sstevel@tonic-gate #include <synch.h>
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <limits.h>
470Sstevel@tonic-gate #include "stdiom.h"
480Sstevel@tonic-gate #include "mse.h"
490Sstevel@tonic-gate
500Sstevel@tonic-gate int
fputs(const char * ptr,FILE * iop)510Sstevel@tonic-gate fputs(const char *ptr, FILE *iop)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate ssize_t ndone = 0L, n;
540Sstevel@tonic-gate unsigned char *cptr, *bufend;
550Sstevel@tonic-gate rmutex_t *lk;
560Sstevel@tonic-gate size_t ptrlen;
570Sstevel@tonic-gate size_t len = 0;
580Sstevel@tonic-gate int c;
590Sstevel@tonic-gate
600Sstevel@tonic-gate FLOCKFILE(lk, iop);
610Sstevel@tonic-gate
620Sstevel@tonic-gate _SET_ORIENTATION_BYTE(iop);
630Sstevel@tonic-gate
640Sstevel@tonic-gate if (_WRTCHK(iop)) {
650Sstevel@tonic-gate FUNLOCKFILE(lk);
660Sstevel@tonic-gate return (EOF);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate bufend = _bufend(iop);
690Sstevel@tonic-gate
700Sstevel@tonic-gate ptrlen = strlen(ptr);
710Sstevel@tonic-gate if ((iop->_flag & _IONBF) == 0) {
720Sstevel@tonic-gate for (; ; ptr += len, ptrlen -= len) {
730Sstevel@tonic-gate while ((n = bufend - (cptr = iop->_ptr)) <= 0) {
740Sstevel@tonic-gate /* full buf */
750Sstevel@tonic-gate if (_xflsbuf(iop) == EOF) {
760Sstevel@tonic-gate FUNLOCKFILE(lk);
770Sstevel@tonic-gate return (EOF);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate }
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * n: number of available bytes in the buffer of 'iop'
820Sstevel@tonic-gate * ptrlen: number of remaining bytes in 'ptr' string
830Sstevel@tonic-gate *
840Sstevel@tonic-gate * If all remaining bytes in 'ptr' can be copied into
850Sstevel@tonic-gate * the buffer of 'iop' (ptrlen <= n), 'len' is set to
860Sstevel@tonic-gate * 'ptrlen'. Otherwise, 'len' is set to 'n'.
870Sstevel@tonic-gate * Then, copies 'len' bytes from 'ptr' to the buffer
880Sstevel@tonic-gate * of 'iop'.
890Sstevel@tonic-gate */
900Sstevel@tonic-gate len = (c = (ptrlen <= n)) ? ptrlen : n;
910Sstevel@tonic-gate (void) memcpy(cptr, ptr, len);
920Sstevel@tonic-gate iop->_cnt -= len;
930Sstevel@tonic-gate iop->_ptr += len;
940Sstevel@tonic-gate if (_needsync(iop, bufend))
950Sstevel@tonic-gate _bufsync(iop, bufend);
960Sstevel@tonic-gate ndone += len;
970Sstevel@tonic-gate if (c) {
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * All bytes in 'ptr' have been copied into
1000Sstevel@tonic-gate * the buffer of 'iop'.
1010Sstevel@tonic-gate * Flush buffer if line-buffered
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate if (iop->_flag & _IOLBF)
1040Sstevel@tonic-gate if (_xflsbuf(iop) == EOF) {
1050Sstevel@tonic-gate FUNLOCKFILE(lk);
1060Sstevel@tonic-gate return (EOF);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate FUNLOCKFILE(lk);
1090Sstevel@tonic-gate if (ndone <= INT_MAX)
1100Sstevel@tonic-gate return ((int)ndone);
1110Sstevel@tonic-gate else
1120Sstevel@tonic-gate return (EOF);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate else
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate /* write out to an unbuffered file */
1190Sstevel@tonic-gate ssize_t num_wrote;
1200Sstevel@tonic-gate ssize_t count = (ssize_t)ptrlen;
1211846Scraigm int fd = GET_FD(iop);
1220Sstevel@tonic-gate
1231846Scraigm while ((num_wrote = write(fd, ptr, (size_t)count)) != count) {
1245891Sraf if (num_wrote <= 0) {
1255891Sraf if (!cancel_active())
1260Sstevel@tonic-gate iop->_flag |= _IOERR;
1275891Sraf FUNLOCKFILE(lk);
1285891Sraf return (EOF);
1295891Sraf }
1305891Sraf count -= num_wrote;
1315891Sraf ptr += num_wrote;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate FUNLOCKFILE(lk);
1340Sstevel@tonic-gate if (ptrlen <= INT_MAX)
1350Sstevel@tonic-gate return ((int)ptrlen);
1360Sstevel@tonic-gate else
1370Sstevel@tonic-gate return (EOF);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate }
140