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
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * 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 */
21*6812Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * 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
30*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include "file64.h"
340Sstevel@tonic-gate #include "mtlib.h"
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <memory.h>
380Sstevel@tonic-gate #include <thread.h>
390Sstevel@tonic-gate #include <synch.h>
400Sstevel@tonic-gate #include <limits.h>
410Sstevel@tonic-gate #include "stdiom.h"
420Sstevel@tonic-gate #include "mse.h"
430Sstevel@tonic-gate
440Sstevel@tonic-gate int
puts(const char * ptr)450Sstevel@tonic-gate puts(const char *ptr)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate ssize_t ndone = 0L, n;
480Sstevel@tonic-gate unsigned char *cptr, *bufend;
490Sstevel@tonic-gate rmutex_t *lk;
500Sstevel@tonic-gate size_t ptrlen;
510Sstevel@tonic-gate size_t len = 0;
520Sstevel@tonic-gate int c;
530Sstevel@tonic-gate
540Sstevel@tonic-gate FLOCKFILE(lk, stdout);
550Sstevel@tonic-gate
560Sstevel@tonic-gate _SET_ORIENTATION_BYTE(stdout);
570Sstevel@tonic-gate
580Sstevel@tonic-gate if (_WRTCHK(stdout)) {
590Sstevel@tonic-gate FUNLOCKFILE(lk);
600Sstevel@tonic-gate return (EOF);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate bufend = _bufend(stdout);
640Sstevel@tonic-gate
650Sstevel@tonic-gate ptrlen = strlen(ptr) + 1; /* adding 1 for '\n' */
660Sstevel@tonic-gate for (; ; ptr += len, ptrlen -= len) {
670Sstevel@tonic-gate while ((n = bufend - (cptr = stdout->_ptr)) <= 0) /* full buf */
680Sstevel@tonic-gate {
690Sstevel@tonic-gate if (_xflsbuf(stdout) == EOF) {
700Sstevel@tonic-gate FUNLOCKFILE(lk);
710Sstevel@tonic-gate return (EOF);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate }
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * n: number of available bytes in the buffer of stdout
760Sstevel@tonic-gate * ptrlen: number of remaining bytes in 'ptr' string
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * If all remaining bytes in 'ptr' can be copied into
790Sstevel@tonic-gate * the buffer of stdout (ptrlen <= n), 'len' is set to
800Sstevel@tonic-gate * 'ptrlen'. Otherwise, 'len' is set to 'n'.
810Sstevel@tonic-gate * Then, copies 'len' bytes from 'ptr' to the buffer
820Sstevel@tonic-gate * of stdout.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate len = (c = (ptrlen <= n)) ? ptrlen : n;
850Sstevel@tonic-gate (void) memcpy(cptr, ptr, len);
860Sstevel@tonic-gate stdout->_cnt -= len;
870Sstevel@tonic-gate stdout->_ptr += len;
880Sstevel@tonic-gate if (_needsync(stdout, bufend))
890Sstevel@tonic-gate _bufsync(stdout, bufend);
900Sstevel@tonic-gate ndone += len;
910Sstevel@tonic-gate if (c) {
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate * All bytes in 'ptr' can be copied into
940Sstevel@tonic-gate * the buffer of stdout.
950Sstevel@tonic-gate * Terminate the buffer of stdout with '\n'
960Sstevel@tonic-gate * and flush line buffer
970Sstevel@tonic-gate */
980Sstevel@tonic-gate stdout->_ptr[-1] = '\n';
990Sstevel@tonic-gate if (stdout->_flag & (_IONBF | _IOLBF)) {
1000Sstevel@tonic-gate /* flush line */
1010Sstevel@tonic-gate if (_xflsbuf(stdout) == EOF) {
1020Sstevel@tonic-gate FUNLOCKFILE(lk);
1030Sstevel@tonic-gate return (EOF);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate FUNLOCKFILE(lk);
1070Sstevel@tonic-gate if (ndone <= INT_MAX)
1080Sstevel@tonic-gate return ((int)ndone);
1090Sstevel@tonic-gate else
1100Sstevel@tonic-gate return (EOF);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate }
114