Fix std file operations

This commit is contained in:
Alex 2023-04-23 22:18:06 +03:00
parent f740c1e736
commit 1000a57531
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
3 changed files with 88 additions and 62 deletions

View File

@ -8,10 +8,14 @@
#define SEEK_CUR 1
#define SEEK_END 2
typedef struct
struct _IO_FILE
{
int unused;
} FILE;
size_t offset;
void *KernelPrivate;
};
typedef struct _IO_FILE FILE;
#ifdef __cplusplus
extern "C"
@ -22,15 +26,15 @@ extern "C"
extern FILE *stdout;
extern FILE *stderr;
int fclose(FILE *stream);
int fflush(FILE *stream);
FILE *fopen(const char *filename, const char *mode);
int fprintf(FILE *stream, const char *format, ...);
int printf(const char *format, ...);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int fclose(FILE *fp);
int fflush(FILE *stream);
int fprintf(FILE *stream, const char *format, ...);
int printf(const char *format, ...);
void setbuf(FILE *stream, char *buf);
int vfprintf(FILE *stream, const char *format, va_list arg);
int vsscanf(const char *s, const char *format, va_list arg);

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/syscalls.h>
@ -6,9 +7,52 @@ FILE *stdin;
FILE *stdout;
FILE *stderr;
int fclose(FILE *stream)
FILE *fopen(const char *filename, const char *mode)
{
return 0;
FILE *FilePtr = malloc(sizeof(FILE));
FilePtr->KernelPrivate = (void *)syscall2(_FileOpen, (uint64_t)filename, (uint64_t)mode);
return FilePtr;
}
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return syscall4(_FileRead, (uint64_t)stream->KernelPrivate, stream->offset, (uint64_t)ptr, size * nmemb);
}
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return syscall4(_FileWrite, (uint64_t)stream->KernelPrivate, stream->offset, (uint64_t)ptr, size * nmemb);
}
int fseek(FILE *stream, long offset, int whence)
{
switch (whence)
{
case SEEK_SET:
stream->offset = offset;
break;
case SEEK_CUR:
stream->offset += offset;
break;
case SEEK_END:
// stream->offset = syscall1(_FileLength, (uint64_t)File->KernelPrivate) + offset;
break;
default:
return -1;
}
return stream->offset;
}
long ftell(FILE *stream)
{
return stream->offset;
}
int fclose(FILE *fp)
{
void *KP = fp->KernelPrivate;
free(fp);
return syscall1(_FileClose, (uint64_t)KP);
}
int fflush(FILE *stream)
@ -16,11 +60,6 @@ int fflush(FILE *stream)
return 0;
}
FILE *fopen(const char *filename, const char *mode)
{
return 0;
}
int fprintf(FILE *stream, const char *format, ...)
{
return 0; // sprintf(char *s, const char *format, ...)
@ -31,26 +70,6 @@ int fprintf(FILE *stream, const char *format, ...)
// return 0;
// }
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return 0;
}
int fseek(FILE *stream, long offset, int whence)
{
return 0;
}
long ftell(FILE *stream)
{
return 0;
}
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return 0;
}
void setbuf(FILE *stream, char *buf)
{
}
@ -72,30 +91,3 @@ int sscanf(const char *s, const char *format, ...)
va_end(args);
return ret;
}
int fputc(int c, FILE *stream)
{
syscall1(_Print, c);
}
int putc(int c, FILE *stream)
{
syscall1(_Print, c);
}
int fputs(const char *s, FILE *stream)
{
for (int i = 0; s[i] != '\0'; i++)
fputc(s[i], stream);
}
int putchar(int c)
{
syscall1(_Print, c);
}
int puts(const char *s)
{
for (int i = 0; s[i] != '\0'; i++)
fputc(s[i], stdout);
}

30
libc/src/std/io/put.c Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdarg.h>
#include <sys/syscalls.h>
int fputc(int c, FILE *stream)
{
syscall1(_Print, c);
}
int putc(int c, FILE *stream)
{
syscall1(_Print, c);
}
int fputs(const char *s, FILE *stream)
{
for (int i = 0; s[i] != '\0'; i++)
fputc(s[i], stream);
}
int putchar(int c)
{
syscall1(_Print, c);
}
int puts(const char *s)
{
for (int i = 0; s[i] != '\0'; i++)
fputc(s[i], stdout);
}