Skip to content
Snippets Groups Projects
Verified Commit 75920d2c authored by Dan Robertson's avatar Dan Robertson
Browse files

Add implementations of sprintf and snprintf

Add implementations of sprintf and snprintf so that we can get a bit
closer to compiling libc-test.
parent 3890ec58
No related branches found
No related tags found
1 merge request!58Add implementations of sprintf and snprintf
......@@ -2,21 +2,39 @@
#define _BITS_STDIO_H
int fprintf(FILE * stream, const char * fmt, ...) {
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfprintf(stream, fmt, ap);
va_end(ap);
return ret;
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfprintf(stream, fmt, ap);
va_end(ap);
return ret;
}
int printf(const char * fmt, ...) {
int ret;
va_list ap;
va_start(ap, fmt);
ret = vprintf(fmt, ap);
va_end(ap);
return ret;
int ret;
va_list ap;
va_start(ap, fmt);
ret = vprintf(fmt, ap);
va_end(ap);
return ret;
}
int snprintf(char *s, size_t n, const char * fmt, ...) {
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsnprintf(s, n, fmt, ap);
va_end(ap);
return ret;
}
int sprintf(char *s, const char * fmt, ...) {
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsprintf(s, fmt, ap);
va_end(ap);
return ret;
}
#endif /* _BITS_STDIO_H */
......@@ -32,4 +32,11 @@ typedef int clockid_t;
typedef void* timer_t;
#ifdef __linux__
#define _SC_PAGE_SIZE 30
#endif
#ifdef __redox__
#define _SC_PAGE_SIZE 8
#endif
#endif /* _SYS_TYPES_H */
......@@ -26,6 +26,7 @@
/printf
/rmdir
/setid
/sprintf
/stdlib/strtol
/unlink
/write
......@@ -21,6 +21,7 @@ BINS=\
rmdir \
setid \
sleep \
sprintf \
stdlib/strtol \
unlink \
write
......
#include <stdio.h>
int main(int argc, char ** argv) {
char buffer[72];
int ret = sprintf(
buffer,
"This string fits in the buffer because it is only %d bytes in length",
68
);
if (ret) {
printf("Failed! %d\n", ret);
return -1;
}
ret = snprintf(
buffer,
72,
"This string is way longer and does not fit in the buffer because it %d bytes in length",
87
);
if (!ret) {
return 0;
} else {
printf("Failed! %d", ret);
return -1;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment