From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paul Durrant Subject: [PATCH v7 5/9] Add an implentation of asprintf() for xen Date: Fri, 9 May 2014 09:40:00 +0100 Message-ID: <1399624804-5109-6-git-send-email-paul.durrant@citrix.com> References: <1399624804-5109-1-git-send-email-paul.durrant@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1399624804-5109-1-git-send-email-paul.durrant@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: Keir Fraser , Ian Campbell , Tim Deegan , Ian Jackson , Paul Durrant , Jan Beulich List-Id: xen-devel@lists.xenproject.org Signed-off-by: Paul Durrant Cc: Ian Campbell Cc: Ian Jackson Cc: Jan Beulich Cc: Keir Fraser Cc: Tim Deegan --- xen/common/vsprintf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/lib.h | 4 ++++ xen/include/xen/stdarg.h | 1 + 3 files changed, 59 insertions(+) diff --git a/xen/common/vsprintf.c b/xen/common/vsprintf.c index 8c43282..c4a3962 100644 --- a/xen/common/vsprintf.c +++ b/xen/common/vsprintf.c @@ -631,6 +631,60 @@ int scnprintf(char * buf, size_t size, const char *fmt, ...) } EXPORT_SYMBOL(scnprintf); +/** + * vasprintf - Format a string and allocate a buffer to place it in + * + * @bufp: Pointer to a pointer to receive the allocated buffer + * @fmt: The format string to use + * @args: Arguments for the format string + * + * -ENOMEM is returned on failure and @bufp is not touched. + * On success, 0 is returned. The buffer passed back is + * guaranteed to be null terminated. The memory is allocated + * from xenheap, so the buffer should be freed with xfree(). + */ +int vasprintf(char **bufp, const char *fmt, va_list args) +{ + va_list args_copy; + size_t size; + char *buf, dummy[1]; + + va_copy(args_copy, args); + size = vsnprintf(dummy, 0, fmt, args_copy); + va_end(args_copy); + + buf = xmalloc_array(char, ++size); + if ( !buf ) + return -ENOMEM; + + (void) vsnprintf(buf, size, fmt, args); + + *bufp = buf; + return 0; +} + +/** + * asprintf - Format a string and place it in a buffer + * @bufp: Pointer to a pointer to receive the allocated buffer + * @fmt: The format string to use + * @...: Arguments for the format string + * + * -ENOMEM is returned on failure and @bufp is not touched. + * On success, 0 is returned. The buffer passed back is + * guaranteed to be null terminated. The memory is allocated + * from xenheap, so the buffer should be freed with xfree(). + */ +int asprintf(char **bufp, const char *fmt, ...) +{ + va_list args; + int i; + + va_start(args, fmt); + i=vasprintf(bufp,fmt,args); + va_end(args); + return i; +} + /* * Local variables: * mode: C diff --git a/xen/include/xen/lib.h b/xen/include/xen/lib.h index 1369b2b..e81b80e 100644 --- a/xen/include/xen/lib.h +++ b/xen/include/xen/lib.h @@ -104,6 +104,10 @@ extern int scnprintf(char * buf, size_t size, const char * fmt, ...) __attribute__ ((format (printf, 3, 4))); extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) __attribute__ ((format (printf, 3, 0))); +extern int asprintf(char ** bufp, const char * fmt, ...) + __attribute__ ((format (printf, 2, 3))); +extern int vasprintf(char ** bufp, const char * fmt, va_list args) + __attribute__ ((format (printf, 2, 0))); long simple_strtol( const char *cp,const char **endp, unsigned int base); diff --git a/xen/include/xen/stdarg.h b/xen/include/xen/stdarg.h index 216fe6d..29249a1 100644 --- a/xen/include/xen/stdarg.h +++ b/xen/include/xen/stdarg.h @@ -2,6 +2,7 @@ #define __XEN_STDARG_H__ typedef __builtin_va_list va_list; +#define va_copy(dest, src) __builtin_va_copy((dest), (src)) #define va_start(ap, last) __builtin_va_start((ap), (last)) #define va_end(ap) __builtin_va_end(ap) #define va_arg __builtin_va_arg -- 1.7.10.4