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>
26 ftruncate (fd, length)
30 return fcntl (fd, F_CHSIZE, length);
33 #else /* not F_CHSIZE */
36 /* By William Kucharski <kucharsk@netcom.com>. */
45 ftruncate (fd, length)
52 if (fstat (fd, &filebuf) < 0)
55 if (filebuf.st_size < length)
57 /* Extend file length. */
58 if (lseek (fd, (length - 1), SEEK_SET) < 0)
61 /* Write a "0" byte. */
62 if (write (fd, "", 1) != 1)
68 /* Truncate length. */
73 fl.l_type = F_WRLCK; /* write lock on file space */
75 /* This relies on the *undocumented* F_FREESP argument to fcntl,
76 which truncates the file so that it ends at the position
77 indicated by fl.l_start. Will minor miracles never cease? */
79 if (fcntl (fd, F_FREESP, &fl) < 0)
86 #else /* not F_CHSIZE nor F_FREESP */
90 ftruncate (fd, length)
94 return chsize (fd, length);
97 #else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
100 ftruncate (fd, length)
108 #endif /* not HAVE_CHSIZE */
109 #endif /* not F_FREESP */
110 #endif /* not F_CHSIZE */