xref: /titanic_50/usr/src/lib/libc/port/stdio/puts.c (revision a24e89c4a1eec8361718d94a6275e6720643284e)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include "lint.h"
33 #include "file64.h"
34 #include "mtlib.h"
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <memory.h>
38 #include <thread.h>
39 #include <synch.h>
40 #include <limits.h>
41 #include "stdiom.h"
42 #include "mse.h"
43 
44 int
45 puts(const char *ptr)
46 {
47 	ssize_t ndone = 0L, n;
48 	unsigned char *cptr, *bufend;
49 	rmutex_t *lk;
50 	size_t	ptrlen;
51 	size_t	len = 0;
52 	int	c;
53 
54 	FLOCKFILE(lk, stdout);
55 
56 	_SET_ORIENTATION_BYTE(stdout);
57 
58 	if (_WRTCHK(stdout)) {
59 		FUNLOCKFILE(lk);
60 		return (EOF);
61 	}
62 
63 	bufend = _bufend(stdout);
64 
65 	ptrlen = strlen(ptr) + 1;	/* adding 1 for '\n' */
66 	for (; ; ptr += len, ptrlen -= len) {
67 		while ((n = bufend - (cptr = stdout->_ptr)) <= 0) /* full buf */
68 		{
69 			if (_xflsbuf(stdout) == EOF) {
70 				FUNLOCKFILE(lk);
71 				return (EOF);
72 			}
73 		}
74 		/*
75 		 * n: number of available bytes in the buffer of stdout
76 		 * ptrlen: number of remaining bytes in 'ptr' string
77 		 *
78 		 * If all remaining bytes in 'ptr' can be copied into
79 		 * the buffer of stdout (ptrlen <= n), 'len' is set to
80 		 * 'ptrlen'.  Otherwise, 'len' is set to 'n'.
81 		 * Then, copies 'len' bytes from 'ptr' to the buffer
82 		 * of stdout.
83 		 */
84 		len = (c = (ptrlen <= n)) ? ptrlen : n;
85 		(void) memcpy(cptr, ptr, len);
86 		stdout->_cnt -= len;
87 		stdout->_ptr += len;
88 		if (_needsync(stdout, bufend))
89 			_bufsync(stdout, bufend);
90 		ndone += len;
91 		if (c) {
92 			/*
93 			 * All bytes in 'ptr' can be copied into
94 			 * the buffer of stdout.
95 			 * Terminate the buffer of stdout with '\n'
96 			 * and flush line buffer
97 			 */
98 			stdout->_ptr[-1] = '\n';
99 			if (stdout->_flag & (_IONBF | _IOLBF)) {
100 				/* flush line */
101 				if (_xflsbuf(stdout) == EOF) {
102 					FUNLOCKFILE(lk);
103 					return (EOF);
104 				}
105 			}
106 			FUNLOCKFILE(lk);
107 			if (ndone <= INT_MAX)
108 				return ((int)ndone);
109 			else
110 				return (EOF);
111 		}
112 	}
113 }
114