假设您想要可移植的代码,glibc 扩展已经过时了。但即使保持 C99 和 POSIX 标准也很有可能,我只写了一个。
您不必重新实现 printf,但不幸的是,您确实需要让代码足够智能以解析 printf 格式字符串,并从中推断出可变参数的 C 类型。
当可变参数放在堆栈上时,不包括类型或大小信息。
void my_variadic_func(fmt, ...)
{
}
my_variadic_func("%i %s %i", 1, "2", 3);
在上面的 64 位系统示例中,使用 48 位寻址,编译器最终可能会分配 4 字节 + 6 字节 + 4 字节 = 14 字节的堆栈内存,并将值打包到其中。我说可能,因为内存分配方式和打包参数是特定于实现的。
这意味着,为了访问上述字符串中%s 的指针值,您需要知道第一个参数的类型为int,因此您可以将va_list 光标移动到正确的点。
获得该类型信息的唯一方法是查看格式字符串,并查看用户指定的类型(在本例中为 %i)。
因此,为了实现@AmbrozBizjak 的建议,将 subfmt 字符串传递给 printf,您需要解析 fmt 字符串,并在每个完整的非自定义 fmt 说明符之后,将 va_list 推进(无论多少字节宽)fmt类型是。
当您点击自定义 fmt 说明符时,您的 va_list 将在正确的位置解压缩参数。然后,您可以使用 va_arg() 获取您的自定义参数(传递正确的类型),并使用它来运行您需要的任何代码,以生成您的自定义 fmt 说明符的输出。
您将先前 printf 调用的输出与自定义 fmt 说明符的输出连接起来,然后继续处理,直到结束,此时您再次调用 printf 以处理格式字符串的其余部分。
代码更复杂(因此我将其包含在下面),但这让您对必须做什么有一个基本的了解。
我的代码也使用了talloc...但是您可以使用标准的内存函数来完成,只是需要更多的字符串处理。
char *custom_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
{
char const *p = fmt, *end = p + strlen(fmt), *fmt_p = p, *fmt_q = p;
char *out = NULL, *out_tmp;
va_list ap_p, ap_q;
out = talloc_strdup(ctx, "");
va_copy(ap_p, ap);
va_copy(ap_q, ap_p);
do {
char *q;
char *custom;
char len[2] = { '\0', '\0' };
long width = 0, group = 0, precision = 0, tmp;
if ((*p != '%') || (*++p == '%')) {
fmt_q = p + 1;
continue; /* literal char */
}
/*
* Check for parameter field
*/
tmp = strtoul(p, &q, 10);
if ((q != p) && (*q == '$')) {
group = tmp;
p = q + 1;
}
/*
* Check for flags
*/
do {
switch (*p) {
case '-':
continue;
case '+':
continue;
case ' ':
continue;
case '0':
continue;
case '#':
continue;
default:
goto done_flags;
}
} while (++p < end);
done_flags:
/*
* Check for width field
*/
if (*p == '*') {
width = va_arg(ap_q, int);
p++;
} else {
width = strtoul(p, &q, 10);
p = q;
}
/*
* Check for precision field
*/
if (*p == '.') {
p++;
precision = strtoul(p, &q, 10);
p = q;
}
/*
* Length modifiers
*/
switch (*p) {
case 'h':
case 'l':
len[0] = *p++;
if ((*p == 'h') || (*p == 'l')) len[1] = *p++;
break;
case 'L':
case 'z':
case 'j':
case 't':
len[0] = *p++;
break;
}
/*
* Types
*/
switch (*p) {
case 'i': /* int */
case 'd': /* int */
case 'u': /* unsigned int */
case 'x': /* unsigned int */
case 'X': /* unsigned int */
case 'o': /* unsigned int */
switch (len[0]) {
case 'h':
if (len[1] == 'h') { /* char (promoted to int) */
(void) va_arg(ap_q, int);
} else {
(void) va_arg(ap_q, int); /* short (promoted to int) */
}
break;
case 'L':
if ((*p == 'i') || (*p == 'd')) {
if (len [1] == 'L') {
(void) va_arg(ap_q, long); /* long */
} else {
(void) va_arg(ap_q, long long); /* long long */
}
} else {
if (len [1] == 'L') {
(void) va_arg(ap_q, unsigned long); /* unsigned long */
} else {
(void) va_arg(ap_q, unsigned long long);/* unsigned long long */
}
}
break;
case 'z':
(void) va_arg(ap_q, size_t); /* size_t */
break;
case 'j':
(void) va_arg(ap_q, intmax_t); /* intmax_t */
break;
case 't':
(void) va_arg(ap_q, ptrdiff_t); /* ptrdiff_t */
break;
case '\0': /* no length modifier */
if ((*p == 'i') || (*p == 'd')) {
(void) va_arg(ap_q, int); /* int */
} else {
(void) va_arg(ap_q, unsigned int); /* unsigned int */
}
}
break;
case 'f': /* double */
case 'F': /* double */
case 'e': /* double */
case 'E': /* double */
case 'g': /* double */
case 'G': /* double */
case 'a': /* double */
case 'A': /* double */
switch (len[0]) {
case 'L':
(void) va_arg(ap_q, long double); /* long double */
break;
case 'l': /* does nothing */
default: /* no length modifier */
(void) va_arg(ap_q, double); /* double */
}
break;
case 's':
(void) va_arg(ap_q, char *); /* char * */
break;
case 'c':
(void) va_arg(ap_q, int); /* char (promoted to int) */
break;
case 'p':
(void) va_arg(ap_q, void *); /* void * */
break;
case 'n':
(void) va_arg(ap_q, int *); /* int * */
break;
/*
* Custom types
*/
case 'v':
{
value_box_t const *value = va_arg(ap_q, value_box_t const *);
/*
* Allocations that are not part of the output
* string need to occur in the NULL ctx so we don't fragment
* any pool associated with it.
*/
custom = value_box_asprint(NULL, value->type, value->datum.enumv, value, '"');
if (!custom) {
talloc_free(out);
return NULL;
}
do_splice:
/*
* Pass part of a format string to printf
*/
if (fmt_q != fmt_p) {
char *sub_fmt;
sub_fmt = talloc_strndup(NULL, fmt_p, fmt_q - fmt_p);
out_tmp = talloc_vasprintf_append_buffer(out, sub_fmt, ap_p);
talloc_free(sub_fmt);
if (!out_tmp) {
oom:
fr_strerror_printf("Out of memory");
talloc_free(out);
talloc_free(custom);
va_end(ap_p);
va_end(ap_q);
return NULL;
}
out = out_tmp;
out_tmp = talloc_strdup_append_buffer(out, custom);
TALLOC_FREE(custom);
if (!out_tmp) goto oom;
out = out_tmp;
va_end(ap_p); /* one time use only */
va_copy(ap_p, ap_q); /* already advanced to the next argument */
}
fmt_p = p + 1;
}
break;
case 'b':
{
uint8_t const *bin = va_arg(ap_q, uint8_t *);
/*
* Only automagically figure out the length
* if it's not specified.
*
* This allows %b to be used with stack buffers,
* so long as the length is specified in the format string.
*/
if (precision == 0) precision = talloc_array_length(bin);
custom = talloc_array(NULL, char, (precision * 2) + 1);
if (!custom) goto oom;
fr_bin2hex(custom, bin, precision);
goto do_splice;
}
default:
break;
}
fmt_q = p + 1;
} while (++p < end);
/*
* Print out the rest of the format string.
*/
if (*fmt_p) {
out_tmp = talloc_vasprintf_append_buffer(out, fmt_p, ap_p);
if (!out_tmp) goto oom;
out = out_tmp;
}
va_end(ap_p);
va_end(ap_q);
return out;
}
编辑:
可能值得做 Linux 人员所做的事情并重载 %p 以创建新的格式说明符,即 %pA %pB。这意味着静态 printf 格式检查不会报错。