vsprintf.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * (C) Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #ifndef __VSPRINTF_H
  24. #define __VSPRINTF_H
  25. ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
  26. /**
  27. * strict_strtoul - convert a string to an unsigned long strictly
  28. * @param cp The string to be converted
  29. * @param base The number base to use
  30. * @param res The converted result value
  31. * @return 0 if conversion is successful and *res is set to the converted
  32. * value, otherwise it returns -EINVAL and *res is set to 0.
  33. *
  34. * strict_strtoul converts a string to an unsigned long only if the
  35. * string is really an unsigned long string, any string containing
  36. * any invalid char at the tail will be rejected and -EINVAL is returned,
  37. * only a newline char at the tail is acceptible because people generally
  38. * change a module parameter in the following way:
  39. *
  40. * echo 1024 > /sys/module/e1000/parameters/copybreak
  41. *
  42. * echo will append a newline to the tail.
  43. *
  44. * simple_strtoul just ignores the successive invalid characters and
  45. * return the converted value of prefix part of the string.
  46. *
  47. * Copied this function from Linux 2.6.38 commit ID:
  48. * 521cb40b0c44418a4fd36dc633f575813d59a43d
  49. *
  50. */
  51. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
  52. unsigned long long simple_strtoull(const char *cp, char **endp,
  53. unsigned int base);
  54. long simple_strtol(const char *cp, char **endp, unsigned int base);
  55. void panic(const char *fmt, ...)
  56. __attribute__ ((format (__printf__, 1, 2), noreturn));
  57. /**
  58. * Format a string and place it in a buffer
  59. *
  60. * @param buf The buffer to place the result into
  61. * @param fmt The format string to use
  62. * @param ... Arguments for the format string
  63. *
  64. * The function returns the number of characters written
  65. * into @buf.
  66. *
  67. * See the vsprintf() documentation for format string extensions over C99.
  68. */
  69. int sprintf(char *buf, const char *fmt, ...)
  70. __attribute__ ((format (__printf__, 2, 3)));
  71. /**
  72. * Format a string and place it in a buffer (va_list version)
  73. *
  74. * @param buf The buffer to place the result into
  75. * @param size The size of the buffer, including the trailing null space
  76. * @param fmt The format string to use
  77. * @param args Arguments for the format string
  78. * @return the number of characters which have been written into
  79. * the @buf not including the trailing '\0'. If @size is == 0 the function
  80. * returns 0.
  81. *
  82. * If you're not already dealing with a va_list consider using scnprintf().
  83. *
  84. * See the vsprintf() documentation for format string extensions over C99.
  85. */
  86. int vsprintf(char *buf, const char *fmt, va_list args);
  87. char *simple_itoa(ulong i);
  88. #ifdef CONFIG_SYS_VSNPRINTF
  89. /**
  90. * Format a string and place it in a buffer
  91. *
  92. * @param buf The buffer to place the result into
  93. * @param size The size of the buffer, including the trailing null space
  94. * @param fmt The format string to use
  95. * @param ... Arguments for the format string
  96. * @return the number of characters which would be
  97. * generated for the given input, excluding the trailing null,
  98. * as per ISO C99. If the return is greater than or equal to
  99. * @size, the resulting string is truncated.
  100. *
  101. * See the vsprintf() documentation for format string extensions over C99.
  102. */
  103. int snprintf(char *buf, size_t size, const char *fmt, ...)
  104. __attribute__ ((format (__printf__, 3, 4)));
  105. /**
  106. * Format a string and place it in a buffer
  107. *
  108. * @param buf The buffer to place the result into
  109. * @param size The size of the buffer, including the trailing null space
  110. * @param fmt The format string to use
  111. * @param ... Arguments for the format string
  112. *
  113. * The return value is the number of characters written into @buf not including
  114. * the trailing '\0'. If @size is == 0 the function returns 0.
  115. *
  116. * See the vsprintf() documentation for format string extensions over C99.
  117. */
  118. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  119. __attribute__ ((format (__printf__, 3, 4)));
  120. /**
  121. * Format a string and place it in a buffer (base function)
  122. *
  123. * @param buf The buffer to place the result into
  124. * @param size The size of the buffer, including the trailing null space
  125. * @param fmt The format string to use
  126. * @param args Arguments for the format string
  127. * @return The number characters which would be generated for the given
  128. * input, excluding the trailing '\0', as per ISO C99. Note that fewer
  129. * characters may be written if this number of characters is >= size.
  130. *
  131. * This function follows C99 vsnprintf, but has some extensions:
  132. * %pS output the name of a text symbol
  133. * %pF output the name of a function pointer
  134. * %pR output the address range in a struct resource
  135. *
  136. * The function returns the number of characters which would be
  137. * generated for the given input, excluding the trailing '\0',
  138. * as per ISO C99.
  139. *
  140. * Call this function if you are already dealing with a va_list.
  141. * You probably want snprintf() instead.
  142. */
  143. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
  144. /**
  145. * Format a string and place it in a buffer (va_list version)
  146. *
  147. * @param buf The buffer to place the result into
  148. * @param size The size of the buffer, including the trailing null space
  149. * @param fmt The format string to use
  150. * @param args Arguments for the format string
  151. * @return the number of characters which have been written into
  152. * the @buf not including the trailing '\0'. If @size is == 0 the function
  153. * returns 0.
  154. *
  155. * If you're not already dealing with a va_list consider using scnprintf().
  156. *
  157. * See the vsprintf() documentation for format string extensions over C99.
  158. */
  159. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  160. #else
  161. /*
  162. * Use macros to silently drop the size parameter. Note that the 'cn'
  163. * versions are the same as the 'n' versions since the functions assume
  164. * there is always enough buffer space when !CONFIG_SYS_VSNPRINTF
  165. */
  166. #define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
  167. #define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
  168. #define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
  169. #define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
  170. #endif /* CONFIG_SYS_VSNPRINTF */
  171. #endif