diff -r 280250e45ba6 -r ebba53de8b18 src/server/util/io.c --- a/src/server/util/io.c Fri May 25 18:16:24 2012 +0200 +++ b/src/server/util/io.c Mon Jul 23 15:13:19 2012 +0200 @@ -30,6 +30,8 @@ #include #include +#include /* asprintf */ + #include "io.h" #include "pool.h" @@ -139,3 +141,103 @@ ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) { return writev(fd, io->iov, io->iovctn); } + + +/* TODO: add asprintf to new file */ + +/* + * asprintf implementation for Solaris 10 + * source from OpenSolaris + * file: /onnv/onnv-gate/usr/src/lib/libc/port/print/asprintf.c + */ + +/* + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +/* + * Copyright (c) 2004 Darren Tucker. + * + * Based originally on asprintf.c from OpenBSD: + * Copyright (c) 1997 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifdef __SunOS_5_10 + +#define INIT_SZ 128 + +/* VARARGS2 */ +int +vasprintf(char **str, const char *format, va_list ap) +{ + char string[INIT_SZ]; + char *newstr; + int ret; + size_t len; + + *str = NULL; + ret = vsnprintf(string, INIT_SZ, format, ap); + if (ret < 0) /* retain the value of errno from vsnprintf() */ + return (-1); + if (ret < INIT_SZ) { + len = ret + 1; + if ((newstr = malloc(len)) == NULL) + return (-1); /* retain errno from malloc() */ + (void) strlcpy(newstr, string, len); + *str = newstr; + return (ret); + } + /* + * We will perform this loop more than once only if some other + * thread modifies one of the vasprintf() arguments after our + * previous call to vsnprintf(). + */ + for (;;) { + if (ret == INT_MAX) { /* Bad length */ + errno = ENOMEM; + return (-1); + } + len = ret + 1; + if ((newstr = malloc(len)) == NULL) + return (-1); /* retain errno from malloc() */ + ret = vsnprintf(newstr, len, format, ap); + if (ret < 0) { /* retain errno from vsnprintf() */ + free(newstr); + return (-1); + } + if (ret < len) { + *str = newstr; + return (ret); + } + free(newstr); + } +} + +int +asprintf(char **str, const char *format, ...) +{ + va_list ap; + int ret; + + *str = NULL; + va_start(ap, format); + ret = vasprintf(str, format, ap); + va_end(ap); + + return (ret); +} + +#endif