Mercurial > hg > octave-kai > gnulib-hg
annotate lib/ftruncate.c @ 2886:dc65428d47a6
*** empty log message ***
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Tue, 17 Oct 2000 06:45:57 +0000 |
parents | 38fd8f5d359d |
children | a535859efd14 |
rev | line source |
---|---|
5 | 1 /* ftruncate emulations that work on some System V's. |
291 | 2 This file is in the public domain. */ |
3 | |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
4 #if HAVE_CONFIG_H |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
5 # include <config.h> |
291 | 6 #endif |
5 | 7 |
8 #include <sys/types.h> | |
9 #include <fcntl.h> | |
10 | |
11 #ifdef F_CHSIZE | |
291 | 12 |
5 | 13 int |
1557 | 14 ftruncate (int fd, off_t length) |
5 | 15 { |
16 return fcntl (fd, F_CHSIZE, length); | |
17 } | |
291 | 18 |
19 #else /* not F_CHSIZE */ | |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
20 # ifdef F_FREESP |
291 | 21 |
22 /* By William Kucharski <kucharsk@netcom.com>. */ | |
5 | 23 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
24 # include <sys/stat.h> |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
25 # include <errno.h> |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
26 # if HAVE_UNISTD_H |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
27 # include <unistd.h> |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
28 # endif |
5 | 29 |
30 int | |
1557 | 31 ftruncate (int fd, off_t length) |
5 | 32 { |
33 struct flock fl; | |
34 struct stat filebuf; | |
35 | |
36 if (fstat (fd, &filebuf) < 0) | |
37 return -1; | |
38 | |
39 if (filebuf.st_size < length) | |
40 { | |
41 /* Extend file length. */ | |
42 if (lseek (fd, (length - 1), SEEK_SET) < 0) | |
43 return -1; | |
44 | |
45 /* Write a "0" byte. */ | |
46 if (write (fd, "", 1) != 1) | |
47 return -1; | |
48 } | |
49 else | |
50 { | |
291 | 51 |
5 | 52 /* Truncate length. */ |
291 | 53 |
5 | 54 fl.l_whence = 0; |
55 fl.l_len = 0; | |
56 fl.l_start = length; | |
291 | 57 fl.l_type = F_WRLCK; /* write lock on file space */ |
5 | 58 |
291 | 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? */ | |
62 | |
5 | 63 if (fcntl (fd, F_FREESP, &fl) < 0) |
64 return -1; | |
65 } | |
66 | |
67 return 0; | |
68 } | |
291 | 69 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
70 # else /* not F_CHSIZE nor F_FREESP */ |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
71 # if HAVE_CHSIZE |
291 | 72 |
5 | 73 int |
1557 | 74 ftruncate (int fd, off_t length) |
5 | 75 { |
76 return chsize (fd, length); | |
77 } | |
291 | 78 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
79 # else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */ |
291 | 80 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
81 # include <errno.h> |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
82 # ifndef errno |
293 | 83 extern int errno; |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
84 # endif |
293 | 85 |
291 | 86 int |
1557 | 87 ftruncate (int fd, off_t length) |
291 | 88 { |
89 errno = EIO; | |
90 return -1; | |
91 } | |
92 | |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
93 # endif /* not HAVE_CHSIZE */ |
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
94 # endif /* not F_FREESP */ |
291 | 95 #endif /* not F_CHSIZE */ |