2014-10-15 01:00:14 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <syscall.h>
|
|
|
|
|
|
|
|
static FILE __stdin = { 1, STDIN_FILENO, 0 };
|
|
|
|
static FILE __stdout = { 1, STDOUT_FILENO, 0 };
|
|
|
|
static FILE __stderr = { 1, STDERR_FILENO, 0 };
|
|
|
|
|
|
|
|
FILE *stdin = &__stdin;
|
|
|
|
FILE *stdout = &__stdout;
|
|
|
|
FILE *stderr = &__stderr;
|
|
|
|
|
|
|
|
static FILE fds[FOPEN_MAX] = {
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
_alloc_file()
|
|
|
|
{
|
2014-10-15 09:10:46 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < FOPEN_MAX; i++) {
|
2014-10-15 01:00:14 +00:00
|
|
|
if (fds[i].in_use == 0) {
|
|
|
|
fds[i].in_use = 1;
|
|
|
|
return &fds[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: malloc
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_free_file(FILE *fh)
|
|
|
|
{
|
|
|
|
fh->fd = 0;
|
|
|
|
fh->offset = 0;
|
|
|
|
fh->in_use = 0;
|
|
|
|
|
|
|
|
// XXX: free
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
fopen(const char *path, const char *mode)
|
|
|
|
{
|
|
|
|
uint64_t fd;
|
|
|
|
FILE *fh;
|
|
|
|
// XXX: handle mode
|
|
|
|
|
2015-01-02 22:04:14 +00:00
|
|
|
fd = OSOpen(path, 0);
|
2014-10-15 01:00:14 +00:00
|
|
|
if (fd == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fh = _alloc_file();
|
|
|
|
fh->fd = fd;
|
|
|
|
fh->offset = 0;
|
|
|
|
|
|
|
|
// XXX: handle append
|
|
|
|
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fclose(FILE *fh)
|
|
|
|
{
|
2015-01-02 22:04:14 +00:00
|
|
|
int status = OSClose(fh->fd);
|
2014-10-15 01:00:14 +00:00
|
|
|
|
|
|
|
_free_file(fh);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
feof(FILE *fh)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fflush(FILE *fh)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
fread(void *buf, size_t size, size_t nmemb, FILE *fh)
|
|
|
|
{
|
2015-01-02 22:04:14 +00:00
|
|
|
return OSRead(fh->fd, buf, 0, size * nmemb);
|
2014-10-15 01:00:14 +00:00
|
|
|
// set errno
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
fwrite(const void *buf, size_t size, size_t nmemb, FILE *fh)
|
|
|
|
{
|
2015-01-02 22:04:14 +00:00
|
|
|
return OSWrite(fh->fd, buf, 0, size * nmemb);
|
2014-10-15 01:00:14 +00:00
|
|
|
// set errno
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fputc(int ch, FILE *fh)
|
|
|
|
{
|
|
|
|
if (fwrite(&ch, 1, 1, fh) == 1)
|
|
|
|
return ch;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fputs(const char *str, FILE *fh)
|
|
|
|
{
|
|
|
|
int status = fwrite(str, strlen(str), 1, fh);
|
|
|
|
if (status > 0)
|
|
|
|
return status;
|
|
|
|
// XXX: error handling
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
2015-01-23 20:39:35 +00:00
|
|
|
int
|
|
|
|
puts(const char *str)
|
|
|
|
{
|
2015-01-23 21:02:31 +00:00
|
|
|
int status, status2;
|
|
|
|
status = fputs(str, stdout);
|
|
|
|
if (status < 0)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
status2 = fputc('\n', stdout);
|
|
|
|
if (status2 < 0)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
return status;
|
2015-01-23 20:39:35 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 01:00:14 +00:00
|
|
|
int
|
|
|
|
fgetc(FILE *fh)
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
if (fread(&ch, 1, 1, fh) == 1)
|
|
|
|
return ch;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
fgets(char *str, int size, FILE *fh)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < (size - 1); i++) {
|
|
|
|
int ch = fgetc(fh);
|
|
|
|
if (ch == EOF)
|
|
|
|
return NULL;
|
|
|
|
str[i] = (char)ch;
|
|
|
|
if (ch == '\n') {
|
|
|
|
str[i + 1] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str[size - 1] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|