PatchworkOS
Loading...
Searching...
No Matches
fseek.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <sys/io.h>
3
4#include "user/common/file.h"
5
6int fseek(FILE* stream, long offset, int whence)
7{
8 mtx_lock(&stream->mtx);
9
10 if (stream->flags & _FILE_WRITE)
11 {
12 if (_file_flush_buffer(stream) == ERR)
13 {
14 mtx_unlock(&stream->mtx);
15 return EOF;
16 }
17 }
18
19 stream->flags &= ~_FILE_EOF;
20
21 if (stream->flags & _FILE_RW)
22 {
23 stream->flags &= ~(_FILE_READ | _FILE_WRITE);
24 }
25
26 if (whence == SEEK_CUR)
27 {
28 offset -= (((int)stream->bufEnd - (int)stream->bufIndex) + stream->ungetIndex);
29 }
30
31 uint64_t result = (_file_seek(stream, offset, whence) != ERR) ? 0 : EOF;
32 mtx_unlock(&stream->mtx);
33 return result;
34}
#define SEEK_CUR
Definition SEEK.h:5
int fseek(FILE *stream, long offset, int whence)
Definition fseek.c:6
#define ERR
Integer error value.
Definition ERR.h:17
uint64_t _file_flush_buffer(FILE *stream)
Definition file.c:158
uint64_t _file_seek(FILE *stream, int64_t offset, int whence)
Definition file.c:202
@ _FILE_READ
Definition file.h:10
@ _FILE_WRITE
Definition file.h:11
@ _FILE_RW
Definition file.h:12
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
#define EOF
Definition stdio.h:25
Definition file.h:34
uint64_t bufIndex
Definition file.h:39
uint64_t bufEnd
Definition file.h:40
_file_flags_t flags
Definition file.h:44
mtx_t mtx
Definition file.h:45
uint64_t ungetIndex
Definition file.h:43
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10