xref: /freebsd/contrib/libfido2/openbsd-compat/posix_win.c (revision 0afa8e065e14bb8fd338d75690e0238c00167d40)
1*0afa8e06SEd Maste /*
2*0afa8e06SEd Maste  * Public domain
3*0afa8e06SEd Maste  *
4*0afa8e06SEd Maste  * File IO compatibility shims
5*0afa8e06SEd Maste  * Brent Cook <bcook@openbsd.org>
6*0afa8e06SEd Maste  */
7*0afa8e06SEd Maste 
8*0afa8e06SEd Maste #define NO_REDEF_POSIX_FUNCTIONS
9*0afa8e06SEd Maste 
10*0afa8e06SEd Maste #include <windows.h>
11*0afa8e06SEd Maste 
12*0afa8e06SEd Maste #include <errno.h>
13*0afa8e06SEd Maste #include <io.h>
14*0afa8e06SEd Maste 
15*0afa8e06SEd Maste #include "posix_win.h"
16*0afa8e06SEd Maste 
17*0afa8e06SEd Maste int
posix_open(const char * path,...)18*0afa8e06SEd Maste posix_open(const char *path, ...)
19*0afa8e06SEd Maste {
20*0afa8e06SEd Maste 	va_list ap;
21*0afa8e06SEd Maste 	int mode = 0;
22*0afa8e06SEd Maste 	int flags;
23*0afa8e06SEd Maste 
24*0afa8e06SEd Maste 	va_start(ap, path);
25*0afa8e06SEd Maste 	flags = va_arg(ap, int);
26*0afa8e06SEd Maste 	if (flags & O_CREAT)
27*0afa8e06SEd Maste 		mode = va_arg(ap, int);
28*0afa8e06SEd Maste 	va_end(ap);
29*0afa8e06SEd Maste 
30*0afa8e06SEd Maste 	flags |= O_BINARY | O_NOINHERIT;
31*0afa8e06SEd Maste 
32*0afa8e06SEd Maste 	return (open(path, flags, mode));
33*0afa8e06SEd Maste }
34*0afa8e06SEd Maste 
35*0afa8e06SEd Maste int
posix_close(int fd)36*0afa8e06SEd Maste posix_close(int fd)
37*0afa8e06SEd Maste {
38*0afa8e06SEd Maste 	return (close(fd));
39*0afa8e06SEd Maste }
40*0afa8e06SEd Maste 
41*0afa8e06SEd Maste ssize_t
posix_read(int fd,void * buf,size_t count)42*0afa8e06SEd Maste posix_read(int fd, void *buf, size_t count)
43*0afa8e06SEd Maste {
44*0afa8e06SEd Maste 	if (count > INT_MAX) {
45*0afa8e06SEd Maste 		errno = EINVAL;
46*0afa8e06SEd Maste 		return (-1);
47*0afa8e06SEd Maste 	}
48*0afa8e06SEd Maste 
49*0afa8e06SEd Maste 	return (read(fd, buf, (unsigned int)count));
50*0afa8e06SEd Maste }
51*0afa8e06SEd Maste 
52*0afa8e06SEd Maste ssize_t
posix_write(int fd,const void * buf,size_t count)53*0afa8e06SEd Maste posix_write(int fd, const void *buf, size_t count)
54*0afa8e06SEd Maste {
55*0afa8e06SEd Maste 	if (count > INT_MAX) {
56*0afa8e06SEd Maste 		errno = EINVAL;
57*0afa8e06SEd Maste 		return (-1);
58*0afa8e06SEd Maste 	}
59*0afa8e06SEd Maste 
60*0afa8e06SEd Maste 	return (write(fd, buf, (unsigned int)count));
61*0afa8e06SEd Maste }
62