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);
how much space is needed.} */
buffer = (char *) xrealloc (buffer, nchars + 1);
- /* @r{Try again.} */
- snprintf (buffer, size, "value of %s is %s",
- name, value);
+ if (buffer != NULL)
+ /* @r{Try again.} */
+ snprintf (buffer, size, "value of %s is %s",
+ name, value);
@}
/* @r{The last call worked, return the string.} */
return buffer;
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:
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
@node Error Recovery
@section Recovering from errors
-You may explicitly clear the error and EOF flags with the @code{clearerr}
+You may explicitly clear the error and EOF flags with the @code{clearerr}
function.
@comment stdio.h
report the error to the user, rather than install complicated recovery
logic.
-One important exception is @code{EINTR} (@pxref{Interrupted Primitives}).
+One important exception is @code{EINTR} (@pxref{Interrupted Primitives}).
Many stream I/O implementations will treat it as an ordinary error, which
can be quite inconvenient. You can avoid this hassle by installing all
signals with the @code{SA_RESTART} flag.