17c478bd9Sstevel@tonic-gate #ifndef LINT
2*9525b14bSRao Shoaib static const char rcsid[] = "$Id: ftruncate.c,v 1.3 2005/04/27 18:16:45 sra Exp $";
37c478bd9Sstevel@tonic-gate #endif
47c478bd9Sstevel@tonic-gate
5*9525b14bSRao Shoaib /*! \file
6*9525b14bSRao Shoaib * \brief
77c478bd9Sstevel@tonic-gate * ftruncate - set file size, BSD Style
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * shortens or enlarges the file as neeeded
107c478bd9Sstevel@tonic-gate * uses some undocumented locking call. It is known to work on SCO unix,
117c478bd9Sstevel@tonic-gate * other vendors should try.
127c478bd9Sstevel@tonic-gate * The #error directive prevents unsupported OSes
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #include "port_before.h"
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate #if defined(M_UNIX)
187c478bd9Sstevel@tonic-gate #define OWN_FTRUNCATE
197c478bd9Sstevel@tonic-gate #include <stdio.h>
207c478bd9Sstevel@tonic-gate #ifdef _XOPEN_SOURCE
217c478bd9Sstevel@tonic-gate #undef _XOPEN_SOURCE
227c478bd9Sstevel@tonic-gate #endif
237c478bd9Sstevel@tonic-gate #ifdef _POSIX_SOURCE
247c478bd9Sstevel@tonic-gate #undef _POSIX_SOURCE
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include "port_after.h"
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate int
__ftruncate(int fd,long wantsize)327c478bd9Sstevel@tonic-gate __ftruncate(int fd, long wantsize) {
337c478bd9Sstevel@tonic-gate long cursize;
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /* determine current file size */
367c478bd9Sstevel@tonic-gate if ((cursize = lseek(fd, 0L, 2)) == -1)
377c478bd9Sstevel@tonic-gate return (-1);
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /* maybe lengthen... */
407c478bd9Sstevel@tonic-gate if (cursize < wantsize) {
417c478bd9Sstevel@tonic-gate if (lseek(fd, wantsize - 1, 0) == -1 ||
427c478bd9Sstevel@tonic-gate write(fd, "", 1) == -1) {
437c478bd9Sstevel@tonic-gate return (-1);
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate return (0);
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate /* maybe shorten... */
497c478bd9Sstevel@tonic-gate if (wantsize < cursize) {
507c478bd9Sstevel@tonic-gate struct flock fl;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate fl.l_whence = 0;
537c478bd9Sstevel@tonic-gate fl.l_len = 0;
547c478bd9Sstevel@tonic-gate fl.l_start = wantsize;
557c478bd9Sstevel@tonic-gate fl.l_type = F_WRLCK;
567c478bd9Sstevel@tonic-gate return (fcntl(fd, F_FREESP, &fl));
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate return (0);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate #ifndef OWN_FTRUNCATE
637c478bd9Sstevel@tonic-gate int __bindcompat_ftruncate;
647c478bd9Sstevel@tonic-gate #endif
65