1 /* ftruncate emulations that work on some System V's.
2 This file is in the public domain. */
12 extern int ftruncate (int, off_t
);
17 ftruncate (int fd
, off_t length
)
19 return fcntl (fd
, F_CHSIZE
, length
);
22 #else /* not F_CHSIZE */
25 /* By William Kucharski <kucharsk@netcom.com>. */
27 # include <sys/stat.h>
31 ftruncate (int fd
, off_t length
)
36 if (fstat (fd
, &filebuf
) < 0)
39 if (filebuf
.st_size
< length
)
41 /* Extend file length. */
42 if (lseek (fd
, (length
- 1), SEEK_SET
) < 0)
45 /* Write a "0" byte. */
46 if (write (fd
, "", 1) != 1)
52 /* Truncate length. */
57 fl
.l_type
= F_WRLCK
; /* write lock on file space */
59 /* This relies on the *undocumented* F_FREESP argument to fcntl,
60 which truncates the file so that it ends at the position
61 indicated by fl.l_start. Will minor miracles never cease? */
63 if (fcntl (fd
, F_FREESP
, &fl
) < 0)
70 # else /* not F_CHSIZE nor F_FREESP */
71 # if HAVE_CHSIZE /* native Windows, e.g. mingw */
74 ftruncate (int fd
, off_t length
)
76 return chsize (fd
, length
);
79 # else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
84 ftruncate (int fd
, off_t length
)
90 # endif /* not HAVE_CHSIZE */
91 # endif /* not F_FREESP */
92 #endif /* not F_CHSIZE */