Merge from vendor branch OPENPAM:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / bsd / ftruncate.c
1 #ifndef LINT
2 static const char rcsid[] = "$Id: ftruncate.c,v 1.1 2001/03/29 06:30:32 marka Exp $";
3 #endif
4
5 /*
6  * ftruncate - set file size, BSD Style
7  *
8  * shortens or enlarges the file as neeeded
9  * uses some undocumented locking call. It is known to work on SCO unix,
10  * other vendors should try.
11  * The #error directive prevents unsupported OSes
12  */
13
14 #include "port_before.h"
15
16 #if defined(M_UNIX)
17 #define OWN_FTRUNCATE
18 #include <stdio.h>
19 #ifdef _XOPEN_SOURCE
20 #undef _XOPEN_SOURCE
21 #endif
22 #ifdef _POSIX_SOURCE
23 #undef _POSIX_SOURCE
24 #endif
25
26 #include <fcntl.h>
27
28 #include "port_after.h"
29
30 int
31 __ftruncate(int fd, long wantsize) {
32         long cursize;
33
34         /* determine current file size */
35         if ((cursize = lseek(fd, 0L, 2)) == -1)
36                 return (-1);
37
38         /* maybe lengthen... */
39         if (cursize < wantsize) {
40                 if (lseek(fd, wantsize - 1, 0) == -1 ||
41                     write(fd, "", 1) == -1) {
42                         return (-1);
43                 }
44                 return (0);
45         }
46
47         /* maybe shorten... */
48         if (wantsize < cursize) {
49                 struct flock fl;
50
51                 fl.l_whence = 0;
52                 fl.l_len = 0;
53                 fl.l_start = wantsize;
54                 fl.l_type = F_WRLCK;
55                 return (fcntl(fd, F_FREESP, &fl));
56         }
57         return (0);
58 }
59 #endif
60
61 #ifndef OWN_FTRUNCATE
62 int __bindcompat_ftruncate;
63 #endif