6 #include <stdio.h> |
6 #include <stdio.h> |
7 #include <stdlib.h> |
7 #include <stdlib.h> |
8 #include <stdarg.h> |
8 #include <stdarg.h> |
9 #include <string.h> |
9 #include <string.h> |
10 |
10 |
|
11 #define BUF_SIZE 1024 |
|
12 |
11 int myprintf (char *fmt, ...) |
13 int myprintf (char *fmt, ...) |
12 { |
14 { |
13 int i, pos = 0; |
15 int i, value, pos = 0, size = 1; |
|
16 char c; |
|
17 |
|
18 char *ptr; |
|
19 char *s; |
|
20 char *s_str; |
|
21 char v_str[28]; |
|
22 |
14 va_list list; |
23 va_list list; |
15 |
24 |
16 char c; |
25 if ((s_str = malloc (BUF_SIZE)) == NULL) { |
17 char *s; |
26 return EOF; |
18 char s_str[1024]; |
27 } |
19 |
|
20 char v[12]; |
|
21 int value; |
|
22 |
28 |
23 va_start (list, fmt); |
29 va_start (list, fmt); |
24 |
30 |
25 while (*fmt) { |
31 while (*fmt) { |
26 switch (*fmt) { |
32 switch (*fmt) { |
27 case '%': |
33 case '%': |
28 switch (*++fmt) { |
34 switch (*++fmt) { |
29 case 'c': |
35 case 'c': |
30 c = va_arg (list, int); |
36 c = va_arg (list, int); |
31 s_str[pos++] = c; |
37 s_str[pos++] = c; |
32 c = *fmt++; |
|
33 break; |
38 break; |
|
39 |
34 case 's': |
40 case 's': |
35 s = va_arg (list, char *); |
41 s = va_arg (list, char *); |
|
42 while ((pos + strlen (s)) > (size * BUF_SIZE)) { |
|
43 if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) { |
|
44 s_str[pos] = '\0'; |
|
45 fputs (s_str, stdout); /* print it anyway... */ |
|
46 return EOF; |
|
47 } |
|
48 s_str = ptr; |
|
49 } |
36 for (i = 0; i < strlen (s); i++) { |
50 for (i = 0; i < strlen (s); i++) { |
37 s_str[pos++] = s[i]; |
51 s_str[pos++] = s[i]; |
38 } |
52 } |
39 c = *fmt++; |
|
40 break; |
53 break; |
|
54 |
41 case 'd': |
55 case 'd': |
42 value = va_arg (list, int); |
56 value = va_arg (list, int); |
43 sprintf (v, "%d", value); |
57 sprintf (v_str, "%d", value); |
44 for (i = 0; i < strlen (v); i++) { |
58 for (i = 0; i < strlen (v_str); i++) { |
45 s_str[pos++] = v[i]; |
59 s_str[pos++] = v_str[i]; |
46 } |
60 } |
47 c = *fmt++; |
|
48 break; |
61 break; |
|
62 |
49 default: |
63 default: |
50 puts ("Unknown character"); |
64 s_str[pos++] = *fmt; |
51 } |
65 } |
|
66 |
|
67 c = *fmt++; |
52 default: |
68 default: |
53 s_str[pos++] = *fmt++; |
69 s_str[pos++] = *fmt++; |
54 } |
70 } |
|
71 |
|
72 if (pos > (size * BUF_SIZE / 2)) { |
|
73 if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) { |
|
74 return EOF; |
|
75 } |
|
76 s_str = ptr; |
|
77 } |
|
78 |
55 va_end (list); |
79 va_end (list); |
56 } |
80 } |
57 |
81 |
58 s_str[pos] = '\0'; |
82 s_str[pos] = '\0'; |
|
83 fputs (s_str, stdout); |
|
84 free (s_str); |
59 |
85 |
60 puts (s_str); |
|
61 return pos; |
86 return pos; |
62 } |
87 } |
63 |
88 |
64 int main (int argc, char **argv) |
89 int main (int argc, char **argv) |
65 { |
90 { |
66 int len; |
91 int len; |
67 |
92 |
68 len = myprintf ("Bugfix needed: %s\t[%d:%d]", "Lines", 20, 30); |
93 len = myprintf ("Printf replacement %% [Version %d - %s]...\n", 5, "LSF Rostock"); |
|
94 |
69 return EXIT_SUCCESS; |
95 return EXIT_SUCCESS; |
70 } |
96 } |