Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "../config.h"
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 static void vwarn_impl(const char*, va_list);
26 static void vwarnx_impl(const char*, va_list);
28 static void
29 vwarn_impl(const char *fmt, va_list ap)
30 {
31 fprintf(stderr, "%s: ", getprogname());
32 vfprintf(stderr, fmt, ap);
33 fprintf(stderr, ": %s\n", strerror(errno));
34 }
36 static void
37 vwarnx_impl(const char *fmt, va_list ap)
38 {
39 fprintf(stderr, "%s: ", getprogname());
40 vfprintf(stderr, fmt, ap);
41 fprintf(stderr, "\n");
42 }
44 void
45 err(int ret, const char *fmt, ...)
46 {
47 va_list ap;
49 va_start(ap, fmt);
50 vwarn_impl(fmt, ap);
51 va_end(ap);
52 exit(ret);
53 }
55 void
56 errx(int ret, const char *fmt, ...)
57 {
58 va_list ap;
60 va_start(ap, fmt);
61 vwarnx_impl(fmt, ap);
62 va_end(ap);
63 exit(ret);
64 }
66 void
67 warn(const char *fmt, ...)
68 {
69 va_list ap;
71 va_start(ap, fmt);
72 vwarn_impl(fmt, ap);
73 va_end(ap);
74 }
76 void
77 warnx(const char *fmt, ...)
78 {
79 va_list ap;
81 va_start(ap, fmt);
82 vwarnx_impl(fmt, ap);
83 va_end(ap);
84 }