]> git.sur5r.net Git - cc65/blob - libsrc/common/fwrite.c
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / libsrc / common / fwrite.c
1 /*
2  * fwrite.c
3  *
4  * Ullrich von Bassewitz, 04.06.1998
5  */
6
7
8
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include "_file.h"
13
14
15
16 size_t fwrite (void* buf, size_t size, size_t count, FILE* f)
17 {
18     int bytes;
19
20     /* Is the file open? */
21     if ((f->f_flags & _FOPEN) == 0) {
22         _errno = EINVAL;                /* File not open */
23         return -1;
24     }
25
26     /* Did we have an error */
27     if ((f->f_flags & _FERROR) != 0) {
28         /* Cannot write to stream */
29         return 0;
30     }
31
32     /* How many bytes to write? */
33     bytes = size * count;
34
35     if (bytes) {
36         /* Write the data. */
37         if (write (f->f_fd, buf, bytes) == -1) {
38             /* Write error */
39             f->f_flags |= _FERROR;
40             return -1;
41         }
42     }
43
44     /* Don't waste time with expensive calculations, assume the write was
45      * complete and return the count of items.
46      */
47     return count;
48 }
49
50
51