]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
feat(ulibc): add missing fdopen() and fileno() to stdio
authorTulio A M Mendes <[email protected]>
Fri, 3 Apr 2026 19:53:14 +0000 (16:53 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 3 Apr 2026 19:53:14 +0000 (16:53 -0300)
Add POSIX fdopen() and fileno() implementations required by
popen/pclose and other stdio operations:
- fdopen(): wraps an existing file descriptor into a FILE*
- fileno(): returns the file descriptor from a FILE*

user/ulibc/src/stdio.c

index 1a3e030b700e137da214d86ef5b2ca4dff94f5b8..257ed12ae696c69003f1118116de45e441d240a9 100644 (file)
@@ -518,6 +518,25 @@ ssize_t getline(char** lineptr, size_t* n, FILE* stream) {
     return getdelim(lineptr, n, '\n', stream);
 }
 
+FILE* fdopen(int fd, const char* mode) {
+    if (fd < 0 || !mode) return (FILE*)0;
+    FILE* fp = (FILE*)malloc(sizeof(FILE));
+    if (!fp) return (FILE*)0;
+    fp->fd = fd;
+    fp->flags = 0;
+    fp->buf_pos = 0;
+    fp->buf_len = 0;
+    if (mode[0] == 'r') fp->flags |= _STDIO_READ;
+    if (mode[0] == 'w' || mode[0] == 'a') fp->flags |= _STDIO_WRITE;
+    if (mode[1] == '+') fp->flags |= _STDIO_READ | _STDIO_WRITE;
+    return fp;
+}
+
+int fileno(FILE* fp) {
+    if (!fp) return -1;
+    return fp->fd;
+}
+
 FILE* popen(const char* command, const char* type) {
     int fds[2];
     extern int pipe(int[2]);