xref: /titanic_44/usr/src/lib/libc/port/stdio/fwrite.c (revision a6e6969cf9cfe2070eae4cd6071f76b0fa4f539f)
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 #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 					if (!cancel_active())
116 						iop->_flag |= _IOERR;
117 					return (0);
118 				} else {
119 					data += n;
120 					bytes -= n;
121 				}
122 			}
123 			iop->_cnt = 0;
124 			iop->_ptr = iop->_base;
125 		}
126 		/*
127 		 * written is in bytes until the return.
128 		 * Then it is divided by size to produce items.
129 		 */
130 		while ((n = write(fileno(iop), dptr, s)) != s) {
131 			if (n == -1) {
132 				if (!cancel_active())
133 					iop->_flag |= _IOERR;
134 				return (written / size);
135 			} else {
136 				dptr += n;
137 				s -= n;
138 				written += n;
139 			}
140 		}
141 		written += n;
142 		return (written / size);
143 	} else while (s > 0) {
144 		if (iop->_cnt < s) {
145 			if (iop->_cnt > 0) {
146 				(void) memcpy(iop->_ptr, (void *)dptr,
147 				    iop->_cnt);
148 				dptr += iop->_cnt;
149 				iop->_ptr += iop->_cnt;
150 				s -= iop->_cnt;
151 			}
152 			if (_xflsbuf(iop) == EOF)
153 				break;
154 		}
155 		if (iop->_cnt >= s) {
156 			char *tmp = (char *)iop->_ptr;
157 
158 			switch (s) {
159 			case 8:
160 				*tmp++ = *dptr++;
161 				/*FALLTHRU*/
162 			case 7:
163 				*tmp++ = *dptr++;
164 				/*FALLTHRU*/
165 			case 6:
166 				*tmp++ = *dptr++;
167 				/*FALLTHRU*/
168 			case 5:
169 				*tmp++ = *dptr++;
170 				/*FALLTHRU*/
171 			case 4:
172 				*tmp++ = *dptr++;
173 				/*FALLTHRU*/
174 			case 3:
175 				*tmp++ = *dptr++;
176 				/*FALLTHRU*/
177 			case 2:
178 				*tmp++ = *dptr++;
179 				/*FALLTHRU*/
180 			case 1:
181 				*tmp++ = *dptr++;
182 				break;
183 			case 0:
184 				return (count);
185 			default:
186 				(void) memcpy(iop->_ptr, (void *)dptr, s);
187 			}
188 			iop->_ptr += s;
189 			iop->_cnt -= s;
190 
191 			return (count);
192 		}
193 	}
194 
195 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
196 }
197