switch from rcS.d to rc[0-6].d
[debian/sudo] / error.c
1 /*
2  * Copyright (c) 2004-2005 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 <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <config.h>
23 #include <compat.h>
24 #include "error.h"
25
26 #ifndef lint
27 __unused static const char rcsid[] = "$Sudo: error.c,v 1.7 2005/11/18 01:39:58 millert Exp $";
28 #endif /* lint */
29
30 static void _warning    __P((int, const char *, va_list));
31        void cleanup     __P((int));
32
33 void
34 #ifdef __STDC__
35 error(int eval, const char *fmt, ...)
36 #else
37 error(eval, fmt, va_alist)
38         int eval;
39         const char *fmt;
40         va_dcl
41 #endif
42 {
43         va_list ap;
44 #ifdef __STDC__
45         va_start(ap, fmt);
46 #else
47         va_start(ap);
48 #endif
49         _warning(1, fmt, ap);
50         va_end(ap);
51         cleanup(0);
52         exit(eval);
53 }
54
55 void
56 #ifdef __STDC__
57 errorx(int eval, const char *fmt, ...)
58 #else
59 errorx(eval, fmt, va_alist)
60         int eval;
61         const char *fmt;
62         va_dcl
63 #endif
64 {
65         va_list ap;
66 #ifdef __STDC__
67         va_start(ap, fmt);
68 #else
69         va_start(ap);
70 #endif
71         _warning(0, fmt, ap);
72         va_end(ap);
73         cleanup(0);
74         exit(eval);
75 }
76
77 void
78 #ifdef __STDC__
79 warning(const char *fmt, ...)
80 #else
81 warning(fmt, va_alist)
82         const char *fmt;
83         va_dcl
84 #endif
85 {
86         va_list ap;
87 #ifdef __STDC__
88         va_start(ap, fmt);
89 #else
90         va_start(ap);
91 #endif
92         _warning(1, fmt, ap);
93         va_end(ap);
94 }
95
96 void
97 #ifdef __STDC__
98 warningx(const char *fmt, ...)
99 #else
100 warningx(fmt, va_alist)
101         const char *fmt;
102         va_dcl
103 #endif
104 {
105         va_list ap;
106 #ifdef __STDC__
107         va_start(ap, fmt);
108 #else
109         va_start(ap);
110 #endif
111         _warning(0, fmt, ap);
112         va_end(ap);
113 }
114
115 static void
116 _warning(use_errno, fmt, ap)
117         int use_errno;
118         const char *fmt;
119         va_list ap;
120 {
121         int serrno = errno;
122
123         fputs(getprogname(), stderr);
124         if (fmt != NULL) {
125                 fputs(": ", stderr);
126                 vfprintf(stderr, fmt, ap);
127         }
128         if (use_errno) {
129             fputs(": ", stderr);
130             fputs(strerror(serrno), stderr);
131         }
132         putc('\n', stderr);
133 }