From 48dff5562cc62e2bdeb048ed777321cc8a42df2d Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 15 Jun 2024 12:05:48 -0400 Subject: [PATCH] posix: device_io: implement fileno() Implement fileno() as required by the POSIX_DEVICE_IO Option Group. Signed-off-by: Chris Friedt --- lib/os/fdtable.c | 10 ++++++++++ lib/posix/options/device_io.c | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/lib/os/fdtable.c b/lib/os/fdtable.c index acf1cf75a92..0c9131d7c39 100644 --- a/lib/os/fdtable.c +++ b/lib/os/fdtable.c @@ -414,6 +414,16 @@ FILE *zvfs_fdopen(int fd, const char *mode) return (FILE *)&fdtable[fd]; } +int zvfs_fileno(FILE *file) +{ + if (!IS_ARRAY_ELEMENT(fdtable, file)) { + errno = EBADF; + return -1; + } + + return (struct fd_entry *)file - fdtable; +} + int zvfs_fstat(int fd, struct stat *buf) { if (_check_fd(fd) < 0) { diff --git a/lib/posix/options/device_io.c b/lib/posix/options/device_io.c index 0de6293ca84..a504545ba03 100644 --- a/lib/posix/options/device_io.c +++ b/lib/posix/options/device_io.c @@ -15,6 +15,7 @@ /* prototypes for external, not-yet-public, functions in fdtable.c or fs.c */ int zvfs_close(int fd); FILE *zvfs_fdopen(int fd, const char *mode); +int zvfs_fileno(FILE *file); int zvfs_open(const char *name, int flags); ssize_t zvfs_read(int fd, void *buf, size_t sz, size_t *from_offset); ssize_t zvfs_write(int fd, const void *buf, size_t sz, size_t *from_offset); @@ -52,6 +53,11 @@ FILE *fdopen(int fd, const char *mode) return zvfs_fdopen(fd, mode); } +int fileno(FILE *file) +{ + return zvfs_fileno(file); +} + int open(const char *name, int flags, ...) { /* FIXME: necessarily need to check for O_CREAT and unpack ... if set */