/source3/lib/dprintf.c

https://repo.or.cz/Samba/vfs_proxy.git · C · 118 lines · 60 code · 24 blank · 34 comment · 5 complexity · 987ee3a711364083af543c3e88e28f88 MD5 · raw file

  1. /*
  2. Unix SMB/CIFS implementation.
  3. display print functions
  4. Copyright (C) Andrew Tridgell 2001
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. this module provides functions for printing internal strings in the "display charset"
  18. This charset may be quite different from the chosen unix charset
  19. Eventually these functions will need to take care of column count constraints
  20. The d_ prefix on print functions in Samba refers to the display character set
  21. conversion
  22. */
  23. #include "includes.h"
  24. int d_vfprintf(FILE *f, const char *format, va_list ap)
  25. {
  26. char *p, *p2;
  27. int ret, maxlen, clen;
  28. const char *msgstr;
  29. va_list ap2;
  30. /* do any message translations */
  31. msgstr = lang_msg(format);
  32. if (!msgstr) return -1;
  33. va_copy(ap2, ap);
  34. ret = vasprintf(&p, msgstr, ap2);
  35. lang_msg_free(msgstr);
  36. if (ret <= 0) {
  37. va_end(ap2);
  38. return ret;
  39. }
  40. /* now we have the string in unix format, convert it to the display
  41. charset, but beware of it growing */
  42. maxlen = ret*2;
  43. again:
  44. p2 = (char *)SMB_MALLOC(maxlen);
  45. if (!p2) {
  46. SAFE_FREE(p);
  47. va_end(ap2);
  48. return -1;
  49. }
  50. clen = convert_string(CH_UNIX, CH_DISPLAY, p, ret, p2, maxlen, True);
  51. if (clen >= maxlen) {
  52. /* it didn't fit - try a larger buffer */
  53. maxlen *= 2;
  54. SAFE_FREE(p2);
  55. goto again;
  56. }
  57. /* good, its converted OK */
  58. SAFE_FREE(p);
  59. ret = fwrite(p2, 1, clen, f);
  60. SAFE_FREE(p2);
  61. va_end(ap2);
  62. return ret;
  63. }
  64. int d_fprintf(FILE *f, const char *format, ...)
  65. {
  66. int ret;
  67. va_list ap;
  68. va_start(ap, format);
  69. ret = d_vfprintf(f, format, ap);
  70. va_end(ap);
  71. return ret;
  72. }
  73. static FILE *outfile;
  74. int d_printf(const char *format, ...)
  75. {
  76. int ret;
  77. va_list ap;
  78. if (!outfile) outfile = stdout;
  79. va_start(ap, format);
  80. ret = d_vfprintf(outfile, format, ap);
  81. va_end(ap);
  82. return ret;
  83. }
  84. /* interactive programs need a way of tell d_*() to write to stderr instead
  85. of stdout */
  86. void display_set_stderr(void)
  87. {
  88. outfile = stderr;
  89. }