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