xref: /illumos-gate/usr/src/lib/libc/port/stdio/fwrite.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
30*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
34*7c478bd9Sstevel@tonic-gate #include "file64.h"
35*7c478bd9Sstevel@tonic-gate #include "mtlib.h"
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <values.h>
39*7c478bd9Sstevel@tonic-gate #include <memory.h>
40*7c478bd9Sstevel@tonic-gate #include <thread.h>
41*7c478bd9Sstevel@tonic-gate #include <synch.h>
42*7c478bd9Sstevel@tonic-gate #include <unistd.h>
43*7c478bd9Sstevel@tonic-gate #include "stdiom.h"
44*7c478bd9Sstevel@tonic-gate #include "mse.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate size_t
47*7c478bd9Sstevel@tonic-gate _fwrite_unlocked(const void *ptr, size_t size, size_t count, FILE *iop);
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate size_t
50*7c478bd9Sstevel@tonic-gate fwrite(const void *ptr, size_t size, size_t count, FILE *iop)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	rmutex_t	*lk;
53*7c478bd9Sstevel@tonic-gate 	size_t	retval;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	_SET_ORIENTATION_BYTE(iop);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	retval = _fwrite_unlocked(ptr, size, count, iop);
60*7c478bd9Sstevel@tonic-gate 	FUNLOCKFILE(lk);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	return (retval);
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate size_t
66*7c478bd9Sstevel@tonic-gate _fwrite_unlocked(const void *ptr, size_t size, size_t count, FILE *iop)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	ssize_t s, n;
69*7c478bd9Sstevel@tonic-gate 	unsigned char *dptr = (unsigned char *)ptr;
70*7c478bd9Sstevel@tonic-gate 	unsigned char *bufend;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	if (_WRTCHK(iop))
73*7c478bd9Sstevel@tonic-gate 		return (0);
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	/*
76*7c478bd9Sstevel@tonic-gate 	 * This test is here to avoid the expensive multiply
77*7c478bd9Sstevel@tonic-gate 	 */
78*7c478bd9Sstevel@tonic-gate 	if (count == 1)
79*7c478bd9Sstevel@tonic-gate 		s = size;
80*7c478bd9Sstevel@tonic-gate 	else if (size == 1)
81*7c478bd9Sstevel@tonic-gate 		s = count;
82*7c478bd9Sstevel@tonic-gate 	else
83*7c478bd9Sstevel@tonic-gate 		s = size * count;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (iop->_flag & _IOLBF) {
86*7c478bd9Sstevel@tonic-gate 		bufend = _bufend(iop);
87*7c478bd9Sstevel@tonic-gate 		iop->_cnt = iop->_base - iop->_ptr;
88*7c478bd9Sstevel@tonic-gate 		while (s > 0) {
89*7c478bd9Sstevel@tonic-gate 			ssize_t buflen = bufend - iop->_base;
90*7c478bd9Sstevel@tonic-gate 			if (--iop->_cnt >= (-buflen) && *dptr != '\n')
91*7c478bd9Sstevel@tonic-gate 				*iop->_ptr++ = *dptr++;
92*7c478bd9Sstevel@tonic-gate 			else if (__flsbuf(*dptr++, iop) == EOF)
93*7c478bd9Sstevel@tonic-gate 				break;
94*7c478bd9Sstevel@tonic-gate 			s--;
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 	} else if (iop->_flag & _IONBF) {
97*7c478bd9Sstevel@tonic-gate 		ssize_t bytes;
98*7c478bd9Sstevel@tonic-gate 		ssize_t written = 0;
99*7c478bd9Sstevel@tonic-gate 		char *data;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		if (size < 1 || count < 1)
102*7c478bd9Sstevel@tonic-gate 			return (0);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 		if (iop->_base != iop->_ptr) {
105*7c478bd9Sstevel@tonic-gate 		/*
106*7c478bd9Sstevel@tonic-gate 		 * Flush any existing data. Doesn't count towards return
107*7c478bd9Sstevel@tonic-gate 		 * value.
108*7c478bd9Sstevel@tonic-gate 		 */
109*7c478bd9Sstevel@tonic-gate 			bytes = iop->_ptr - iop->_base;
110*7c478bd9Sstevel@tonic-gate 			data = (char *)iop->_base;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 			while ((n = write(fileno(iop), data, (size_t)bytes))
113*7c478bd9Sstevel@tonic-gate 			    != bytes) {
114*7c478bd9Sstevel@tonic-gate 				if (n == -1) {
115*7c478bd9Sstevel@tonic-gate 					iop->_flag |= _IOERR;
116*7c478bd9Sstevel@tonic-gate 					return (0);
117*7c478bd9Sstevel@tonic-gate 				} else {
118*7c478bd9Sstevel@tonic-gate 					data += n;
119*7c478bd9Sstevel@tonic-gate 					bytes -= n;
120*7c478bd9Sstevel@tonic-gate 				}
121*7c478bd9Sstevel@tonic-gate 			}
122*7c478bd9Sstevel@tonic-gate 			iop->_cnt = 0;
123*7c478bd9Sstevel@tonic-gate 			iop->_ptr = iop->_base;
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 		/*
126*7c478bd9Sstevel@tonic-gate 		 * written is in bytes until the return.
127*7c478bd9Sstevel@tonic-gate 		 * Then it is divided by size to produce items.
128*7c478bd9Sstevel@tonic-gate 		 */
129*7c478bd9Sstevel@tonic-gate 		while ((n = write(fileno(iop), dptr, s)) != s) {
130*7c478bd9Sstevel@tonic-gate 			if (n == -1) {
131*7c478bd9Sstevel@tonic-gate 				iop->_flag |= _IOERR;
132*7c478bd9Sstevel@tonic-gate 				return (written / size);
133*7c478bd9Sstevel@tonic-gate 			} else {
134*7c478bd9Sstevel@tonic-gate 				dptr += n;
135*7c478bd9Sstevel@tonic-gate 				s -= n;
136*7c478bd9Sstevel@tonic-gate 				written += n;
137*7c478bd9Sstevel@tonic-gate 			}
138*7c478bd9Sstevel@tonic-gate 		}
139*7c478bd9Sstevel@tonic-gate 		written += n;
140*7c478bd9Sstevel@tonic-gate 		return (written / size);
141*7c478bd9Sstevel@tonic-gate 	} else while (s > 0) {
142*7c478bd9Sstevel@tonic-gate 		if (iop->_cnt < s) {
143*7c478bd9Sstevel@tonic-gate 			if (iop->_cnt > 0) {
144*7c478bd9Sstevel@tonic-gate 				(void) memcpy(iop->_ptr, (void *)dptr,
145*7c478bd9Sstevel@tonic-gate 				    iop->_cnt);
146*7c478bd9Sstevel@tonic-gate 				dptr += iop->_cnt;
147*7c478bd9Sstevel@tonic-gate 				iop->_ptr += iop->_cnt;
148*7c478bd9Sstevel@tonic-gate 				s -= iop->_cnt;
149*7c478bd9Sstevel@tonic-gate 			}
150*7c478bd9Sstevel@tonic-gate 			if (_xflsbuf(iop) == EOF)
151*7c478bd9Sstevel@tonic-gate 				break;
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 		if (iop->_cnt >= s) {
154*7c478bd9Sstevel@tonic-gate 			char *tmp = (char *)iop->_ptr;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 			switch (s) {
157*7c478bd9Sstevel@tonic-gate 			case 8:
158*7c478bd9Sstevel@tonic-gate 				*tmp++ = *dptr++;
159*7c478bd9Sstevel@tonic-gate 				/*FALLTHRU*/
160*7c478bd9Sstevel@tonic-gate 			case 7:
161*7c478bd9Sstevel@tonic-gate 				*tmp++ = *dptr++;
162*7c478bd9Sstevel@tonic-gate 				/*FALLTHRU*/
163*7c478bd9Sstevel@tonic-gate 			case 6:
164*7c478bd9Sstevel@tonic-gate 				*tmp++ = *dptr++;
165*7c478bd9Sstevel@tonic-gate 				/*FALLTHRU*/
166*7c478bd9Sstevel@tonic-gate 			case 5:
167*7c478bd9Sstevel@tonic-gate 				*tmp++ = *dptr++;
168*7c478bd9Sstevel@tonic-gate 				/*FALLTHRU*/
169*7c478bd9Sstevel@tonic-gate 			case 4:
170*7c478bd9Sstevel@tonic-gate 				*tmp++ = *dptr++;
171*7c478bd9Sstevel@tonic-gate 				/*FALLTHRU*/
172*7c478bd9Sstevel@tonic-gate 			case 3:
173*7c478bd9Sstevel@tonic-gate 				*tmp++ = *dptr++;
174*7c478bd9Sstevel@tonic-gate 				/*FALLTHRU*/
175*7c478bd9Sstevel@tonic-gate 			case 2:
176*7c478bd9Sstevel@tonic-gate 				*tmp++ = *dptr++;
177*7c478bd9Sstevel@tonic-gate 				/*FALLTHRU*/
178*7c478bd9Sstevel@tonic-gate 			case 1:
179*7c478bd9Sstevel@tonic-gate 				*tmp++ = *dptr++;
180*7c478bd9Sstevel@tonic-gate 				break;
181*7c478bd9Sstevel@tonic-gate 			case 0:
182*7c478bd9Sstevel@tonic-gate 				return (count);
183*7c478bd9Sstevel@tonic-gate 			default:
184*7c478bd9Sstevel@tonic-gate 				(void) memcpy(iop->_ptr, (void *)dptr, s);
185*7c478bd9Sstevel@tonic-gate 			}
186*7c478bd9Sstevel@tonic-gate 			iop->_ptr += s;
187*7c478bd9Sstevel@tonic-gate 			iop->_cnt -= s;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 			return (count);
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
194*7c478bd9Sstevel@tonic-gate }
195