xref: /onnv-gate/usr/src/lib/libc/port/stdio/fwrite.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55891Sraf  * Common Development and Distribution License (the "License").
65891Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215891Sraf 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include "file64.h"
340Sstevel@tonic-gate #include "mtlib.h"
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <values.h>
380Sstevel@tonic-gate #include <memory.h>
390Sstevel@tonic-gate #include <thread.h>
400Sstevel@tonic-gate #include <synch.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include "stdiom.h"
430Sstevel@tonic-gate #include "mse.h"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate size_t
460Sstevel@tonic-gate _fwrite_unlocked(const void *ptr, size_t size, size_t count, FILE *iop);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate size_t
fwrite(const void * ptr,size_t size,size_t count,FILE * iop)490Sstevel@tonic-gate fwrite(const void *ptr, size_t size, size_t count, FILE *iop)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	rmutex_t	*lk;
520Sstevel@tonic-gate 	size_t	retval;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	_SET_ORIENTATION_BYTE(iop);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	retval = _fwrite_unlocked(ptr, size, count, iop);
590Sstevel@tonic-gate 	FUNLOCKFILE(lk);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	return (retval);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate 
640Sstevel@tonic-gate size_t
_fwrite_unlocked(const void * ptr,size_t size,size_t count,FILE * iop)650Sstevel@tonic-gate _fwrite_unlocked(const void *ptr, size_t size, size_t count, FILE *iop)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	ssize_t s, n;
680Sstevel@tonic-gate 	unsigned char *dptr = (unsigned char *)ptr;
690Sstevel@tonic-gate 	unsigned char *bufend;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (_WRTCHK(iop))
720Sstevel@tonic-gate 		return (0);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	/*
750Sstevel@tonic-gate 	 * This test is here to avoid the expensive multiply
760Sstevel@tonic-gate 	 */
770Sstevel@tonic-gate 	if (count == 1)
780Sstevel@tonic-gate 		s = size;
790Sstevel@tonic-gate 	else if (size == 1)
800Sstevel@tonic-gate 		s = count;
810Sstevel@tonic-gate 	else
820Sstevel@tonic-gate 		s = size * count;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if (iop->_flag & _IOLBF) {
850Sstevel@tonic-gate 		bufend = _bufend(iop);
860Sstevel@tonic-gate 		iop->_cnt = iop->_base - iop->_ptr;
870Sstevel@tonic-gate 		while (s > 0) {
880Sstevel@tonic-gate 			ssize_t buflen = bufend - iop->_base;
890Sstevel@tonic-gate 			if (--iop->_cnt >= (-buflen) && *dptr != '\n')
900Sstevel@tonic-gate 				*iop->_ptr++ = *dptr++;
910Sstevel@tonic-gate 			else if (__flsbuf(*dptr++, iop) == EOF)
920Sstevel@tonic-gate 				break;
930Sstevel@tonic-gate 			s--;
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 	} else if (iop->_flag & _IONBF) {
960Sstevel@tonic-gate 		ssize_t bytes;
970Sstevel@tonic-gate 		ssize_t written = 0;
980Sstevel@tonic-gate 		char *data;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 		if (size < 1 || count < 1)
1010Sstevel@tonic-gate 			return (0);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 		if (iop->_base != iop->_ptr) {
1040Sstevel@tonic-gate 		/*
1050Sstevel@tonic-gate 		 * Flush any existing data. Doesn't count towards return
1060Sstevel@tonic-gate 		 * value.
1070Sstevel@tonic-gate 		 */
1080Sstevel@tonic-gate 			bytes = iop->_ptr - iop->_base;
1090Sstevel@tonic-gate 			data = (char *)iop->_base;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 			while ((n = write(fileno(iop), data, (size_t)bytes))
1120Sstevel@tonic-gate 			    != bytes) {
1130Sstevel@tonic-gate 				if (n == -1) {
1145891Sraf 					if (!cancel_active())
1155891Sraf 						iop->_flag |= _IOERR;
1160Sstevel@tonic-gate 					return (0);
1170Sstevel@tonic-gate 				} else {
1180Sstevel@tonic-gate 					data += n;
1190Sstevel@tonic-gate 					bytes -= n;
1200Sstevel@tonic-gate 				}
1210Sstevel@tonic-gate 			}
1220Sstevel@tonic-gate 			iop->_cnt = 0;
1230Sstevel@tonic-gate 			iop->_ptr = iop->_base;
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 		/*
1260Sstevel@tonic-gate 		 * written is in bytes until the return.
1270Sstevel@tonic-gate 		 * Then it is divided by size to produce items.
1280Sstevel@tonic-gate 		 */
1290Sstevel@tonic-gate 		while ((n = write(fileno(iop), dptr, s)) != s) {
1300Sstevel@tonic-gate 			if (n == -1) {
1315891Sraf 				if (!cancel_active())
1325891Sraf 					iop->_flag |= _IOERR;
1330Sstevel@tonic-gate 				return (written / size);
1340Sstevel@tonic-gate 			} else {
1350Sstevel@tonic-gate 				dptr += n;
1360Sstevel@tonic-gate 				s -= n;
1370Sstevel@tonic-gate 				written += n;
1380Sstevel@tonic-gate 			}
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 		written += n;
1410Sstevel@tonic-gate 		return (written / size);
1420Sstevel@tonic-gate 	} else while (s > 0) {
1430Sstevel@tonic-gate 		if (iop->_cnt < s) {
1440Sstevel@tonic-gate 			if (iop->_cnt > 0) {
1450Sstevel@tonic-gate 				(void) memcpy(iop->_ptr, (void *)dptr,
1460Sstevel@tonic-gate 				    iop->_cnt);
1470Sstevel@tonic-gate 				dptr += iop->_cnt;
1480Sstevel@tonic-gate 				iop->_ptr += iop->_cnt;
1490Sstevel@tonic-gate 				s -= iop->_cnt;
1500Sstevel@tonic-gate 			}
1510Sstevel@tonic-gate 			if (_xflsbuf(iop) == EOF)
1520Sstevel@tonic-gate 				break;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 		if (iop->_cnt >= s) {
1550Sstevel@tonic-gate 			char *tmp = (char *)iop->_ptr;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 			switch (s) {
1580Sstevel@tonic-gate 			case 8:
1590Sstevel@tonic-gate 				*tmp++ = *dptr++;
1600Sstevel@tonic-gate 				/*FALLTHRU*/
1610Sstevel@tonic-gate 			case 7:
1620Sstevel@tonic-gate 				*tmp++ = *dptr++;
1630Sstevel@tonic-gate 				/*FALLTHRU*/
1640Sstevel@tonic-gate 			case 6:
1650Sstevel@tonic-gate 				*tmp++ = *dptr++;
1660Sstevel@tonic-gate 				/*FALLTHRU*/
1670Sstevel@tonic-gate 			case 5:
1680Sstevel@tonic-gate 				*tmp++ = *dptr++;
1690Sstevel@tonic-gate 				/*FALLTHRU*/
1700Sstevel@tonic-gate 			case 4:
1710Sstevel@tonic-gate 				*tmp++ = *dptr++;
1720Sstevel@tonic-gate 				/*FALLTHRU*/
1730Sstevel@tonic-gate 			case 3:
1740Sstevel@tonic-gate 				*tmp++ = *dptr++;
1750Sstevel@tonic-gate 				/*FALLTHRU*/
1760Sstevel@tonic-gate 			case 2:
1770Sstevel@tonic-gate 				*tmp++ = *dptr++;
1780Sstevel@tonic-gate 				/*FALLTHRU*/
1790Sstevel@tonic-gate 			case 1:
1800Sstevel@tonic-gate 				*tmp++ = *dptr++;
1810Sstevel@tonic-gate 				break;
1820Sstevel@tonic-gate 			case 0:
1830Sstevel@tonic-gate 				return (count);
1840Sstevel@tonic-gate 			default:
1850Sstevel@tonic-gate 				(void) memcpy(iop->_ptr, (void *)dptr, s);
1860Sstevel@tonic-gate 			}
1870Sstevel@tonic-gate 			iop->_ptr += s;
1880Sstevel@tonic-gate 			iop->_cnt -= s;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 			return (count);
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	return (size != 0 ? count - ((s + size - 1) / size) : 0);
1950Sstevel@tonic-gate }
196