Skip to content

Commit

Permalink
Improves userland libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
HAKarlsson committed Nov 8, 2023
1 parent af03764 commit b51c1e6
Show file tree
Hide file tree
Showing 5 changed files with 659 additions and 468 deletions.
2 changes: 2 additions & 0 deletions common/inc/altc/altio.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once
#include <stddef.h>

int alt_getchar(void);
int alt_putchar(char c);
int alt_putstr(const char *str);
int alt_puts(const char *str);
int alt_gets(char *str);
int alt_printf(const char *fmt, ...);
int alt_snprintf(char *restrict s, size_t n, char *restrict fmt, ...);
71 changes: 71 additions & 0 deletions common/src/altc/snprintf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdarg.h>
#include <stddef.h>

static char *write_hex(char *restrict dst, const char *end,
unsigned long long val)
{
if (!val && dst != end) {
*(dst++) = '0';
} else if (dst != end) {
int i = 0;
char buf[16];
while (val) {
int tmp = val & 0xF;
buf[i++] = tmp < 10 ? ('0' + tmp) : 'A' + (tmp - 10);
val >>= 4;
}
while (i > 0 && dst != end)
*(dst++) = buf[--i];
}
return dst;
}

static char *write_str(char *restrict dst, const char *end, char *restrict src)
{
while (dst != end && *src != '\0')
*(dst++) = *(src++);
return dst;
}

static char *write_char(char *restrict dst, const char *end, char c)
{
if (dst != end)
*(dst++) = c;
return dst;
}

int alt_snprintf(char *restrict str, size_t size, const char *restrict fmt, ...)
{
va_list args;
char *s = str;
const char *end = str + size - 1;
va_start(args, fmt);
while (*fmt != '\0' && s != end) {
if (*(fmt++) != '%') {
s = write_char(s, end, *(fmt - 1));
continue;
}
switch (*(fmt++)) {
case '%':
s = write_char(s, end, '%');
break;
case 'c':
s = write_char(s, end, va_arg(args, int));
break;
case 's':
s = write_str(s, end, va_arg(args, char *));
break;
case 'x':
s = write_hex(s, end, va_arg(args, unsigned int));
break;
case 'X':
s = write_hex(s, end, va_arg(args, unsigned long long));
break;
case '\0':
break;
}
}
*s = '\0';
va_end(args);
return s - str;
}
Loading

0 comments on commit b51c1e6

Please sign in to comment.