1 /* ftruncate emulations that work on some System V's.
2 This file is in the public domain. */
14 ftruncate (int fd
, off_t length
)
16 return fcntl (fd
, F_CHSIZE
, length
);
19 #else /* not F_CHSIZE */
22 /* By William Kucharski <kucharsk@netcom.com>. */
24 # 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 */
74 ftruncate (int fd
, off_t length
)
76 return chsize (fd
, length
);
79 # else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
87 ftruncate (int fd
, off_t length
)
93 # endif /* not HAVE_CHSIZE */
94 # endif /* not F_FREESP */
95 #endif /* not F_CHSIZE */