diff options
author | wollman <wollman@FreeBSD.org> | 1995-04-13 18:04:11 +0000 |
---|---|---|
committer | wollman <wollman@FreeBSD.org> | 1995-04-13 18:04:11 +0000 |
commit | ddebc4599e2be894d8a68c955ffc9e3e0d232e24 (patch) | |
tree | fbf6927f1eb28df85e2a0dd7f66f9d426763b114 /lib | |
parent | 234ee48c9e8ebbf0d2db32614b86983704b71010 (diff) | |
download | FreeBSD-src-ddebc4599e2be894d8a68c955ffc9e3e0d232e24.zip FreeBSD-src-ddebc4599e2be894d8a68c955ffc9e3e0d232e24.tar.gz |
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).
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/gen/err.3 | 24 | ||||
-rw-r--r-- | lib/libc/gen/err.c | 58 |
2 files changed, 64 insertions, 18 deletions
diff --git a/lib/libc/gen/err.3 b/lib/libc/gen/err.3 index 2a3f4c3..a80edeb 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 <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 diff --git a/lib/libc/gen/err.c b/lib/libc/gen/err.c index 71cd1d9..63ef8a4 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"); } |