Mercurial > hg > octave-shane > gnulib-hg
annotate lib/ftruncate.c @ 8199:51d32a83a7df
Move more declarations into <unistd.h>.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Mon, 19 Feb 2007 02:23:32 +0000 |
parents | 8a1a9361108c |
children | 717accda417a |
rev | line source |
---|---|
5 | 1 /* ftruncate emulations that work on some System V's. |
291 | 2 This file is in the public domain. */ |
3 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
4 #include <config.h> |
5 | 5 |
8199
51d32a83a7df
Move more declarations into <unistd.h>.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
6 /* Specification. */ |
51d32a83a7df
Move more declarations into <unistd.h>.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
7 #include <unistd.h> |
51d32a83a7df
Move more declarations into <unistd.h>.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
8 |
5 | 9 #include <sys/types.h> |
10 #include <fcntl.h> | |
11 | |
12 #ifdef F_CHSIZE | |
291 | 13 |
5 | 14 int |
1557 | 15 ftruncate (int fd, off_t length) |
5 | 16 { |
17 return fcntl (fd, F_CHSIZE, length); | |
18 } | |
291 | 19 |
20 #else /* not F_CHSIZE */ | |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
21 # ifdef F_FREESP |
291 | 22 |
23 /* By William Kucharski <kucharsk@netcom.com>. */ | |
5 | 24 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
25 # include <sys/stat.h> |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
26 # include <errno.h> |
5 | 27 |
28 int | |
1557 | 29 ftruncate (int fd, off_t length) |
5 | 30 { |
31 struct flock fl; | |
32 struct stat filebuf; | |
33 | |
34 if (fstat (fd, &filebuf) < 0) | |
35 return -1; | |
36 | |
37 if (filebuf.st_size < length) | |
38 { | |
39 /* Extend file length. */ | |
40 if (lseek (fd, (length - 1), SEEK_SET) < 0) | |
41 return -1; | |
42 | |
43 /* Write a "0" byte. */ | |
44 if (write (fd, "", 1) != 1) | |
45 return -1; | |
46 } | |
47 else | |
48 { | |
291 | 49 |
5 | 50 /* Truncate length. */ |
291 | 51 |
5 | 52 fl.l_whence = 0; |
53 fl.l_len = 0; | |
54 fl.l_start = length; | |
291 | 55 fl.l_type = F_WRLCK; /* write lock on file space */ |
5 | 56 |
291 | 57 /* This relies on the *undocumented* F_FREESP argument to fcntl, |
58 which truncates the file so that it ends at the position | |
59 indicated by fl.l_start. Will minor miracles never cease? */ | |
60 | |
5 | 61 if (fcntl (fd, F_FREESP, &fl) < 0) |
62 return -1; | |
63 } | |
64 | |
65 return 0; | |
66 } | |
291 | 67 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
68 # else /* not F_CHSIZE nor F_FREESP */ |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
69 # if HAVE_CHSIZE |
291 | 70 |
5 | 71 int |
1557 | 72 ftruncate (int fd, off_t length) |
5 | 73 { |
74 return chsize (fd, length); | |
75 } | |
291 | 76 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
77 # else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */ |
291 | 78 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
79 # include <errno.h> |
293 | 80 |
291 | 81 int |
1557 | 82 ftruncate (int fd, off_t length) |
291 | 83 { |
84 errno = EIO; | |
85 return -1; | |
86 } | |
87 | |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
88 # endif /* not HAVE_CHSIZE */ |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
89 # endif /* not F_FREESP */ |
291 | 90 #endif /* not F_CHSIZE */ |