1
/* ftruncate emulations that work on some System V's.
2
This file is in the public domain. */
15
ftruncate (int fd, off_t length)
17
return fcntl (fd, F_CHSIZE, length);
20
#else /* not F_CHSIZE */
23
/* By William Kucharski <kucharsk@netcom.com>. */
25
# include <sys/stat.h>
29
ftruncate (int fd, off_t length)
34
if (fstat (fd, &filebuf) < 0)
37
if (filebuf.st_size < length)
39
/* Extend file length. */
40
if (lseek (fd, (length - 1), SEEK_SET) < 0)
43
/* Write a "0" byte. */
44
if (write (fd, "", 1) != 1)
50
/* Truncate length. */
55
fl.l_type = F_WRLCK; /* write lock on file space */
57
/* This relies on the *undocumented* F_FREESP argument to fcntl,
58
which truncates the file so that it ends at the position
59
indicated by fl.l_start. Will minor miracles never cease? */
61
if (fcntl (fd, F_FREESP, &fl) < 0)
68
# else /* not F_CHSIZE nor F_FREESP */
69
# if HAVE_CHSIZE /* native Windows, e.g. mingw */
72
ftruncate (int fd, off_t length)
74
return chsize (fd, length);
77
# else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
82
ftruncate (int fd, off_t length)
88
# endif /* not HAVE_CHSIZE */
89
# endif /* not F_FREESP */
90
#endif /* not F_CHSIZE */