forked from PoliEcho-holding-org/kern
Fixed errors
This commit is contained in:
@@ -70,8 +70,8 @@ int vfprintf(FILE* stream, const char* fmt, va_list va);
|
|||||||
// NOTE: (v)sprinf() Has been depricated and removed as it is unsafe (potencial buffer overflow). Please use (v)snprintf()
|
// NOTE: (v)sprinf() Has been depricated and removed as it is unsafe (potencial buffer overflow). Please use (v)snprintf()
|
||||||
int snprintf(char* str, size_t size, const char* fmt, ...);
|
int snprintf(char* str, size_t size, const char* fmt, ...);
|
||||||
int vsnprintf(char* str, size_t size, const char* fmt, va_list va);
|
int vsnprintf(char* str, size_t size, const char* fmt, va_list va);
|
||||||
int vpprintf(void (*outfn)(char c, void* arg), void* arg, const char* fmt, ...)
|
int pprintf(void (*outfn)(char c, void* arg), void* arg, const char* fmt, ...);
|
||||||
int vpprintf(void (*outfn)(char c, void* arg), void* arg, const char* fmt, va_list va)
|
int vpprintf(void (*outfn)(char c, void* arg), void* arg, const char* fmt, va_list va);
|
||||||
|
|
||||||
int scanf(const char* fmt, ...);
|
int scanf(const char* fmt, ...);
|
||||||
int vscanf(const char* fmt, va_list va);
|
int vscanf(const char* fmt, va_list va);
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ inline int syscall(int nr, int a0, int a1, int a2, int a3, int a4) {
|
|||||||
"movl %%eax, %0" // Return
|
"movl %%eax, %0" // Return
|
||||||
: "=r"(ret) // Output
|
: "=r"(ret) // Output
|
||||||
: "r"(nr), "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(a4) // Input
|
: "r"(nr), "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(a4) // Input
|
||||||
: "eax", "ebx", "ecx", "edx", "esi", "edi" // Clobbered regs
|
|
||||||
);
|
);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -39,9 +38,9 @@ inline int syscall(int nr, int a0, int a1, int a2, int a3, int a4) {
|
|||||||
inline int _exit(int status) { return syscall(SYS_EXIT, status, 0, 0, 0, 0); }
|
inline int _exit(int status) { return syscall(SYS_EXIT, status, 0, 0, 0, 0); }
|
||||||
inline int read(int fd, void* ptr, size_t count) { return syscall(SYS_READ, fd, (int)ptr, (int)count, 0, 0); }
|
inline int read(int fd, void* ptr, size_t count) { return syscall(SYS_READ, fd, (int)ptr, (int)count, 0, 0); }
|
||||||
inline int write(int fd, const void* ptr, size_t count) { return syscall(SYS_WRITE, fd, (int)ptr, (int)count, 0, 0); }
|
inline int write(int fd, const void* ptr, size_t count) { return syscall(SYS_WRITE, fd, (int)ptr, (int)count, 0, 0); }
|
||||||
inline int open(const char* path, int oflag) { return syscall(SYS_OPEN, (int)path, oflag, 0, 0); }
|
inline int open(const char* path, int oflag) { return syscall(SYS_OPEN, (int)path, oflag, 0, 0, 0); }
|
||||||
inline int close(int fd) { return syscall(SYS_CLOSE, fd, 0, 0, 0, 0); }
|
inline int close(int fd) { return syscall(SYS_CLOSE, fd, 0, 0, 0, 0); }
|
||||||
inline int seek(int fd, size_t offset, int whence) { return syscall(SYS_SEEK, fd, offset, whence); }
|
inline int seek(int fd, size_t offset, int whence) { return syscall(SYS_SEEK, fd, offset, whence, 0, 0); }
|
||||||
inline int _malloc(int size) { return syscall(SYS_MALLOC, size, 0, 0, 0, 0); }
|
inline int _malloc(int size) { return syscall(SYS_MALLOC, size, 0, 0, 0, 0); }
|
||||||
inline int _free(void* ptr) { return syscall(SYS_FREE, (int)ptr, 0, 0, 0, 0); }
|
inline int _free(void* ptr) { return syscall(SYS_FREE, (int)ptr, 0, 0, 0, 0); }
|
||||||
inline int _sleep(int t) { return syscall(SYS_SLEEP, t, 0, 0, 0, 0); }
|
inline int _sleep(int t) { return syscall(SYS_SLEEP, t, 0, 0, 0, 0); }
|
||||||
|
|||||||
+3
-3
@@ -2,14 +2,14 @@
|
|||||||
#include <syscall.h>
|
#include <syscall.h>
|
||||||
|
|
||||||
int getchar(void) { return getc(stdin); }
|
int getchar(void) { return getc(stdin); }
|
||||||
int putchar(int c) { return putc(stdout); }
|
int putchar(int c) { return putc(c, stdout); }
|
||||||
|
|
||||||
int getc(FILE* stream) {
|
int getc(FILE* stream) {
|
||||||
if (!stream) { return EOF; }
|
if (!stream) { return EOF; }
|
||||||
|
|
||||||
unsigned char ch;
|
unsigned char ch;
|
||||||
ssize_t res = read(stream->_file, &ch, 1);
|
ssize_t res = read(stream->_file, &ch, 1);
|
||||||
if (res <= 0) { return EOF; }
|
if (res <= 0) { return EOF; }
|
||||||
|
|
||||||
return (int)ch;
|
return (int)ch;
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ int putc(int c, FILE* stream) {
|
|||||||
|
|
||||||
unsigned char ch = (unsigned char)c;
|
unsigned char ch = (unsigned char)c;
|
||||||
ssize_t res = write(stream->_file, &c, 1);
|
ssize_t res = write(stream->_file, &c, 1);
|
||||||
if (res <= 0) { return EOF; }
|
if (res <= 0) { return EOF; }
|
||||||
|
|
||||||
return (int)ch;
|
return (int)ch;
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-10
@@ -2,6 +2,7 @@
|
|||||||
#include <syscall.h>
|
#include <syscall.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
FILE* fopen(const char* path, const char* mode) {
|
FILE* fopen(const char* path, const char* mode) {
|
||||||
FILE* fp = (FILE*)malloc(sizeof(FILE));
|
FILE* fp = (FILE*)malloc(sizeof(FILE));
|
||||||
@@ -15,13 +16,13 @@ FILE* fopen(const char* path, const char* mode) {
|
|||||||
else if (strcmp(mode, "w+") == 0) { flags = O_RDWR | O_CREAT | O_TRUNC; }
|
else if (strcmp(mode, "w+") == 0) { flags = O_RDWR | O_CREAT | O_TRUNC; }
|
||||||
else if (strcmp(mode, "a+") == 0) { flags = O_RDWR | O_CREAT | O_APPEND; }
|
else if (strcmp(mode, "a+") == 0) { flags = O_RDWR | O_CREAT | O_APPEND; }
|
||||||
else { // Unsuppored mode
|
else { // Unsuppored mode
|
||||||
free(file);
|
free(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = open(path, flags);
|
int fd = open(path, flags);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
free(FILE);
|
free(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,24 +46,24 @@ size_t fread(void* ptr, size_t size, size_t count, FILE* stream) {
|
|||||||
if (!stream || !ptr || size == 0 || count == 0) { return 0; }
|
if (!stream || !ptr || size == 0 || count == 0) { return 0; }
|
||||||
|
|
||||||
size_t total = size * count;
|
size_t total = size * count;
|
||||||
ssize_t read = read(stream->_file, ptr, total);
|
ssize_t readb = read(stream->_file, ptr, total);
|
||||||
|
|
||||||
if (read < 0) return 0;
|
if (readb < 0) return 0;
|
||||||
|
|
||||||
stream->offset += read;
|
stream->_offset += readb;
|
||||||
return read / size;
|
return readb / size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t fwrite(void* ptr, size_t size, size_t count, FILE* stream) {
|
size_t fwrite(void* ptr, size_t size, size_t count, FILE* stream) {
|
||||||
if (!stream || !ptr || size == 0 || count == 0) { return 0; }
|
if (!stream || !ptr || size == 0 || count == 0) { return 0; }
|
||||||
|
|
||||||
size_t total = size * count;
|
size_t total = size * count;
|
||||||
ssize_t written = write(stream->_file, ptr, total);
|
ssize_t writtenb = write(stream->_file, ptr, total);
|
||||||
|
|
||||||
if (read < 0) return 0;
|
if (writtenb < 0) return 0;
|
||||||
|
|
||||||
stream->offset += written;
|
stream->_offset += writtenb;
|
||||||
return written / size;
|
return writtenb / size;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
|||||||
+12
-7
@@ -11,8 +11,9 @@ typedef struct {
|
|||||||
} printfEnv_t;
|
} printfEnv_t;
|
||||||
|
|
||||||
/* Helpers */
|
/* Helpers */
|
||||||
static void _dynamicPutc(char c, printfEnv_t* env) {
|
static void _dynamicPutc(char c, void* arg) {
|
||||||
if (!env->buf) { return; }
|
printfEnv_t* env = (printfEnv_t*)arg;
|
||||||
|
if (!env->buf) { return; }
|
||||||
|
|
||||||
if (env->offset < env->buflen) {
|
if (env->offset < env->buflen) {
|
||||||
env->buf[env->offset++] = c;
|
env->buf[env->offset++] = c;
|
||||||
@@ -27,8 +28,9 @@ static void _dynamicPutc(char c, printfEnv_t* env) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _staticPutc(char c, printfEnv_t* env) {
|
static void _staticPutc(char c, void* arg) {
|
||||||
if (!env->buf) { return; }
|
printfEnv_t* env = (printfEnv_t*)arg;
|
||||||
|
if (!env->buf) { return; }
|
||||||
|
|
||||||
if (env->offset < env->buflen) {
|
if (env->offset < env->buflen) {
|
||||||
env->buf[env->offset] = c;
|
env->buf[env->offset] = c;
|
||||||
@@ -37,7 +39,10 @@ static void _staticPutc(char c, printfEnv_t* env) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Impementation */
|
/* Impementation */
|
||||||
int vpprintf(void (*outfn)(char c, void* arg), void* arg, const char* fmt, va_list va) { /* TODO */ }
|
int vpprintf(void (*outfn)(char c, void* arg), void* arg, const char* fmt, va_list va) {
|
||||||
|
// TODO
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int vsnprintf(char* str, size_t size, const char* fmt, va_list va) {
|
int vsnprintf(char* str, size_t size, const char* fmt, va_list va) {
|
||||||
printfEnv_t env;
|
printfEnv_t env;
|
||||||
@@ -71,10 +76,10 @@ int vfprintf(FILE* stream, const char* fmt, va_list va) {
|
|||||||
int vprintf(const char* fmt, va_list va) { return vfprintf(stdout, fmt, va); }
|
int vprintf(const char* fmt, va_list va) { return vfprintf(stdout, fmt, va); }
|
||||||
|
|
||||||
// Print using an custom putc function
|
// Print using an custom putc function
|
||||||
int pprintf(void (*outfn)(char c), const char* fmt, ...) {
|
int pprintf(void (*outfn)(char c, void* arg), void* arg, const char* fmt, ...) {
|
||||||
va_list va;
|
va_list va;
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
int ret = vpprintf(outfn, fmt, va);
|
int ret = vpprintf(outfn, arg, fmt, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
int scanf(const char* fmt, ...);
|
|
||||||
int vscanf(const char* fmt, va_list va);
|
|
||||||
int fscanf(FILE* stream, const char* fmt, ...);
|
|
||||||
int vfscanf(FILE* stream, const char* fmt, va_list va);
|
|
||||||
int sscanf(const char* str, const char* fmt, ...);
|
|
||||||
int vsscanf(const char* str, const char* fmt, va_list va);
|
|
||||||
|
|
||||||
+1
-1
@@ -33,5 +33,5 @@ void* realloc(void* ptr, size_t size) {
|
|||||||
return newptr;
|
return newptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free(void* ptr) { _free(ptr); }
|
void free(void* ptr) { _free(ptr); }
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
void sleep(size_t s) { _sleep(s*1000); }
|
void sleep(size_t s) { _sleep(s*1000); }
|
||||||
|
|
||||||
// Millisecond sleep
|
// Millisecond sleep
|
||||||
void msleep(size_t ms) { _sleep(s); }
|
void msleep(size_t ms) { _sleep(ms); }
|
||||||
|
|
||||||
// Microsecond sleep
|
// Microsecond sleep
|
||||||
void usleep(size_t us) { /* TODO */ }
|
void usleep(size_t us) { /* TODO */ }
|
||||||
|
|||||||
Reference in New Issue
Block a user