fwrite.c revision 249810
1127664Sbms/*-
2172677Smlaier * Copyright (c) 1990, 1993
3172677Smlaier *	The Regents of the University of California.  All rights reserved.
4127664Sbms *
5127664Sbms * This code is derived from software contributed to Berkeley by
6127664Sbms * Chris Torek.
7127664Sbms *
8127664Sbms * Redistribution and use in source and binary forms, with or without
9127664Sbms * modification, are permitted provided that the following conditions
10127664Sbms * are met:
11127664Sbms * 1. Redistributions of source code must retain the above copyright
12127664Sbms *    notice, this list of conditions and the following disclaimer.
13127664Sbms * 2. Redistributions in binary form must reproduce the above copyright
14127664Sbms *    notice, this list of conditions and the following disclaimer in the
15172677Smlaier *    documentation and/or other materials provided with the distribution.
16172677Smlaier * 3. Neither the name of the University nor the names of its contributors
17172677Smlaier *    may be used to endorse or promote products derived from this software
18172677Smlaier *    without specific prior written permission.
19127664Sbms *
20127664Sbms * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21127664Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22127664Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23127664Sbms * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24127664Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25127664Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26127664Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27127664Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28127664Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29127664Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30127664Sbms * SUCH DAMAGE.
31127664Sbms */
32127664Sbms
33127664Sbms#if defined(LIBC_SCCS) && !defined(lint)
34127664Sbmsstatic char sccsid[] = "@(#)fwrite.c	8.1 (Berkeley) 6/4/93";
35127664Sbms#endif /* LIBC_SCCS and not lint */
36214518Srpaulo#include <sys/cdefs.h>
37127664Sbms__FBSDID("$FreeBSD: head/lib/libc/stdio/fwrite.c 249810 2013-04-23 14:36:44Z emaste $");
38127664Sbms
39127664Sbms#include "namespace.h"
40127664Sbms#include <errno.h>
41127664Sbms#include <stdint.h>
42127664Sbms#include <stdio.h>
43127664Sbms#include "un-namespace.h"
44127664Sbms#include "local.h"
45172677Smlaier#include "fvwrite.h"
46127664Sbms#include "libc_private.h"
47127664Sbms
48127664Sbms/*
49127664Sbms * Write `count' objects (each size `size') from memory to the given file.
50127664Sbms * Return the number of whole objects written.
51127664Sbms */
52127664Sbmssize_t
53127664Sbmsfwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)
54127664Sbms{
55127664Sbms	size_t n;
56127664Sbms	struct __suio uio;
57127664Sbms	struct __siov iov;
58127664Sbms
59127664Sbms	/*
60127664Sbms	 * ANSI and SUSv2 require a return value of 0 if size or count are 0.
61127664Sbms	 */
62127664Sbms	if ((count == 0) || (size == 0))
63127664Sbms		return (0);
64127664Sbms
65127664Sbms	/*
66127664Sbms	 * Check for integer overflow.  As an optimization, first check that
67127664Sbms	 * at least one of {count, size} is at least 2^16, since if both
68127664Sbms	 * values are less than that, their product can't possible overflow
69127664Sbms	 * (size_t is always at least 32 bits on FreeBSD).
70127664Sbms	 */
71127664Sbms	if (((count | size) > 0xFFFF) &&
72127664Sbms	    (count > SIZE_MAX / size)) {
73127664Sbms		errno = EINVAL;
74127664Sbms		fp->_flags |= __SERR;
75127664Sbms		return (0);
76127664Sbms	}
77127664Sbms
78127664Sbms	n = count * size;
79127664Sbms
80127664Sbms	iov.iov_base = (void *)buf;
81127664Sbms	uio.uio_resid = iov.iov_len = n;
82127664Sbms	uio.uio_iov = &iov;
83127664Sbms	uio.uio_iovcnt = 1;
84127664Sbms
85127664Sbms	FLOCKFILE(fp);
86127664Sbms	ORIENT(fp, -1);
87127664Sbms	/*
88127664Sbms	 * The usual case is success (__sfvwrite returns 0);
89127664Sbms	 * skip the divide if this happens, since divides are
90127664Sbms	 * generally slow and since this occurs whenever size==0.
91127664Sbms	 */
92127664Sbms	if (__sfvwrite(fp, &uio) != 0)
93127664Sbms	    count = (n - uio.uio_resid) / size;
94127664Sbms	FUNLOCKFILE(fp);
95127664Sbms	return (count);
96127664Sbms}
97127664Sbms