有些系统,例如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. 认识php钩子-转白俊遥的博客

    认识php钩子-转载白俊遥的博客 我们先来回顾下原本的开发流程:产品汪搞出了一堆需求:当用户注册成功后需要发送短信.发送邮件等等:然后聪明机智勇敢的程序猿们就一扑而上:把这些需求转换成代码扔在 用户注 ...

  2. sublime简单配置

    Preferences------->settings user { "font_face": "Courier New", "font_siz ...

  3. Javascript 插件统一的实现步骤

    步骤: // 1. 定义立即调用的函数 +function($){ "use strict"; //使用严格模式ES5支持 //后续步骤 // 2. xx 插件类及原型方法的定义 ...

  4. 金山词霸每日一句开放平台 .NET demo

    先附上地址:http://open.iciba.com/?c=api 小金山提供了2种获取数据的方式 1. 通过填入自己的网站名称.网址.邮箱地址 来生成一段javascript脚本,直接将生成的代码 ...

  5. openerp 常见问题 OpenERP为什么选择了时区后时间还是不对?(转载)

    OpenERP为什么选择了时区后时间还是不对? 原文地址:http://cn.openerp.cn/%E4%B8%BA%E4%BB%80%E4%B9%88%E9%80%89%E6%8B%A9%E4%B ...

  6. deep learning学习环境Theano安装(win8+win7)

    官网安装说明Installing Theano:http://deeplearning.net/software/theano/install.html#alternative-anacondace ...

  7. IOS笔记 : addChildViewController

    一下addChildViewController,一个ViewController可以添加多个子ViewController,但是这 些子ViewController只有一个是显示到父视图中的,可以通 ...

  8. [转载]--类unix系统如何初始化shell

    Shell的配置文件 当我们在linux中打开一个shell的时候,系统会读取相关的配置文件来初始化shell(其他unix-like OS也一样).配置文件包括以下这些: 1. 全局配置(对所有用户 ...

  9. Java当中的I/O的字节流

    I/O是input/output的缩写,即输入输出系统. I/O操作即从数据源中读取数据,以及将数据写入到数据目的地中.读的来源(如文件.键盘.网络)和写的目的地(如文件.屏幕.网络)分为很多种. 数 ...

  10. 算法系列7《CVN》

    计算CVN时使用二个64位的验证密钥,KeyA和KeyB. 1) 计算CVN 的数据源包括: 主账号(PAN).卡失效期和服务代码,从左至右顺序编排. 4123456789012345+8701+11 ...