10Sstevel@tonic-gate #ifndef LINT
2*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: ftruncate.c,v 1.3 2005/04/27 18:16:45 sra Exp $";
30Sstevel@tonic-gate #endif
40Sstevel@tonic-gate
5*11038SRao.Shoaib@Sun.COM /*! \file
6*11038SRao.Shoaib@Sun.COM * \brief
70Sstevel@tonic-gate * ftruncate - set file size, BSD Style
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * shortens or enlarges the file as neeeded
100Sstevel@tonic-gate * uses some undocumented locking call. It is known to work on SCO unix,
110Sstevel@tonic-gate * other vendors should try.
120Sstevel@tonic-gate * The #error directive prevents unsupported OSes
130Sstevel@tonic-gate */
140Sstevel@tonic-gate
150Sstevel@tonic-gate #include "port_before.h"
160Sstevel@tonic-gate
170Sstevel@tonic-gate #if defined(M_UNIX)
180Sstevel@tonic-gate #define OWN_FTRUNCATE
190Sstevel@tonic-gate #include <stdio.h>
200Sstevel@tonic-gate #ifdef _XOPEN_SOURCE
210Sstevel@tonic-gate #undef _XOPEN_SOURCE
220Sstevel@tonic-gate #endif
230Sstevel@tonic-gate #ifdef _POSIX_SOURCE
240Sstevel@tonic-gate #undef _POSIX_SOURCE
250Sstevel@tonic-gate #endif
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <fcntl.h>
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include "port_after.h"
300Sstevel@tonic-gate
310Sstevel@tonic-gate int
__ftruncate(int fd,long wantsize)320Sstevel@tonic-gate __ftruncate(int fd, long wantsize) {
330Sstevel@tonic-gate long cursize;
340Sstevel@tonic-gate
350Sstevel@tonic-gate /* determine current file size */
360Sstevel@tonic-gate if ((cursize = lseek(fd, 0L, 2)) == -1)
370Sstevel@tonic-gate return (-1);
380Sstevel@tonic-gate
390Sstevel@tonic-gate /* maybe lengthen... */
400Sstevel@tonic-gate if (cursize < wantsize) {
410Sstevel@tonic-gate if (lseek(fd, wantsize - 1, 0) == -1 ||
420Sstevel@tonic-gate write(fd, "", 1) == -1) {
430Sstevel@tonic-gate return (-1);
440Sstevel@tonic-gate }
450Sstevel@tonic-gate return (0);
460Sstevel@tonic-gate }
470Sstevel@tonic-gate
480Sstevel@tonic-gate /* maybe shorten... */
490Sstevel@tonic-gate if (wantsize < cursize) {
500Sstevel@tonic-gate struct flock fl;
510Sstevel@tonic-gate
520Sstevel@tonic-gate fl.l_whence = 0;
530Sstevel@tonic-gate fl.l_len = 0;
540Sstevel@tonic-gate fl.l_start = wantsize;
550Sstevel@tonic-gate fl.l_type = F_WRLCK;
560Sstevel@tonic-gate return (fcntl(fd, F_FREESP, &fl));
570Sstevel@tonic-gate }
580Sstevel@tonic-gate return (0);
590Sstevel@tonic-gate }
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate
620Sstevel@tonic-gate #ifndef OWN_FTRUNCATE
630Sstevel@tonic-gate int __bindcompat_ftruncate;
640Sstevel@tonic-gate #endif
65