1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/****************************************************************************
* libc/stdio/lib_libfread.c
*
* Copyright (C) 2007-2009, 2011-2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "lib_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Global Constant Data
****************************************************************************/
/****************************************************************************
* Global Variables
****************************************************************************/
/****************************************************************************
* Private Constant Data
****************************************************************************/
/****************************************************************************
* Private Variables
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: lib_fread
****************************************************************************/
ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
{
unsigned char *dest = (unsigned char*)ptr;
ssize_t bytes_read;
#if CONFIG_STDIO_BUFFER_SIZE > 0
int ret;
#endif
/* Make sure that reading from this stream is allowed */
if (!stream || (stream->fs_oflags & O_RDOK) == 0)
{
set_errno(EBADF);
bytes_read = -1;
}
else
{
/* The stream must be stable until we complete the read */
lib_take_semaphore(stream);
#if CONFIG_NUNGET_CHARS > 0
/* First, re-read any previously ungotten characters */
while ((stream->fs_nungotten > 0) && (count > 0))
{
/* Decrement the count of ungotten bytes to get an index */
stream->fs_nungotten--;
/* Return the last ungotten byte */
*dest++ = stream->fs_ungotten[stream->fs_nungotten];
/* That's one less byte that we have to read */
count--;
}
#endif
#if CONFIG_STDIO_BUFFER_SIZE > 0
/* If the buffer is currently being used for write access, then
* flush all of the buffered write data. We do not support concurrent
* buffered read/write access.
*/
ret = lib_wrflush(stream);
if (ret < 0)
{
lib_give_semaphore(stream);
return ret;
}
/* Now get any other needed chars from the buffer or the file. */
while (count > 0)
{
/* Is there readable data in the buffer? */
while ((count > 0) && (stream->fs_bufpos < stream->fs_bufread))
{
/* Yes, copy a byte into the user buffer */
*dest++ = *stream->fs_bufpos++;
count--;
}
/* The buffer is empty OR we have already supplied the number of
* bytes requested in the read. Check if we need to read
* more from the file.
*/
if (count > 0)
{
size_t buffer_available;
/* We need to read more data into the buffer from the file */
/* Mark the buffer empty */
stream->fs_bufpos = stream->fs_bufread = stream->fs_bufstart;
/* How much space is available in the buffer? */
buffer_available = stream->fs_bufend - stream->fs_bufread;
/* Will the number of bytes that we need to read fit into
* the buffer space that is available? If the read size is
* larger than the buffer, then read some of the data
* directly into the user's buffer.
*/
if (count > buffer_available)
{
bytes_read = read(stream->fs_fd, dest, count);
if (bytes_read < 0)
{
/* An error occurred on the read. The error code is
* in the 'errno' variable.
*/
goto errout_with_errno;
}
else if (bytes_read == 0)
{
/* We are at the end of the file. But we may already
* have buffered data. In that case, we will report
* the EOF indication later.
*/
goto shortread;
}
else
{
/* Some bytes were read. Adjust the dest pointer */
dest += bytes_read;
/* Were all of the requested bytes read? */
if ((size_t)bytes_read < count)
{
/* No. We must be at the end of file. */
goto shortread;
}
else
{
/* Yes. We are done. */
count = 0;
}
}
}
else
{
/* The number of bytes required to satisfy the read
* is less than or equal to the size of the buffer
* space that we have left. Read as much as we can
* into the buffer.
*/
bytes_read = read(stream->fs_fd, stream->fs_bufread, buffer_available);
if (bytes_read < 0)
{
/* An error occurred on the read. The error code is
* in the 'errno' variable.
*/
goto errout_with_errno;
}
else if (bytes_read == 0)
{
/* We are at the end of the file. But we may already
* have buffered data. In that case, we will report
* the EOF indication later.
*/
goto shortread;
}
else
{
/* Some bytes were read */
stream->fs_bufread += bytes_read;
}
}
}
}
#else
/* Now get any other needed chars from the file. */
while (count > 0)
{
bytes_read = read(stream->fs_fd, dest, count);
if (bytes_read < 0)
{
/* An error occurred on the read. The error code is
* in the 'errno' variable.
*/
goto errout_with_errno;
}
else if (bytes_read == 0)
{
/* We are at the end of the file. But we may already
* have buffered data. In that case, we will report
* the EOF indication later.
*/
break;
}
else
{
dest += bytes_read;
count -= bytes_read;
}
}
#endif
/* Here after a successful (but perhaps short) read */
#if CONFIG_STDIO_BUFFER_SIZE > 0
shortread:
#endif
bytes_read = dest - (unsigned char*)ptr;
/* Set or clear the EOF indicator. If we get here because of a
* short read and the total number of* bytes read is zero, then
* we must be at the end-of-file.
*/
if (bytes_read > 0)
{
stream->fs_flags &= ~__FS_FLAG_EOF;
}
else
{
stream->fs_flags |= __FS_FLAG_EOF;
}
lib_give_semaphore(stream);
}
return bytes_read;
/* Error exits */
errout_with_errno:
lib_give_semaphore(stream);
return -get_errno();
}
|