Last change
on this file since 3745 was 3745, checked in by mdewsnip, 21 years ago |
Addition of MG package for search and retrieval
|
-
Property svn:executable
set to
*
-
Property svn:keywords
set to
Author Date Id Revision
|
File size:
1.8 KB
|
Line | |
---|
1 | /* ftruncate emulations that work on some System V's.
|
---|
2 | This file is in the public domain. */
|
---|
3 |
|
---|
4 | #ifdef HAVE_CONFIG_H
|
---|
5 | # ifdef __WIN32__ /* [RPAP - Feb 97: WIN32 Port] */
|
---|
6 | # include <win32cfg.h>
|
---|
7 | # else
|
---|
8 | # include <sysfuncs.h>
|
---|
9 | # endif
|
---|
10 | #endif
|
---|
11 |
|
---|
12 | #include <sys/types.h>
|
---|
13 | #include <fcntl.h>
|
---|
14 |
|
---|
15 | #ifdef F_CHSIZE
|
---|
16 |
|
---|
17 | int
|
---|
18 | ftruncate (fd, length)
|
---|
19 | int fd;
|
---|
20 | off_t length;
|
---|
21 | {
|
---|
22 | return fcntl (fd, F_CHSIZE, length);
|
---|
23 | }
|
---|
24 |
|
---|
25 | #else /* not F_CHSIZE */
|
---|
26 | #ifdef F_FREESP
|
---|
27 |
|
---|
28 | /* By William Kucharski <[email protected]>. */
|
---|
29 |
|
---|
30 | #include <sys/stat.h>
|
---|
31 | #include <errno.h>
|
---|
32 | #ifdef HAVE_UNISTD_H
|
---|
33 | #include <unistd.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | int
|
---|
37 | ftruncate (fd, length)
|
---|
38 | int fd;
|
---|
39 | off_t length;
|
---|
40 | {
|
---|
41 | struct flock fl;
|
---|
42 | struct stat filebuf;
|
---|
43 |
|
---|
44 | if (fstat (fd, &filebuf) < 0)
|
---|
45 | return -1;
|
---|
46 |
|
---|
47 | if (filebuf.st_size < length)
|
---|
48 | {
|
---|
49 | /* Extend file length. */
|
---|
50 | if (lseek (fd, (length - 1), SEEK_SET) < 0)
|
---|
51 | return -1;
|
---|
52 |
|
---|
53 | /* Write a "0" byte. */
|
---|
54 | if (write (fd, "", 1) != 1)
|
---|
55 | return -1;
|
---|
56 | }
|
---|
57 | else
|
---|
58 | {
|
---|
59 |
|
---|
60 | /* Truncate length. */
|
---|
61 |
|
---|
62 | fl.l_whence = 0;
|
---|
63 | fl.l_len = 0;
|
---|
64 | fl.l_start = length;
|
---|
65 | fl.l_type = F_WRLCK; /* write lock on file space */
|
---|
66 |
|
---|
67 | /* This relies on the *undocumented* F_FREESP argument to fcntl,
|
---|
68 | which truncates the file so that it ends at the position
|
---|
69 | indicated by fl.l_start. Will minor miracles never cease? */
|
---|
70 |
|
---|
71 | if (fcntl (fd, F_FREESP, &fl) < 0)
|
---|
72 | return -1;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | #else /* not F_CHSIZE nor F_FREESP */
|
---|
79 | #ifdef HAVE_CHSIZE
|
---|
80 |
|
---|
81 | int
|
---|
82 | ftruncate (fd, length)
|
---|
83 | int fd;
|
---|
84 | off_t length;
|
---|
85 | {
|
---|
86 | return chsize (fd, length);
|
---|
87 | }
|
---|
88 |
|
---|
89 | #else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */
|
---|
90 |
|
---|
91 | #include <errno.h>
|
---|
92 | #ifndef errno
|
---|
93 | extern int errno;
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | int
|
---|
97 | ftruncate (fd, length)
|
---|
98 | int fd;
|
---|
99 | off_t length;
|
---|
100 | {
|
---|
101 | errno = EIO;
|
---|
102 | return -1;
|
---|
103 | }
|
---|
104 |
|
---|
105 | #endif /* not HAVE_CHSIZE */
|
---|
106 | #endif /* not F_FREESP */
|
---|
107 | #endif /* not F_CHSIZE */
|
---|
Note:
See
TracBrowser
for help on using the repository browser.