Skip to content

Commit

Permalink
LibC: Add some things needed to build GNU bc.
Browse files Browse the repository at this point in the history
This patch adds vprintf(), sig_atomic_t, random() and strdup().
bc doesn't build yet, but it will.
  • Loading branch information
awesomekling committed Feb 1, 2019
1 parent feed67e commit 76f53b4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions LibC/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef __sighandler_t sighandler_t;

typedef uint32_t sigset_t;
typedef void siginfo_t;
typedef uint32_t sig_atomic_t;

struct sigaction {
union {
Expand Down
7 changes: 6 additions & 1 deletion LibC/stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,16 @@ int fprintf(FILE* stream, const char* fmt, ...)
return ret;
}

int vprintf(const char* fmt, va_list ap)
{
return printf_internal(stdout_putch, nullptr, fmt, ap);
}

int printf(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = printf_internal(stdout_putch, nullptr, fmt, ap);
int ret = vprintf(fmt, ap);
va_end(ap);
return ret;
}
Expand Down
1 change: 1 addition & 0 deletions LibC/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ int feof(FILE*);
int fflush(FILE*);
size_t fread(void* ptr, size_t size, size_t nmemb, FILE*);
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE*);
int vprintf(const char* fmt, va_list);
int vfprintf(FILE*, const char* fmt, va_list);
int vsprintf(char* buffer, const char* fmt, va_list);
int vsnprintf(char* buffer, size_t, const char* fmt, va_list);
Expand Down
10 changes: 10 additions & 0 deletions LibC/stdlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,14 @@ int abs(int i)
return i < 0 ? -i : i;
}

long int random()
{
return rand();
}

void srandom(unsigned seed)
{
srand(seed);
}

}
3 changes: 3 additions & 0 deletions LibC/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ int abs(int);
int rand();
void srand(unsigned seed);

long int random();
void srandom(unsigned seed);

__END_DECLS

9 changes: 9 additions & 0 deletions LibC/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <signal.h>
#include <assert.h>
#include <stdlib.h>
#include <AK/Types.h>

extern "C" {
Expand Down Expand Up @@ -51,6 +52,14 @@ size_t strlen(const char* str)
return len;
}

char* strdup(const char* str)
{
size_t len = strlen(str);
char* new_str = (char*)malloc(len);
strcpy(new_str, str);
return new_str;
}

int strcmp(const char* s1, const char* s2)
{
while (*s1 == *s2++)
Expand Down

0 comments on commit 76f53b4

Please sign in to comment.