PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/samba3/source/lib/dprintf.c

https://gitlab.com/envieidoc/tomato
C | 113 lines | 55 code | 23 blank | 35 comment | 5 complexity | 04b3af402fbc82824fea4768820d495f 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 2 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, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /*
  18. this module provides functions for printing internal strings in the "display charset"
  19. This charset may be quite different from the chosen unix charset
  20. Eventually these functions will need to take care of column count constraints
  21. The d_ prefix on print functions in Samba refers to the display character set
  22. conversion
  23. */
  24. #include "includes.h"
  25. int d_vfprintf(FILE *f, const char *format, va_list ap)
  26. {
  27. char *p, *p2;
  28. int ret, maxlen, clen;
  29. const char *msgstr;
  30. va_list ap2;
  31. /* do any message translations */
  32. msgstr = lang_msg(format);
  33. if (!msgstr) return -1;
  34. VA_COPY(ap2, ap);
  35. ret = vasprintf(&p, msgstr, ap2);
  36. lang_msg_free(msgstr);
  37. if (ret <= 0) return ret;
  38. /* now we have the string in unix format, convert it to the display
  39. charset, but beware of it growing */
  40. maxlen = ret*2;
  41. again:
  42. p2 = (char *)SMB_MALLOC(maxlen);
  43. if (!p2) {
  44. SAFE_FREE(p);
  45. return -1;
  46. }
  47. clen = convert_string(CH_UNIX, CH_DISPLAY, p, ret, p2, maxlen, True);
  48. if (clen >= maxlen) {
  49. /* it didn't fit - try a larger buffer */
  50. maxlen *= 2;
  51. SAFE_FREE(p2);
  52. goto again;
  53. }
  54. /* good, its converted OK */
  55. SAFE_FREE(p);
  56. ret = fwrite(p2, 1, clen, f);
  57. SAFE_FREE(p2);
  58. return ret;
  59. }
  60. int d_fprintf(FILE *f, const char *format, ...)
  61. {
  62. int ret;
  63. va_list ap;
  64. va_start(ap, format);
  65. ret = d_vfprintf(f, format, ap);
  66. va_end(ap);
  67. return ret;
  68. }
  69. static FILE *outfile;
  70. int d_printf(const char *format, ...)
  71. {
  72. int ret;
  73. va_list ap;
  74. if (!outfile) outfile = stdout;
  75. va_start(ap, format);
  76. ret = d_vfprintf(outfile, format, ap);
  77. va_end(ap);
  78. return ret;
  79. }
  80. /* interactive programs need a way of tell d_*() to write to stderr instead
  81. of stdout */
  82. void display_set_stderr(void)
  83. {
  84. outfile = stderr;
  85. }