Fix klibc return values

This commit is contained in:
Mathieu Maret 2021-01-24 22:20:43 +01:00
parent 73b721dbc1
commit b305653aac
2 changed files with 42 additions and 21 deletions

View File

@ -127,36 +127,46 @@ char *strzcpy(register char *dst, register const char *src, register int len)
return dst;
}
void printf(const char *format, ...)
int printf(const char *format, ...)
{
int ret;
va_list ap;
va_start(ap, format);
vprintf(format, ap);
ret = vprintf(format, ap);
va_end(ap);
return ret;
}
void puts(const char *str)
int puts(const char *str)
{
int ret = 0;
while (*str) {
putc(*(str++));
ret++;
}
return ret;
}
void putc(const char str)
int putc(const char c)
{
VGAputc(str);
serialPutc(str);
VGAputc(c);
serialPutc(c);
return (unsigned char)c;
}
void printInt64(long long int integer)
int printInt64(long long int integer)
{
char num[sizeof(integer) *
3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3)
// = 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
int i = 0;
int ret = 0;
if (integer < 0) {
putc('-');
ret++;
}
do {
@ -167,18 +177,22 @@ void printInt64(long long int integer)
for (i = i - 1; i >= 0; i--) {
putc(num[i] + '0');
ret++;
}
return ret;
}
void printInt(int integer)
int printInt(int integer)
{
char num[sizeof(integer) *
3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3)
// = 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
int i = 0;
int i = 0;
int ret = 0;
if (integer < 0) {
putc('-');
ret++;
}
do {
@ -189,12 +203,15 @@ void printInt(int integer)
for (i = i - 1; i >= 0; i--) {
putc(num[i] + '0');
ret++;
}
return ret;
}
void vprintf(const char *format, va_list ap)
int vprintf(const char *format, va_list ap)
{
int i = 0;
int i = 0;
int ret = 0;
while (format[i] != '\0') {
switch (format[i]) {
case '%':
@ -202,7 +219,7 @@ void vprintf(const char *format, va_list ap)
case 'i':
case 'd': {
int d = va_arg(ap, int);
printInt(d);
ret += printInt(d);
break;
}
case 'p':
@ -210,23 +227,25 @@ void vprintf(const char *format, va_list ap)
char val[sizeof(int) * 2];
int d = va_arg(ap, int);
itoa(d, val, 16);
puts(val);
ret += puts(val);
break;
}
case 'c': {
int c = va_arg(ap, int);
putc((char)c);
ret++;
break;
}
case 's': {
char *str = va_arg(ap, char *);
if (!str)
str = "[NULL STR]";
puts(str);
ret += puts(str);
break;
}
case '%':
putc('%');
ret++;
break;
case 'l':
switch (format[i + 2]) {
@ -234,7 +253,7 @@ void vprintf(const char *format, va_list ap)
switch (format[i + 3]) {
case 'd': {
long long int d = va_arg(ap, long long int);
printInt64(d);
ret += printInt64(d);
break;
}
case 'p':
@ -242,7 +261,7 @@ void vprintf(const char *format, va_list ap)
char val[sizeof(long long int) * 2];
long long int d = va_arg(ap, long long int);
itoa(d, val, 16);
puts(val);
ret += puts(val);
break;
}
}
@ -251,7 +270,7 @@ void vprintf(const char *format, va_list ap)
case 'i':
case 'd': {
long int d = va_arg(ap, long int);
printInt64(d);
ret += printInt64(d);
break;
}
}
@ -263,7 +282,9 @@ void vprintf(const char *format, va_list ap)
default:
putc(format[i]);
ret++;
}
i++;
}
return ret;
}

View File

@ -19,10 +19,10 @@ int strlen(const char s[]);
unsigned int strnlen(const char *s, size_t count);
int strcmp(const char s1[], const char s2[]);
char *strzcpy(char *dst, const char *src, int len);
void puts(const char *str);
void putc(const char str);
void vprintf(const char *format, va_list ap);
void printf(const char *format, ...);
int puts(const char *str);
int putc(const char str);
int vprintf(const char *format, va_list ap);
int printf(const char *format, ...);
/*
* Dummy printk for disabled debugging statements to use whilst maintaining