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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <sys/types.h>
46 _fwrite_unlocked(const void *ptr
, size_t size
, size_t count
, FILE *iop
);
49 fwrite(const void *ptr
, size_t size
, size_t count
, FILE *iop
)
56 _SET_ORIENTATION_BYTE(iop
);
58 retval
= _fwrite_unlocked(ptr
, size
, count
, iop
);
65 _fwrite_unlocked(const void *ptr
, size_t size
, size_t count
, FILE *iop
)
68 unsigned char *dptr
= (unsigned char *)ptr
;
69 unsigned char *bufend
;
75 * This test is here to avoid the expensive multiply
84 if (iop
->_flag
& _IOLBF
) {
85 bufend
= _bufend(iop
);
86 iop
->_cnt
= iop
->_base
- iop
->_ptr
;
88 ssize_t buflen
= bufend
- iop
->_base
;
89 if (--iop
->_cnt
>= (-buflen
) && *dptr
!= '\n')
90 *iop
->_ptr
++ = *dptr
++;
91 else if (__flsbuf(*dptr
++, iop
) == EOF
)
95 } else if (iop
->_flag
& _IONBF
) {
100 if (size
< 1 || count
< 1)
103 if (iop
->_base
!= iop
->_ptr
) {
105 * Flush any existing data. Doesn't count towards return
108 bytes
= iop
->_ptr
- iop
->_base
;
109 data
= (char *)iop
->_base
;
111 while ((n
= write(fileno(iop
), data
, (size_t)bytes
))
114 if (!cancel_active())
115 iop
->_flag
|= _IOERR
;
123 iop
->_ptr
= iop
->_base
;
126 * written is in bytes until the return.
127 * Then it is divided by size to produce items.
129 while ((n
= write(fileno(iop
), dptr
, s
)) != s
) {
131 if (!cancel_active())
132 iop
->_flag
|= _IOERR
;
133 return (written
/ size
);
141 return (written
/ size
);
142 } else while (s
> 0) {
145 (void) memcpy(iop
->_ptr
, (void *)dptr
,
148 iop
->_ptr
+= iop
->_cnt
;
151 if (_xflsbuf(iop
) == EOF
)
154 if (iop
->_cnt
>= s
) {
155 char *tmp
= (char *)iop
->_ptr
;
185 (void) memcpy(iop
->_ptr
, (void *)dptr
, s
);
194 return (size
!= 0 ? count
- ((s
+ size
- 1) / size
) : 0);