r23792: convert Samba4 to GPLv3
[kai/samba.git] / source4 / lib / util / dprintf.c
1 /* 
2    Unix SMB/CIFS implementation.
3    display print functions
4    Copyright (C) Andrew Tridgell 2001
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 /*
22   this module provides functions for printing internal strings in the "display charset"
23   This charset may be quite different from the chosen unix charset
24
25   Eventually these functions will need to take care of column count constraints
26
27   The d_ prefix on print functions in Samba refers to the display character set
28   conversion
29 */
30
31 #include "includes.h"
32 #include "system/locale.h"
33
34 _PUBLIC_ int d_vfprintf(FILE *f, const char *format, va_list ap) _PRINTF_ATTRIBUTE(2,0)
35 {
36         char *p, *p2;
37         int ret, maxlen, clen;
38         va_list ap2;
39
40         /* do any message translations */
41         va_copy(ap2, ap);
42         ret = vasprintf(&p, format, ap2);
43         va_end(ap2);
44
45         if (ret <= 0) return ret;
46
47         /* now we have the string in unix format, convert it to the display
48            charset, but beware of it growing */
49         maxlen = ret*2;
50 again:
51         p2 = malloc(maxlen);
52         if (!p2) {
53                 SAFE_FREE(p);
54                 return -1;
55         }
56         clen = convert_string(CH_UNIX, CH_DISPLAY, p, ret, p2, maxlen);
57         if (clen == -1) {
58                 /* the string can't be converted - do the best we can,
59                    filling in non-printing chars with '?' */
60                 int i;
61                 for (i=0;i<ret;i++) {
62                         if (isprint(p[i]) || isspace(p[i])) {
63                                 fwrite(p+i, 1, 1, f);
64                         } else {
65                                 fwrite("?", 1, 1, f);
66                         }
67                 }
68                 SAFE_FREE(p);
69                 SAFE_FREE(p2);
70                 return ret;
71         }
72
73
74         if (clen >= maxlen) {
75                 /* it didn't fit - try a larger buffer */
76                 maxlen *= 2;
77                 SAFE_FREE(p2);
78                 goto again;
79         }
80
81         /* good, its converted OK */
82         SAFE_FREE(p);
83         ret = fwrite(p2, 1, clen, f);
84         SAFE_FREE(p2);
85
86         return ret;
87 }
88
89
90 _PUBLIC_ int d_fprintf(FILE *f, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
91 {
92         int ret;
93         va_list ap;
94
95         va_start(ap, format);
96         ret = d_vfprintf(f, format, ap);
97         va_end(ap);
98
99         return ret;
100 }
101
102 static FILE *outfile;
103
104 _PUBLIC_ int d_printf(const char *format, ...) _PRINTF_ATTRIBUTE(1,2)
105 {
106         int ret;
107         va_list ap;
108
109         if (!outfile) outfile = stdout;
110         
111         va_start(ap, format);
112         ret = d_vfprintf(outfile, format, ap);
113         va_end(ap);
114
115         return ret;
116 }
117
118 /* interactive programs need a way of tell d_*() to write to stderr instead
119    of stdout */
120 _PUBLIC_ void display_set_stderr(void)
121 {
122         outfile = stderr;
123 }