1 /* ftruncate emulations that work on some System V's.
2 This file is in the public domain. */
5 #if defined (CONFIG_BROKETS)
6 /* We use <config.h> instead of "config.h" so that a compilation
7 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
8 (which it would do because it found this file in $srcdir). */
15 #include <sys/types.h>
20 ftruncate (fd, length)
24 return fcntl (fd, F_CHSIZE, length);
28 /* The following function was written by
29 kucharsk@Solbourne.com (William Kucharski) */
36 ftruncate (fd, length)
43 if (fstat (fd, &filebuf) < 0)
46 if (filebuf.st_size < length)
48 /* Extend file length. */
49 if (lseek (fd, (length - 1), SEEK_SET) < 0)
52 /* Write a "0" byte. */
53 if (write (fd, "", 1) != 1)
58 /* Truncate length. */
62 fl.l_type = F_WRLCK; /* Write lock on file space. */
64 /* This relies on the UNDOCUMENTED F_FREESP argument to
65 fcntl, which truncates the file so that it ends at the
66 position indicated by fl.l_start.
67 Will minor miracles never cease? */
68 if (fcntl (fd, F_FREESP, &fl) < 0)
76 ftruncate (fd, length)
80 return chsize (fd, length);