2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
5 POSIX.1-2008 function ftruncate().
14 /*****************************************************************************
26 Truncate a file to a specified length
29 fd - the descriptor of the file being truncated.
30 The file must be open for writing
31 lenght - The file will have at most this size
34 0 on success or -1 on errorr.
37 If the file previously was larger than this size, the extra data
38 is lost. If the file previously was shorter, it is
39 unspecified whether the file is left unchanged or is
40 extended. In the latter case the extended part reads as
53 ******************************************************************************/
58 fdesc
*fdesc
= __getfdesc(fd
);
66 if(fdesc
->fcb
->privflags
& _FCB_ISDIR
)
72 if (!(fdesc
->fcb
->flags
& (O_WRONLY
|O_RDWR
)))
78 oldpos
= Seek(fdesc
->fcb
->handle
, 0, OFFSET_END
);
79 size
= Seek(fdesc
->fcb
->handle
, 0, OFFSET_CURRENT
);
80 Seek(fdesc
->fcb
->handle
, oldpos
, OFFSET_BEGINNING
);
82 if ((length
= SetFileSize(fdesc
->fcb
->handle
, length
, OFFSET_BEGINNING
)) == -1)
84 errno
= __stdc_ioerr2errno(IoErr());
90 char buf
[16]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
94 oldpos
= Seek(fdesc
->fcb
->handle
, size
, OFFSET_BEGINNING
);
98 FWrite(fdesc
->fcb
->handle
, buf
, 16, 1);
102 FWrite(fdesc
->fcb
->handle
, buf
, length
, 1);
104 Flush(fdesc
->fcb
->handle
);
105 Seek(fdesc
->fcb
->handle
, oldpos
, OFFSET_BEGINNING
);