vsprintf.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * (C) Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef __VSPRINTF_H
  8. #define __VSPRINTF_H
  9. ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
  10. /**
  11. * strict_strtoul - convert a string to an unsigned long strictly
  12. * @param cp The string to be converted
  13. * @param base The number base to use
  14. * @param res The converted result value
  15. * @return 0 if conversion is successful and *res is set to the converted
  16. * value, otherwise it returns -EINVAL and *res is set to 0.
  17. *
  18. * strict_strtoul converts a string to an unsigned long only if the
  19. * string is really an unsigned long string, any string containing
  20. * any invalid char at the tail will be rejected and -EINVAL is returned,
  21. * only a newline char at the tail is acceptible because people generally
  22. * change a module parameter in the following way:
  23. *
  24. * echo 1024 > /sys/module/e1000/parameters/copybreak
  25. *
  26. * echo will append a newline to the tail.
  27. *
  28. * simple_strtoul just ignores the successive invalid characters and
  29. * return the converted value of prefix part of the string.
  30. *
  31. * Copied this function from Linux 2.6.38 commit ID:
  32. * 521cb40b0c44418a4fd36dc633f575813d59a43d
  33. *
  34. */
  35. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
  36. unsigned long long simple_strtoull(const char *cp, char **endp,
  37. unsigned int base);
  38. long simple_strtol(const char *cp, char **endp, unsigned int base);
  39. /**
  40. * panic() - Print a message and reset/hang
  41. *
  42. * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
  43. * defined, then it will hang instead of reseting.
  44. *
  45. * @param fmt: printf() format string for message, which should not include
  46. * \n, followed by arguments
  47. */
  48. void panic(const char *fmt, ...)
  49. __attribute__ ((format (__printf__, 1, 2), noreturn));
  50. /**
  51. * panic_str() - Print a message and reset/hang
  52. *
  53. * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
  54. * defined, then it will hang instead of reseting.
  55. *
  56. * This function can be used instead of panic() when your board does not
  57. * already use printf(), * to keep code size small.
  58. *
  59. * @param fmt: string to display, which should not include \n
  60. */
  61. void panic_str(const char *str) __attribute__ ((noreturn));
  62. /**
  63. * Format a string and place it in a buffer
  64. *
  65. * @param buf The buffer to place the result into
  66. * @param fmt The format string to use
  67. * @param ... Arguments for the format string
  68. *
  69. * The function returns the number of characters written
  70. * into @buf.
  71. *
  72. * See the vsprintf() documentation for format string extensions over C99.
  73. */
  74. int sprintf(char *buf, const char *fmt, ...)
  75. __attribute__ ((format (__printf__, 2, 3)));
  76. /**
  77. * Format a string and place it in a buffer (va_list version)
  78. *
  79. * @param buf The buffer to place the result into
  80. * @param size The size of the buffer, including the trailing null space
  81. * @param fmt The format string to use
  82. * @param args Arguments for the format string
  83. * @return the number of characters which have been written into
  84. * the @buf not including the trailing '\0'. If @size is == 0 the function
  85. * returns 0.
  86. *
  87. * If you're not already dealing with a va_list consider using scnprintf().
  88. *
  89. * See the vsprintf() documentation for format string extensions over C99.
  90. */
  91. int vsprintf(char *buf, const char *fmt, va_list args);
  92. char *simple_itoa(ulong i);
  93. #ifdef CONFIG_SYS_VSNPRINTF
  94. /**
  95. * Format a string and place it in a buffer
  96. *
  97. * @param buf The buffer to place the result into
  98. * @param size The size of the buffer, including the trailing null space
  99. * @param fmt The format string to use
  100. * @param ... Arguments for the format string
  101. * @return the number of characters which would be
  102. * generated for the given input, excluding the trailing null,
  103. * as per ISO C99. If the return is greater than or equal to
  104. * @size, the resulting string is truncated.
  105. *
  106. * See the vsprintf() documentation for format string extensions over C99.
  107. */
  108. int snprintf(char *buf, size_t size, const char *fmt, ...)
  109. __attribute__ ((format (__printf__, 3, 4)));
  110. /**
  111. * Format a string and place it in a buffer
  112. *
  113. * @param buf The buffer to place the result into
  114. * @param size The size of the buffer, including the trailing null space
  115. * @param fmt The format string to use
  116. * @param ... Arguments for the format string
  117. *
  118. * The return value is the number of characters written into @buf not including
  119. * the trailing '\0'. If @size is == 0 the function returns 0.
  120. *
  121. * See the vsprintf() documentation for format string extensions over C99.
  122. */
  123. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  124. __attribute__ ((format (__printf__, 3, 4)));
  125. /**
  126. * Format a string and place it in a buffer (base function)
  127. *
  128. * @param buf The buffer to place the result into
  129. * @param size The size of the buffer, including the trailing null space
  130. * @param fmt The format string to use
  131. * @param args Arguments for the format string
  132. * @return The number characters which would be generated for the given
  133. * input, excluding the trailing '\0', as per ISO C99. Note that fewer
  134. * characters may be written if this number of characters is >= size.
  135. *
  136. * This function follows C99 vsnprintf, but has some extensions:
  137. * %pS output the name of a text symbol
  138. * %pF output the name of a function pointer
  139. * %pR output the address range in a struct resource
  140. *
  141. * The function returns the number of characters which would be
  142. * generated for the given input, excluding the trailing '\0',
  143. * as per ISO C99.
  144. *
  145. * Call this function if you are already dealing with a va_list.
  146. * You probably want snprintf() instead.
  147. */
  148. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
  149. /**
  150. * Format a string and place it in a buffer (va_list version)
  151. *
  152. * @param buf The buffer to place the result into
  153. * @param size The size of the buffer, including the trailing null space
  154. * @param fmt The format string to use
  155. * @param args Arguments for the format string
  156. * @return the number of characters which have been written into
  157. * the @buf not including the trailing '\0'. If @size is == 0 the function
  158. * returns 0.
  159. *
  160. * If you're not already dealing with a va_list consider using scnprintf().
  161. *
  162. * See the vsprintf() documentation for format string extensions over C99.
  163. */
  164. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  165. #else
  166. /*
  167. * Use macros to silently drop the size parameter. Note that the 'cn'
  168. * versions are the same as the 'n' versions since the functions assume
  169. * there is always enough buffer space when !CONFIG_SYS_VSNPRINTF
  170. */
  171. #define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
  172. #define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
  173. #define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
  174. #define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
  175. #endif /* CONFIG_SYS_VSNPRINTF */
  176. /**
  177. * print_grouped_ull() - print a value with digits grouped by ','
  178. *
  179. * This prints a value with grouped digits, like 12,345,678 to make it easier
  180. * to read.
  181. *
  182. * @val: Value to print
  183. * @digits: Number of digiits to print
  184. */
  185. void print_grouped_ull(unsigned long long int_val, int digits);
  186. bool str2off(const char *p, loff_t *num);
  187. bool str2long(const char *p, ulong *num);
  188. #endif