summaryrefslogtreecommitdiff
path: root/cpu/sysdeps/unix/sysv/linux/i386/sys-lseek.c
blob: 05d8ad5028e8f692cc3d65ced7fa67f6e9758ef2 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/***************************************************************
  SYS-LSEEK.C

       This file contains Linux/i386/ILP32 implementation of
       __cpu_sys_lseek().

       PART OF : CPU - library .

       USAGE   : Internal only .

       Copyright (C) 2026  by Andrew V.Kosteltsev.
       All Rights Reserved.
 ***************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <limits.h>
#include <unistd.h>

#include <libmcpu.h>
#include <cpu-sysdeps.h>

__mpu_int64_t
__cpu_sys_lseek( int fd, __mpu_int64_t offset, __mpu_uint64_t whence )
{
  off_t host_offset;
  off_t ret;
  int host_whence;

  switch( whence )
  {
  case __CPU_SEEK_SET: host_whence = SEEK_SET; break;
  case __CPU_SEEK_CUR: host_whence = SEEK_CUR; break;
  case __CPU_SEEK_END: host_whence = SEEK_END; break;
  default: return( -(__mpu_int64_t)__MCPU_EINVAL );
  }

  host_offset = (off_t)offset;
  if( (__mpu_int64_t)host_offset != offset ) return( -(__mpu_int64_t)__MCPU_EOVERFLOW );

  ret = lseek( fd, host_offset, host_whence );
  if( ret == (off_t)-1 ) return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );

  if( (off_t)(__mpu_int64_t)ret != ret ) return( -(__mpu_int64_t)__MCPU_EOVERFLOW );
  return( (__mpu_int64_t)ret );
}