myprintf.c added - replaces varargs.c
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 58 9609c54a54d3
parent 57 7739518acafb
child 59 a7ba10b68915
myprintf.c added - replaces varargs.c committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
myprintf.c
varargs.c
--- a/Makefile
+++ b/Makefile
@@ -30,7 +30,7 @@
 	cunit \
 	md5 \
 	md5rec \
-	varargs \
+	myprintf \
 	concatenation \
 	alpha_beta \
 	life \
@@ -186,7 +186,7 @@
 	@echo Linking $< ...
 	@$(CC) -o $@ $< -llsf
 
-varargs: varargs.o
+myprintf: myprintf.o
 	@echo Linking $< ...
 	@$(CC) -o $@ $<
 
new file mode 100644
--- /dev/null
+++ b/myprintf.c
@@ -0,0 +1,70 @@
+/**
+ * test/demos/myprintf.c
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+int myprintf (char *fmt, ...)
+{
+    int i, pos = 0;
+    va_list list;
+
+    char c;
+    char *s;
+    char s_str[1024];
+
+    char v[12];
+    int value;
+
+    va_start (list, fmt);
+
+    while (*fmt) {
+        switch (*fmt) {
+        case '%':
+            switch (*++fmt) {
+            case 'c':
+                c = va_arg (list, int);
+                s_str[pos++] = c;
+                c = *fmt++;
+                break;
+            case 's':
+                s = va_arg (list, char *);
+                for (i = 0; i < strlen (s); i++) {
+                    s_str[pos++] = s[i];
+                }
+                c = *fmt++;
+                break;
+            case 'd':
+                value = va_arg (list, int);
+                sprintf (v, "%d", value);
+                for (i = 0; i < strlen (v); i++) {
+                    s_str[pos++] = v[i];
+                }
+                c = *fmt++;
+                break;
+            default:
+                puts ("Unknown character");
+            }
+        default:
+            s_str[pos++] = *fmt++;
+        }
+        va_end (list);
+    }
+
+    s_str[pos] = '\0';
+
+    puts (s_str);
+    return pos;
+}
+
+int main (int argc, char **argv)
+{
+    int len;
+
+    len = myprintf ("Bugfix needed: %s\t[%d:%d]", "Lines", 20, 30);
+    return EXIT_SUCCESS;
+}
deleted file mode 100644
--- a/varargs.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- * test/demos/varargs.c
- * Copyright (C) 2008 Markus Broeker
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-
-int params (char *fmt, ...)
-{
-    int i = 0;
-
-    va_list list;
-
-    char c;
-    char *s;
-
-    int value;
-
-    va_start (list, fmt);
-
-    while (*list) {
-        switch (*fmt++) {
-        case 'c':
-            c = va_arg (list, int);
-
-            printf ("  Char: %c\n", c);
-            i++;
-            break;
-        case 's':
-            s = va_arg (list, char *);
-
-            printf ("String: %s\n", s);
-            i++;
-            break;
-        case 'd':
-            value = va_arg (list, int);
-
-            printf (" Value: %d\n", value);
-            i++;
-            break;
-        default:
-            printf ("Unknown format character\n");
-        }
-    }
-
-    va_end (list);
-
-    return i;
-}
-
-int main (int argc, char **argv)
-{
-
-    printf ("Number of Parameter: %d\n", params ("Xcssssd", 'I', "made", "this", "application", "in", 2008, NULL));
-
-    return EXIT_SUCCESS;
-}