myprintf.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 58 9609c54a54d3
child 60 47c13ca8c4d0
permissions -rw-r--r--
myprintf.c added - replaces varargs.c committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * test/demos/myprintf.c
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdarg.h>
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <string.h>
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
int myprintf (char *fmt, ...)
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
{
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
    int i, pos = 0;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    va_list list;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    char c;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    char *s;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    char s_str[1024];
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    char v[12];
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    int value;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    va_start (list, fmt);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    while (*fmt) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
        switch (*fmt) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
        case '%':
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
            switch (*++fmt) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
            case 'c':
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
                c = va_arg (list, int);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
                s_str[pos++] = c;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
                c = *fmt++;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
                break;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
            case 's':
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
                s = va_arg (list, char *);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
                for (i = 0; i < strlen (s); i++) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
                    s_str[pos++] = s[i];
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
                }
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
                c = *fmt++;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
                break;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
            case 'd':
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
                value = va_arg (list, int);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
                sprintf (v, "%d", value);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
                for (i = 0; i < strlen (v); i++) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
                    s_str[pos++] = v[i];
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
                }
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
                c = *fmt++;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
                break;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
            default:
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
                puts ("Unknown character");
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
            }
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
        default:
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
            s_str[pos++] = *fmt++;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
        }
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        va_end (list);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    }
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    s_str[pos] = '\0';
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    puts (s_str);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
    return pos;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
}
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
int main (int argc, char **argv)
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
{
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    int len;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    len = myprintf ("Bugfix needed: %s\t[%d:%d]", "Lines", 20, 30);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
    return EXIT_SUCCESS;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
}