OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.18.50/] [libiberty/] [vsnprintf.c] - Blame information for rev 438

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.