xref: /titanic_50/usr/src/lib/libc/port/stdio/fwrite.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 <values.h>
39 #include <memory.h>
40 #include <thread.h>
41 #include <synch.h>
42 #include <unistd.h>
43 #include "stdiom.h"
44 #include "mse.h"
45 
46 size_t
47 _fwrite_unlocked(const void *ptr, size_t size, size_t count, FILE *iop);
48 
49 size_t
50 fwrite(const void *ptr, size_t size, size_t count, FILE *iop)
51 {
52 	rmutex_t	*lk;
53 	size_t	retval;
54 
55 	FLOCKFILE(lk, iop);
56 
57 	_SET_ORIENTATION_BYTE(iop);
58 
59 	retval = _fwrite_unlocked(ptr, size, count, iop);
60 	FUNLOCKFILE(lk);
61 
62 	return (retval);
63 }
64 
65 size_t
66 _fwrite_unlocked(const void *ptr, size_t size, size_t count, FILE *iop)
67 {
68 	ssize_t s, n;
69 	unsigned char *dptr = (unsigned char *)ptr;
70 	unsigned char *bufend;
71 
72 	if (_WRTCHK(iop))
73 		return (0);
74 
75 	/*
76 	 * This test is here to avoid the expensive multiply
77 	 */
78 	if (count == 1)
79 		s = size;
80 	else if (size == 1)
81 		s = count;
82 	else
83 		s = size * count;
84 
85 	if (iop->_flag & _IOLBF) {
86 		bufend = _bufend(iop);
87 		iop->_cnt = iop->_base - iop->_ptr;
88 		while (s > 0) {
89 			ssize_t buflen = bufend - iop->_base;
90 			if (--iop->_cnt >= (-buflen) && *dptr != '\n')
91 				*iop->_ptr++ = *dptr++;
92 			else if (__flsbuf(*dptr++, iop) == EOF)
93 				break;
94 			s--;
95 		}
96 	} else if (iop->_flag & _IONBF) {
97 		ssize_t bytes;
98 		ssize_t written = 0;
99 		char *data;
100 
101 		if (size < 1 || count < 1)
102 			return (0);
103 
104 		if (iop->_base != iop->_ptr) {
105 		/*
106 		 * Flush any existing data. Doesn't count towards return
107 		 * value.
108 		 */
109 			bytes = iop->_ptr - iop->_base;
110 			data = (char *)iop->_base;
111 
112 			while ((n = write(fileno(iop), data, (size_t)bytes))
113 			    != bytes) {
114 				if (n == -1) {
115 					iop->_flag |= _IOERR;
116 					return (0);
117 				} else {
118 					data += n;
119 					bytes -= n;
120 				}
121 			}
122 			iop->_cnt = 0;
123 			iop->_ptr = iop->_base;
124 		}
125 		/*
126 		 * written is in bytes until the return.
127 		 * Then it is divided by size to produce items.
128 		 */
129 		while ((n = write(fileno(iop), dptr, s)) != s) {
130 			if (n == -1) {
131 				iop->_flag |= _IOERR;
132 				return (written / size);
133 			} else {
134 				dptr += n;
135 				s -= n;
136 				written += n;
137 			}
138 		}
139 		written += n;
140 		return (written / size);
141 	} else while (s > 0) {
142 		if (iop->_cnt < s) {
143 			if (iop->_cnt > 0) {
144 				(void) memcpy(iop->_ptr, (void *)dptr,
145 				    iop->_cnt);
146 				dptr += iop->_cnt;
147 				iop->_ptr += iop->_cnt;
148 				s -= iop->_cnt;
149 			}
150 			if (_xflsbuf(iop) == EOF)
151 				break;
152 		}
153 		if (iop->_cnt >= s) {
154 			char *tmp = (char *)iop->_ptr;
155 
156 			switch (s) {
157 			case 8:
158 				*tmp++ = *dptr++;
159 				/*FALLTHRU*/
160 			case 7:
161 				*tmp++ = *dptr++;
162 				/*FALLTHRU*/
163 			case 6:
164 				*tmp++ = *dptr++;
165 				/*FALLTHRU*/
166 			case 5:
167 				*tmp++ = *dptr++;
168 				/*FALLTHRU*/
169 			case 4:
170 				*tmp++ = *dptr++;
171 				/*FALLTHRU*/
172 			case 3:
173 				*tmp++ = *dptr++;
174 				/*FALLTHRU*/
175 			case 2:
176 				*tmp++ = *dptr++;
177 				/*FALLTHRU*/
178 			case 1:
179 				*tmp++ = *dptr++;
180 				break;
181 			case 0:
182 				return (count);
183 			default:
184 				(void) memcpy(iop->_ptr, (void *)dptr, s);
185 			}
186 			iop->_ptr += s;
187 			iop->_cnt -= s;
188 
189 			return (count);
190 		}
191 	}
192 
193 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
194 }
195