1 /* @(#)filewrite.c 1.18 12/02/26 Copyright 1986, 1995-2012 J. Schilling */
2 /*
3 * Copyright (c) 1986, 1995-2012 J. Schilling
4 */
5 /*
6 * The contents of this file are subject to the terms of the
7 * Common Development and Distribution License, Version 1.0 only
8 * (the "License"). You may not use this file except in compliance
9 * with the License.
10 *
11 * See the file CDDL.Schily.txt in this distribution for details.
12 * A copy of the CDDL is also available via the Internet at
13 * http://www.opensource.org/licenses/cddl1.txt
14 *
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file CDDL.Schily.txt from this distribution.
17 */
18
19 #include "schilyio.h"
20
21 static char _writeerr[] = "file_write_err";
22
23 #ifdef HAVE_USG_STDIO
24
25 EXPORT ssize_t
filewrite(f,vbuf,len)26 filewrite(f, vbuf, len)
27 register FILE *f;
28 void *vbuf;
29 size_t len;
30 {
31 register int n;
32 ssize_t cnt;
33 char *buf = vbuf;
34
35 down2(f, _IOWRT, _IORW);
36
37 if (f->_flag & _IONBF) {
38 cnt = _niwrite(fileno(f), buf, len);
39 if (cnt < 0) {
40 f->_flag |= _IOERR;
41 if (!(my_flag(f) & _JS_IONORAISE))
42 raisecond(_writeerr, 0L);
43 }
44 return (cnt);
45 }
46 cnt = 0;
47 while (len > 0) {
48 if (f->_cnt <= 0) {
49 if (usg_flsbuf((unsigned char) *buf++, f) == EOF)
50 break;
51 cnt++;
52 if (--len == 0)
53 break;
54 }
55 if ((n = f->_cnt >= len ? len : f->_cnt) > 0) {
56 f->_ptr = (unsigned char *)movebytes(buf, f->_ptr, n);
57 buf += n;
58 f->_cnt -= n;
59 cnt += n;
60 len -= n;
61 }
62 }
63 if (!ferror(f))
64 return (cnt);
65 if (!(my_flag(f) & _JS_IONORAISE) && !(_io_glflag & _JS_IONORAISE))
66 raisecond(_writeerr, 0L);
67 return (-1);
68 }
69
70 #else
71
72 EXPORT ssize_t
filewrite(f,vbuf,len)73 filewrite(f, vbuf, len)
74 register FILE *f;
75 void *vbuf;
76 size_t len;
77 {
78 ssize_t cnt;
79 char *buf = vbuf;
80
81 down2(f, _IOWRT, _IORW);
82
83 if (my_flag(f) & _JS_IOUNBUF)
84 return (_niwrite(fileno(f), buf, len));
85 cnt = fwrite(buf, 1, len, f);
86
87 if (!ferror(f))
88 return (cnt);
89 if (!(my_flag(f) & _JS_IONORAISE) && !(_io_glflag & _JS_IONORAISE))
90 raisecond(_writeerr, 0L);
91 return (-1);
92 }
93
94 #endif /* HAVE_USG_STDIO */
95