vsprintf.h 7.7 KB

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