myprintf.c added - replaces varargs.c
committer: Markus Bröker <mbroeker@largo.homelinux.org>
/**
* 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;
}