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