xref: /original-bsd/lib/libc/gen/err.c (revision c4695039)
1 /*-
2  * Copyright (c) 1993 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)err.c	5.2 (Berkeley) 03/19/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 	int eval;
103 	const char *fmt;
104 	va_dcl
105 #endif
106 {
107 	va_list ap;
108 #if __STDC__
109 	va_start(ap, fmt);
110 #else
111 	va_start(ap);
112 #endif
113 	vwarn(fmt, ap);
114 	va_end(ap);
115 }
116 
117 void
118 vwarn(fmt, ap)
119 	const char *fmt;
120 	va_list ap;
121 {
122 	int sverrno;
123 
124 	sverrno = errno;
125 	(void)fprintf(stderr, "%s: ", __progname);
126 	if (fmt != NULL) {
127 		(void)vfprintf(stderr, fmt, ap);
128 		(void)fprintf(stderr, ": ");
129 	}
130 	(void)fprintf(stderr, "%s\n", strerror(sverrno));
131 }
132 
133 void
134 #ifdef __STDC__
135 warnx(const char *fmt, ...)
136 #else
137 warnx(fmt, va_alist)
138 	int eval;
139 	const char *fmt;
140 	va_dcl
141 #endif
142 {
143 	va_list ap;
144 #ifdef __STDC__
145 	va_start(ap, fmt);
146 #else
147 	va_start(ap);
148 #endif
149 	vwarnx(fmt, ap);
150 	va_end(ap);
151 }
152 
153 void
154 vwarnx(fmt, ap)
155 	const char *fmt;
156 	va_list ap;
157 {
158 	(void)fprintf(stderr, "%s: ", __progname);
159 	if (fmt != NULL)
160 		(void)vfprintf(stderr, fmt, ap);
161 	(void)fprintf(stderr, "\n");
162 }
163