Init the object variable for the registergroup so that
[cake.git] / compiler / clib / ftruncate.c
blob65808ccf1f65845a5cc297e3fcbceee2802674ae
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->fcb->isdir)
69 errno = EISDIR;
70 return -1;
73 if (!(fdesc->fcb->flags & (O_WRONLY|O_RDWR)))
75 errno = EINVAL;
76 return -1;
79 oldpos = Seek(fdesc->fcb->fh, 0, OFFSET_END);
80 size = Seek(fdesc->fcb->fh, 0, OFFSET_CURRENT);
81 Seek(fdesc->fcb->fh, oldpos, OFFSET_BEGINNING);
83 if ((length = SetFileSize(fdesc->fcb->fh, length, OFFSET_BEGINNING)) == -1)
85 errno = IoErr2errno(IoErr());
86 return -1;
88 else
89 if (size < length)
91 char buf[16]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
93 length -= size;
95 oldpos = Seek(fdesc->fcb->fh, size, OFFSET_BEGINNING);
97 while (length >= 16)
99 FWrite(fdesc->fcb->fh, buf, 16, 1);
100 length -= 16;
102 if (length)
103 FWrite(fdesc->fcb->fh, buf, length, 1);
105 Flush(fdesc->fcb->fh);
106 Seek(fdesc->fcb->fh, oldpos, OFFSET_BEGINNING);
109 return 0;