xref: /original-bsd/lib/libc/gen/err.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)err.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <err.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #ifdef __STDC__
19 #include <stdarg.h>
20 #else
21 #include <varargs.h>
22 #endif
23 
24 extern char *__progname;		/* Program name, from crt0. */
25 
26 __dead void
27 #ifdef __STDC__
28 err(int eval, const char *fmt, ...)
29 #else
30 err(eval, fmt, va_alist)
31 	int eval;
32 	const char *fmt;
33 	va_dcl
34 #endif
35 {
36 	va_list ap;
37 #if __STDC__
38 	va_start(ap, fmt);
39 #else
40 	va_start(ap);
41 #endif
42 	verr(eval, fmt, ap);
43 	va_end(ap);
44 }
45 
46 __dead void
47 verr(eval, fmt, ap)
48 	int eval;
49 	const char *fmt;
50 	va_list ap;
51 {
52 	int sverrno;
53 
54 	sverrno = errno;
55 	(void)fprintf(stderr, "%s: ", __progname);
56 	if (fmt != NULL) {
57 		(void)vfprintf(stderr, fmt, ap);
58 		(void)fprintf(stderr, ": ");
59 	}
60 	(void)fprintf(stderr, "%s\n", strerror(sverrno));
61 	exit(eval);
62 }
63 
64 __dead void
65 #if __STDC__
66 errx(int eval, const char *fmt, ...)
67 #else
68 errx(eval, fmt, va_alist)
69 	int eval;
70 	const char *fmt;
71 	va_dcl
72 #endif
73 {
74 	va_list ap;
75 #if __STDC__
76 	va_start(ap, fmt);
77 #else
78 	va_start(ap);
79 #endif
80 	verrx(eval, fmt, ap);
81 	va_end(ap);
82 }
83 
84 __dead void
85 verrx(eval, fmt, ap)
86 	int eval;
87 	const char *fmt;
88 	va_list ap;
89 {
90 	(void)fprintf(stderr, "%s: ", __progname);
91 	if (fmt != NULL)
92 		(void)vfprintf(stderr, fmt, ap);
93 	(void)fprintf(stderr, "\n");
94 	exit(eval);
95 }
96 
97 void
98 #if __STDC__
99 warn(const char *fmt, ...)
100 #else
101 warn(fmt, va_alist)
102 	const char *fmt;
103 	va_dcl
104 #endif
105 {
106 	va_list ap;
107 #if __STDC__
108 	va_start(ap, fmt);
109 #else
110 	va_start(ap);
111 #endif
112 	vwarn(fmt, ap);
113 	va_end(ap);
114 }
115 
116 void
117 vwarn(fmt, ap)
118 	const char *fmt;
119 	va_list ap;
120 {
121 	int sverrno;
122 
123 	sverrno = errno;
124 	(void)fprintf(stderr, "%s: ", __progname);
125 	if (fmt != NULL) {
126 		(void)vfprintf(stderr, fmt, ap);
127 		(void)fprintf(stderr, ": ");
128 	}
129 	(void)fprintf(stderr, "%s\n", strerror(sverrno));
130 }
131 
132 void
133 #ifdef __STDC__
134 warnx(const char *fmt, ...)
135 #else
136 warnx(fmt, va_alist)
137 	const char *fmt;
138 	va_dcl
139 #endif
140 {
141 	va_list ap;
142 #ifdef __STDC__
143 	va_start(ap, fmt);
144 #else
145 	va_start(ap);
146 #endif
147 	vwarnx(fmt, ap);
148 	va_end(ap);
149 }
150 
151 void
152 vwarnx(fmt, ap)
153 	const char *fmt;
154 	va_list ap;
155 {
156 	(void)fprintf(stderr, "%s: ", __progname);
157 	if (fmt != NULL)
158 		(void)vfprintf(stderr, fmt, ap);
159 	(void)fprintf(stderr, "\n");
160 }
161