From b4b4fb871e1e7fb242285af7a06e4f62d6c1fad7 Mon Sep 17 00:00:00 2001 From: Garrett Wollman Date: Sat, 12 Sep 1998 21:02:22 +0000 Subject: [PATCH] A small last-minute iitem for 3.0: - Fix some style errors I made back in 1995. - Add a new flavor of the err(3) family, which takes an explicit errno argument rather than implicitly examining errno. This will make it easier to use these functions in conjunction with modern library interfaces that return an errno value explicitly. --- include/err.h | 7 ++- lib/libc/gen/err.3 | 70 +++++++++++++++++---- lib/libc/gen/err.c | 152 +++++++++++++++++++++------------------------ 3 files changed, 137 insertions(+), 92 deletions(-) diff --git a/include/err.h b/include/err.h index fd02a61b8b99..a3464af86a8d 100644 --- a/include/err.h +++ b/include/err.h @@ -30,7 +30,8 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)err.h 8.1 (Berkeley) 6/2/93 + * From: @(#)err.h 8.1 (Berkeley) 6/2/93 + * $Id$ */ #ifndef _ERR_H_ @@ -49,10 +50,14 @@ __BEGIN_DECLS void err __P((int, const char *, ...)) __dead2; void verr __P((int, const char *, _BSD_VA_LIST_)) __dead2; +void errc __P((int, int, const char *, ...)) __dead2; +void verrc __P((int, int, const char *, _BSD_VA_LIST_)) __dead2; void errx __P((int, const char *, ...)) __dead2; void verrx __P((int, const char *, _BSD_VA_LIST_)) __dead2; void warn __P((const char *, ...)); void vwarn __P((const char *, _BSD_VA_LIST_)); +void warnc __P((int, const char *, ...)); +void vwarnc __P((int, const char *, _BSD_VA_LIST_)); void warnx __P((const char *, ...)); void vwarnx __P((const char *, _BSD_VA_LIST_)); void err_set_file __P((void *)); diff --git a/lib/libc/gen/err.3 b/lib/libc/gen/err.3 index 698e6de3b2a6..1654a6b5c123 100644 --- a/lib/libc/gen/err.3 +++ b/lib/libc/gen/err.3 @@ -30,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" From: @(#)err.3 8.1 (Berkeley) 6/9/93 -.\" $Id: err.3,v 1.7 1997/03/19 00:43:13 bde Exp $ +.\" $Id: err.3,v 1.8 1998/04/22 19:59:55 rnordier Exp $ .\" .Dd April 13, 1995 .Dt ERR 3 @@ -38,10 +38,14 @@ .Sh NAME .Nm err , .Nm verr , +.Nm errc , +.Nm verrc , .Nm errx , .Nm verrx , .Nm warn , .Nm vwarn , +.Nm warnc , +.Nm vwarnc , .Nm warnx , .Nm vwarnx , .Nm err_set_file , @@ -52,23 +56,32 @@ .Ft void .Fn err "int eval" "const char *fmt" "..." .Ft void +.Fn errc "int eval" "int code" "const char *fmt" "..." +.Ft void .Fn errx "int eval" "const char *fmt" "..." .Ft void .Fn warn "const char *fmt" "..." .Ft void -.Fn warnx "const char *fmt" "..." +.Fn warnc "int code" "const char *fmt" "..." .Ft void -.Fn err_set_file "void *fp" +.Fn warnx "const char *fmt" "..." +.Fd #include +.Ft void +.Fn err_set_file "FILE *fp" .Ft void .Fn err_set_exit "void (*exitf)(int)" .Fd #include .Ft void .Fn verr "int eval" "const char *fmt" "va_list args" .Ft void +.Fn verrc "int eval" "int code" "const char *fmt" "va_list args" +.Ft void .Fn verrx "int eval" "const char *fmt" "va_list args" .Ft void .Fn vwarn "const char *fmt" "va_list args" .Ft void +.Fn vwarnc "int code" "const char *fmt" "va_list args" +.Ft void .Fn vwarnx "const char *fmt" "va_list args" .Sh DESCRIPTION The @@ -85,22 +98,37 @@ If the .Va fmt argument is not NULL, the formatted error message is output. In the case of the -.Fn err , -.Fn verr , -.Fn warn , +.Fn errc , +.Fn verrc , +.Fn warnc , and -.Fn vwarn +.Fn vwarnc functions, -the error message string affiliated with the current value of -the global variable -.Va errno -is also output, +the error message string affiliated with the +.Va code +argument is also output, preceded by another colon and space if necessary. In all cases, the output is followed by a newline character. .Pp The .Fn err , .Fn verr , +.Fn warn , +and +.Fn vwarn +functions use the global variable +.Va errno +rather than the +.Va code +argument of the +.Fn errc +family +.Pp +The +.Fn err , +.Fn verr , +.Fn errc , +.Fn verrc , .Fn errx , and .Fn verrx @@ -136,6 +164,14 @@ if ((fd = open(raw_device, O_RDONLY, 0)) == -1) if ((fd = open(block_device, O_RDONLY, 0)) == -1) err(1, "%s", block_device); .Ed +.Pp +Warn of an error without using the global variable +.Va errno : +.Bd -literal -offset indent +error = my_function(); /* returns a value from */ +if (error != 0) + warnc(error, "my_function"); +.Ed .Sh SEE ALSO .Xr exit 3 , .Xr strerror 3 @@ -146,3 +182,15 @@ and .Fn warn functions first appeared in .Bx 4.4 . +The +.Fn err_set_file +and +.Fn err_set_exit +functions first appeared in +.Fx 2.1 . +The +.Fn errc +and +.Fn warnc +functions first appeared in +.Fx 3.0 . diff --git a/lib/libc/gen/err.c b/lib/libc/gen/err.c index bf7d3470515c..ede146563151 100644 --- a/lib/libc/gen/err.c +++ b/lib/libc/gen/err.c @@ -29,11 +29,13 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * From: @(#)err.c 8.1 (Berkeley) 6/4/93 */ -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; -#endif /* LIBC_SCCS and not lint */ +#if defined(LIBC_RCS) && !defined(lint) +static const char rcsid[] = + "$Id$"; +#endif /* LIBC_RCS and not lint */ #include #include @@ -41,17 +43,18 @@ static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; #include #include -#ifdef __STDC__ #include -#else -#include -#endif extern char *__progname; /* Program name, from crt0. */ static FILE *err_file; /* file to use for error output */ static void (*err_exit)(int); +/* + * This is declared to take a `void *' so that the caller is not required + * to include first. However, it is really a `FILE *', and the + * manual page documents it as such. + */ void err_set_file(void *fp) { @@ -68,22 +71,11 @@ err_set_exit(void (*ef)(int)) } void -#ifdef __STDC__ err(int eval, const char *fmt, ...) -#else -err(eval, fmt, va_alist) - int eval; - const char *fmt; - va_dcl -#endif { va_list ap; -#if __STDC__ va_start(ap, fmt); -#else - va_start(ap); -#endif - verr(eval, fmt, ap); + verrc(eval, errno, fmt, ap); va_end(ap); } @@ -93,38 +85,43 @@ verr(eval, fmt, ap) const char *fmt; va_list ap; { - int sverrno; + verrc(eval, errno, fmt, ap); +} - sverrno = errno; - if (! err_file) +void +errc(int eval, int code, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verrc(eval, code, fmt, ap); + va_end(ap); +} + +void +verrc(eval, code, fmt, ap) + int eval; + int code; + const char *fmt; + va_list ap; +{ + if (err_file == 0) err_set_file((FILE *)0); - (void)fprintf(err_file, "%s: ", __progname); + fprintf(err_file, "%s: ", __progname); if (fmt != NULL) { - (void)vfprintf(err_file, fmt, ap); - (void)fprintf(err_file, ": "); + vfprintf(err_file, fmt, ap); + fprintf(err_file, ": "); } - (void)fprintf(err_file, "%s\n", strerror(sverrno)); - if(err_exit) + fprintf(err_file, "%s\n", strerror(code)); + if (err_exit) err_exit(eval); exit(eval); } void -#if __STDC__ errx(int eval, const char *fmt, ...) -#else -errx(eval, fmt, va_alist) - int eval; - const char *fmt; - va_dcl -#endif { va_list ap; -#if __STDC__ va_start(ap, fmt); -#else - va_start(ap); -#endif verrx(eval, fmt, ap); va_end(ap); } @@ -135,33 +132,23 @@ verrx(eval, fmt, ap) const char *fmt; va_list ap; { - if (! err_file) + if (err_file == 0) err_set_file((FILE *)0); - (void)fprintf(err_file, "%s: ", __progname); + fprintf(err_file, "%s: ", __progname); if (fmt != NULL) - (void)vfprintf(err_file, fmt, ap); - (void)fprintf(err_file, "\n"); + vfprintf(err_file, fmt, ap); + fprintf(err_file, "\n"); if (err_exit) err_exit(eval); exit(eval); } void -#if __STDC__ warn(const char *fmt, ...) -#else -warn(fmt, va_alist) - const char *fmt; - va_dcl -#endif { va_list ap; -#if __STDC__ va_start(ap, fmt); -#else - va_start(ap); -#endif - vwarn(fmt, ap); + vwarnc(errno, fmt, ap); va_end(ap); } @@ -170,35 +157,40 @@ vwarn(fmt, ap) const char *fmt; va_list ap; { - int sverrno; - - sverrno = errno; - if (! err_file) - err_set_file((FILE *)0); - (void)fprintf(err_file, "%s: ", __progname); - if (fmt != NULL) { - (void)vfprintf(err_file, fmt, ap); - (void)fprintf(err_file, ": "); - } - (void)fprintf(err_file, "%s\n", strerror(sverrno)); + vwarnc(errno, fmt, ap); } void -#ifdef __STDC__ -warnx(const char *fmt, ...) -#else -warnx(fmt, va_alist) - const char *fmt; - va_dcl -#endif +warnc(int code, const char *fmt, ...) { va_list ap; -#ifdef __STDC__ va_start(ap, fmt); -#else - va_start(ap); -#endif - vwarnx(fmt, ap); + vwarnc(code, fmt, ap); + va_end(ap); +} + +void +vwarnc(code, fmt, ap) + int code; + const char *fmt; + va_list ap; +{ + if (err_file == 0) + err_set_file((FILE *)0); + fprintf(err_file, "%s: ", __progname); + if (fmt != NULL) { + vfprintf(err_file, fmt, ap); + fprintf(err_file, ": "); + } + fprintf(err_file, "%s\n", strerror(code)); +} + +void +warnx(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarn(fmt, ap); va_end(ap); } @@ -207,10 +199,10 @@ vwarnx(fmt, ap) const char *fmt; va_list ap; { - if (! err_file) + if (err_file == 0) err_set_file((FILE *)0); - (void)fprintf(err_file, "%s: ", __progname); + fprintf(err_file, "%s: ", __progname); if (fmt != NULL) - (void)vfprintf(err_file, fmt, ap); - (void)fprintf(err_file, "\n"); + vfprintf(err_file, fmt, ap); + fprintf(err_file, "\n"); }