1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
29
30 #include "lint.h"
31 #include "file64.h"
32 #include "mtlib.h"
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <memory.h>
36 #include <thread.h>
37 #include <synch.h>
38 #include <limits.h>
39 #include "stdiom.h"
40 #include "mse.h"
41
42 int
puts(const char * ptr)43 puts(const char *ptr)
44 {
45 ssize_t ndone = 0L, n;
46 unsigned char *cptr, *bufend;
47 rmutex_t *lk;
48 size_t ptrlen;
49 size_t len = 0;
50 int c;
51
52 FLOCKFILE(lk, stdout);
53
54 _SET_ORIENTATION_BYTE(stdout);
55
56 if (_WRTCHK(stdout)) {
57 FUNLOCKFILE(lk);
58 return (EOF);
59 }
60
61 bufend = _bufend(stdout);
62
63 ptrlen = strlen(ptr) + 1; /* adding 1 for '\n' */
64 for (; ; ptr += len, ptrlen -= len) {
65 while ((n = bufend - (cptr = stdout->_ptr)) <= 0) /* full buf */
66 {
67 if (_xflsbuf(stdout) == EOF) {
68 FUNLOCKFILE(lk);
69 return (EOF);
70 }
71 }
72 /*
73 * n: number of available bytes in the buffer of stdout
74 * ptrlen: number of remaining bytes in 'ptr' string
75 *
76 * If all remaining bytes in 'ptr' can be copied into
77 * the buffer of stdout (ptrlen <= n), 'len' is set to
78 * 'ptrlen'. Otherwise, 'len' is set to 'n'.
79 * Then, copies 'len' bytes from 'ptr' to the buffer
80 * of stdout.
81 */
82 len = (c = (ptrlen <= n)) ? ptrlen : n;
83 (void) memcpy(cptr, ptr, len);
84 stdout->_cnt -= len;
85 stdout->_ptr += len;
86 if (_needsync(stdout, bufend))
87 _bufsync(stdout, bufend);
88 ndone += len;
89 if (c) {
90 /*
91 * All bytes in 'ptr' can be copied into
92 * the buffer of stdout.
93 * Terminate the buffer of stdout with '\n'
94 * and flush line buffer
95 */
96 stdout->_ptr[-1] = '\n';
97 if (stdout->_flag & (_IONBF | _IOLBF)) {
98 /* flush line */
99 if (_xflsbuf(stdout) == EOF) {
100 FUNLOCKFILE(lk);
101 return (EOF);
102 }
103 }
104 FUNLOCKFILE(lk);
105 if (ndone <= INT_MAX)
106 return ((int)ndone);
107 else
108 return (EOF);
109 }
110 }
111 }
112