summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwollman <wollman@FreeBSD.org>1998-09-12 21:02:22 +0000
committerwollman <wollman@FreeBSD.org>1998-09-12 21:02:22 +0000
commit714948938bb3a857ee0b2a1a96699da355945a00 (patch)
tree4019dfbd1d3d423e72c58b7c3a178e5c9f084e59
parentf549419b9d0103fba3ed7f225896f3127b7508f6 (diff)
downloadFreeBSD-src-714948938bb3a857ee0b2a1a96699da355945a00.zip
FreeBSD-src-714948938bb3a857ee0b2a1a96699da355945a00.tar.gz
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.
-rw-r--r--include/err.h7
-rw-r--r--lib/libc/gen/err.366
-rw-r--r--lib/libc/gen/err.c142
3 files changed, 130 insertions, 85 deletions
diff --git a/include/err.h b/include/err.h
index fd02a61..a3464af 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 698e6de..1654a6b 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 warnc "int code" "const char *fmt" "..."
+.Ft void
.Fn warnx "const char *fmt" "..."
+.Fd #include <stdio.h>
.Ft void
-.Fn err_set_file "void *fp"
+.Fn err_set_file "FILE *fp"
.Ft void
.Fn err_set_exit "void (*exitf)(int)"
.Fd #include <stdarg.h>
.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 errc ,
+.Fn verrc ,
+.Fn warnc ,
+and
+.Fn vwarnc
+functions,
+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,
-the error message string affiliated with the current value of
-the global variable
-.Va errno
-is also output,
-preceded by another colon and space if necessary.
-In all cases, the output is followed by a newline character.
+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 <errno.h> */
+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 bf7d347..ede1465 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 <err.h>
#include <errno.h>
@@ -41,17 +43,18 @@ static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93";
#include <stdlib.h>
#include <string.h>
-#ifdef __STDC__
#include <stdarg.h>
-#else
-#include <varargs.h>
-#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 <stdio.h> 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);
+}
+
+void
+errc(int eval, int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verrc(eval, code, fmt, ap);
+ va_end(ap);
+}
- sverrno = errno;
- if (! err_file)
+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;
+ vwarnc(errno, fmt, ap);
+}
- sverrno = errno;
- if (! err_file)
+void
+warnc(int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ 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);
- (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));
+ fprintf(err_file, "%s\n", strerror(code));
}
void
-#ifdef __STDC__
warnx(const char *fmt, ...)
-#else
-warnx(fmt, va_alist)
- const char *fmt;
- va_dcl
-#endif
{
va_list ap;
-#ifdef __STDC__
va_start(ap, fmt);
-#else
- va_start(ap);
-#endif
- vwarnx(fmt, ap);
+ 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");
}
OpenPOWER on IntegriCloud