snprintf.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: snprintf.c /main/3 1996/06/11 17:14:27 cde-hal $ */
  24. #include <sys/types.h>
  25. #include "cdefs.h"
  26. #include <compat.h>
  27. #include <string.h>
  28. #include <stdio.h>
  29. #ifdef __STDC__
  30. #include <stdarg.h>
  31. #else
  32. #include <varargs.h>
  33. #endif
  34. int
  35. #ifdef __STDC__
  36. snprintf(char *str, size_t n, const char *fmt, ...)
  37. #else
  38. snprintf(str, n, fmt, va_alist)
  39. char *str;
  40. size_t n;
  41. const char *fmt;
  42. va_dcl
  43. #endif
  44. {
  45. va_list ap;
  46. #ifdef VSPRINTF_CHARSTAR
  47. char *rp;
  48. #else
  49. int rval;
  50. #endif
  51. #ifdef __STDC__
  52. va_start(ap, fmt);
  53. #else
  54. va_start(ap);
  55. #endif
  56. #ifdef VSPRINTF_CHARSTAR
  57. rp = (char*)(size_t)vsnprintf(str, n, fmt, ap);
  58. va_end(ap);
  59. return (strlen(rp));
  60. #else
  61. rval = vsnprintf(str, n, fmt, ap);
  62. va_end(ap);
  63. return (rval);
  64. #endif
  65. }
  66. #if 0
  67. int
  68. vsnprintf(str, n, fmt, ap)
  69. char *str;
  70. size_t n;
  71. const char *fmt;
  72. va_list ap;
  73. {
  74. #ifdef VSPRINTF_CHARSTAR
  75. return (strlen((char*)(size_t)vsnprintf(str, fmt, ap)));
  76. #else
  77. return (vsnprintf(str, fmt, ap));
  78. #endif
  79. }
  80. #endif