xref: /original-bsd/lib/libc/stdio/fwrite.c (revision feb5f8e2)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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	5.5 (Berkeley) 02/24/91";
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 fwrite(buf, size, count, fp)
24 	const void *buf;
25 	size_t size, count;
26 	FILE *fp;
27 {
28 	size_t n;
29 	struct __suio uio;
30 	struct __siov iov;
31 
32 	iov.iov_base = (void *)buf;
33 	uio.uio_resid = iov.iov_len = n = count * size;
34 	uio.uio_iov = &iov;
35 	uio.uio_iovcnt = 1;
36 
37 	/*
38 	 * The usual case is success (__sfvwrite returns 0);
39 	 * skip the divide if this happens, since divides are
40 	 * generally slow and since this occurs whenever size==0.
41 	 */
42 	if (__sfvwrite(fp, &uio) == 0)
43 		return (count);
44 	return ((n - uio.uio_resid) / size);
45 }
46