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>
21 ftruncate (fd, length)
25 return fcntl (fd, F_CHSIZE, length);
28 #else /* not F_CHSIZE */
31 /* By William Kucharski <kucharsk@netcom.com>. */
38 ftruncate (fd, length)
45 if (fstat (fd, &filebuf) < 0)
48 if (filebuf.st_size < length)
50 /* Extend file length. */
51 if (lseek (fd, (length - 1), SEEK_SET) < 0)
54 /* Write a "0" byte. */
55 if (write (fd, "", 1) != 1)
61 /* Truncate length. */
66 fl.l_type = F_WRLCK; /* write lock on file space */
68 /* This relies on the *undocumented* F_FREESP argument to fcntl,
69 which truncates the file so that it ends at the position
70 indicated by fl.l_start. Will minor miracles never cease? */
72 if (fcntl (fd, F_FREESP, &fl) < 0)
79 #else /* not F_CHSIZE nor F_FREESP */
83 ftruncate (fd, length)
87 return chsize (fd, length);
90 #else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
98 ftruncate (fd, length)
106 #endif /* not HAVE_CHSIZE */
107 #endif /* not F_FREESP */
108 #endif /* not F_CHSIZE */