1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:57:36 +00:00

Add setvbuf(), setlinebuf(), setbuf().

This commit is contained in:
Andreas Kling 2018-11-11 10:11:09 +01:00
parent 7cc4caee4f
commit e48182d91b
6 changed files with 86 additions and 32 deletions

View file

@ -5,27 +5,17 @@
extern "C" int main(int, char**); extern "C" int main(int, char**);
FILE __default_streams[3];
int errno; int errno;
FILE* stdin;
FILE* stdout;
FILE* stderr;
char** environ; char** environ;
extern "C" void __malloc_init(); extern "C" void __malloc_init();
extern "C" void __stdio_init();
extern "C" int _start() extern "C" int _start()
{ {
errno = 0; errno = 0;
memset(__default_streams, 0, sizeof(__default_streams));
__default_streams[0].fd = 0;
stdin = &__default_streams[0];
__default_streams[1].fd = 1;
stdout = &__default_streams[1];
__default_streams[2].fd = 2;
stderr = &__default_streams[2];
__stdio_init();
__malloc_init(); __malloc_init();
StringImpl::initializeGlobals(); StringImpl::initializeGlobals();

View file

@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
#define PATH_MAX 4096 #define PATH_MAX 4096
#define BUFSIZ 1024
#define INT_MAX INT32_MAX #define INT_MAX INT32_MAX
#define INT_MIN INT32_MIN #define INT_MIN INT32_MIN

View file

@ -6,11 +6,70 @@
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <Kernel/Syscall.h> #include <Kernel/Syscall.h>
#include <AK/printf.cpp> #include <AK/printf.cpp>
extern "C" { extern "C" {
static FILE __default_streams[3];
FILE* stdin;
FILE* stdout;
FILE* stderr;
void init_FILE(FILE& fp, int fd, int mode)
{
fp.fd = fd;
fp.buffer = fp.default_buffer;
fp.buffer_size = BUFSIZ;
fp.mode = mode;
}
static FILE* make_FILE(int fd)
{
auto* fp = (FILE*)malloc(sizeof(FILE));
memset(fp, 0, sizeof(FILE));
init_FILE(*fp, fd, isatty(fd));
return fp;
}
void __stdio_init()
{
stdin = &__default_streams[0];
stdout = &__default_streams[1];
stderr = &__default_streams[2];
init_FILE(*stdin, 0, isatty(0) ? _IOLBF : _IOFBF);
init_FILE(*stdout, 1, isatty(1) ? _IOLBF : _IOFBF);
init_FILE(*stderr, 2, _IONBF);
}
int setvbuf(FILE* stream, char* buf, int mode, size_t size)
{
if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF) {
errno = EINVAL;
return -1;
}
stream->mode = mode;
if (buf) {
stream->buffer = buf;
stream->buffer_size = size;
} else {
stream->buffer = stream->default_buffer;
stream->buffer_size = BUFSIZ;
}
return 0;
}
void setbuf(FILE* stream, char* buf)
{
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
}
void setlinebuf(FILE* stream, char* buf)
{
setvbuf(stream, buf, buf ? _IOLBF : _IONBF, BUFSIZ);
}
int fileno(FILE* stream) int fileno(FILE* stream)
{ {
assert(stream); assert(stream);
@ -28,10 +87,10 @@ int fflush(FILE* stream)
// FIXME: Implement buffered streams, duh. // FIXME: Implement buffered streams, duh.
if (!stream) if (!stream)
return -EBADF; return -EBADF;
if (!stream->write_buffer_index) if (!stream->buffer_index)
return 0; return 0;
int rc = write(stream->fd, stream->write_buffer, stream->write_buffer_index); int rc = write(stream->fd, stream->buffer, stream->buffer_index);
stream->write_buffer_index = 0; stream->buffer_index = 0;
return rc; return rc;
} }
@ -75,9 +134,11 @@ int getchar()
int fputc(int ch, FILE* stream) int fputc(int ch, FILE* stream)
{ {
assert(stream); assert(stream);
assert(stream->write_buffer_index < __STDIO_FILE_BUFFER_SIZE); assert(stream->buffer_index < stream->buffer_size);
stream->write_buffer[stream->write_buffer_index++] = ch; stream->buffer[stream->buffer_index++] = ch;
if (ch == '\n' || stream->write_buffer_index >= __STDIO_FILE_BUFFER_SIZE) if (stream->buffer_index >= stream->buffer_size)
fflush(stream);
else if (stream->mode == _IOLBF && ch == '\n')
fflush(stream); fflush(stream);
if (stream->eof) if (stream->eof)
return EOF; return EOF;
@ -218,10 +279,7 @@ FILE* fopen(const char* pathname, const char* mode)
int fd = open(pathname, O_RDONLY); int fd = open(pathname, O_RDONLY);
if (fd < 0) if (fd < 0)
return nullptr; return nullptr;
auto* fp = (FILE*)malloc(sizeof(FILE)); return make_FILE(fd);
memset(fp, 0, sizeof(FILE));
fp->fd = fd;
return fp;
} }
FILE* fdopen(int fd, const char* mode) FILE* fdopen(int fd, const char* mode)
@ -229,10 +287,7 @@ FILE* fdopen(int fd, const char* mode)
assert(!strcmp(mode, "r") || !strcmp(mode, "rb")); assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));
if (fd < 0) if (fd < 0)
return nullptr; return nullptr;
auto* fp = (FILE*)malloc(sizeof(FILE)); return make_FILE(fd);
memset(fp, 0, sizeof(FILE));
fp->fd = fd;
return fp;
} }
int fclose(FILE* stream) int fclose(FILE* stream)

