xref: /haiku/src/system/libroot/posix/unistd/truncate.c (revision a085e81e62d7a860f809b4fb7c7bf5654c396985)
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 #include <errno_private.h>
15 
16 
17 #define RETURN_AND_SET_ERRNO(err) \
18 	if (err < 0) { \
19 		__set_errno(err); \
20 		return -1; \
21 	} \
22 	return err;
23 
24 
25 int
26 truncate(const char *path, off_t newSize)
27 {
28 	struct stat stat;
29 	status_t status;
30 
31 	stat.st_size = newSize;
32 	status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
33 		B_STAT_SIZE);
34 
35 	RETURN_AND_SET_ERRNO(status);
36 }
37 
38 
39 int
40 ftruncate(int fd, off_t newSize)
41 {
42 	struct stat stat;
43 	status_t status;
44 
45 	stat.st_size = newSize;
46 	status = _kern_write_stat(fd, NULL, false, &stat, sizeof(struct stat),
47 		B_STAT_SIZE);
48 
49 	RETURN_AND_SET_ERRNO(status);
50 }
51 
52