1 /* ftruncate emulations that work on some System V's.
2 This file is in the public domain. */
12 ftruncate (int fd, off_t length)
14 return fcntl (fd, F_CHSIZE, length);
17 #else /* not F_CHSIZE */
20 /* By William Kucharski <kucharsk@netcom.com>. */
22 # include <sys/stat.h>
27 ftruncate (int fd, off_t length)
32 if (fstat (fd, &filebuf) < 0)
35 if (filebuf.st_size < length)
37 /* Extend file length. */
38 if (lseek (fd, (length - 1), SEEK_SET) < 0)
41 /* Write a "0" byte. */
42 if (write (fd, "", 1) != 1)
48 /* Truncate length. */
53 fl.l_type = F_WRLCK; /* write lock on file space */
55 /* This relies on the *undocumented* F_FREESP argument to fcntl,
56 which truncates the file so that it ends at the position
57 indicated by fl.l_start. Will minor miracles never cease? */
59 if (fcntl (fd, F_FREESP, &fl) < 0)
66 # else /* not F_CHSIZE nor F_FREESP */
70 ftruncate (int fd, off_t length)
72 return chsize (fd, length);
75 # else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
80 ftruncate (int fd, off_t length)
86 # endif /* not HAVE_CHSIZE */
87 # endif /* not F_FREESP */
88 #endif /* not F_CHSIZE */