xref: /titanic_51/usr/src/lib/libc/port/stdio/fputs.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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 /*
34  * Ptr args aren't checked for NULL because the program would be a
35  * catastrophic mess anyway.  Better to abort than just to return NULL.
36  */
37 #include "synonyms.h"
38 #include "file64.h"
39 #include "mtlib.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include <thread.h>
43 #include <synch.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46 #include <limits.h>
47 #include "stdiom.h"
48 #include "mse.h"
49 
50 int
51 fputs(const char *ptr, FILE *iop)
52 {
53 	ssize_t ndone = 0L, n;
54 	unsigned char *cptr, *bufend;
55 	rmutex_t *lk;
56 	size_t	ptrlen;
57 	size_t	len = 0;
58 	int	c;
59 
60 	FLOCKFILE(lk, iop);
61 
62 	_SET_ORIENTATION_BYTE(iop);
63 
64 	if (_WRTCHK(iop)) {
65 		FUNLOCKFILE(lk);
66 		return (EOF);
67 	}
68 	bufend = _bufend(iop);
69 
70 	ptrlen = strlen(ptr);
71 	if ((iop->_flag & _IONBF) == 0) {
72 		for (; ; ptr += len, ptrlen -= len) {
73 			while ((n = bufend - (cptr = iop->_ptr)) <= 0) {
74 				/* full buf */
75 				if (_xflsbuf(iop) == EOF) {
76 					FUNLOCKFILE(lk);
77 					return (EOF);
78 				}
79 			}
80 			/*
81 			 * n: number of available bytes in the buffer of 'iop'
82 			 * ptrlen: number of remaining bytes in 'ptr' string
83 			 *
84 			 * If all remaining bytes in 'ptr' can be copied into
85 			 * the buffer of 'iop' (ptrlen <= n), 'len' is set to
86 			 * 'ptrlen'.  Otherwise, 'len' is set to 'n'.
87 			 * Then, copies 'len' bytes from 'ptr' to the buffer
88 			 * of 'iop'.
89 			 */
90 			len = (c = (ptrlen <= n)) ? ptrlen : n;
91 			(void) memcpy(cptr, ptr, len);
92 			iop->_cnt -= len;
93 			iop->_ptr += len;
94 			if (_needsync(iop, bufend))
95 				_bufsync(iop, bufend);
96 			ndone += len;
97 			if (c) {
98 				/*
99 				 * All bytes in 'ptr' have been copied into
100 				 * the buffer of 'iop'.
101 				 * Flush buffer if line-buffered
102 				 */
103 				if (iop->_flag & _IOLBF)
104 					if (_xflsbuf(iop) == EOF) {
105 						FUNLOCKFILE(lk);
106 						return (EOF);
107 					}
108 				FUNLOCKFILE(lk);
109 				if (ndone <= INT_MAX)
110 					return ((int)ndone);
111 				else
112 					return (EOF);
113 			}
114 		}
115 	}
116 	else
117 	{
118 		/* write out to an unbuffered file */
119 		ssize_t num_wrote;
120 		ssize_t count = (ssize_t)ptrlen;
121 
122 		while ((num_wrote = write(iop->_file, ptr,
123 			(size_t)count)) != count) {
124 				if (num_wrote <= 0) {
125 					iop->_flag |= _IOERR;
126 					FUNLOCKFILE(lk);
127 					return (EOF);
128 				}
129 				count -= num_wrote;
130 				ptr += num_wrote;
131 		}
132 		FUNLOCKFILE(lk);
133 		if (ptrlen <= INT_MAX)
134 			return ((int)ptrlen);
135 		else
136 			return (EOF);
137 	}
138 }
139