PatchworkOS
Loading...
Searching...
No Matches
fread.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3
4#include "user/common/file.h"
6
7size_t fread(void* _RESTRICT ptr, size_t size, size_t nmemb, FILE* _RESTRICT stream)
8{
9 mtx_lock(&stream->mtx);
10
11 uint64_t n = 0;
12
13 if (_file_prepare_read(stream) != ERR)
14 {
15 for (; n < nmemb; n++)
16 {
17 // TODO: For better performance, read block-wise, not byte-wise.
18 for (uint64_t i = 0; i < size; i++)
19 {
20 if (_FILE_CHECK_AVAIL(stream) == ERR)
21 {
22 mtx_unlock(&stream->mtx);
23 return n;
24 }
25
26 ((uint8_t*)ptr)[n * size + i] = _FILE_GETC(stream);
27 }
28 }
29 }
30
31 mtx_unlock(&stream->mtx);
32 return n;
33}
size_t fread(void *_RESTRICT ptr, size_t size, size_t nmemb, FILE *_RESTRICT stream)
Definition fread.c:7
#define ERR
Integer error value.
Definition ERR.h:17
#define _RESTRICT
Definition config.h:17
uint64_t _file_prepare_read(FILE *stream)
Definition file.c:224
#define _FILE_GETC(stream)
Definition file.h:49
#define _FILE_CHECK_AVAIL(fh)
Definition file.h:53
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
Definition file.h:34
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10