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