Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / librols / stdio / filewrite.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)filewrite.c      1.14 04/08/08 Copyright 1986, 1995-2003 J. Schilling */
14 /*
15  *      Copyright (c) 1986, 1995-2003 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along with
28  * this program; see the file COPYING.  If not, write to the Free Software
29  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #include "schilyio.h"
33
34 static  char    _writeerr[]     = "file_write_err";
35
36 #ifdef  HAVE_USG_STDIO
37
38 EXPORT int
39 filewrite(f, vbuf, len)
40         register FILE   *f;
41         void    *vbuf;
42         int     len;
43 {
44         register int    n;
45         int     cnt;
46         char            *buf = vbuf;
47
48         down2(f, _IOWRT, _IORW);
49
50         if (f->_flag & _IONBF) {
51                 cnt = write(fileno(f), buf, len);
52                 if (cnt < 0) {
53                         f->_flag |= _IOERR;
54                         if (!(my_flag(f) & _IONORAISE))
55                                 raisecond(_writeerr, 0L);
56                 }
57                 return (cnt);
58         }
59         cnt = 0;
60         while (len > 0) {
61                 if (f->_cnt <= 0) {
62                         if (usg_flsbuf((unsigned char) *buf++, f) == EOF)
63                                 break;
64                         cnt++;
65                         if (--len == 0)
66                                 break;
67                 }
68                 if ((n = f->_cnt >= len ? len : f->_cnt) > 0) {
69                         f->_ptr = (unsigned char *)movebytes(buf, f->_ptr, n);
70                         buf += n;
71                         f->_cnt -= n;
72                         cnt += n;
73                         len -= n;
74                 }
75         }
76         if (!ferror(f))
77                 return (cnt);
78         if (!(my_flag(f) & _IONORAISE))
79         raisecond(_writeerr, 0L);
80         return (-1);
81 }
82
83 #else
84
85 EXPORT int
86 filewrite(f, vbuf, len)
87         register FILE   *f;
88         void    *vbuf;
89         int     len;
90 {
91         int     cnt;
92         char            *buf = vbuf;
93
94         down2(f, _IOWRT, _IORW);
95
96         if (my_flag(f) & _IOUNBUF)
97                 return (write(fileno(f), buf, len));
98         cnt = fwrite(buf, 1, len, f);
99
100         if (!ferror(f))
101                 return (cnt);
102         if (!(my_flag(f) & _IONORAISE))
103         raisecond(_writeerr, 0L);
104         return (-1);
105 }
106
107 #endif  /* HAVE_USG_STDIO */