xref: /haiku/src/system/libroot/posix/unistd/truncate.c (revision 24159a0c7d6d6dcba9f2a0c1a7c08d2c8167f21b)
1 /*
2 ** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the Haiku License.
4 */
5 
6 
7 #include <fs_interface.h>
8 
9 #include <unistd.h>
10 #include <syscalls.h>
11 #include <errno.h>
12 
13 
14 #define RETURN_AND_SET_ERRNO(err) \
15 	if (err < 0) { \
16 		errno = err; \
17 		return -1; \
18 	} \
19 	return err;
20 
21 
22 int
23 truncate(const char *path, off_t newSize)
24 {
25 	struct stat stat;
26 	status_t status;
27 
28 	stat.st_size = newSize;
29 	status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
30 		FS_WRITE_STAT_SIZE);
31 
32 	RETURN_AND_SET_ERRNO(status);
33 }
34 
35 
36 int
37 ftruncate(int fd, off_t newSize)
38 {
39 	struct stat stat;
40 	status_t status;
41 
42 	stat.st_size = newSize;
43 	status = _kern_write_stat(fd, NULL, false, &stat, sizeof(struct stat),
44 		FS_WRITE_STAT_SIZE);
45 
46 	RETURN_AND_SET_ERRNO(status);
47 }
48 
49