Просмотр исходного кода

Add implementations of sprintf and snprintf

Add implementations of sprintf and snprintf so that we can get a bit
closer to compiling libc-test.
Dan Robertson 7 лет назад
Родитель
Сommit
75920d2c12
5 измененных файлов с 69 добавлено и 12 удалено
  1. 30 12
      include/bits/stdio.h
  2. 7 0
      include/sys/types.h
  3. 1 0
      tests/.gitignore
  4. 1 0
      tests/Makefile
  5. 30 0
      tests/sprintf.c

+ 30 - 12
include/bits/stdio.h

@@ -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 */

+ 7 - 0
include/sys/types.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 */

+ 1 - 0
tests/.gitignore

@@ -26,6 +26,7 @@
 /printf
 /rmdir
 /setid
+/sprintf
 /stdlib/strtol
 /unlink
 /write

+ 1 - 0
tests/Makefile

@@ -21,6 +21,7 @@ BINS=\
 	rmdir \
 	setid \
 	sleep \
+	sprintf \
   	stdlib/strtol \
 	unlink \
 	write

+ 30 - 0
tests/sprintf.c

@@ -0,0 +1,30 @@
+
+#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;
+    }
+}