s3/docs: Fix serveral typos.
[Samba.git] / source / lib / dprintf.c
bloba3bb5be43af53c226de31b8d2101cbc6d1a7f081
1 /*
2 Unix SMB/CIFS implementation.
3 display print functions
4 Copyright (C) Andrew Tridgell 2001
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.
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.
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/>.
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
25 Eventually these functions will need to take care of column count constraints
27 The d_ prefix on print functions in Samba refers to the display character set
28 conversion
31 #include "includes.h"
33 int d_vfprintf(FILE *f, const char *format, va_list ap)
35 char *p, *p2;
36 int ret, maxlen, clen;
37 const char *msgstr;
38 va_list ap2;
40 /* do any message translations */
41 msgstr = lang_msg(format);
42 if (!msgstr) return -1;
44 VA_COPY(ap2, ap);
46 ret = vasprintf(&p, msgstr, ap2);
48 lang_msg_free(msgstr);
50 if (ret <= 0) {
51 va_end(ap2);
52 return ret;
55 /* now we have the string in unix format, convert it to the display
56 charset, but beware of it growing */
57 maxlen = ret*2;
58 again:
59 p2 = (char *)SMB_MALLOC(maxlen);
60 if (!p2) {
61 SAFE_FREE(p);
62 va_end(ap2);
63 return -1;
65 clen = convert_string(CH_UNIX, CH_DISPLAY, p, ret, p2, maxlen, True);
67 if (clen >= maxlen) {
68 /* it didn't fit - try a larger buffer */
69 maxlen *= 2;
70 SAFE_FREE(p2);
71 goto again;
74 /* good, its converted OK */
75 SAFE_FREE(p);
76 ret = fwrite(p2, 1, clen, f);
77 SAFE_FREE(p2);
79 va_end(ap2);
81 return ret;
85 int d_fprintf(FILE *f, const char *format, ...)
87 int ret;
88 va_list ap;
90 va_start(ap, format);
91 ret = d_vfprintf(f, format, ap);
92 va_end(ap);
94 return ret;
97 static FILE *outfile;
99 int d_printf(const char *format, ...)
101 int ret;
102 va_list ap;
104 if (!outfile) outfile = stdout;
106 va_start(ap, format);
107 ret = d_vfprintf(outfile, format, ap);
108 va_end(ap);
110 return ret;
113 /* interactive programs need a way of tell d_*() to write to stderr instead
114 of stdout */
115 void display_set_stderr(void)
117 outfile = stderr;