[PATCH 2 of 3] chg: add util function abortmsge to print error with errno
Yuya Nishihara
yuya at tcha.org
Tue Apr 5 11:54:53 EDT 2016
On Tue, 5 Apr 2016 08:33:25 -0700, Danek Duvall wrote:
> Jun Wu wrote:
>
> > -void abortmsg(const char *fmt, ...)
> > +void abortmsge(const char *fmt, ...)
> > {
> > va_list args;
> > va_start(args, fmt);
> > fsetcolor(stderr, "1;31");
> > fputs("chg: abort: ", stderr);
> > vfprintf(stderr, fmt, args);
> > + if (errno != 0)
> > + fprintf(stderr, " (errno = %d, %s)", errno, strerror(errno));
>
> At least vfprintf() can fail, setting errno; I'm pretty sure fputs() can,
> too. I don't know about fsetcolor(), but I'd suspect so.
>
> So you probably need to preserve errno first thing coming in to the
> function, and then check that, or errno might not be what you think it is.
> Either that, or pass it in as the first argument, but that'll take more
> restructuring of the code.
Aah, good point. I prefer the errno argument. Anyway, all callers had errno
argument, so it should be easy.
More information about the Mercurial-devel
mailing list