~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source3/lib/dprintf.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

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