summaryrefslogtreecommitdiff
path: root/cpu/sysdeps/unix/sysv/linux/i386/sys-truncate.c
blob: 12adcacc2b5c97a41a26d372f28765584938d78a (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
51
52
53
54
55
56
57
58
59
/***************************************************************
  SYS-TRUNCATE.C

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

       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 <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

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


__mpu_int64_t
__cpu_sys_truncate( int dirfd, const char *path, __mpu_int64_t length )
{
  off_t  host_length;

  if( !path ) return( -(__mpu_int64_t)__MCPU_EFAULT );

  host_length = (off_t)length;
  if( (__mpu_int64_t)host_length != length )
    return( -(__mpu_int64_t)__MCPU_EOVERFLOW );

  {
    int fd;
    int error;

    fd = openat( dirfd, path, O_WRONLY | O_CLOEXEC );
    if( fd < 0 ) return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );

    if( ftruncate( fd, host_length ) < 0 )
    {
      error = errno;
      (void)close( fd );
      return( -(__mpu_int64_t)__cpu_errno_from_host( error ) );
    }

    (void)close( fd );
  }

  return( 0 );

} /* End of __cpu_sys_truncate() */