Add err_set_file() and err_set_exit() functions to make it possible for

programs which use err(3) to work nicely in a wider range of environments
(e.g., dialog).
This commit is contained in:
Garrett Wollman 1995-04-13 18:04:11 +00:00
parent ef46188e27
commit 3688be0eeb
3 changed files with 66 additions and 18 deletions

View File

@ -55,6 +55,8 @@ void warn __P((const char *, ...));
void vwarn __P((const char *, _BSD_VA_LIST_));
void warnx __P((const char *, ...));
void vwarnx __P((const char *, _BSD_VA_LIST_));
void err_set_file __P((void *));
void err_set_exit __P((void (*)__P((int))));
__END_DECLS
#endif /* !_ERR_H_ */

View File

@ -29,9 +29,10 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)err.3 8.1 (Berkeley) 6/9/93
.\" From: @(#)err.3 8.1 (Berkeley) 6/9/93
.\" $Id$
.\"
.Dd "June 9, 1993"
.Dd April 13, 1995
.Dt ERR 3
.Os BSD 4
.Sh NAME
@ -42,7 +43,9 @@
.Nm warn ,
.Nm vwarn ,
.Nm warnx ,
.Nm vwarnx
.Nm vwarnx ,
.Nm err_set_file ,
.Nm err_set_exit
.Nd formatted error messages
.Sh SYNOPSIS
.Fd #include <err.h>
@ -62,13 +65,19 @@
.Fn warnx "const char *fmt" "..."
.Ft void
.Fn vwarnx "const char *fmt" "va_list args"
.Ft void
.Fn err_set_file "void *fp"
.Ft void
.Fn err_set_exit "void (*exitf)(int)"
.Sh DESCRIPTION
The
.Fn err
and
.Fn warn
family of functions display a formatted error message on the standard
error output.
error output, or on another file specified using the
.Fn err_set_file
function.
In all cases, the last component of the program name, a colon character,
and a space are output.
If the
@ -95,6 +104,13 @@ and
.Fn verrx
functions do not return, but exit with the value of the argument
.Fa eval .
The
.Fn err_set_exit
function can be used to specify a function which is called before
.Xr exit 2
to perform any necessary cleanup; passing a null function pointer for
.Va exitf
resets the hook to do nothing.
.Sh EXAMPLES
Display the current errno information string and exit:
.Bd -literal -offset indent

View File

@ -49,6 +49,24 @@ static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93";
extern char *__progname; /* Program name, from crt0. */
static FILE *err_file; /* file to use for error output */
static void (*err_exit)(int);
void
err_set_file(void *fp)
{
if (fp)
err_file = fp;
else
err_file = stderr;
}
void
err_set_exit(void (*ef)(int))
{
err_exit = ef;
}
__dead void
#ifdef __STDC__
err(int eval, const char *fmt, ...)
@ -78,12 +96,16 @@ verr(eval, fmt, ap)
int sverrno;
sverrno = errno;
(void)fprintf(stderr, "%s: ", __progname);
if (! err_file)
err_set_file((FILE *)0);
(void)fprintf(err_file, "%s: ", __progname);
if (fmt != NULL) {
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, ": ");
(void)vfprintf(err_file, fmt, ap);
(void)fprintf(err_file, ": ");
}
(void)fprintf(stderr, "%s\n", strerror(sverrno));
(void)fprintf(err_file, "%s\n", strerror(sverrno));
if(err_exit)
err_exit(eval);
exit(eval);
}
@ -113,10 +135,14 @@ verrx(eval, fmt, ap)
const char *fmt;
va_list ap;
{
(void)fprintf(stderr, "%s: ", __progname);
if (! err_file)
err_set_file((FILE *)0);
(void)fprintf(err_file, "%s: ", __progname);
if (fmt != NULL)
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, "\n");
(void)vfprintf(err_file, fmt, ap);
(void)fprintf(err_file, "\n");
if (err_exit)
err_exit(eval);
exit(eval);
}
@ -147,12 +173,14 @@ vwarn(fmt, ap)
int sverrno;
sverrno = errno;
(void)fprintf(stderr, "%s: ", __progname);
if (! err_file)
err_set_file((FILE *)0);
(void)fprintf(err_file, "%s: ", __progname);
if (fmt != NULL) {
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, ": ");
(void)vfprintf(err_file, fmt, ap);
(void)fprintf(err_file, ": ");
}
(void)fprintf(stderr, "%s\n", strerror(sverrno));
(void)fprintf(err_file, "%s\n", strerror(sverrno));
}
void
@ -179,8 +207,10 @@ vwarnx(fmt, ap)
const char *fmt;
va_list ap;
{
(void)fprintf(stderr, "%s: ", __progname);
if (! err_file)
err_set_file((FILE *)0);
(void)fprintf(err_file, "%s: ", __progname);
if (fmt != NULL)
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, "\n");
(void)vfprintf(err_file, fmt, ap);
(void)fprintf(err_file, "\n");
}