Skip to content
Snippets Groups Projects
Unverified Commit a227338a authored by Jeremy Soller's avatar Jeremy Soller Committed by GitHub
Browse files

Merge pull request #58 from dlrobertson/sprintf

Add implementations of sprintf and snprintf
parents 639ea091 0ee34fe8
No related branches found
No related tags found
No related merge requests found
...@@ -2,21 +2,39 @@ ...@@ -2,21 +2,39 @@
#define _BITS_STDIO_H #define _BITS_STDIO_H
int fprintf(FILE * stream, const char * fmt, ...) { int fprintf(FILE * stream, const char * fmt, ...) {
int ret; int ret;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
ret = vfprintf(stream, fmt, ap); ret = vfprintf(stream, fmt, ap);
va_end(ap); va_end(ap);
return ret; return ret;
} }
int printf(const char * fmt, ...) { int printf(const char * fmt, ...) {
int ret; int ret;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
ret = vprintf(fmt, ap); ret = vprintf(fmt, ap);
va_end(ap); va_end(ap);
return ret; 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 */ #endif /* _BITS_STDIO_H */
...@@ -32,4 +32,11 @@ typedef int clockid_t; ...@@ -32,4 +32,11 @@ typedef int clockid_t;
typedef void* timer_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 */ #endif /* _SYS_TYPES_H */
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
/printf /printf
/rmdir /rmdir
/setid /setid
/sprintf
/stdlib/strtol /stdlib/strtol
/string/strncmp /string/strncmp
/unlink /unlink
......
...@@ -21,6 +21,7 @@ BINS=\ ...@@ -21,6 +21,7 @@ BINS=\
rmdir \ rmdir \
setid \ setid \
sleep \ sleep \
sprintf \
stdlib/strtol \ stdlib/strtol \
string/strncmp \ string/strncmp \
unlink \ unlink \
......
#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