Imported Upstream version 1.8.7
[debian/sudo] / common / sudo_printf.c
1 /*
2  * Copyright (c) 2010-2012 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 #include <stdio.h>
21 #ifdef STDC_HEADERS
22 # include <stdlib.h>
23 # include <stddef.h>
24 #else
25 # ifdef HAVE_STDLIB_H
26 #  include <stdlib.h>
27 # endif
28 #endif /* STDC_HEADERS */
29 #include <stdarg.h>
30 #include <errno.h>
31
32 #include "missing.h"
33 #include "sudo_plugin.h"
34 #include "sudo_debug.h"
35
36 int
37 _sudo_printf(int msg_type, const char *fmt, ...)
38 {
39     va_list ap;
40     char *buf;
41     int len = -1;
42
43     switch (msg_type) {
44     case SUDO_CONV_INFO_MSG:
45         va_start(ap, fmt);
46         len = vfprintf(stdout, fmt, ap);
47         va_end(ap);
48         break;
49     case SUDO_CONV_ERROR_MSG:
50         va_start(ap, fmt);
51         len = vfprintf(stderr, fmt, ap);
52         va_end(ap);
53         break;
54     case SUDO_CONV_DEBUG_MSG:
55         /* XXX - add debug version of vfprintf()? */
56         va_start(ap, fmt);
57         len = vasprintf(&buf, fmt, ap);
58         va_end(ap);
59         if (len != -1)
60             sudo_debug_write(buf, len, 0);
61         break;
62     default:
63         errno = EINVAL;
64         break;
65     }
66
67     return len;
68 }
69
70 sudo_printf_t sudo_printf = _sudo_printf;