Add %m to userspace printf

%m as a format specifier is a nonstandard glibc extension, but I like it so I'm implementing it.
What it does is print the value of strerror(errno), without consuming any arguments to printf().
This commit is contained in:
apio 2022-10-08 18:44:14 +02:00
parent 4b74c14f1b
commit 49c7900407
2 changed files with 17 additions and 1 deletions

View File

@ -15,7 +15,7 @@ int main()
printf("Allocating 4 MB of memory... %p\n", allocated);
sleep(1);
} while ((allocated = malloc(CHUNK)));
perror("malloc");
printf("Memory allocation failed: %m\n");
printf("Press any key to restart.\n");
return 1;
}

View File

@ -1,3 +1,4 @@
#include <errno.h>
#include <luna.h>
#include <stdarg.h>
#include <stddef.h>
@ -251,6 +252,21 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
if (buffer_insert_index == 1024) flush_buffer();
break;
}
case 'm': {
const char* str = strerror(errno);
while (strlen(str) > 1024)
{
flush_buffer();
memcpy(buffer, str, 1024);
str += 1024;
buffer_insert_index = 1024;
}
if (buffer_insert_index + strlen(str) > 1024) flush_buffer();
memcpy(buffer + buffer_insert_index, str, strlen(str));
buffer_insert_index += strlen(str);
if (buffer_insert_index == 1024) flush_buffer();
break;
}
case 's': {
const char* str = va_arg(ap, const char*);
while (strlen(str) > 1024)