Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / librols / stdio / fileread.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 /* @(#)fileread.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    _readerr[]      = "file_read_err";
35
36 #ifdef  HAVE_USG_STDIO
37
38 EXPORT int
39 fileread(f, buf, len)
40         register FILE   *f;
41         void    *buf;
42         int     len;
43 {
44         int     cnt;
45         register int    n;
46
47         down2(f, _IOREAD, _IORW);
48
49         if (f->_flag & _IONBF) {
50                 cnt = _niread(fileno(f), buf, len);
51                 if (cnt < 0) {
52                         f->_flag |= _IOERR;
53                         if (!(my_flag(f) & _IONORAISE))
54                                 raisecond(_readerr, 0L);
55                 }
56                 if (cnt == 0 && len)
57                         f->_flag |= _IOEOF;
58                 return (cnt);
59         }
60         cnt = 0;
61         while (len > 0) {
62                 if (f->_cnt <= 0) {
63                         if (usg_filbuf(f) == EOF)
64                                 break;
65                         f->_cnt++;
66                         f->_ptr--;
67                 }
68                 n = f->_cnt >= len ? len : f->_cnt;
69                 buf = (void *)movebytes(f->_ptr, buf, n);
70                 f->_ptr += n;
71                 f->_cnt -= n;
72                 cnt += n;
73                 len -= n;
74         }
75         if (!ferror(f))
76                 return (cnt);
77         if (!(my_flag(f) & _IONORAISE))
78                 raisecond(_readerr, 0L);
79         return (-1);
80 }
81
82 #else
83
84 EXPORT int
85 fileread(f, buf, len)
86         register FILE   *f;
87         void    *buf;
88         int     len;
89 {
90         int     cnt;
91
92         down2(f, _IOREAD, _IORW);
93
94         if (my_flag(f) & _IOUNBUF)
95                 return (_niread(fileno(f), buf, len));
96         cnt = fread(buf, 1, len, f);
97
98         if (!ferror(f))
99                 return (cnt);
100         if (!(my_flag(f) & _IONORAISE))
101                 raisecond(_readerr, 0L);
102         return (-1);
103 }
104
105 #endif  /* HAVE_USG_STDIO */