comparison err.c @ 0:73af139d1a94

Initial revision
author Guido Berhoerster <guido+sencrypt@berhoerster.name>
date Tue, 28 Jan 2014 19:23:16 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:73af139d1a94
1 /*
2 * Copyright (C) 2011 Guido Berhoerster <guido+sencrypt@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "err.h"
30
31 static char __progname[] = "unknown";
32
33 void
34 err(int eval, const char *fmt, ...)
35 {
36 va_list args;
37
38 va_start(args, fmt);
39 vwarn(fmt, args);
40 va_end(args);
41
42 exit(eval);
43 }
44
45 void
46 errx(int eval, const char *fmt, ...)
47 {
48 va_list args;
49
50 va_start(args, fmt);
51 vwarnx(fmt, args);
52 va_end(args);
53
54 exit(eval);
55 }
56
57 void
58 warn(const char *fmt, ...)
59 {
60 va_list args;
61
62 va_start(args, fmt);
63 vwarn(fmt, args);
64 va_end(args);
65 }
66
67 void
68 warnx(const char *fmt, ...)
69 {
70 va_list args;
71
72 va_start(args, fmt);
73 vwarnx(fmt, args);
74 va_end(args);
75 }
76
77 void
78 verr(int eval, const char *fmt, va_list args)
79 {
80 vwarn(fmt, args);
81
82 exit(eval);
83 }
84
85 void
86 verrx(int eval, const char *fmt, va_list args)
87 {
88 vwarnx(fmt, args);
89
90 exit(eval);
91 }
92
93 void
94 vwarn(const char *fmt, va_list args)
95 {
96 int old_errno = errno;
97
98 fprintf(stderr, "%s: ", __progname);
99 if (fmt != NULL) {
100 vfprintf(stderr, fmt, args);
101 fprintf(stderr, ": ");
102 }
103 fprintf(stderr, "%s\n", strerror(old_errno));
104 errno = old_errno;
105 }
106
107 void
108 vwarnx(const char *fmt, va_list args)
109 {
110 fprintf(stderr, "%s: ", __progname);
111 if (fmt != NULL) {
112 vfprintf(stderr, fmt, args);
113 }
114 fputc('\n', stderr);
115 }