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

View File

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