add a missing section header table index conversion
[tangerine.git] / compiler / clib / ftruncate.c
blob068b99922ffebf9a5ed3abcbdab039d4b18f8181
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function ftruncate().
6 */
8 #include <dos/dos.h>
9 #include <proto/dos.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include "__errno.h"
13 #include "__open.h"
15 /*****************************************************************************
17 NAME */
18 #include <unistd.h>
20 int ftruncate (
22 /* SYNOPSIS */
23 int fd,
24 off_t length)
26 /* FUNCTION
27 Truncate a file to a specified length
29 INPUTS
30 fd - the descriptor of the file being truncated.
31 The file must be open for writing
32 lenght - The file will have at most this size
34 RESULT
35 0 on success or -1 on errorr.
37 NOTES
38 If the file previously was larger than this size, the extra data
39 is lost. If the file previously was shorter, it is
40 unspecified whether the file is left unchanged or is
41 extended. In the latter case the extended part reads as
42 zero bytes.
45 EXAMPLE
47 BUGS
49 SEE ALSO
50 open(), truncate()
52 INTERNALS
54 ******************************************************************************/
56 ULONG oldpos;
57 size_t size;
59 fdesc *fdesc = __getfdesc(fd);
61 if (!fdesc)
63 errno = EBADF;
64 return -1;
67 if (!(fdesc->flags & (O_WRONLY|O_RDWR)))
69 errno = EINVAL;
70 return -1;
73 oldpos = Seek(fdesc->fh, 0, OFFSET_END);
74 size = Seek(fdesc->fh, 0, OFFSET_CURRENT);
75 Seek(fdesc->fh, oldpos, OFFSET_BEGINNING);
77 if ((length = SetFileSize(fdesc->fh, length, OFFSET_BEGINNING)) == -1)
79 errno = IoErr2errno(IoErr());
80 return -1;
82 else
83 if (size < length)
85 char buf[16]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
87 length -= size;
89 oldpos = Seek(fdesc->fh, size, OFFSET_BEGINNING);
91 while (length >= 16)
93 FWrite(fdesc->fh, buf, 16, 1);
94 length -= 16;
96 if (length)
97 FWrite(fdesc->fh, buf, length, 1);
99 Flush(fdesc->fh);
100 Seek(fdesc->fh, oldpos, OFFSET_BEGINNING);
103 return 0;