1 | /* $Id: $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * Fake Unix stuff for Solaris.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2005-2007 knut st. osmundsen <[email protected]>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This program is free software; you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation; either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * This program is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with This program; if not, write to the Free Software
|
---|
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 |
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <stdarg.h>
|
---|
28 | #ifdef __sun__
|
---|
29 | # undef va_copy /* why? */
|
---|
30 | #endif
|
---|
31 | #include "solfakes.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | int asprintf(char **strp, const char *fmt, ...)
|
---|
35 | {
|
---|
36 | int rc;
|
---|
37 | va_list va;
|
---|
38 | va_start(va, fmt);
|
---|
39 | rc = vasprintf(strp, fmt, va);
|
---|
40 | va_end(va);
|
---|
41 | return rc;
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | int vasprintf(char **strp, const char *fmt, va_list va)
|
---|
46 | {
|
---|
47 | int rc;
|
---|
48 | char *psz;
|
---|
49 | size_t cb = 1024;
|
---|
50 |
|
---|
51 | *strp = NULL;
|
---|
52 | for (;;)
|
---|
53 | {
|
---|
54 | va_list va2;
|
---|
55 |
|
---|
56 | psz = malloc(cb);
|
---|
57 | if (!psz)
|
---|
58 | return -1;
|
---|
59 |
|
---|
60 | #ifdef va_copy
|
---|
61 | va_copy(va2, va);
|
---|
62 | rc = snprintf(psz, cb, fmt, va2);
|
---|
63 | va_end(vaCopy);
|
---|
64 | #else
|
---|
65 | va2 = va;
|
---|
66 | rc = snprintf(psz, cb, fmt, va2);
|
---|
67 | #endif
|
---|
68 | if (rc < 0 || (size_t)rc < cb)
|
---|
69 | break;
|
---|
70 | cb *= 2;
|
---|
71 | free(psz);
|
---|
72 | }
|
---|
73 |
|
---|
74 | *strp = psz;
|
---|
75 | return rc;
|
---|
76 | }
|
---|
77 |
|
---|