| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | display print functions | 
|---|
| 4 | Copyright (C) Andrew Tridgell 2001 | 
|---|
| 5 | Copyright (C) Jelmer Vernooij 2007 | 
|---|
| 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 3 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, see <http://www.gnu.org/licenses/>. | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | /* | 
|---|
| 23 | this module provides functions for printing internal strings in the | 
|---|
| 24 | "display charset". | 
|---|
| 25 |  | 
|---|
| 26 | This charset may be quite different from the chosen unix charset. | 
|---|
| 27 |  | 
|---|
| 28 | Eventually these functions will need to take care of column count constraints | 
|---|
| 29 |  | 
|---|
| 30 | The d_ prefix on print functions in Samba refers to the display character set | 
|---|
| 31 | conversion | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "includes.h" | 
|---|
| 35 | #include "system/locale.h" | 
|---|
| 36 | #include "param/param.h" | 
|---|
| 37 |  | 
|---|
| 38 | static smb_iconv_t display_cd = (smb_iconv_t)-1; | 
|---|
| 39 |  | 
|---|
| 40 | void d_set_iconv(smb_iconv_t cd) | 
|---|
| 41 | { | 
|---|
| 42 | if (display_cd != (smb_iconv_t)-1) | 
|---|
| 43 | talloc_free(display_cd); | 
|---|
| 44 |  | 
|---|
| 45 | display_cd = cd; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | _PUBLIC_ int d_vfprintf(FILE *f, const char *format, va_list ap) | 
|---|
| 49 | { | 
|---|
| 50 | char *p, *p2; | 
|---|
| 51 | int ret, clen; | 
|---|
| 52 | va_list ap2; | 
|---|
| 53 |  | 
|---|
| 54 | /* If there's nothing to convert, take a shortcut */ | 
|---|
| 55 | if (display_cd == (smb_iconv_t)-1) { | 
|---|
| 56 | return vfprintf(f, format, ap); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | /* do any message translations */ | 
|---|
| 60 | va_copy(ap2, ap); | 
|---|
| 61 | ret = vasprintf(&p, format, ap2); | 
|---|
| 62 | va_end(ap2); | 
|---|
| 63 |  | 
|---|
| 64 | if (ret <= 0) return ret; | 
|---|
| 65 |  | 
|---|
| 66 | clen = iconv_talloc(NULL, display_cd, p, ret, (void **)&p2); | 
|---|
| 67 | if (clen == -1) { | 
|---|
| 68 | /* the string can't be converted - do the best we can, | 
|---|
| 69 | filling in non-printing chars with '?' */ | 
|---|
| 70 | int i; | 
|---|
| 71 | for (i=0;i<ret;i++) { | 
|---|
| 72 | if (isprint(p[i]) || isspace(p[i])) { | 
|---|
| 73 | fwrite(p+i, 1, 1, f); | 
|---|
| 74 | } else { | 
|---|
| 75 | fwrite("?", 1, 1, f); | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 | SAFE_FREE(p); | 
|---|
| 79 | return ret; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | /* good, its converted OK */ | 
|---|
| 83 | SAFE_FREE(p); | 
|---|
| 84 | ret = fwrite(p2, 1, clen, f); | 
|---|
| 85 | talloc_free(p2); | 
|---|
| 86 |  | 
|---|
| 87 | return ret; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | _PUBLIC_ int d_fprintf(FILE *f, const char *format, ...) | 
|---|
| 92 | { | 
|---|
| 93 | int ret; | 
|---|
| 94 | va_list ap; | 
|---|
| 95 |  | 
|---|
| 96 | va_start(ap, format); | 
|---|
| 97 | ret = d_vfprintf(f, format, ap); | 
|---|
| 98 | va_end(ap); | 
|---|
| 99 |  | 
|---|
| 100 | return ret; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | _PUBLIC_ int d_printf(const char *format, ...) | 
|---|
| 104 | { | 
|---|
| 105 | int ret; | 
|---|
| 106 | va_list ap; | 
|---|
| 107 |  | 
|---|
| 108 | va_start(ap, format); | 
|---|
| 109 | ret = d_vfprintf(stdout, format, ap); | 
|---|
| 110 | va_end(ap); | 
|---|
| 111 |  | 
|---|
| 112 | return ret; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|