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