-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af03764
commit b51c1e6
Showing
5 changed files
with
659 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ...); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.