snprintf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * This file is shared between libc and the kernel, so don't put anything
  31. * in here that won't work in both contexts.
  32. */
  33. #ifdef _KERNEL
  34. #include <types.h>
  35. #include <lib.h>
  36. #else
  37. #include <stdio.h>
  38. #endif /* _KERNEL */
  39. #include <stdarg.h>
  40. /*
  41. * Standard C string/IO function: printf into a character buffer.
  42. */
  43. /*
  44. * Context structure for snprintf: buffer to print into, maximum
  45. * length, and index of the next character to write.
  46. *
  47. * Note that while the length argument to snprintf includes space for
  48. * a null terminator, SNP.buflen does not. This is to make something
  49. * vaguely reasonable happen if a length of 0 is passed to snprintf.
  50. */
  51. typedef struct {
  52. char *buf;
  53. size_t buflen;
  54. size_t bufpos;
  55. } SNP;
  56. /*
  57. * Send function for snprintf. This is the function handed to the
  58. * printf guts. It gets called with mydata pointing to the context,
  59. * and some string data of length LEN in DATA. DATA is not necessarily
  60. * null-terminated.
  61. */
  62. static
  63. void
  64. __snprintf_send(void *mydata, const char *data, size_t len)
  65. {
  66. SNP *snp = mydata;
  67. unsigned i;
  68. /* For each character we're sent... */
  69. for (i=0; i<len; i++) {
  70. /* If we aren't past the length, */
  71. if (snp->bufpos < snp->buflen) {
  72. /* store the character */
  73. snp->buf[snp->bufpos] = data[i];
  74. /* and increment the position. */
  75. snp->bufpos++;
  76. }
  77. }
  78. }
  79. /*
  80. * The va_list version of snprintf.
  81. */
  82. int
  83. vsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
  84. {
  85. int chars;
  86. SNP snp;
  87. /*
  88. * Fill in the context structure.
  89. * We set snp.buflen to the number of characters that can be
  90. * written (excluding the null terminator) so as not to have
  91. * to special-case the possibility that we got passed a length
  92. * of zero elsewhere.
  93. */
  94. snp.buf = buf;
  95. if (len==0) {
  96. snp.buflen = 0;
  97. }
  98. else {
  99. snp.buflen = len-1;
  100. }
  101. snp.bufpos = 0;
  102. /* Call __vprintf to do the actual work. */
  103. chars = __vprintf(__snprintf_send, &snp, fmt, ap);
  104. /*
  105. * Add a null terminator. If the length *we were passed* is greater
  106. * than zero, we reserved a space in the buffer for the terminator,
  107. * so this won't overflow. If the length we were passed is zero,
  108. * nothing will have been or should be written anyway, and buf
  109. * might even be NULL. (C99 explicitly endorses this possibility.)
  110. */
  111. if (len > 0) {
  112. buf[snp.bufpos] = 0;
  113. }
  114. /*
  115. * Return the number of characters __vprintf processed.
  116. * According to C99, snprintf should return this number, not
  117. * the number of characters actually stored, and should not
  118. * return -1 on overflow but only on other errors. (All none
  119. * of them since we don't do multibyte characters...)
  120. */
  121. return chars;
  122. }
  123. /*
  124. * snprintf - hand off to vsnprintf.
  125. */
  126. int
  127. snprintf(char *buf, size_t len, const char *fmt, ...)
  128. {
  129. int chars;
  130. va_list ap;
  131. va_start(ap, fmt);
  132. chars = vsnprintf(buf, len, fmt, ap);
  133. va_end(ap);
  134. return chars;
  135. }