2018-10-31 01:09:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2018-10-31 16:50:43 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2018-11-11 09:11:09 +00:00
|
|
|
#include <unistd.h>
|
2018-11-17 14:56:09 +00:00
|
|
|
#include <fcntl.h>
|
2018-10-27 14:43:03 +00:00
|
|
|
#include <AK/printf.cpp>
|
2019-01-14 13:21:51 +00:00
|
|
|
#include <Kernel/Syscall.h>
|
2018-10-22 11:57:25 +00:00
|
|
|
|
2018-10-27 14:43:03 +00:00
|
|
|
extern "C" {
|
2018-10-22 11:57:25 +00:00
|
|
|
|
2018-11-11 09:11:09 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-11-11 14:36:40 +00:00
|
|
|
stream->buffer_index = 0;
|
2018-11-11 09:11:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setbuf(FILE* stream, char* buf)
|
|
|
|
{
|
|
|
|
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
|
|
|
|
}
|
|
|
|
|
2018-11-11 14:36:40 +00:00
|
|
|
void setlinebuf(FILE* stream)
|
2018-11-11 09:11:09 +00:00
|
|
|
{
|
2018-11-11 14:36:40 +00:00
|
|
|
setvbuf(stream, nullptr, _IOLBF, 0);
|
2018-11-11 09:11:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
int fileno(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
return stream->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int feof(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
return stream->eof;
|
|
|
|
}
|
|
|
|
|
2018-11-05 13:56:05 +00:00
|
|
|
int fflush(FILE* stream)
|
|
|
|
{
|
|
|
|
// FIXME: Implement buffered streams, duh.
|
|
|
|
if (!stream)
|
|
|
|
return -EBADF;
|
2018-11-11 09:11:09 +00:00
|
|
|
if (!stream->buffer_index)
|
2018-11-08 00:23:23 +00:00
|
|
|
return 0;
|
2018-11-11 09:11:09 +00:00
|
|
|
int rc = write(stream->fd, stream->buffer, stream->buffer_index);
|
|
|
|
stream->buffer_index = 0;
|
2018-11-08 00:23:23 +00:00
|
|
|
return rc;
|
2018-11-05 13:56:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
char* fgets(char* buffer, int size, FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
ssize_t nread = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (nread >= size)
|
|
|
|
break;
|
|
|
|
char ch = fgetc(stream);
|
|
|
|
if (feof(stream))
|
|
|
|
break;
|
|
|
|
buffer[nread++] = ch;
|
|
|
|
if (!ch || ch == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nread < size)
|
|
|
|
buffer[nread] = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fgetc(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
char ch;
|
2018-10-31 18:49:22 +00:00
|
|
|
fread(&ch, sizeof(char), 1, stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getc(FILE* stream)
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2018-10-31 16:50:43 +00:00
|
|
|
return fgetc(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getchar()
|
|
|
|
{
|
|
|
|
return getc(stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fputc(int ch, FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-11-11 09:11:09 +00:00
|
|
|
assert(stream->buffer_index < stream->buffer_size);
|
|
|
|
stream->buffer[stream->buffer_index++] = ch;
|
|
|
|
if (stream->buffer_index >= stream->buffer_size)
|
|
|
|
fflush(stream);
|
2018-11-11 14:36:40 +00:00
|
|
|
else if (stream->mode == _IONBF || (stream->mode == _IOLBF && ch == '\n'))
|
2018-11-08 00:23:23 +00:00
|
|
|
fflush(stream);
|
2018-11-07 09:23:16 +00:00
|
|
|
if (stream->eof)
|
|
|
|
return EOF;
|
2018-10-27 14:43:03 +00:00
|
|
|
return (byte)ch;
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
int putc(int ch, FILE* stream)
|
|
|
|
{
|
|
|
|
return fputc(ch, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
int putchar(int ch)
|
|
|
|
{
|
|
|
|
return putc(ch, stdout);
|
|
|
|
}
|
|
|
|
|
2018-11-07 09:23:16 +00:00
|
|
|
int fputs(const char* s, FILE* stream)
|
|
|
|
{
|
|
|
|
for (; *s; ++s) {
|
|
|
|
int rc = putc(*s, stream);
|
|
|
|
if (rc == EOF)
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
return putc('\n', stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
int puts(const char* s)
|
|
|
|
{
|
2018-11-08 00:23:23 +00:00
|
|
|
return fputs(s, stdout);
|
2018-11-07 09:23:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
void clearerr(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
stream->eof = false;
|
2018-11-07 09:23:16 +00:00
|
|
|
stream->error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ferror(FILE* stream)
|
|
|
|
{
|
|
|
|
return stream->error;
|
2018-10-31 16:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
ssize_t nread = read(stream->fd, ptr, nmemb * size);
|
|
|
|
if (nread < 0)
|
|
|
|
return 0;
|
|
|
|
if (nread == 0)
|
|
|
|
stream->eof = true;
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-11-08 00:23:23 +00:00
|
|
|
fflush(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
ssize_t nwritten = write(stream->fd, ptr, nmemb * size);
|
|
|
|
if (nwritten < 0)
|
|
|
|
return 0;
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fseek(FILE* stream, long offset, int whence)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
off_t off = lseek(stream->fd, offset, whence);
|
|
|
|
if (off < 0)
|
|
|
|
return off;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long ftell(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
return lseek(stream->fd, 0, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewind(FILE* stream)
|
|
|
|
{
|
|
|
|
fseek(stream, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2018-10-30 14:33:37 +00:00
|
|
|
static void sys_putch(char*&, char ch)
|
2019-01-14 13:21:51 +00:00
|
|
|
{
|
|
|
|
syscall(SC_putch, ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sys_printf(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int ret = printfInternal(sys_putch, nullptr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stdout_putch(char*&, char ch)
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2018-10-30 14:33:37 +00:00
|
|
|
putchar(ch);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 09:14:56 +00:00
|
|
|
static FILE* __current_stream = nullptr;
|
|
|
|
static void stream_putch(char*&, char ch)
|
|
|
|
{
|
2018-11-08 00:23:23 +00:00
|
|
|
fputc(ch, __current_stream);
|
2018-10-31 09:14:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 23:11:08 +00:00
|
|
|
int vfprintf(FILE* stream, const char* fmt, va_list ap)
|
|
|
|
{
|
|
|
|
__current_stream = stream;
|
|
|
|
return printfInternal(stream_putch, nullptr, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fprintf(FILE* stream, const char* fmt, ...)
|
2018-10-31 09:14:56 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2018-11-16 23:11:08 +00:00
|
|
|
int ret = vfprintf(stream, fmt, ap);
|
2018-10-31 09:14:56 +00:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:57:25 +00:00
|
|
|
int printf(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2019-01-14 13:21:51 +00:00
|
|
|
int ret = printfInternal(stdout_putch, nullptr, fmt, ap);
|
2018-10-22 11:57:25 +00:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_putch(char*& bufptr, char ch)
|
|
|
|
{
|
|
|
|
*bufptr++ = ch;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:11:08 +00:00
|
|
|
int vsprintf(char* buffer, const char* fmt, va_list ap)
|
|
|
|
{
|
|
|
|
int ret = printfInternal(buffer_putch, buffer, fmt, ap);
|
|
|
|
buffer[ret] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:57:25 +00:00
|
|
|
int sprintf(char* buffer, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2018-11-16 23:11:08 +00:00
|
|
|
int ret = vsprintf(buffer, fmt, ap);
|
2018-10-22 11:57:25 +00:00
|
|
|
buffer[ret] = '\0';
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:56:21 +00:00
|
|
|
void perror(const char* s)
|
|
|
|
{
|
2018-10-31 20:37:09 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", s, strerror(errno));
|
2018-10-26 12:56:21 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
FILE* fopen(const char* pathname, const char* mode)
|
|
|
|
{
|
|
|
|
assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));
|
|
|
|
int fd = open(pathname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return nullptr;
|
2018-11-11 09:11:09 +00:00
|
|
|
return make_FILE(fd);
|
2018-10-31 16:50:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 18:01:22 +00:00
|
|
|
FILE* fdopen(int fd, const char* mode)
|
|
|
|
{
|
|
|
|
assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));
|
|
|
|
if (fd < 0)
|
|
|
|
return nullptr;
|
2018-11-11 09:11:09 +00:00
|
|
|
return make_FILE(fd);
|
2018-11-05 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
int fclose(FILE* stream)
|
|
|
|
{
|
|
|
|
int rc = close(stream->fd);
|
|
|
|
free(stream);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|