xref: /csrg-svn/lib/libc/stdio/fwrite.c (revision 46089)
1*46089Sbostic /*-
2*46089Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46089Sbostic  * All rights reserved.
4*46089Sbostic  *
5*46089Sbostic  * This code is derived from software contributed to Berkeley by
6*46089Sbostic  * Chris Torek.
7*46089Sbostic  *
8*46089Sbostic  * %sccs.include.redist.c%
921408Sdist  */
1021408Sdist 
1126655Sdonn #if defined(LIBC_SCCS) && !defined(lint)
12*46089Sbostic static char sccsid[] = "@(#)fwrite.c	5.3 (Berkeley) 01/20/91";
13*46089Sbostic #endif /* LIBC_SCCS and not lint */
1421408Sdist 
15*46089Sbostic #include <stdio.h>
16*46089Sbostic #include <string.h>
17*46089Sbostic #include "local.h"
18*46089Sbostic #include "fvwrite.h"
1916550Skarels 
20*46089Sbostic /*
21*46089Sbostic  * Write `count' objects (each size `size') from memory to the given file.
22*46089Sbostic  * Return the number of whole objects written.
23*46089Sbostic  */
24*46089Sbostic fwrite(buf, size, count, fp)
25*46089Sbostic 	void *buf;
26*46089Sbostic 	size_t size, count;
27*46089Sbostic 	FILE *fp;
2816550Skarels {
29*46089Sbostic 	size_t n;
30*46089Sbostic 	struct __suio uio;
31*46089Sbostic 	struct __siov iov;
3216550Skarels 
33*46089Sbostic 	iov.iov_base = buf;
34*46089Sbostic 	uio.uio_resid = iov.iov_len = n = count * size;
35*46089Sbostic 	uio.uio_iov = &iov;
36*46089Sbostic 	uio.uio_iovcnt = 1;
37*46089Sbostic 
38*46089Sbostic 	/*
39*46089Sbostic 	 * The usual case is success (__sfvwrite returns 0);
40*46089Sbostic 	 * skip the divide if this happens, since divides are
41*46089Sbostic 	 * generally slow and since this occurs whenever size==0.
42*46089Sbostic 	 */
43*46089Sbostic 	if (__sfvwrite(fp, &uio) == 0)
44*46089Sbostic 		return (count);
45*46089Sbostic 	return ((n - uio.uio_resid) / size);
4616550Skarels }
47