Revision a03f7874 hw/virtio-9p.c
b/hw/virtio-9p.c | ||
---|---|---|
21 | 21 |
int dotu = 1; |
22 | 22 |
int debug_9p_pdu; |
23 | 23 |
|
24 |
static void v9fs_string_init(V9fsString *str) |
|
25 |
{ |
|
26 |
str->data = NULL; |
|
27 |
str->size = 0; |
|
28 |
} |
|
29 |
|
|
30 |
static void v9fs_string_free(V9fsString *str) |
|
31 |
{ |
|
32 |
qemu_free(str->data); |
|
33 |
str->data = NULL; |
|
34 |
str->size = 0; |
|
35 |
} |
|
36 |
|
|
37 |
static void v9fs_string_null(V9fsString *str) |
|
38 |
{ |
|
39 |
v9fs_string_free(str); |
|
40 |
} |
|
41 |
|
|
42 |
static int number_to_string(void *arg, char type) |
|
43 |
{ |
|
44 |
unsigned int ret = 0; |
|
45 |
|
|
46 |
switch (type) { |
|
47 |
case 'u': { |
|
48 |
unsigned int num = *(unsigned int *)arg; |
|
49 |
|
|
50 |
do { |
|
51 |
ret++; |
|
52 |
num = num/10; |
|
53 |
} while (num); |
|
54 |
break; |
|
55 |
} |
|
56 |
default: |
|
57 |
printf("Number_to_string: Unknown number format\n"); |
|
58 |
return -1; |
|
59 |
} |
|
60 |
|
|
61 |
return ret; |
|
62 |
} |
|
63 |
|
|
64 |
static int v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap) |
|
65 |
{ |
|
66 |
va_list ap2; |
|
67 |
char *iter = (char *)fmt; |
|
68 |
int len = 0; |
|
69 |
int nr_args = 0; |
|
70 |
char *arg_char_ptr; |
|
71 |
unsigned int arg_uint; |
|
72 |
|
|
73 |
/* Find the number of %'s that denotes an argument */ |
|
74 |
for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) { |
|
75 |
nr_args++; |
|
76 |
iter++; |
|
77 |
} |
|
78 |
|
|
79 |
len = strlen(fmt) - 2*nr_args; |
|
80 |
|
|
81 |
if (!nr_args) { |
|
82 |
goto alloc_print; |
|
83 |
} |
|
84 |
|
|
85 |
va_copy(ap2, ap); |
|
86 |
|
|
87 |
iter = (char *)fmt; |
|
88 |
|
|
89 |
/* Now parse the format string */ |
|
90 |
for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) { |
|
91 |
iter++; |
|
92 |
switch (*iter) { |
|
93 |
case 'u': |
|
94 |
arg_uint = va_arg(ap2, unsigned int); |
|
95 |
len += number_to_string((void *)&arg_uint, 'u'); |
|
96 |
break; |
|
97 |
case 's': |
|
98 |
arg_char_ptr = va_arg(ap2, char *); |
|
99 |
len += strlen(arg_char_ptr); |
|
100 |
break; |
|
101 |
case 'c': |
|
102 |
len += 1; |
|
103 |
break; |
|
104 |
default: |
|
105 |
fprintf(stderr, |
|
106 |
"v9fs_string_alloc_printf:Incorrect format %c", *iter); |
|
107 |
return -1; |
|
108 |
} |
|
109 |
iter++; |
|
110 |
} |
|
111 |
|
|
112 |
alloc_print: |
|
113 |
*strp = qemu_malloc((len + 1) * sizeof(**strp)); |
|
114 |
|
|
115 |
return vsprintf(*strp, fmt, ap); |
|
116 |
} |
|
117 |
|
|
118 |
static void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...) |
|
119 |
{ |
|
120 |
va_list ap; |
|
121 |
int err; |
|
122 |
|
|
123 |
v9fs_string_free(str); |
|
124 |
|
|
125 |
va_start(ap, fmt); |
|
126 |
err = v9fs_string_alloc_printf(&str->data, fmt, ap); |
|
127 |
BUG_ON(err == -1); |
|
128 |
va_end(ap); |
|
129 |
|
|
130 |
str->size = err; |
|
131 |
} |
|
132 |
|
|
133 |
static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs) |
|
134 |
{ |
|
135 |
v9fs_string_free(lhs); |
|
136 |
v9fs_string_sprintf(lhs, "%s", rhs->data); |
|
137 |
} |
|
138 |
|
|
139 |
static size_t v9fs_string_size(V9fsString *str) |
|
140 |
{ |
|
141 |
return str->size; |
|
142 |
} |
|
143 |
|
|
24 | 144 |
static V9fsPDU *alloc_pdu(V9fsState *s) |
25 | 145 |
{ |
26 | 146 |
V9fsPDU *pdu = NULL; |
... | ... | |
311 | 431 |
* They will be removed in the subsequent patches */ |
312 | 432 |
(void)pdu_unmarshal; |
313 | 433 |
(void) complete_pdu; |
434 |
(void) v9fs_string_init; |
|
435 |
(void) v9fs_string_free; |
|
436 |
(void) v9fs_string_null; |
|
437 |
(void) v9fs_string_sprintf; |
|
438 |
(void) v9fs_string_copy; |
|
439 |
(void) v9fs_string_size; |
|
440 |
|
|
314 | 441 |
|
315 | 442 |
} |
316 | 443 |
static void v9fs_version(V9fsState *s, V9fsPDU *pdu) |
Also available in: Unified diff