vsprintf.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. long long simple_strtoll(const char *cp, char **endp, unsigned int base);
  41. /**
  42. * trailing_strtol() - extract a trailing integer from a string
  43. *
  44. * Given a string this finds a trailing number on the string and returns it.
  45. * For example, "abc123" would return 123.
  46. *
  47. * @str: String to exxamine
  48. * @return training number if found, else -1
  49. */
  50. long trailing_strtol(const char *str);
  51. /**
  52. * trailing_strtoln() - extract a trailing integer from a fixed-length string
  53. *
  54. * Given a fixed-length string this finds a trailing number on the string
  55. * and returns it. For example, "abc123" would return 123. Only the
  56. * characters between @str and @end - 1 are examined. If @end is NULL, it is
  57. * set to str + strlen(str).
  58. *
  59. * @str: String to exxamine
  60. * @end: Pointer to end of string to examine, or NULL to use the
  61. * whole string
  62. * @return training number if found, else -1
  63. */
  64. long trailing_strtoln(const char *str, const char *end);
  65. /**
  66. * panic() - Print a message and reset/hang
  67. *
  68. * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
  69. * defined, then it will hang instead of resetting.
  70. *
  71. * @param fmt: printf() format string for message, which should not include
  72. * \n, followed by arguments
  73. */
  74. void panic(const char *fmt, ...)
  75. __attribute__ ((format (__printf__, 1, 2), noreturn));
  76. /**
  77. * panic_str() - Print a message and reset/hang
  78. *
  79. * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
  80. * defined, then it will hang instead of resetting.
  81. *
  82. * This function can be used instead of panic() when your board does not
  83. * already use printf(), * to keep code size small.
  84. *
  85. * @param fmt: string to display, which should not include \n
  86. */
  87. void panic_str(const char *str) __attribute__ ((noreturn));
  88. /**
  89. * Format a string and place it in a buffer
  90. *
  91. * @param buf The buffer to place the result into
  92. * @param fmt The format string to use
  93. * @param ... Arguments for the format string
  94. *
  95. * The function returns the number of characters written
  96. * into @buf.
  97. *
  98. * See the vsprintf() documentation for format string extensions over C99.
  99. */
  100. int sprintf(char *buf, const char *fmt, ...)
  101. __attribute__ ((format (__printf__, 2, 3)));
  102. /**
  103. * Format a string and place it in a buffer (va_list version)
  104. *
  105. * @param buf The buffer to place the result into
  106. * @param fmt The format string to use
  107. * @param args Arguments for the format string
  108. * @return the number of characters which have been written into
  109. * the @buf not including the trailing '\0'.
  110. *
  111. * If you're not already dealing with a va_list consider using scnprintf().
  112. *
  113. * See the vsprintf() documentation for format string extensions over C99.
  114. */
  115. int vsprintf(char *buf, const char *fmt, va_list args);
  116. char *simple_itoa(ulong i);
  117. /**
  118. * Format a string and place it in a buffer
  119. *
  120. * @param buf The buffer to place the result into
  121. * @param size The size of the buffer, including the trailing null space
  122. * @param fmt The format string to use
  123. * @param ... Arguments for the format string
  124. * @return the number of characters which would be
  125. * generated for the given input, excluding the trailing null,
  126. * as per ISO C99. If the return is greater than or equal to
  127. * @size, the resulting string is truncated.
  128. *
  129. * See the vsprintf() documentation for format string extensions over C99.
  130. */
  131. int snprintf(char *buf, size_t size, const char *fmt, ...)
  132. __attribute__ ((format (__printf__, 3, 4)));
  133. /**
  134. * Format a string and place it in a buffer
  135. *
  136. * @param buf The buffer to place the result into
  137. * @param size The size of the buffer, including the trailing null space
  138. * @param fmt The format string to use
  139. * @param ... Arguments for the format string
  140. *
  141. * The return value is the number of characters written into @buf not including
  142. * the trailing '\0'. If @size is == 0 the function returns 0.
  143. *
  144. * See the vsprintf() documentation for format string extensions over C99.
  145. */
  146. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  147. __attribute__ ((format (__printf__, 3, 4)));
  148. /**
  149. * Format a string and place it in a buffer (base function)
  150. *
  151. * @param buf The buffer to place the result into
  152. * @param size The size of the buffer, including the trailing null space
  153. * @param fmt The format string to use
  154. * @param args Arguments for the format string
  155. * @return The number characters which would be generated for the given
  156. * input, excluding the trailing '\0', as per ISO C99. Note that fewer
  157. * characters may be written if this number of characters is >= size.
  158. *
  159. * This function follows C99 vsnprintf, but has some extensions:
  160. * %pS output the name of a text symbol
  161. * %pF output the name of a function pointer
  162. * %pR output the address range in a struct resource
  163. *
  164. * The function returns the number of characters which would be
  165. * generated for the given input, excluding the trailing '\0',
  166. * as per ISO C99.
  167. *
  168. * Call this function if you are already dealing with a va_list.
  169. * You probably want snprintf() instead.
  170. */
  171. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
  172. /**
  173. * Format a string and place it in a buffer (va_list version)
  174. *
  175. * @param buf The buffer to place the result into
  176. * @param size The size of the buffer, including the trailing null space
  177. * @param fmt The format string to use
  178. * @param args Arguments for the format string
  179. * @return the number of characters which have been written into
  180. * the @buf not including the trailing '\0'. If @size is == 0 the function
  181. * returns 0.
  182. *
  183. * If you're not already dealing with a va_list consider using scnprintf().
  184. *
  185. * See the vsprintf() documentation for format string extensions over C99.
  186. */
  187. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  188. /**
  189. * print_grouped_ull() - print a value with digits grouped by ','
  190. *
  191. * This prints a value with grouped digits, like 12,345,678 to make it easier
  192. * to read.
  193. *
  194. * @val: Value to print
  195. * @digits: Number of digiits to print
  196. */
  197. void print_grouped_ull(unsigned long long int_val, int digits);
  198. bool str2off(const char *p, loff_t *num);
  199. bool str2long(const char *p, ulong *num);
  200. /**
  201. * strmhz() - Convert a value to a Hz string
  202. *
  203. * This creates a string indicating the number of MHz of a value. For example,
  204. * 2700000 produces "2.7".
  205. * @buf: Buffer to hold output string, which must be large enough
  206. * @hz: Value to convert
  207. */
  208. char *strmhz(char *buf, unsigned long hz);
  209. /**
  210. * str_to_upper() - Convert a string to upper case
  211. *
  212. * This simply uses toupper() on each character of the string.
  213. *
  214. * @in: String to convert (must be large enough to hold the output string)
  215. * @out: Buffer to put converted string
  216. * @len: Number of bytes available in @out (SIZE_MAX for all)
  217. */
  218. void str_to_upper(const char *in, char *out, size_t len);
  219. /**
  220. * sscanf - Unformat a buffer into a list of arguments
  221. * @buf: input buffer
  222. * @fmt: formatting of buffer
  223. * @...: resulting arguments
  224. */
  225. int sscanf(const char *buf, const char *fmt, ...);
  226. #endif