merge from gcc
[deliverable/binutils-gdb.git] / libiberty / vsnprintf.c
CommitLineData
2ed1e5cc
DD
1/* Implement the vsnprintf function.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
4
5This file is part of the libiberty library. This library is free
6software; you can redistribute it and/or modify it under the
7terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11This library is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20As a special exception, if you link this library with files
21compiled with a GNU compiler to produce an executable, this does not cause
22the resulting executable to be covered by the GNU General Public License.
23This exception does not however invalidate any other reasons why
24the executable file might be covered by the GNU General Public License. */
25
26/*
27
28@deftypefn Supplemental int vsnprintf (char *@var{buf}, size_t @var{n}, const char *@var{format}, va_list @var{ap})
29
30This function is similar to vsprintf, but it will print at most
31@var{n} characters. On error the return value is -1, otherwise it
32returns the number of characters that would have been printed had
33@var{n} been sufficiently large, regardless of the actual value of
34@var{n}. Note some pre-C99 system libraries do not implement this
35correctly so users cannot generally rely on the return value if the
36system version of this function is used.
37
38@end deftypefn
39
40*/
41
42#include "config.h"
43#include "ansidecl.h"
44
45#ifdef ANSI_PROTOTYPES
46#include <stdarg.h>
47#else
48#include <varargs.h>
49#endif
50#ifdef HAVE_STRING_H
51#include <string.h>
52#endif
53#ifdef HAVE_STDLIB_H
54#include <stdlib.h>
55#endif
56
57#include "libiberty.h"
58
59/* This implementation relies on a working vasprintf. */
60int
61vsnprintf (s, n, format, ap)
62 char * s;
63 size_t n;
64 const char *format;
65 va_list ap;
66{
67 char *buf = 0;
68 int result = vasprintf (&buf, format, ap);
69
70 if (!buf)
71 return -1;
72 if (result < 0)
73 {
74 free (buf);
75 return -1;
76 }
77
78 result = strlen (buf);
79 if (n > 0)
80 {
81 strncpy (s, buf, n);
82 if (n - 1 < (size_t) result)
83 s[n - 1] = 0;
84 }
85 free (buf);
86 return result;
87}
88
89#ifdef TEST
90/* Set the buffer to a known state. */
91#define CLEAR(BUF) do { memset ((BUF), 'X', sizeof (BUF)); (BUF)[14] = '\0'; } while (0)
92/* For assertions. */
93#define VERIFY(P) do { if (!(P)) abort(); } while (0)
94
95static int ATTRIBUTE_PRINTF_3
96checkit VPARAMS ((char *s, size_t n, const char *format, ...))
97{
98 int result;
99 VA_OPEN (ap, format);
100 VA_FIXEDARG (ap, char *, s);
101 VA_FIXEDARG (ap, size_t, n);
102 VA_FIXEDARG (ap, const char *, format);
103 result = vsnprintf (s, n, format, ap);
104 VA_CLOSE (ap);
105 return result;
106}
107
108extern int main PARAMS ((void));
109int
110main ()
111{
112 char buf[128];
113 int status;
114
115 CLEAR (buf);
116 status = checkit (buf, 10, "%s:%d", "foobar", 9);
117 VERIFY (status==8 && strcmp (buf, "foobar:9") == 0);
118
119 CLEAR (buf);
120 status = checkit (buf, 9, "%s:%d", "foobar", 9);
121 VERIFY (status==8 && strcmp (buf, "foobar:9") == 0);
122
123 CLEAR (buf);
124 status = checkit (buf, 8, "%s:%d", "foobar", 9);
125 VERIFY (status==8 && strcmp (buf, "foobar:") == 0);
126
127 CLEAR (buf);
128 status = checkit (buf, 7, "%s:%d", "foobar", 9);
129 VERIFY (status==8 && strcmp (buf, "foobar") == 0);
130
131 CLEAR (buf);
132 status = checkit (buf, 6, "%s:%d", "foobar", 9);
133 VERIFY (status==8 && strcmp (buf, "fooba") == 0);
134
135 CLEAR (buf);
136 status = checkit (buf, 2, "%s:%d", "foobar", 9);
137 VERIFY (status==8 && strcmp (buf, "f") == 0);
138
139 CLEAR (buf);
140 status = checkit (buf, 1, "%s:%d", "foobar", 9);
141 VERIFY (status==8 && strcmp (buf, "") == 0);
142
143 CLEAR (buf);
144 status = checkit (buf, 0, "%s:%d", "foobar", 9);
145 VERIFY (status==8 && strcmp (buf, "XXXXXXXXXXXXXX") == 0);
146
147 return 0;
148}
149#endif /* TEST */
This page took 0.028358 seconds and 4 git commands to generate.