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 ...
随机推荐
- C# Datetime类常用技巧
C#类常用技巧 //今天DateTime.Now.Date.ToShortDateString();//昨天,也就是今天的日期减一DateTime.Now.AddDays(-1).ToShortDat ...
- varnish状态引擎2
如何让varnish支持虚拟主机: if (req.http.host = "www.nihao.com") { } 强制对某资源的请求,不检查缓存: 上图表示以/test1.ht ...
- nginx安装 nginx: [emerg] getpwnam(“www”) failed 错误
inux 64系统中安装nginx1.3时如果出现错误:nginx: [emerg] getpwnam(“www”) failed解决方法1: 在nginx.conf中 把user nobo ...
- Windows 8.1激活问题
今天电脑开机,莫名的出现“你的Windows证书即将过期”. 系统本来用HEU_KMS_Activator 已激活成功,但查看系统激活状态时,结果如下图所示: 显示系统是已激活成功的.但还是有提示说系 ...
- 双栈排序(codevs 1170)题解
[问题描述] Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈 ...
- java android 中的Toast
package com.example.my1; import android.os.Bundle;import android.app.Activity;import android.content ...
- EditText 监听回车事件 避免2次触发
// 侦听回车事件 EidtText txtSN = (EditText) findViewById(R.id.txtSN); txtSN.setOnEditorActionListener(new ...
- django-url调度器-高级篇
我们在中级篇中学会了如何进行反向解析,但是有这样一个问题,在为 url 命名的时候,名字不能重复,否则会导致各种各样的问题.在 url 还少的时候保证不重名还是比较简单的,但是 url 多起来以后就比 ...
- UICollectionView [NSIndexPath section]: message sent to deallocated instance
在UICollectionView上加UITapGestureRecognizer手势时,点击哪都报 [NSIndexPath section]: message sent to deallocate ...
- Nunit单元测试的使用
先建立一个需要测试的项目 安装nunit 通过nuget安装Install-Package Nunit 类前加[TestFixture] 要测试的方法前加[Test] using System; u ...