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