libc: Implement fputs, fputc, putc and putchar

This commit is contained in:
apio 2022-10-12 10:05:14 +02:00
parent eaf7a1620b
commit 44834b8a0f
3 changed files with 33 additions and 7 deletions

View File

@ -43,7 +43,8 @@ int main()
{ {
if (gettid() == 0) // why are we the idle task? if (gettid() == 0) // why are we the idle task?
{ {
__luna_abort("SHENANIGANS! init is tid 0 (which is reserved for the idle task)\n"); fprintf(stderr, "SHENANIGANS! init is tid 0 (which is reserved for the idle task)\n");
return 1;
} }
printf("Welcome to Luna!\n"); printf("Welcome to Luna!\n");

View File

@ -27,13 +27,13 @@ extern "C"
FILE* fopen(const char*, const char*); FILE* fopen(const char*, const char*);
int fprintf(FILE*, const char*, ...); int fprintf(FILE*, const char*, ...);
size_t fread(void*, size_t, size_t, FILE*); size_t fread(void*, size_t, size_t, FILE*);
int fseek(FILE*, long, int); int fseek(FILE*, long, int); // Not implemented.
long ftell(FILE*); long ftell(FILE*); // Not implemented.
size_t fwrite(const void*, size_t, size_t, FILE*); size_t fwrite(const void*, size_t, size_t, FILE*);
int ferror(FILE*); int ferror(FILE*);
int feof(FILE*); int feof(FILE*);
void clearerr(FILE*); void clearerr(FILE*);
void setbuf(FILE*, char*); void setbuf(FILE*, char*); // Not implemented.
int vfprintf(FILE*, const char*, va_list); int vfprintf(FILE*, const char*, va_list);
int printf(const char*, ...); int printf(const char*, ...);
int vprintf(const char*, va_list); int vprintf(const char*, va_list);
@ -42,6 +42,10 @@ extern "C"
int vsprintf(char*, const char*, va_list); int vsprintf(char*, const char*, va_list);
int vsnprintf(char*, size_t, const char*, va_list); int vsnprintf(char*, size_t, const char*, va_list);
int puts(const char*); int puts(const char*);
int fputs(const char*, FILE*);
int fputc(int, FILE*);
int putc(int, FILE*);
int putchar(int);
void perror(const char*); void perror(const char*);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -10,10 +10,31 @@ extern "C"
{ {
int puts(const char* s) int puts(const char* s)
{ {
long nwritten = fwrite(s, strlen(s), 1, stdout); int nwritten = fputs(s, stdout);
if (nwritten < 0) return -1; if (nwritten < 0) return -1;
nwritten += fwrite("\n", 1, 1, stdout); if (putchar('\n') < 0) return -1;
return (int)nwritten; return nwritten + 1;
}
int fputs(const char* s, FILE* stream)
{
int result = (int)fwrite(s, strlen(s), 1, stream);
if (ferror(stream)) return -1;
return result;
}
int fputc(int c, FILE* stream)
{
char chr = (char)c;
fwrite(&chr, 1, 1, stream);
if (ferror(stream)) { return -1; }
return c;
}
int putc(int c, FILE* stream)
{
return fputc(c, stream);
}
int putchar(int c)
{
return fputc(c, stdout);
} }
void perror(const char* s) void perror(const char* s)
{ {