add doc about interaction with RAMRUN to README.Debian in response to #581393
[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 static void _warning    __P((int, const char *, va_list));
27        void cleanup     __P((int));
28
29 void
30 #ifdef __STDC__
31 error(int eval, const char *fmt, ...)
32 #else
33 error(eval, fmt, va_alist)
34         int eval;
35         const char *fmt;
36         va_dcl
37 #endif
38 {
39         va_list ap;
40 #ifdef __STDC__
41         va_start(ap, fmt);
42 #else
43         va_start(ap);
44 #endif
45         _warning(1, fmt, ap);
46         va_end(ap);
47         cleanup(0);
48         exit(eval);
49 }
50
51 void
52 #ifdef __STDC__
53 errorx(int eval, const char *fmt, ...)
54 #else
55 errorx(eval, fmt, va_alist)
56         int eval;
57         const char *fmt;
58         va_dcl
59 #endif
60 {
61         va_list ap;
62 #ifdef __STDC__
63         va_start(ap, fmt);
64 #else
65         va_start(ap);
66 #endif
67         _warning(0, fmt, ap);
68         va_end(ap);
69         cleanup(0);
70         exit(eval);
71 }
72
73 void
74 #ifdef __STDC__
75 warning(const char *fmt, ...)
76 #else
77 warning(fmt, va_alist)
78         const char *fmt;
79         va_dcl
80 #endif
81 {
82         va_list ap;
83 #ifdef __STDC__
84         va_start(ap, fmt);
85 #else
86         va_start(ap);
87 #endif
88         _warning(1, fmt, ap);
89         va_end(ap);
90 }
91
92 void
93 #ifdef __STDC__
94 warningx(const char *fmt, ...)
95 #else
96 warningx(fmt, va_alist)
97         const char *fmt;
98         va_dcl
99 #endif
100 {
101         va_list ap;
102 #ifdef __STDC__
103         va_start(ap, fmt);
104 #else
105         va_start(ap);
106 #endif
107         _warning(0, fmt, ap);
108         va_end(ap);
109 }
110
111 static void
112 _warning(use_errno, fmt, ap)
113         int use_errno;
114         const char *fmt;
115         va_list ap;
116 {
117         int serrno = errno;
118
119         fputs(getprogname(), stderr);
120         if (fmt != NULL) {
121                 fputs(": ", stderr);
122                 vfprintf(stderr, fmt, ap);
123         }
124         if (use_errno) {
125             fputs(": ", stderr);
126             fputs(strerror(serrno), stderr);
127         }
128         putc('\n', stderr);
129 }