myprintf.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 60 47c13ca8c4d0
parent 58 9609c54a54d3
child 62 b7061c0e239f
permissions -rw-r--r--
Memory Management * myprintf.c: Dynamic Memory allocation added * prog_limit: fixed a segfault 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
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    11
#define BUF_SIZE 1024
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    12
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
int myprintf (char *fmt, ...)
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
{
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    15
    int i, value, pos = 0, size = 1;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    16
    char c;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    17
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    18
    char *ptr;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    19
    char *s;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    20
    char *s_str;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    21
    char v_str[28];
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    22
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    va_list list;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    25
    if ((s_str = malloc (BUF_SIZE)) == NULL) {
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    26
        return EOF;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    27
    }
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    va_start (list, fmt);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    while (*fmt) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        switch (*fmt) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        case '%':
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
            switch (*++fmt) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
            case 'c':
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
                c = va_arg (list, int);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
                s_str[pos++] = c;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
                break;
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    39
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
            case 's':
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
                s = va_arg (list, char *);
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    42
                while ((pos + strlen (s)) > (size * BUF_SIZE)) {
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    43
                    if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) {
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    44
                        s_str[pos] = '\0';
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    45
                        fputs (s_str, stdout);  /* print it anyway... */
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    46
                        return EOF;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    47
                    }
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    48
                    s_str = ptr;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    49
                }
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
                for (i = 0; i < strlen (s); i++) {
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
                    s_str[pos++] = s[i];
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
                }
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
                break;
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    54
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
            case 'd':
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
                value = va_arg (list, int);
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    57
                sprintf (v_str, "%d", value);
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    58
                for (i = 0; i < strlen (v_str); i++) {
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    59
                    s_str[pos++] = v_str[i];
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
                }
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
                break;
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    62
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
            default:
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    64
                s_str[pos++] = *fmt;
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
            }
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    66
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    67
            c = *fmt++;
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
        default:
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
            s_str[pos++] = *fmt++;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
        }
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    71
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    72
        if (pos > (size * BUF_SIZE / 2)) {
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    73
            if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) {
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    74
                return EOF;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    75
            }
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    76
            s_str = ptr;
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    77
        }
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    78
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
        va_end (list);
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
    }
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
    s_str[pos] = '\0';
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    83
    fputs (s_str, stdout);
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    84
    free (s_str);
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
    return pos;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
}
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
int main (int argc, char **argv)
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
{
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
    int len;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
60
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    93
    len = myprintf ("Printf replacement %% [Version %d - %s]...\n", 5, "LSF Rostock");
47c13ca8c4d0 Memory Management
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 58
diff changeset
    94
58
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
    return EXIT_SUCCESS;
9609c54a54d3 myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
}