blob: 20489f471b53a8a510a07c17289123c909f55a27 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include <stdarg.h>
#include <stdint.h>
enum TYPE {
TYPE_INT = 1,
TYPE_UINT = 2,
TYPE_HEX = 3,
TYPE_STRING = 4
};
static void hex(char **str, int x) {
int n;
char *s = "0x";
while(*s)
*(*str)++ = *s++;
n = sizeof(x) << 1;
while(n--) {
*(*str)++ = ((unsigned int) x >> 28) + 0x30;
x = x << 4;
}
}
int vsprintf(char *str, char *fmt, va_list ap) {
int x;
char *s;
char *start = str;
uint8_t type;
while(*fmt) {
if(*fmt != '%') {
*str++ = *fmt++;
continue;
}
type = 0;
fmt++;
while(1) {
switch(*fmt) {
case 's':
if(type)
goto done;
type = TYPE_STRING;
break;
case 'x':
if(type)
goto done;
type = TYPE_HEX;
break;
default:
goto done;
break;
}
fmt++;
}
done:
switch(type) {
case TYPE_STRING:
s = va_arg(ap, char*);
while(*s)
*str++ = *s++;
break;
case TYPE_HEX:
x = va_arg(ap, int);
hex(&str, x);
break;
}
}
*str = 0;
return (str - start);
}
|