| 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 "intl/lang_tdb.h"
|
|---|
| 33 |
|
|---|
| 34 | int d_vfprintf(FILE *f, const char *format, va_list ap)
|
|---|
| 35 | {
|
|---|
| 36 | char *p = NULL, *p2 = NULL;
|
|---|
| 37 | int ret, maxlen, clen;
|
|---|
| 38 | const char *msgstr;
|
|---|
| 39 | va_list ap2;
|
|---|
| 40 |
|
|---|
| 41 | va_copy(ap2, ap);
|
|---|
| 42 |
|
|---|
| 43 | /* do any message translations */
|
|---|
| 44 | msgstr = lang_msg(format);
|
|---|
| 45 | if (!msgstr) {
|
|---|
| 46 | ret = -1;
|
|---|
| 47 | goto out;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | ret = vasprintf(&p, msgstr, ap2);
|
|---|
| 51 |
|
|---|
| 52 | lang_msg_free(msgstr);
|
|---|
| 53 |
|
|---|
| 54 | if (ret <= 0) {
|
|---|
| 55 | ret = -1;
|
|---|
| 56 | goto out;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /* now we have the string in unix format, convert it to the display
|
|---|
| 60 | charset, but beware of it growing */
|
|---|
| 61 | maxlen = ret*2;
|
|---|
| 62 | again:
|
|---|
| 63 | p2 = (char *)SMB_MALLOC(maxlen);
|
|---|
| 64 | if (!p2) {
|
|---|
| 65 | ret = -1;
|
|---|
| 66 | goto out;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | clen = convert_string(CH_UNIX, CH_DISPLAY, p, ret, p2, maxlen, True);
|
|---|
| 70 | if (clen == -1) {
|
|---|
| 71 | ret = -1;
|
|---|
| 72 | goto out;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | if (clen >= maxlen) {
|
|---|
| 76 | /* it didn't fit - try a larger buffer */
|
|---|
| 77 | maxlen *= 2;
|
|---|
| 78 | SAFE_FREE(p2);
|
|---|
| 79 | goto again;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /* good, its converted OK */
|
|---|
| 83 | ret = fwrite(p2, 1, clen, f);
|
|---|
| 84 | out:
|
|---|
| 85 |
|
|---|
| 86 | SAFE_FREE(p);
|
|---|
| 87 | SAFE_FREE(p2);
|
|---|
| 88 | va_end(ap2);
|
|---|
| 89 |
|
|---|
| 90 | return ret;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | int d_fprintf(FILE *f, const char *format, ...)
|
|---|
| 95 | {
|
|---|
| 96 | int ret;
|
|---|
| 97 | va_list ap;
|
|---|
| 98 |
|
|---|
| 99 | va_start(ap, format);
|
|---|
| 100 | ret = d_vfprintf(f, format, ap);
|
|---|
| 101 | va_end(ap);
|
|---|
| 102 |
|
|---|
| 103 | return ret;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static FILE *outfile;
|
|---|
| 107 |
|
|---|
| 108 | int d_printf(const char *format, ...)
|
|---|
| 109 | {
|
|---|
| 110 | int ret;
|
|---|
| 111 | va_list ap;
|
|---|
| 112 |
|
|---|
| 113 | if (!outfile) outfile = stdout;
|
|---|
| 114 |
|
|---|
| 115 | va_start(ap, format);
|
|---|
| 116 | ret = d_vfprintf(outfile, format, ap);
|
|---|
| 117 | va_end(ap);
|
|---|
| 118 |
|
|---|
| 119 | return ret;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /* interactive programs need a way of tell d_*() to write to stderr instead
|
|---|
| 123 | of stdout */
|
|---|
| 124 | void display_set_stderr(void)
|
|---|
| 125 | {
|
|---|
| 126 | outfile = stderr;
|
|---|
| 127 | }
|
|---|