1 /* ftruncate emulations that work on some System V's.
2 This file is in the public domain. */
14 ftruncate (fd, length)
18 return fcntl (fd, F_CHSIZE, length);
21 #else /* not F_CHSIZE */
24 /* By William Kucharski <kucharsk@netcom.com>. */
26 # include <sys/stat.h>
33 ftruncate (fd, length)
40 if (fstat (fd, &filebuf) < 0)
43 if (filebuf.st_size < length)
45 /* Extend file length. */
46 if (lseek (fd, (length - 1), SEEK_SET) < 0)
49 /* Write a "0" byte. */
50 if (write (fd, "", 1) != 1)
56 /* Truncate length. */
61 fl.l_type = F_WRLCK; /* write lock on file space */
63 /* This relies on the *undocumented* F_FREESP argument to fcntl,
64 which truncates the file so that it ends at the position
65 indicated by fl.l_start. Will minor miracles never cease? */
67 if (fcntl (fd, F_FREESP, &fl) < 0)
74 # else /* not F_CHSIZE nor F_FREESP */
78 ftruncate (fd, length)
82 return chsize (fd, length);
85 # else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
93 ftruncate (fd, length)
101 # endif /* not HAVE_CHSIZE */
102 # endif /* not F_FREESP */
103 #endif /* not F_CHSIZE */