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>. */
40 ftruncate (fd, length)
47 if (fstat (fd, &filebuf) < 0)
50 if (filebuf.st_size < length)
52 /* Extend file length. */
53 if (lseek (fd, (length - 1), SEEK_SET) < 0)
56 /* Write a "0" byte. */
57 if (write (fd, "", 1) != 1)
63 /* Truncate length. */
68 fl.l_type = F_WRLCK; /* write lock on file space */
70 /* This relies on the *undocumented* F_FREESP argument to fcntl,
71 which truncates the file so that it ends at the position
72 indicated by fl.l_start. Will minor miracles never cease? */
74 if (fcntl (fd, F_FREESP, &fl) < 0)
81 #else /* not F_CHSIZE nor F_FREESP */
85 ftruncate (fd, length)
89 return chsize (fd, length);
92 #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 */