vsprintf.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. void panic(const char *fmt, ...)
  40. __attribute__ ((format (__printf__, 1, 2), noreturn));
  41. /**
  42. * Format a string and place it in a buffer
  43. *
  44. * @param buf The buffer to place the result into
  45. * @param fmt The format string to use
  46. * @param ... Arguments for the format string
  47. *
  48. * The function returns the number of characters written
  49. * into @buf.
  50. *
  51. * See the vsprintf() documentation for format string extensions over C99.
  52. */
  53. int sprintf(char *buf, const char *fmt, ...)
  54. __attribute__ ((format (__printf__, 2, 3)));
  55. /**
  56. * Format a string and place it in a buffer (va_list version)
  57. *
  58. * @param buf The buffer to place the result into
  59. * @param size The size of the buffer, including the trailing null space
  60. * @param fmt The format string to use
  61. * @param args Arguments for the format string
  62. * @return the number of characters which have been written into
  63. * the @buf not including the trailing '\0'. If @size is == 0 the function
  64. * returns 0.
  65. *
  66. * If you're not already dealing with a va_list consider using scnprintf().
  67. *
  68. * See the vsprintf() documentation for format string extensions over C99.
  69. */
  70. int vsprintf(char *buf, const char *fmt, va_list args);
  71. char *simple_itoa(ulong i);
  72. #ifdef CONFIG_SYS_VSNPRINTF
  73. /**
  74. * Format a string and place it in a buffer
  75. *
  76. * @param buf The buffer to place the result into
  77. * @param size The size of the buffer, including the trailing null space
  78. * @param fmt The format string to use
  79. * @param ... Arguments for the format string
  80. * @return the number of characters which would be
  81. * generated for the given input, excluding the trailing null,
  82. * as per ISO C99. If the return is greater than or equal to
  83. * @size, the resulting string is truncated.
  84. *
  85. * See the vsprintf() documentation for format string extensions over C99.
  86. */
  87. int snprintf(char *buf, size_t size, const char *fmt, ...)
  88. __attribute__ ((format (__printf__, 3, 4)));
  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. *
  97. * The return value is the number of characters written into @buf not including
  98. * the trailing '\0'. If @size is == 0 the function returns 0.
  99. *
  100. * See the vsprintf() documentation for format string extensions over C99.
  101. */
  102. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  103. __attribute__ ((format (__printf__, 3, 4)));
  104. /**
  105. * Format a string and place it in a buffer (base function)
  106. *
  107. * @param buf The buffer to place the result into
  108. * @param size The size of the buffer, including the trailing null space
  109. * @param fmt The format string to use
  110. * @param args Arguments for the format string
  111. * @return The number characters which would be generated for the given
  112. * input, excluding the trailing '\0', as per ISO C99. Note that fewer
  113. * characters may be written if this number of characters is >= size.
  114. *
  115. * This function follows C99 vsnprintf, but has some extensions:
  116. * %pS output the name of a text symbol
  117. * %pF output the name of a function pointer
  118. * %pR output the address range in a struct resource
  119. *
  120. * The function returns the number of characters which would be
  121. * generated for the given input, excluding the trailing '\0',
  122. * as per ISO C99.
  123. *
  124. * Call this function if you are already dealing with a va_list.
  125. * You probably want snprintf() instead.
  126. */
  127. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
  128. /**
  129. * Format a string and place it in a buffer (va_list version)
  130. *
  131. * @param buf The buffer to place the result into
  132. * @param size The size of the buffer, including the trailing null space
  133. * @param fmt The format string to use
  134. * @param args Arguments for the format string
  135. * @return the number of characters which have been written into
  136. * the @buf not including the trailing '\0'. If @size is == 0 the function
  137. * returns 0.
  138. *
  139. * If you're not already dealing with a va_list consider using scnprintf().
  140. *
  141. * See the vsprintf() documentation for format string extensions over C99.
  142. */
  143. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  144. #else
  145. /*
  146. * Use macros to silently drop the size parameter. Note that the 'cn'
  147. * versions are the same as the 'n' versions since the functions assume
  148. * there is always enough buffer space when !CONFIG_SYS_VSNPRINTF
  149. */
  150. #define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
  151. #define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
  152. #define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
  153. #define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
  154. #endif /* CONFIG_SYS_VSNPRINTF */
  155. /**
  156. * print_grouped_ull() - print a value with digits grouped by ','
  157. *
  158. * This prints a value with grouped digits, like 12,345,678 to make it easier
  159. * to read.
  160. *
  161. * @val: Value to print
  162. * @digits: Number of digiits to print
  163. */
  164. void print_grouped_ull(unsigned long long int_val, int digits);
  165. #endif