(Dynamic Output): Document the return value of asprintf. Also make the asprintf/snprintf examples a little better (check for some error returns).

This commit is contained in:
Ulrich Drepper 2000-12-25 00:44:08 +00:00
parent b708b1ca77
commit 7ba73c63c0
1 changed files with 15 additions and 6 deletions

View File

@ -1613,6 +1613,9 @@ make_message (char *name, char *value)
int nchars;
@end group
@group
if (buffer == NULL)
return NULL;
/* @r{Try to print in the allocated space.} */
nchars = snprintf (buffer, size, "value of %s is %s",
name, value);
@ -1624,6 +1627,7 @@ make_message (char *name, char *value)
how much space is needed.} */
buffer = (char *) xrealloc (buffer, nchars + 1);
if (buffer != NULL)
/* @r{Try again.} */
snprintf (buffer, size, "value of %s is %s",
name, value);
@ -1659,6 +1663,10 @@ buffer you allocate in advance. The @var{ptr} argument should be the
address of a @code{char *} object, and @code{asprintf} stores a pointer
to the newly allocated string at that location.
The return value is the number of characters allocated for the buffer, or
less than zero if an error occured. Usually this means that the buffer
could not be allocated.
Here is how to use @code{asprintf} to get the same result as the
@code{snprintf} example, but more easily:
@ -1669,7 +1677,8 @@ char *
make_message (char *name, char *value)
@{
char *result;
asprintf (&result, "value of %s is %s", name, value);
if (asprintf (&result, "value of %s is %s", name, value) < 0)
return NULL;
return result;
@}
@end smallexample