1 /*
2 * Copyright (c) 2024, Tenstorrent AI ULC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stddef.h>
8 #include <stdio.h>
9 #include <stdint.h>
10
11 #include <zephyr/posix/fcntl.h>
12 #include <zephyr/posix/poll.h>
13 #include <zephyr/posix/unistd.h>
14 #include <zephyr/posix/sys/select.h>
15
16 /* prototypes for external, not-yet-public, functions in fdtable.c or fs.c */
17 int zvfs_close(int fd);
18 FILE *zvfs_fdopen(int fd, const char *mode);
19 int zvfs_fileno(FILE *file);
20 int zvfs_open(const char *name, int flags, int mode);
21 ssize_t zvfs_read(int fd, void *buf, size_t sz, size_t *from_offset);
22 ssize_t zvfs_write(int fd, const void *buf, size_t sz, size_t *from_offset);
23
FD_CLR(int fd,struct zvfs_fd_set * fdset)24 void FD_CLR(int fd, struct zvfs_fd_set *fdset)
25 {
26 return ZVFS_FD_CLR(fd, fdset);
27 }
28
FD_ISSET(int fd,struct zvfs_fd_set * fdset)29 int FD_ISSET(int fd, struct zvfs_fd_set *fdset)
30 {
31 return ZVFS_FD_ISSET(fd, fdset);
32 }
33
FD_SET(int fd,struct zvfs_fd_set * fdset)34 void FD_SET(int fd, struct zvfs_fd_set *fdset)
35 {
36 ZVFS_FD_SET(fd, fdset);
37 }
38
FD_ZERO(fd_set * fdset)39 void FD_ZERO(fd_set *fdset)
40 {
41 ZVFS_FD_ZERO(fdset);
42 }
43
close(int fd)44 int close(int fd)
45 {
46 return zvfs_close(fd);
47 }
48 #ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_CLOSE
49 FUNC_ALIAS(close, _close, int);
50 #endif
51
fdopen(int fd,const char * mode)52 FILE *fdopen(int fd, const char *mode)
53 {
54 return zvfs_fdopen(fd, mode);
55 }
56
fileno(FILE * file)57 int fileno(FILE *file)
58 {
59 return zvfs_fileno(file);
60 }
61
open(const char * name,int flags,...)62 int open(const char *name, int flags, ...)
63 {
64 int mode = 0;
65 va_list args;
66
67 if ((flags & O_CREAT) != 0) {
68 va_start(args, flags);
69 mode = va_arg(args, int);
70 va_end(args);
71 }
72
73 return zvfs_open(name, flags, mode);
74 }
75 #ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_OPEN
76 FUNC_ALIAS(open, _open, int);
77 #endif
78
poll(struct pollfd * fds,int nfds,int timeout)79 int poll(struct pollfd *fds, int nfds, int timeout)
80 {
81 return zvfs_poll(fds, nfds, timeout);
82 }
83
pread(int fd,void * buf,size_t count,off_t offset)84 ssize_t pread(int fd, void *buf, size_t count, off_t offset)
85 {
86 size_t off = (size_t)offset;
87
88 if (offset < 0) {
89 errno = EINVAL;
90 return -1;
91 }
92
93 return zvfs_read(fd, buf, count, (size_t *)&off);
94 }
95
pselect(int nfds,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,const struct timespec * timeout,const void * sigmask)96 int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
97 const struct timespec *timeout, const void *sigmask)
98 {
99 return zvfs_select(nfds, readfds, writefds, exceptfds, timeout, sigmask);
100 }
101
pwrite(int fd,void * buf,size_t count,off_t offset)102 ssize_t pwrite(int fd, void *buf, size_t count, off_t offset)
103 {
104 size_t off = (size_t)offset;
105
106 if (offset < 0) {
107 errno = EINVAL;
108 return -1;
109 }
110
111 return zvfs_write(fd, buf, count, (size_t *)&off);
112 }
113
read(int fd,void * buf,size_t sz)114 ssize_t read(int fd, void *buf, size_t sz)
115 {
116 return zvfs_read(fd, buf, sz, NULL);
117 }
118 #ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_READ
119 FUNC_ALIAS(read, _read, ssize_t);
120 #endif
121
select(int nfds,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout)122 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
123 {
124 struct timespec to = {
125 .tv_sec = (timeout == NULL) ? 0 : timeout->tv_sec,
126 .tv_nsec = (long)((timeout == NULL) ? 0 : timeout->tv_usec * NSEC_PER_USEC)};
127
128 return zvfs_select(nfds, readfds, writefds, exceptfds, (timeout == NULL) ? NULL : &to,
129 NULL);
130 }
131
write(int fd,const void * buf,size_t sz)132 ssize_t write(int fd, const void *buf, size_t sz)
133 {
134 return zvfs_write(fd, buf, sz, NULL);
135 }
136 #ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_WRITE
137 FUNC_ALIAS(write, _write, ssize_t);
138 #endif
139