xref: /titanic_44/usr/src/lib/libc/port/stdio/fputs.c (revision 209e49b2ff611e7d61ff58e13756ae67f51be550)
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 
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 "lint.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 		int fd = GET_FD(iop);
122 
123 		while ((num_wrote = write(fd, ptr, (size_t)count)) != count) {
124 			if (num_wrote <= 0) {
125 				if (!cancel_active())
126 					iop->_flag |= _IOERR;
127 				FUNLOCKFILE(lk);
128 				return (EOF);
129 			}
130 			count -= num_wrote;
131 			ptr += num_wrote;
132 		}
133 		FUNLOCKFILE(lk);
134 		if (ptrlen <= INT_MAX)
135 			return ((int)ptrlen);
136 		else
137 			return (EOF);
138 	}
139 }
140