vasprintf的实现
有些系统,例如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的实现的更多相关文章
- 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 ...
- centos 7.0 编译安装php 5.6.7
编译安装php参考资料 MySQL PHP API http://dev.mysql.com/doc/apis-php/en/index.html nginx + php +mysql 最简单安装 官 ...
- centos 7.0 phpize 扩展php
phpize扩展php模块 phpize 所在目录 /usr/etc/php/bin/phpize 查看当前php配置情况 /usr/etc/php/bin/下面的php [root@localhos ...
- centos 7.0 编译安装mysql 5.6.22 再次总结 成功编译安装~ 越来越熟练了~
查找php.ini文件所在位置 [root@localhost /]# find -name php.ini ./usr/etc/php/etc/php.ini mysql官网的安装说明http:// ...
- 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 ...
- 编译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 ...
- c语言中格式化输出函数的研究
<一>; 1.前言 在gcc编程中,我们比较经常用到的字符格式化输出函数是printf的,实际上gcc继承了c语言处理字符具有强大功能的风格,它提供了一系列的格式化输出函数,主要存在两个库 ...
- JZ2440开发笔记(2)——minicom的安装和配置使用【转】
一.安装配置minicom 1.安装minicom lingd@ubuntu:~$ sudo apt-get install minicom 2.配置minicom lingd@ubuntu:~$ s ...
- GlusterFS源代码解析 —— GlusterFS 日志
Logging.c: /* Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part ...
随机推荐
- ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)
解决Can't connect to MySQL server on 'localhost' tomcat连接mysql,大概是c3p0配置和mysql配置都有问题,导致了内存溢出,几天后,mysq ...
- WordPress 主题开发 - (十三) Archive模板 待翻译
What archive.php does (and all its related templates) is show posts based on a select criteria. A da ...
- throw和throws
uncheckException的处理 class User{ private int age; public void setAge(int age){ if(age < 0){ //生成异常 ...
- vim代码补全-spf13,YouCompleteMe
vim代码补全 现在的图形界面的IDE(Integrated Development Environment)一般具有语法高亮,语法检查,自动补全功能,大大提高了编程的效率. vim作为文本编辑器其强 ...
- c语言中通过指针将数值赋值到制定内存地址
1.一种直观的方法 假设现在需要往内存0x12ff7c地址上存入一个整型数0x100.我们怎么才能做到呢? 我们知道可以通过一个指针向其指向的内存地址写入数据,那么这里的内存地址0x12ff7c其本质 ...
- Android--从相册中选取照片并返回结果
启动系统相册去选择图片 //从相册中选取的方法 private void selectPhoto(){ Intent intent = new Intent(Intent.ACTION_PICK); ...
- android EditText获取光标位置并安插字符删除字符
android EditText获取光标位置并插入字符删除字符1.获取光标位置int index = editText.getSelectionStart(); 2.在光标处插入字符int index ...
- mvc中使用knockoutjs和ajax
虽然说knockoutjs 官网上写的非常的清楚!但是像我这样的英语呕吐患者,真是虐心啊!今天我写下做个记录,也为那些初次使用的同学给予帮助, 首先我说一下今天我说的内容只是应用不做原理探究,如果没有 ...
- iOS 七大手势之轻拍,长按,旋转手势识别器方法
一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听 ...
- (转)android Fragments详解三:实现Fragment的界面
为fragment添加用户界面 fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layo ...