summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/fseek.c
blob: af12cd2c8bb7ff5774abfb23add8f7e12e6e631c (plain)
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
#include <unistd.h>
#include <io.h>
#include <stdio.h>
#include <atomic.h>

int fseek(FILE *stream, long offset, int whence)
{
	if (stream == NULL || stream->fd < 0)
		return -1;

	LIBC_LOCK(stream->lock);

	stream->buf_pos = 0;
	stream->buf_len = 0;
	stream->flags &= ~_IO_EOF;

	off_t result = lseek(stream->fd, offset, whence);

	LIBC_UNLOCK(stream->lock);

	if (result == (off_t)-1) {
		stream->flags |= _IO_ERR;
		return -1;
	}

	stream->offset = result;
	return 0;
}