View file

@ -2,6 +2,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <sys/types.h> #include <sys/types.h>
#include <limits.h>
__BEGIN_DECLS __BEGIN_DECLS
#ifndef EOF #ifndef EOF
@ -12,16 +13,19 @@ __BEGIN_DECLS
#define SEEK_CUR 1 #define SEEK_CUR 1
#define SEEK_END 2 #define SEEK_END 2
#define __STDIO_FILE_BUFFER_SIZE 128 #define _IOFBF 0
#define _IOLBF 1
#define _IONBF 2
struct __STDIO_FILE { struct __STDIO_FILE {
int fd; int fd;
int eof; int eof;
int error; int error;
char read_buffer[__STDIO_FILE_BUFFER_SIZE]; int mode;
size_t read_buffer_index; char* buffer;
char write_buffer[__STDIO_FILE_BUFFER_SIZE]; size_t buffer_size;
size_t write_buffer_index; size_t buffer_index;
char default_buffer[BUFSIZ];
}; };
typedef struct __STDIO_FILE FILE; typedef struct __STDIO_FILE FILE;
@ -55,6 +59,9 @@ int fputs(const char*, FILE*);
void perror(const char*); void perror(const char*);
int sscanf (const char* buf, const char* fmt, ...); int sscanf (const char* buf, const char* fmt, ...);
int fscanf(FILE*, const char* fmt, ...); int fscanf(FILE*, const char* fmt, ...);
int setvbuf(FILE*, char* buf, int mode, size_t);
void setbuf(FILE*, char* buf);
void setlinebuf(FILE*, char* buf);
__END_DECLS __END_DECLS

View file

@ -212,7 +212,7 @@ int isatty(int fd)
int getdtablesize() int getdtablesize()
{ {
int rc = Syscall::invoke(Syscall::SC_getdtablesize); int rc = Syscall::invoke(Syscall::SC_getdtablesize);
__RETURN_WITH_ERRNO(rc, 1, 0); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
int dup(int old_fd) int dup(int old_fd)

View file

@ -50,6 +50,7 @@ int dup2(int old_fd, int new_fd);
int pipe(int pipefd[2]); int pipe(int pipefd[2]);
unsigned int alarm(unsigned int seconds); unsigned int alarm(unsigned int seconds);
int access(const char* pathname, int mode); int access(const char* pathname, int mode);
int isatty(int fd);
#define WEXITSTATUS(status) (((status) & 0xff00) >> 8) #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
#define WTERMSIG(status) ((status) & 0x7f) #define WTERMSIG(status) ((status) & 0x7f)