/[gcl]/gcl/binutils/libiberty/vasprintf.c
ViewVC logotype

Diff of /gcl/binutils/libiberty/vasprintf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by camm, Fri Aug 9 05:36:31 2002 UTC revision 1.1.1.1.20.1 by camm, Fri Sep 30 02:10:43 2005 UTC
# Line 1  Line 1 
1  /* Like vsprintf but provides a pointer to malloc'd storage, which must  /* Like vsprintf but provides a pointer to malloc'd storage, which must
2     be freed by the caller.     be freed by the caller.
3     Copyright (C) 1994 Free Software Foundation, Inc.     Copyright (C) 1994, 2003 Free Software Foundation, Inc.
4    
5  This file is part of the libiberty library.  This file is part of the libiberty library.
6  Libiberty is free software; you can redistribute it and/or  Libiberty is free software; you can redistribute it and/or
# Line 27  Boston, MA 02111-1307, USA.  */ Line 27  Boston, MA 02111-1307, USA.  */
27  #else  #else
28  #include <varargs.h>  #include <varargs.h>
29  #endif  #endif
30    #if !defined (va_copy) && defined (__va_copy)
31    # define va_copy(d,s)  __va_copy((d),(s))
32    #endif
33  #include <stdio.h>  #include <stdio.h>
34  #ifdef HAVE_STRING_H  #ifdef HAVE_STRING_H
35  #include <string.h>  #include <string.h>
# Line 52  you pass a pointer to a pointer.  This f Line 55  you pass a pointer to a pointer.  This f
55  of the buffer needed, allocate memory with @code{malloc}, and store a  of the buffer needed, allocate memory with @code{malloc}, and store a
56  pointer to the allocated memory in @code{*@var{resptr}}.  The value  pointer to the allocated memory in @code{*@var{resptr}}.  The value
57  returned is the same as @code{vsprintf} would return.  If memory could  returned is the same as @code{vsprintf} would return.  If memory could
58  not be allocated, zero is returned and @code{NULL} is stored in  not be allocated, minus one is returned and @code{NULL} is stored in
59  @code{*@var{resptr}}.  @code{*@var{resptr}}.
60    
61  @end deftypefn  @end deftypefn
62    
63  */  */
64    
65  static int int_vasprintf PARAMS ((char **, const char *, va_list *));  static int int_vasprintf PARAMS ((char **, const char *, va_list));
66    
67  static int  static int
68  int_vasprintf (result, format, args)  int_vasprintf (result, format, args)
69       char **result;       char **result;
70       const char *format;       const char *format;
71       va_list *args;       va_list args;
72  {  {
73    const char *p = format;    const char *p = format;
74    /* Add one to make sure that it is never zero, which might cause malloc    /* Add one to make sure that it is never zero, which might cause malloc
# Line 73  int_vasprintf (result, format, args) Line 76  int_vasprintf (result, format, args)
76    int total_width = strlen (format) + 1;    int total_width = strlen (format) + 1;
77    va_list ap;    va_list ap;
78    
79    memcpy ((PTR) &ap, (PTR) args, sizeof (va_list));  #ifdef va_copy
80      va_copy (ap, args);
81    #else
82      memcpy ((PTR) &ap, (PTR) &args, sizeof (va_list));
83    #endif
84    
85    while (*p != '\0')    while (*p != '\0')
86      {      {
# Line 135  int_vasprintf (result, format, args) Line 142  int_vasprintf (result, format, args)
142            p++;            p++;
143          }          }
144      }      }
145    #ifdef va_copy
146      va_end (ap);
147    #endif
148  #ifdef TEST  #ifdef TEST
149    global_total_width = total_width;    global_total_width = total_width;
150  #endif  #endif
151    *result = malloc (total_width);    *result = (char *) malloc (total_width);
152    if (*result != NULL)    if (*result != NULL)
153      return vsprintf (*result, format, *args);      return vsprintf (*result, format, args);
154    else    else
155      return 0;      return -1;
156  }  }
157    
158  int  int
# Line 155  vasprintf (result, format, args) Line 165  vasprintf (result, format, args)
165       va_list args;       va_list args;
166  #endif  #endif
167  {  {
168    return int_vasprintf (result, format, &args);    return int_vasprintf (result, format, args);
169  }  }
170    
171  #ifdef TEST  #ifdef TEST

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.1.20.1

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26