]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/err.c
Libc-763.12.tar.gz
[apple/libc.git] / gen / FreeBSD / err.c
CommitLineData
9385eb3d 1/*-
e9ce8d39
A
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
e9ce8d39
A
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
9385eb3d
A
30#if defined(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93";
32#endif /* LIBC_SCCS and not lint */
33#include <sys/cdefs.h>
1f2f436a 34__FBSDID("$FreeBSD: src/lib/libc/gen/err.c,v 1.15 2008/04/03 20:36:44 imp Exp $");
e9ce8d39 35
9385eb3d 36#include "namespace.h"
e9ce8d39
A
37#include <err.h>
38#include <errno.h>
9385eb3d 39#include <stdarg.h>
e9ce8d39
A
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
9385eb3d 43#include "un-namespace.h"
e9ce8d39 44
9385eb3d 45#include "libc_private.h"
e9ce8d39 46
9385eb3d
A
47static FILE *err_file; /* file to use for error output */
48static void (*err_exit)(int);
49
50/*
51 * This is declared to take a `void *' so that the caller is not required
52 * to include <stdio.h> first. However, it is really a `FILE *', and the
53 * manual page documents it as such.
54 */
55void
56err_set_file(void *fp)
57{
58 if (fp)
59 err_file = fp;
60 else
61 err_file = stderr;
62}
63
64void
65err_set_exit(void (*ef)(int))
66{
67 err_exit = ef;
68}
69
70__weak_reference(_err, err);
71
72void
73_err(int eval, const char *fmt, ...)
e9ce8d39
A
74{
75 va_list ap;
e9ce8d39 76 va_start(ap, fmt);
9385eb3d 77 verrc(eval, errno, fmt, ap);
e9ce8d39
A
78 va_end(ap);
79}
80
9385eb3d 81void
e9ce8d39
A
82verr(eval, fmt, ap)
83 int eval;
84 const char *fmt;
85 va_list ap;
86{
9385eb3d
A
87 verrc(eval, errno, fmt, ap);
88}
e9ce8d39 89
9385eb3d
A
90void
91errc(int eval, int code, const char *fmt, ...)
92{
93 va_list ap;
94 va_start(ap, fmt);
95 verrc(eval, code, fmt, ap);
96 va_end(ap);
97}
98
99void
1f2f436a 100verrc(int eval, int code, const char *fmt, va_list ap)
9385eb3d
A
101{
102 if (err_file == 0)
103 err_set_file((FILE *)0);
104 fprintf(err_file, "%s: ", _getprogname());
e9ce8d39 105 if (fmt != NULL) {
9385eb3d
A
106 vfprintf(err_file, fmt, ap);
107 fprintf(err_file, ": ");
e9ce8d39 108 }
9385eb3d
A
109 fprintf(err_file, "%s\n", strerror(code));
110 if (err_exit)
111 err_exit(eval);
e9ce8d39
A
112 exit(eval);
113}
114
9385eb3d 115void
e9ce8d39 116errx(int eval, const char *fmt, ...)
e9ce8d39
A
117{
118 va_list ap;
e9ce8d39 119 va_start(ap, fmt);
e9ce8d39
A
120 verrx(eval, fmt, ap);
121 va_end(ap);
122}
123
9385eb3d 124void
1f2f436a 125verrx(int eval, const char *fmt, va_list ap)
e9ce8d39 126{
9385eb3d
A
127 if (err_file == 0)
128 err_set_file((FILE *)0);
129 fprintf(err_file, "%s: ", _getprogname());
e9ce8d39 130 if (fmt != NULL)
9385eb3d
A
131 vfprintf(err_file, fmt, ap);
132 fprintf(err_file, "\n");
133 if (err_exit)
134 err_exit(eval);
e9ce8d39
A
135 exit(eval);
136}
137
9385eb3d
A
138__weak_reference(_warn, warn);
139
e9ce8d39 140void
9385eb3d 141_warn(const char *fmt, ...)
e9ce8d39
A
142{
143 va_list ap;
e9ce8d39 144 va_start(ap, fmt);
9385eb3d 145 vwarnc(errno, fmt, ap);
e9ce8d39
A
146 va_end(ap);
147}
148
149void
1f2f436a 150vwarn(const char *fmt, va_list ap)
e9ce8d39 151{
9385eb3d
A
152 vwarnc(errno, fmt, ap);
153}
154
155void
156warnc(int code, const char *fmt, ...)
157{
158 va_list ap;
159 va_start(ap, fmt);
160 vwarnc(code, fmt, ap);
161 va_end(ap);
162}
163
164void
1f2f436a 165vwarnc(int code, const char *fmt, va_list ap)
9385eb3d
A
166{
167 if (err_file == 0)
168 err_set_file((FILE *)0);
169 fprintf(err_file, "%s: ", _getprogname());
e9ce8d39 170 if (fmt != NULL) {
9385eb3d
A
171 vfprintf(err_file, fmt, ap);
172 fprintf(err_file, ": ");
e9ce8d39 173 }
9385eb3d 174 fprintf(err_file, "%s\n", strerror(code));
e9ce8d39
A
175}
176
177void
e9ce8d39 178warnx(const char *fmt, ...)
e9ce8d39
A
179{
180 va_list ap;
e9ce8d39 181 va_start(ap, fmt);
e9ce8d39
A
182 vwarnx(fmt, ap);
183 va_end(ap);
184}
185
186void
1f2f436a 187vwarnx(const char *fmt, va_list ap)
e9ce8d39 188{
9385eb3d
A
189 if (err_file == 0)
190 err_set_file((FILE *)0);
191 fprintf(err_file, "%s: ", _getprogname());
e9ce8d39 192 if (fmt != NULL)
9385eb3d
A
193 vfprintf(err_file, fmt, ap);
194 fprintf(err_file, "\n");
e9ce8d39 195}