有些系统,例如AIX,没有vasprintf和asprintf ,如果是做porting,有时需要写一个简单的代替。

代码如下:

 #if !defined(HAVE_VASPRINTF)
#if defined(WINDOWS)
int
vasprintf (char **ptr, const char *format, va_list ap)
{
int len; len = _vscprintf_p (format, ap) + ;
*ptr = (char *) malloc (len * sizeof (char));
if (!*ptr)
{
return -;
} return _vsprintf_p (*ptr, len, format, ap);
}
#else
int
vasprintf (char **ptr, const char *format, va_list ap)
{
va_list ap_copy; /* Make sure it is determinate, despite manuals indicating otherwise */
*ptr = ; va_copy(ap_copy, ap);
int count = vsnprintf(NULL, , format, ap);
if (count >= )
{
char* buffer = malloc(count + );
if (buffer != NULL)
{
count = vsnprintf(buffer, count + , format, ap_copy);
if (count < )
free(buffer);
else
*ptr = buffer;
}
}
va_end(ap_copy); return count; }
#endif
#endif /* !HAVE_VASPRINTF */ #if !defined(HAVE_ASPRINTF)
int
asprintf (char **ptr, const char *format, ...)
{
va_list ap;
int ret; *ptr = NULL; va_start (ap, format);
ret = vasprintf (ptr, format, ap);
va_end (ap); return ret;
}
#endif /* !HAVE_ASPRINTF */
下面是一个完整的例子:
 #include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h> int
myvasprintf (char **ptr, const char *format, va_list ap)
{
va_list ap_copy; /* Make sure it is determinate, despite manuals indicating otherwise */
*ptr = ; va_copy(ap_copy, ap);
int count = vsnprintf(NULL, , format, ap);
if (count >= )
{
char* buffer = malloc(count + );
if (buffer != NULL)
{
count = vsnprintf(buffer, count + , format, ap_copy);
if (count < )
free(buffer);
else
*ptr = buffer;
}
}
va_end(ap_copy); return count; } void
print_warning (char **buffer, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
myvasprintf (buffer, fmt, ap);
va_end (ap);
} int main()
{
char *buffer;
int a = ;
int b = ;
print_warning(&buffer, "hello, world, %d * %d = %d, %s\n", a, b, a * b, "finished!!" ); printf(buffer);
return ;
}

上述代码的执行结果如下:

hello, world, 99999 * 9 = 899991, finished!!

vasprintf的实现的更多相关文章

  1. centos 7.0 编译安装php 7.0.3

    php下载页面 http://cn2.php.net/downloads.php 7.0.3多地区下载页面 http://cn2.php.net/get/php-7.0.3.tar.gz/from/a ...

  2. centos 7.0 编译安装php 5.6.7

    编译安装php参考资料 MySQL PHP API http://dev.mysql.com/doc/apis-php/en/index.html nginx + php +mysql 最简单安装 官 ...

  3. centos 7.0 phpize 扩展php

    phpize扩展php模块 phpize 所在目录 /usr/etc/php/bin/phpize 查看当前php配置情况 /usr/etc/php/bin/下面的php [root@localhos ...

  4. centos 7.0 编译安装mysql 5.6.22 再次总结 成功编译安装~ 越来越熟练了~

    查找php.ini文件所在位置 [root@localhost /]# find -name php.ini ./usr/etc/php/etc/php.ini mysql官网的安装说明http:// ...

  5. centos 7.0 PHP 5.6.5 安装过程 (php+nginx)

    php网址 http://php.net/downloads.php 首先下载 php-5.6.5.tar.gz [root@localhost src]# wget http://cn2.php.n ...

  6. 编译cscope-15.8a遇到的问题与解决方案

    1)环境 主机:Linux ubuntu 3.2.0-32-generic-pae #51-Ubuntu SMP Wed Sep 26 21:54:23 UTC 2012 i686 i686 i386 ...

  7. c语言中格式化输出函数的研究

    <一>; 1.前言 在gcc编程中,我们比较经常用到的字符格式化输出函数是printf的,实际上gcc继承了c语言处理字符具有强大功能的风格,它提供了一系列的格式化输出函数,主要存在两个库 ...

  8. JZ2440开发笔记(2)——minicom的安装和配置使用【转】

    一.安装配置minicom 1.安装minicom lingd@ubuntu:~$ sudo apt-get install minicom 2.配置minicom lingd@ubuntu:~$ s ...

  9. GlusterFS源代码解析 —— GlusterFS 日志

    Logging.c: /* Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part ...

随机推荐

  1. C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)

    一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...

  2. 网页打包安卓APP流程

    搭建环境过程: 1. 安装JDK. 参见http://www.cnblogs.com/Li-Cheng/p/4334985.html. 注:实质上到该网址上下载好JDK安装包,安装后添加一个环境变量: ...

  3. varnish状态引擎2

    如何让varnish支持虚拟主机: if (req.http.host = "www.nihao.com") { } 强制对某资源的请求,不检查缓存: 上图表示以/test1.ht ...

  4. Android sqlite3工具的使用

    sqlite3 <数据库名称> 进入数据库操作模式 eg: sqlite3 contacts.db .tables 查看所有的表 eg: .table .schema 查看查看库中所有表的 ...

  5. FileOutputSream

    package cd.itcast.fileinputstream; import java.io.File; import java.io.FileNotFoundException; import ...

  6. 在ASP.NET MVC中以post方式传递数组参数的示例

    最近在工作中用到了在ASP.NET MVC中以post方式传递数组参数的情况,记录下来,以供参考. 一.准备参数对象 在本例中,我会传递两个数组参数:一个字符串数组,一个自定义对象数组.这个自定义对象 ...

  7. 关于 mvc 中 连字符 - 和下划线 _转换的问题。

     [潜水]大崔||哈尔滨(759666247) 10:02:16  如图   C#不承认 “-”[知府]古道今-湖北\xig<systemobject@126.com> 10:03:54  ...

  8. 浅谈Objective-C异常处理

    -----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...

  9. 1084. Broken Keyboard (20)

    On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters ...

  10. linux 命令 more

    more命令: 从前往后读取文件,启动时加载整个文件,让整个文件的内容从上到下显示在屏幕上. 可以逐页读取,空格(space):下一页,b键(back):上一页,而且还有搜索字符串的功能. more ...