fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / stdio / fwrite.c
blob1c91632b8879be808d29b4bda3f36e26ba1a9f41
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 FUNCTION
20 <<fwrite>>---write array elements
22 INDEX
23 fwrite
25 ANSI_SYNOPSIS
26 #include <stdio.h>
27 size_t fwrite(const void *<[buf]>, size_t <[size]>,
28 size_t <[count]>, FILE *<[fp]>);
30 TRAD_SYNOPSIS
31 #include <stdio.h>
32 size_t fwrite(<[buf]>, <[size]>, <[count]>, <[fp]>)
33 char *<[buf]>;
34 size_t <[size]>;
35 size_t <[count]>;
36 FILE *<[fp]>;
38 DESCRIPTION
39 <<fwrite>> attempts to copy, starting from the memory location
40 <[buf]>, <[count]> elements (each of size <[size]>) into the file or
41 stream identified by <[fp]>. <<fwrite>> may copy fewer elements than
42 <[count]> if an error intervenes.
44 <<fwrite>> also advances the file position indicator (if any) for
45 <[fp]> by the number of @emph{characters} actually written.
47 RETURNS
48 If <<fwrite>> succeeds in writing all the elements you specify, the
49 result is the same as the argument <[count]>. In any event, the
50 result is the number of complete elements that <<fwrite>> copied to
51 the file.
53 PORTABILITY
54 ANSI C requires <<fwrite>>.
56 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
57 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
60 #if defined(LIBC_SCCS) && !defined(lint)
61 static char sccsid[] = "%W% (Berkeley) %G%";
62 #endif /* LIBC_SCCS and not lint */
64 #include <stdio.h>
65 #include <string.h>
66 #if 0
67 #include <sys/stdc.h>
68 #endif
69 #include "local.h"
70 #if 1
71 #include "fvwrite.h"
72 #endif
75 * Write `count' objects (each size `size') from memory to the given file.
76 * Return the number of whole objects written.
79 size_t
80 _DEFUN (fwrite, (buf, size, count, fp),
81 _CONST _PTR buf _AND
82 size_t size _AND
83 size_t count _AND
84 FILE * fp)
86 size_t n;
87 struct __suio uio;
88 struct __siov iov;
90 iov.iov_base = buf;
91 uio.uio_resid = iov.iov_len = n = count * size;
92 uio.uio_iov = &iov;
93 uio.uio_iovcnt = 1;
96 * The usual case is success (__sfvwrite returns 0);
97 * skip the divide if this happens, since divides are
98 * generally slow and since this occurs whenever size==0.
101 _flockfile(fp);
102 if (__sfvwrite (fp, &uio) == 0)
104 _funlockfile(fp);
105 return count;
107 _funlockfile(fp);
108 return (n - uio.uio_resid) / size;