From 3688be0eeb59367d9dd33e4b0963813b72ce0ef3 Mon Sep 17 00:00:00 2001 From: Garrett Wollman Date: Thu, 13 Apr 1995 18:04:11 +0000 Subject: [PATCH] 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). --- include/err.h | 2 ++ lib/libc/gen/err.3 | 24 +++++++++++++++---- lib/libc/gen/err.c | 58 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/include/err.h b/include/err.h index 8236f0077606..2f065e73ef53 100644 --- a/include/err.h +++ b/include/err.h @@ -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_ */ diff --git a/lib/libc/gen/err.3 b/lib/libc/gen/err.3 index 2a3f4c391fd3..a80edeb4c607 100644 --- a/lib/libc/gen/err.3 +++ b/lib/libc/gen/err.3 @@ -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 @@ -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 diff --git a/lib/libc/gen/err.c b/lib/libc/gen/err.c index 71cd1d9d52aa..63ef8a4de346 100644 --- a/lib/libc/gen/err.c +++ b/lib/libc/gen/err.c @@ -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"); }