projects/sencrypt

view err.c @ 21:5f9dc8f3c53e

Release version 3
author Guido Berhoerster <guido+sencrypt@berhoerster.name>
date Mon Aug 05 11:19:02 2019 +0200 (2019-08-05)
parents
children
line source
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 */
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
29 #include "err.h"
31 static char __progname[] = "unknown";
33 void
34 err(int eval, const char *fmt, ...)
35 {
36 va_list args;
38 va_start(args, fmt);
39 vwarn(fmt, args);
40 va_end(args);
42 exit(eval);
43 }
45 void
46 errx(int eval, const char *fmt, ...)
47 {
48 va_list args;
50 va_start(args, fmt);
51 vwarnx(fmt, args);
52 va_end(args);
54 exit(eval);
55 }
57 void
58 warn(const char *fmt, ...)
59 {
60 va_list args;
62 va_start(args, fmt);
63 vwarn(fmt, args);
64 va_end(args);
65 }
67 void
68 warnx(const char *fmt, ...)
69 {
70 va_list args;
72 va_start(args, fmt);
73 vwarnx(fmt, args);
74 va_end(args);
75 }
77 void
78 verr(int eval, const char *fmt, va_list args)
79 {
80 vwarn(fmt, args);
82 exit(eval);
83 }
85 void
86 verrx(int eval, const char *fmt, va_list args)
87 {
88 vwarnx(fmt, args);
90 exit(eval);
91 }
93 void
94 vwarn(const char *fmt, va_list args)
95 {
96 int old_errno = errno;
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 }
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 }