daily update
[binutils.git] / gold / ftruncate.c
blobfc0bbc898f58c0d5aac258bb867c0c4a232b0f98
1 /* ftruncate emulations that work on some System V's.
2 This file is in the public domain. */
4 #include <config.h>
6 /* Specification. */
7 #include <unistd.h>
9 #include <sys/types.h>
10 #include <fcntl.h>
12 extern int ftruncate (int, off_t);
14 #ifdef F_CHSIZE
16 int
17 ftruncate (int fd, off_t length)
19 return fcntl (fd, F_CHSIZE, length);
22 #else /* not F_CHSIZE */
23 # ifdef F_FREESP
25 /* By William Kucharski <kucharsk@netcom.com>. */
27 # include <sys/stat.h>
28 # include <errno.h>
30 int
31 ftruncate (int fd, off_t length)
33 struct flock fl;
34 struct stat filebuf;
36 if (fstat (fd, &filebuf) < 0)
37 return -1;
39 if (filebuf.st_size < length)
41 /* Extend file length. */
42 if (lseek (fd, (length - 1), SEEK_SET) < 0)
43 return -1;
45 /* Write a "0" byte. */
46 if (write (fd, "", 1) != 1)
47 return -1;
49 else
52 /* Truncate length. */
54 fl.l_whence = 0;
55 fl.l_len = 0;
56 fl.l_start = length;
57 fl.l_type = F_WRLCK; /* write lock on file space */
59 /* This relies on the *undocumented* F_FREESP argument to fcntl,
60 which truncates the file so that it ends at the position
61 indicated by fl.l_start. Will minor miracles never cease? */
63 if (fcntl (fd, F_FREESP, &fl) < 0)
64 return -1;
67 return 0;
70 # else /* not F_CHSIZE nor F_FREESP */
71 # if HAVE_CHSIZE /* native Windows, e.g. mingw */
73 int
74 ftruncate (int fd, off_t length)
76 return chsize (fd, length);
79 # else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
81 # include <errno.h>
83 int
84 ftruncate (int fd, off_t length)
86 errno = EIO;
87 return -1;
90 # endif /* not HAVE_CHSIZE */
91 # endif /* not F_FREESP */
92 #endif /* not F_CHSIZE */