Merge commit 'upstream/1.8.1p2'
[debian/sudo] / src / error.c
1 /*
2  * Copyright (c) 2004-2005, 2010 Todd C. Miller <Todd.Miller@courtesan.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  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "missing.h"
27 #include "error.h"
28
29 static void _warning(int, const char *, va_list);
30        void cleanup(int);
31
32 void
33 error(int eval, const char *fmt, ...)
34 {
35         va_list ap;
36         va_start(ap, fmt);
37         _warning(1, fmt, ap);
38         va_end(ap);
39         cleanup(0);
40         exit(eval);
41 }
42
43 void
44 errorx(int eval, const char *fmt, ...)
45 {
46         va_list ap;
47         va_start(ap, fmt);
48         _warning(0, fmt, ap);
49         va_end(ap);
50         cleanup(0);
51         exit(eval);
52 }
53
54 void
55 warning(const char *fmt, ...)
56 {
57         va_list ap;
58         va_start(ap, fmt);
59         _warning(1, fmt, ap);
60         va_end(ap);
61 }
62
63 void
64 warningx(const char *fmt, ...)
65 {
66         va_list ap;
67         va_start(ap, fmt);
68         _warning(0, fmt, ap);
69         va_end(ap);
70 }
71
72 static void
73 _warning(int use_errno, const char *fmt, va_list ap)
74 {
75         int serrno = errno;
76
77         fputs(getprogname(), stderr);
78         if (fmt != NULL) {
79                 fputs(": ", stderr);
80                 vfprintf(stderr, fmt, ap);
81         }
82         if (use_errno) {
83             fputs(": ", stderr);
84             fputs(strerror(serrno), stderr);
85         }
86         putc('\n', stderr);
87 }