r2023: If there's garbage in the pidfile, we should not panic but assume that no one
[tprouty/samba.git] / source / lib / dprintf.c
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21
22 /*
23   this module provides functions for printing internal strings in the "display charset"
24   This charset may be quite different from the chosen unix charset
25
26   Eventually these functions will need to take care of column count constraints
27
28   The d_ prefix on print functions in Samba refers to the display character set
29   conversion
30 */
31
32 #include "includes.h"
33
34  int d_vfprintf(FILE *f, const char *format, va_list ap)
35 {
36         char *p, *p2;
37         int ret, maxlen, clen;
38         const char *msgstr;
39         va_list ap2;
40
41         /* do any message translations */
42         msgstr = lang_msg(format);
43         if (!msgstr) return -1;
44
45         VA_COPY(ap2, ap);
46
47         ret = vasprintf(&p, msgstr, ap2);
48
49         lang_msg_free(msgstr);
50
51         if (ret <= 0) return ret;
52
53         /* now we have the string in unix format, convert it to the display
54            charset, but beware of it growing */
55         maxlen = ret*2;
56 again:
57         p2 = malloc(maxlen);
58         if (!p2) {
59                 SAFE_FREE(p);
60                 return -1;
61         }
62         clen = convert_string(CH_UNIX, CH_DISPLAY, p, ret, p2, maxlen, True);
63
64         if (clen >= maxlen) {
65                 /* it didn't fit - try a larger buffer */
66                 maxlen *= 2;
67                 SAFE_FREE(p2);
68                 goto again;
69         }
70
71         /* good, its converted OK */
72         SAFE_FREE(p);
73         ret = fwrite(p2, 1, clen, f);
74         SAFE_FREE(p2);
75
76         return ret;
77 }
78
79
80  int d_fprintf(FILE *f, const char *format, ...)
81 {
82         int ret;
83         va_list ap;
84
85         va_start(ap, format);
86         ret = d_vfprintf(f, format, ap);
87         va_end(ap);
88
89         return ret;
90 }
91
92 static FILE *outfile;
93
94  int d_printf(const char *format, ...)
95 {
96         int ret;
97         va_list ap;
98
99         if (!outfile) outfile = stdout;
100         
101         va_start(ap, format);
102         ret = d_vfprintf(outfile, format, ap);
103         va_end(ap);
104
105         return ret;
106 }
107
108 /* interactive programs need a way of tell d_*() to write to stderr instead
109    of stdout */
110 void display_set_stderr(void)
111 {
112         outfile = stderr;
113 }