| @@ -1,82 +1,73 @@ | | | @@ -1,82 +1,73 @@ |
1 | /* $NetBSD: snprintf.c,v 1.5 2007/07/20 00:11:25 tnn Exp $ */ | | 1 | /* $NetBSD: snprintf.c,v 1.6 2008/02/14 19:14:54 tnn Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2007 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2007 Tobias Nygren <tnn@NetBSD.org> |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | | |
8 | * by Tobias Nygren. | | | |
9 | * | | | |
10 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 9 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
18 | * 3. All advertising materials mentioning features or use of this software | | | |
19 | * must display the following acknowledgement: | | | |
20 | * This product includes software developed by the NetBSD | | | |
21 | * Foundation, Inc. and its contributors. | | | |
22 | * 4. Neither the name of The NetBSD Foundation nor the names of its | | | |
23 | * contributors may be used to endorse or promote products derived | | | |
24 | * from this software without specific prior written permission. | | | |
25 | * | | 15 | * |
26 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
27 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
28 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
29 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 19 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY |
30 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
36 | * POSSIBILITY OF SUCH DAMAGE. | | | |
37 | */ | | 26 | */ |
38 | | | 27 | |
39 | #include <nbcompat.h> | | 28 | #include <nbcompat.h> |
40 | #include <stdio.h> | | 29 | #include <stdio.h> |
41 | #include <stdlib.h> | | 30 | #include <stdlib.h> |
42 | #include <string.h> | | 31 | #include <string.h> |
43 | | | 32 | |
44 | #if HAVE_STDARG_H | | 33 | #if HAVE_STDARG_H |
45 | #include <stdarg.h> | | 34 | #include <stdarg.h> |
46 | #endif | | 35 | #endif |
47 | | | 36 | |
| | | 37 | #ifdef MIN |
| | | 38 | #undef MIN |
| | | 39 | #endif |
48 | #define MIN(a, b) ((a)<(b)?(a):(b)) | | 40 | #define MIN(a, b) ((a)<(b)?(a):(b)) |
49 | | | 41 | |
50 | int | | 42 | int |
51 | snprintf(char *str, size_t size, const char *format, ...) | | 43 | snprintf(char *str, size_t size, const char *format, ...) |
52 | { | | 44 | { |
53 | va_list ap; | | 45 | va_list ap; |
54 | int len; | | 46 | int len; |
55 | | | 47 | |
56 | va_start(ap, format); | | 48 | va_start(ap, format); |
57 | len = vsnprintf(str, size, format, ap); | | 49 | len = vsnprintf(str, size, format, ap); |
58 | va_end(ap); | | 50 | va_end(ap); |
59 | return len; | | 51 | return len; |
60 | } | | 52 | } |
61 | | | 53 | |
62 | static FILE *devnull = 0; | | | |
63 | | | | |
64 | int | | 54 | int |
65 | vsnprintf(char *str, size_t size, const char *format, va_list ap) | | 55 | vsnprintf(char *str, size_t size, const char *format, va_list ap) |
66 | { | | 56 | { |
67 | int len; | | 57 | int len; |
68 | char buf[129]; | | 58 | char buf[129]; |
69 | char *p = buf; | | 59 | char *p = buf; |
| | | 60 | static FILE *devnull = 0; |
70 | | | 61 | |
71 | if (!devnull) { | | 62 | if (!devnull) { |
72 | devnull = fopen("/dev/null", "w"); | | 63 | devnull = fopen("/dev/null", "w"); |
73 | if (!devnull) | | 64 | if (!devnull) |
74 | return -1; | | 65 | return -1; |
75 | } | | 66 | } |
76 | | | 67 | |
77 | len = vfprintf(devnull, format, ap); | | 68 | len = vfprintf(devnull, format, ap); |
78 | if (len < 0) | | 69 | if (len < 0) |
79 | return len; | | 70 | return len; |
80 | | | 71 | |
81 | if (len > 128) { | | 72 | if (len > 128) { |
82 | p = malloc(len + 1); | | 73 | p = malloc(len + 1); |