util: Move become_daemon.c to samba-util-core
[samba.git] / lib / util / dprintf.c
index e4f02758eb8e0c99c610a2dd4695329c42fa314c..749fb3d5a21657669dedc1e9b0841793e1156371 100644 (file)
 
 #include "includes.h"
 #include "system/locale.h"
-#include "param/param.h"
 
-static smb_iconv_t display_cd = (smb_iconv_t)-1;
+static int d_vfprintf(FILE *f, const char *format, va_list ap)
+                     PRINTF_ATTRIBUTE(2,0);
 
-void d_set_iconv(smb_iconv_t cd)
+static int d_vfprintf(FILE *f, const char *format, va_list ap) 
 {
-       display_cd = cd;
-}
-
-_PUBLIC_ int d_vfprintf(FILE *f, const char *format, va_list ap) 
-{
-       char *p, *p2;
-       int ret, clen;
-       va_list ap2;
-
-       /* If there's nothing to convert, take a shortcut */
-       if (display_cd == (smb_iconv_t)-1) {
-               return vfprintf(f, format, ap);
-       }
-
-       /* do any message translations */
-       va_copy(ap2, ap);
-       ret = vasprintf(&p, format, ap2);
-       va_end(ap2);
-
-       if (ret <= 0) return ret;
-
-       clen = convert_string_talloc_descriptor(NULL, display_cd, p, ret, (void **)&p2);
-        if (clen == -1) {
-               /* the string can't be converted - do the best we can,
-                  filling in non-printing chars with '?' */
-               int i;
-               for (i=0;i<ret;i++) {
-                       if (isprint(p[i]) || isspace(p[i])) {
-                               fwrite(p+i, 1, 1, f);
-                       } else {
-                               fwrite("?", 1, 1, f);
-                       }
-               }
-               SAFE_FREE(p);
-               return ret;
-        }
-
-       /* good, its converted OK */
-       SAFE_FREE(p);
-       ret = fwrite(p2, 1, clen, f);
-       talloc_free(p2);
-
-       return ret;
+       return vfprintf(f, format, ap);
 }
 
 
@@ -97,15 +55,25 @@ _PUBLIC_ int d_fprintf(FILE *f, const char *format, ...)
        return ret;
 }
 
-_PUBLIC_ int d_printf(const char *format, ...)
+static FILE *outfile;
+
+_PUBLIC_  int d_printf(const char *format, ...)
 {
        int ret;
-       va_list ap;
-
-       va_start(ap, format);
-       ret = d_vfprintf(stdout, format, ap);
-       va_end(ap);
-
-       return ret;
+       va_list ap;
+       
+       if (!outfile) outfile = stdout;
+       
+       va_start(ap, format);
+       ret = d_vfprintf(outfile, format, ap);
+       va_end(ap);
+       
+       return ret;
 }
 
+/* interactive programs need a way of tell d_*() to write to stderr instead
+   of stdout */
+void display_set_stderr(void)
+{
+       outfile = stderr;
+}