xref: /haiku/src/system/libroot/posix/unistd/write.c (revision e6b30aee0fd7a23d6a6baab9f3718945a0cd838a)
1 /*
2  * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2001, Manuel J. Petit. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 
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 ssize_t
24 write(int fd, void const *buffer, size_t bufferSize)
25 {
26 	int status = _kern_write(fd, -1, buffer, bufferSize);
27 
28 	RETURN_AND_SET_ERRNO(status);
29 }
30 
31 
32 ssize_t
33 write_pos(int fd, off_t pos, const void *buffer, size_t bufferSize)
34 {
35 	ssize_t status;
36 	if (pos < 0) {
37 		errno = B_BAD_VALUE;
38 		return -1;
39 	}
40 	status = _kern_write(fd, pos, buffer, bufferSize);
41 
42 	RETURN_AND_SET_ERRNO(status);
43 }
44 
45 
46 ssize_t
47 pwrite(int fd, const void *buffer, size_t bufferSize, off_t pos)
48 {
49 	ssize_t status;
50 	if (pos < 0) {
51 		errno = B_BAD_VALUE;
52 		return -1;
53 	}
54 	status = _kern_write(fd, pos, buffer, bufferSize);
55 
56 	RETURN_AND_SET_ERRNO(status);
57 }
58