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