11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 4. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
3090049Sobrien#if defined(LIBC_SCCS) && !defined(lint)
3190096Sbdestatic char sccsid[] = "@(#)err.c	8.1 (Berkeley) 6/4/93";
3290049Sobrien#endif /* LIBC_SCCS and not lint */
3390039Sobrien#include <sys/cdefs.h>
3490039Sobrien__FBSDID("$FreeBSD$");
351573Srgrimes
3682496Sbde#include "namespace.h"
371573Srgrimes#include <err.h>
381573Srgrimes#include <errno.h>
3993399Smarkm#include <stdarg.h>
401573Srgrimes#include <stdio.h>
411573Srgrimes#include <stdlib.h>
421573Srgrimes#include <string.h>
4393399Smarkm#include "un-namespace.h"
441573Srgrimes
4593399Smarkm#include "libc_private.h"
461573Srgrimes
477803Swollmanstatic FILE *err_file; /* file to use for error output */
487803Swollmanstatic void (*err_exit)(int);
497803Swollman
5039112Swollman/*
5139112Swollman * This is declared to take a `void *' so that the caller is not required
5239112Swollman * to include <stdio.h> first.  However, it is really a `FILE *', and the
5339112Swollman * manual page documents it as such.
5439112Swollman */
557803Swollmanvoid
567803Swollmanerr_set_file(void *fp)
577803Swollman{
587803Swollman	if (fp)
597803Swollman		err_file = fp;
607803Swollman	else
617803Swollman		err_file = stderr;
627803Swollman}
637803Swollman
647803Swollmanvoid
657803Swollmanerr_set_exit(void (*ef)(int))
667803Swollman{
677803Swollman	err_exit = ef;
687803Swollman}
697803Swollman
7086250Sbde__weak_reference(_err, err);
7186250Sbde
7218286Sbdevoid
7386250Sbde_err(int eval, const char *fmt, ...)
7439112Swollman{
7539112Swollman	va_list ap;
7639112Swollman	va_start(ap, fmt);
7739112Swollman	verrc(eval, errno, fmt, ap);
7839112Swollman	va_end(ap);
7939112Swollman}
8039112Swollman
8139112Swollmanvoid
8239112Swollmanverr(eval, fmt, ap)
831573Srgrimes	int eval;
841573Srgrimes	const char *fmt;
8539112Swollman	va_list ap;
861573Srgrimes{
8739112Swollman	verrc(eval, errno, fmt, ap);
8839112Swollman}
8939112Swollman
9039112Swollmanvoid
9139112Swollmanerrc(int eval, int code, const char *fmt, ...)
9239112Swollman{
931573Srgrimes	va_list ap;
941573Srgrimes	va_start(ap, fmt);
9539112Swollman	verrc(eval, code, fmt, ap);
961573Srgrimes	va_end(ap);
971573Srgrimes}
981573Srgrimes
9918286Sbdevoid
100177893Simpverrc(int eval, int code, const char *fmt, va_list ap)
1011573Srgrimes{
10239112Swollman	if (err_file == 0)
1037803Swollman		err_set_file((FILE *)0);
10493399Smarkm	fprintf(err_file, "%s: ", _getprogname());
1051573Srgrimes	if (fmt != NULL) {
10639112Swollman		vfprintf(err_file, fmt, ap);
10739112Swollman		fprintf(err_file, ": ");
1081573Srgrimes	}
10939112Swollman	fprintf(err_file, "%s\n", strerror(code));
11039112Swollman	if (err_exit)
1117803Swollman		err_exit(eval);
1121573Srgrimes	exit(eval);
1131573Srgrimes}
1141573Srgrimes
11518286Sbdevoid
1161573Srgrimeserrx(int eval, const char *fmt, ...)
1171573Srgrimes{
1181573Srgrimes	va_list ap;
1191573Srgrimes	va_start(ap, fmt);
1201573Srgrimes	verrx(eval, fmt, ap);
1211573Srgrimes	va_end(ap);
1221573Srgrimes}
1231573Srgrimes
12418286Sbdevoid
125177893Simpverrx(int eval, const char *fmt, va_list ap)
1261573Srgrimes{
12739112Swollman	if (err_file == 0)
1287803Swollman		err_set_file((FILE *)0);
12993399Smarkm	fprintf(err_file, "%s: ", _getprogname());
1301573Srgrimes	if (fmt != NULL)
13139112Swollman		vfprintf(err_file, fmt, ap);
13239112Swollman	fprintf(err_file, "\n");
1337803Swollman	if (err_exit)
1347803Swollman		err_exit(eval);
1351573Srgrimes	exit(eval);
1361573Srgrimes}
1371573Srgrimes
13882496Sbde__weak_reference(_warn, warn);
13982496Sbde
1401573Srgrimesvoid
14182496Sbde_warn(const char *fmt, ...)
1421573Srgrimes{
1431573Srgrimes	va_list ap;
1441573Srgrimes	va_start(ap, fmt);
14539112Swollman	vwarnc(errno, fmt, ap);
1461573Srgrimes	va_end(ap);
1471573Srgrimes}
1481573Srgrimes
1491573Srgrimesvoid
150177893Simpvwarn(const char *fmt, va_list ap)
1511573Srgrimes{
15239112Swollman	vwarnc(errno, fmt, ap);
15339112Swollman}
1541573Srgrimes
15539112Swollmanvoid
15639112Swollmanwarnc(int code, const char *fmt, ...)
15739112Swollman{
15839112Swollman	va_list ap;
15939112Swollman	va_start(ap, fmt);
16039112Swollman	vwarnc(code, fmt, ap);
16139112Swollman	va_end(ap);
16239112Swollman}
16339112Swollman
16439112Swollmanvoid
165177893Simpvwarnc(int code, const char *fmt, va_list ap)
16639112Swollman{
16739112Swollman	if (err_file == 0)
1687803Swollman		err_set_file((FILE *)0);
16993399Smarkm	fprintf(err_file, "%s: ", _getprogname());
1701573Srgrimes	if (fmt != NULL) {
17139112Swollman		vfprintf(err_file, fmt, ap);
17239112Swollman		fprintf(err_file, ": ");
1731573Srgrimes	}
17439112Swollman	fprintf(err_file, "%s\n", strerror(code));
1751573Srgrimes}
1761573Srgrimes
1771573Srgrimesvoid
1781573Srgrimeswarnx(const char *fmt, ...)
1791573Srgrimes{
1801573Srgrimes	va_list ap;
1811573Srgrimes	va_start(ap, fmt);
18239202Swollman	vwarnx(fmt, ap);
1831573Srgrimes	va_end(ap);
1841573Srgrimes}
1851573Srgrimes
1861573Srgrimesvoid
187177893Simpvwarnx(const char *fmt, va_list ap)
1881573Srgrimes{
18939112Swollman	if (err_file == 0)
1907803Swollman		err_set_file((FILE *)0);
19193399Smarkm	fprintf(err_file, "%s: ", _getprogname());
1921573Srgrimes	if (fmt != NULL)
19339112Swollman		vfprintf(err_file, fmt, ap);
19439112Swollman	fprintf(err_file, "\n");
1951573Srgrimes}
196