#ifndef LINT
static const char rcsid[] = "$Id: ftruncate.c,v 1.1.352.3 2005/06/22 22:05:45 marka Exp $";
#endif
#include "port_before.h"
#if defined(M_UNIX)
#define OWN_FTRUNCATE
#include <stdio.h>
#ifdef _XOPEN_SOURCE
#undef _XOPEN_SOURCE
#endif
#ifdef _POSIX_SOURCE
#undef _POSIX_SOURCE
#endif
#include <fcntl.h>
#include "port_after.h"
int
__ftruncate(int fd, long wantsize) {
long cursize;
if ((cursize = lseek(fd, 0L, 2)) == -1)
return (-1);
if (cursize < wantsize) {
if (lseek(fd, wantsize - 1, 0) == -1 ||
write(fd, "", 1) == -1) {
return (-1);
}
return (0);
}
if (wantsize < cursize) {
struct flock fl;
fl.l_whence = 0;
fl.l_len = 0;
fl.l_start = wantsize;
fl.l_type = F_WRLCK;
return (fcntl(fd, F_FREESP, &fl));
}
return (0);
}
#endif
#ifndef OWN_FTRUNCATE
int __bindcompat_ftruncate;
#endif