#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <fcntl.h>
#ifdef F_CHSIZE
int
ftruncate (int fd, off_t length)
{
return fcntl (fd, F_CHSIZE, length);
}
#else
# ifdef F_FREESP
# include <sys/stat.h>
# include <errno.h>
# if HAVE_UNISTD_H
# include <unistd.h>
# endif
int
ftruncate (int fd, off_t length)
{
struct flock fl;
struct stat filebuf;
if (fstat (fd, &filebuf) < 0)
return -1;
if (filebuf.st_size < length)
{
if (lseek (fd, (length - 1), SEEK_SET) < 0)
return -1;
if (write (fd, "", 1) != 1)
return -1;
}
else
{
fl.l_whence = 0;
fl.l_len = 0;
fl.l_start = length;
fl.l_type = F_WRLCK;
if (fcntl (fd, F_FREESP, &fl) < 0)
return -1;
}
return 0;
}
# else
# if HAVE_CHSIZE
int
ftruncate (int fd, off_t length)
{
return chsize (fd, length);
}
# else
# include <errno.h>
# ifndef errno
extern int errno;
# endif
int
ftruncate (int fd, off_t length)
{
errno = EIO;
return -1;
}
# endif
# endif
#endif