linux/bootstrap: use Forbid/Permit only when thread is main AROS thread
[AROS.git] / compiler / stdc / fread.c
blob62d754400db283772c6bcc6a1afb9f77a0b13ac5
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function fread().
6 */
7 #include <proto/dos.h>
8 #include <errno.h>
10 #include "__stdio.h"
12 /*****************************************************************************
14 NAME */
15 #include <stdio.h>
17 size_t fread (
19 /* SYNOPSIS */
20 void * restrict buf,
21 size_t size,
22 size_t nblocks,
23 FILE * restrict stream)
25 /* FUNCTION
26 Read an amount of bytes from a stream.
28 INPUTS
29 buf - The buffer to read the bytes into
30 size - Size of one block to read
31 nblocks - The number of blocks to read
32 stream - Read from this stream
34 RESULT
35 The number of blocks read. This may range from 0 when the stream
36 contains no more blocks up to nblocks. In case of an error, 0 is
37 returned.
39 NOTES
41 EXAMPLE
43 BUGS
45 SEE ALSO
46 fopen(), fwrite()
48 INTERNALS
50 ******************************************************************************/
52 LONG cnt;
54 if (size == 0 || nblocks == 0)
55 return 0;
57 if (!(stream->flags & __STDCIO_STDIO_READ))
59 SetIoErr(ERROR_READ_PROTECTED);
60 errno = EACCES;
61 stream->flags |= __STDCIO_STDIO_ERROR;
62 return 0;
65 cnt = FRead (stream->fh, buf, size, nblocks);
67 if (cnt == -1)
69 errno = __stdc_ioerr2errno (IoErr ());
70 stream->flags |= __STDCIO_STDIO_ERROR;
72 cnt = 0;
74 else if (cnt < nblocks)
76 stream->flags |= __STDCIO_STDIO_EOF;
79 return (size_t)cnt;
80 } /* fread */