Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007, The xFTPd Project. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * * Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * * Neither the name of the xFTPd Project nor the |
| 16 | * names of its contributors may be used to endorse or promote products |
| 17 | * derived from this software without specific prior written permission. |
| 18 | * |
| 19 | * * Redistributions of this project or parts of this project in any form |
| 20 | * must retain the following aknowledgment: |
| 21 | * "This product includes software developed by the xFTPd Project. |
| 22 | * http://www.xftpd.com/ - http://www.xftpd.org/" |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE xFTPd PROJECT ``AS IS'' AND ANY |
| 25 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | * DISCLAIMED. IN NO EVENT SHALL THE xFTPd PROJECT BE LIABLE FOR ANY |
| 28 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | /* MinGW lacks asprintf */ |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdarg.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | |
| 43 | int vasprintf(char **strp, const char *fmt, va_list ap) |
| 44 | { |
| 45 | FILE *dev_null; |
| 46 | int arg_len; |
| 47 | |
| 48 | dev_null = fopen("nul", "w"); |
| 49 | arg_len = vfprintf(dev_null, fmt, ap); |
| 50 | if(arg_len != -1) { |
| 51 | *strp = (char *)malloc((size_t)arg_len + 1); |
| 52 | arg_len = vsprintf(*strp, fmt, ap); |
| 53 | } else *strp = NULL; |
| 54 | fclose(dev_null); |
| 55 | return arg_len; |
| 56 | } |
| 57 | |
| 58 | int asprintf(char **strp, const char *fmt, ...) |
| 59 | { |
| 60 | int result; |
| 61 | |
| 62 | va_list args; |
| 63 | va_start(args, fmt); |
| 64 | result = vasprintf(strp, fmt, args); |
| 65 | va_end(args); |
| 66 | return result; |
| 67 | } |