vsprintf.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. #include <dragonstub/dragonstub.h>
  2. #include <dragonstub/linux/stdarg.h>
  3. #include <dragonstub/bug.h>
  4. #include <dragonstub/minmax.h>
  5. #include <dragonstub/limits.h>
  6. #include <dragonstub/compiler_types.h>
  7. #include <dragonstub/linux/err.h>
  8. #include <dragonstub/linux/byteorder.h>
  9. #include <dragonstub/linux/div64.h>
  10. struct printf_spec {
  11. unsigned int type : 8; /* format_type enum */
  12. signed int field_width : 24; /* width of output field */
  13. unsigned int flags : 8; /* flags to number() */
  14. unsigned int base : 8; /* number base, 8, 10 or 16 only */
  15. signed int precision : 16; /* # of digits/chars */
  16. } __packed;
  17. #define SIGN 1 /* unsigned/signed, must be 1 */
  18. #define LEFT 2 /* left justified */
  19. #define PLUS 4 /* show plus */
  20. #define SPACE 8 /* space if plus */
  21. #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
  22. #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
  23. #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
  24. static_assert(SIGN == 1);
  25. static_assert(ZEROPAD == ('0' - ' '));
  26. static_assert(SMALL == ('a' ^ 'A'));
  27. enum format_type {
  28. FORMAT_TYPE_NONE, /* Just a string part */
  29. FORMAT_TYPE_WIDTH,
  30. FORMAT_TYPE_PRECISION,
  31. FORMAT_TYPE_CHAR,
  32. FORMAT_TYPE_STR,
  33. FORMAT_TYPE_PTR,
  34. FORMAT_TYPE_PERCENT_CHAR,
  35. FORMAT_TYPE_INVALID,
  36. FORMAT_TYPE_LONG_LONG,
  37. FORMAT_TYPE_ULONG,
  38. FORMAT_TYPE_LONG,
  39. FORMAT_TYPE_UBYTE,
  40. FORMAT_TYPE_BYTE,
  41. FORMAT_TYPE_USHORT,
  42. FORMAT_TYPE_SHORT,
  43. FORMAT_TYPE_UINT,
  44. FORMAT_TYPE_INT,
  45. FORMAT_TYPE_SIZE_T,
  46. FORMAT_TYPE_PTRDIFF
  47. };
  48. static noinline_for_stack int skip_atoi(const char **s)
  49. {
  50. int i = 0;
  51. do {
  52. i = i * 10 + *((*s)++) - '0';
  53. } while (isdigit(**s));
  54. return i;
  55. }
  56. /*
  57. * Decimal conversion is by far the most typical, and is used for
  58. * /proc and /sys data. This directly impacts e.g. top performance
  59. * with many processes running. We optimize it for speed by emitting
  60. * two characters at a time, using a 200 byte lookup table. This
  61. * roughly halves the number of multiplications compared to computing
  62. * the digits one at a time. Implementation strongly inspired by the
  63. * previous version, which in turn used ideas described at
  64. * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
  65. * from the author, Douglas W. Jones).
  66. *
  67. * It turns out there is precisely one 26 bit fixed-point
  68. * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
  69. * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
  70. * range happens to be somewhat larger (x <= 1073741898), but that's
  71. * irrelevant for our purpose.
  72. *
  73. * For dividing a number in the range [10^4, 10^6-1] by 100, we still
  74. * need a 32x32->64 bit multiply, so we simply use the same constant.
  75. *
  76. * For dividing a number in the range [100, 10^4-1] by 100, there are
  77. * several options. The simplest is (x * 0x147b) >> 19, which is valid
  78. * for all x <= 43698.
  79. */
  80. static const u16 decpair[100] = {
  81. #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
  82. _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
  83. _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
  84. _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
  85. _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
  86. _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
  87. _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
  88. _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
  89. _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
  90. _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
  91. _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
  92. #undef _
  93. };
  94. /*
  95. * This will print a single '0' even if r == 0, since we would
  96. * immediately jump to out_r where two 0s would be written but only
  97. * one of them accounted for in buf. This is needed by ip4_string
  98. * below. All other callers pass a non-zero value of r.
  99. */
  100. static noinline_for_stack char *put_dec_trunc8(char *buf, unsigned r)
  101. {
  102. unsigned q;
  103. /* 1 <= r < 10^8 */
  104. if (r < 100)
  105. goto out_r;
  106. /* 100 <= r < 10^8 */
  107. q = (r * (u64)0x28f5c29) >> 32;
  108. *((u16 *)buf) = decpair[r - 100 * q];
  109. buf += 2;
  110. /* 1 <= q < 10^6 */
  111. if (q < 100)
  112. goto out_q;
  113. /* 100 <= q < 10^6 */
  114. r = (q * (u64)0x28f5c29) >> 32;
  115. *((u16 *)buf) = decpair[q - 100 * r];
  116. buf += 2;
  117. /* 1 <= r < 10^4 */
  118. if (r < 100)
  119. goto out_r;
  120. /* 100 <= r < 10^4 */
  121. q = (r * 0x147b) >> 19;
  122. *((u16 *)buf) = decpair[r - 100 * q];
  123. buf += 2;
  124. out_q:
  125. /* 1 <= q < 100 */
  126. r = q;
  127. out_r:
  128. /* 1 <= r < 100 */
  129. *((u16 *)buf) = decpair[r];
  130. buf += r < 10 ? 1 : 2;
  131. return buf;
  132. }
  133. #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
  134. static noinline_for_stack char *put_dec_full8(char *buf, unsigned r)
  135. {
  136. unsigned q;
  137. /* 0 <= r < 10^8 */
  138. q = (r * (u64)0x28f5c29) >> 32;
  139. *((u16 *)buf) = decpair[r - 100 * q];
  140. buf += 2;
  141. /* 0 <= q < 10^6 */
  142. r = (q * (u64)0x28f5c29) >> 32;
  143. *((u16 *)buf) = decpair[q - 100 * r];
  144. buf += 2;
  145. /* 0 <= r < 10^4 */
  146. q = (r * 0x147b) >> 19;
  147. *((u16 *)buf) = decpair[r - 100 * q];
  148. buf += 2;
  149. /* 0 <= q < 100 */
  150. *((u16 *)buf) = decpair[q];
  151. buf += 2;
  152. return buf;
  153. }
  154. static noinline_for_stack char *put_dec(char *buf, unsigned long long n)
  155. {
  156. if (n >= 100 * 1000 * 1000)
  157. buf = put_dec_full8(buf, do_div(n, 100 * 1000 * 1000));
  158. /* 1 <= n <= 1.6e11 */
  159. if (n >= 100 * 1000 * 1000)
  160. buf = put_dec_full8(buf, do_div(n, 100 * 1000 * 1000));
  161. /* 1 <= n < 1e8 */
  162. return put_dec_trunc8(buf, n);
  163. }
  164. #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
  165. static void
  166. put_dec_full4(char *buf, unsigned r)
  167. {
  168. unsigned q;
  169. /* 0 <= r < 10^4 */
  170. q = (r * 0x147b) >> 19;
  171. *((u16 *)buf) = decpair[r - 100*q];
  172. buf += 2;
  173. /* 0 <= q < 100 */
  174. *((u16 *)buf) = decpair[q];
  175. }
  176. /*
  177. * Call put_dec_full4 on x % 10000, return x / 10000.
  178. * The approximation x/10000 == (x * 0x346DC5D7) >> 43
  179. * holds for all x < 1,128,869,999. The largest value this
  180. * helper will ever be asked to convert is 1,125,520,955.
  181. * (second call in the put_dec code, assuming n is all-ones).
  182. */
  183. static noinline_for_stack
  184. unsigned put_dec_helper4(char *buf, unsigned x)
  185. {
  186. uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
  187. put_dec_full4(buf, x - q * 10000);
  188. return q;
  189. }
  190. /* Based on code by Douglas W. Jones found at
  191. * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
  192. * (with permission from the author).
  193. * Performs no 64-bit division and hence should be fast on 32-bit machines.
  194. */
  195. static
  196. char *put_dec(char *buf, unsigned long long n)
  197. {
  198. uint32_t d3, d2, d1, q, h;
  199. if (n < 100*1000*1000)
  200. return put_dec_trunc8(buf, n);
  201. d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
  202. h = (n >> 32);
  203. d2 = (h ) & 0xffff;
  204. d3 = (h >> 16); /* implicit "& 0xffff" */
  205. /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
  206. = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
  207. q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
  208. q = put_dec_helper4(buf, q);
  209. q += 7671 * d3 + 9496 * d2 + 6 * d1;
  210. q = put_dec_helper4(buf+4, q);
  211. q += 4749 * d3 + 42 * d2;
  212. q = put_dec_helper4(buf+8, q);
  213. q += 281 * d3;
  214. buf += 12;
  215. if (q)
  216. buf = put_dec_trunc8(buf, q);
  217. else while (buf[-1] == '0')
  218. --buf;
  219. return buf;
  220. }
  221. #endif
  222. #define FIELD_WIDTH_MAX ((1 << 23) - 1)
  223. #define PRECISION_MAX ((1 << 15) - 1)
  224. static void set_field_width(struct printf_spec *spec, int width)
  225. {
  226. spec->field_width = width;
  227. if (WARN_ONCE(spec->field_width != width, "field width %d too large",
  228. width)) {
  229. spec->field_width =
  230. clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
  231. }
  232. }
  233. static void set_precision(struct printf_spec *spec, int prec)
  234. {
  235. spec->precision = prec;
  236. if (WARN_ONCE(spec->precision != prec, "precision %d too large",
  237. prec)) {
  238. spec->precision = clamp(prec, 0, PRECISION_MAX);
  239. }
  240. }
  241. static noinline_for_stack char *
  242. number(char *buf, char *end, unsigned long long num, struct printf_spec spec)
  243. {
  244. /* put_dec requires 2-byte alignment of the buffer. */
  245. char tmp[3 * sizeof(num)] __aligned(2);
  246. char sign;
  247. char locase;
  248. int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  249. int i;
  250. bool is_zero = num == 0LL;
  251. int field_width = spec.field_width;
  252. int precision = spec.precision;
  253. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  254. * produces same digits or (maybe lowercased) letters */
  255. locase = (spec.flags & SMALL);
  256. if (spec.flags & LEFT)
  257. spec.flags &= ~ZEROPAD;
  258. sign = 0;
  259. if (spec.flags & SIGN) {
  260. if ((signed long long)num < 0) {
  261. sign = '-';
  262. num = -(signed long long)num;
  263. field_width--;
  264. } else if (spec.flags & PLUS) {
  265. sign = '+';
  266. field_width--;
  267. } else if (spec.flags & SPACE) {
  268. sign = ' ';
  269. field_width--;
  270. }
  271. }
  272. if (need_pfx) {
  273. if (spec.base == 16)
  274. field_width -= 2;
  275. else if (!is_zero)
  276. field_width--;
  277. }
  278. /* generate full string in tmp[], in reverse order */
  279. i = 0;
  280. if (num < spec.base)
  281. tmp[i++] = hex_asc_upper[num] | locase;
  282. else if (spec.base != 10) { /* 8 or 16 */
  283. int mask = spec.base - 1;
  284. int shift = 3;
  285. if (spec.base == 16)
  286. shift = 4;
  287. do {
  288. tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] |
  289. locase);
  290. num >>= shift;
  291. } while (num);
  292. } else { /* base 10 */
  293. i = put_dec(tmp, num) - tmp;
  294. }
  295. /* printing 100 using %2d gives "100", not "00" */
  296. if (i > precision)
  297. precision = i;
  298. /* leading space padding */
  299. field_width -= precision;
  300. if (!(spec.flags & (ZEROPAD | LEFT))) {
  301. while (--field_width >= 0) {
  302. if (buf < end)
  303. *buf = ' ';
  304. ++buf;
  305. }
  306. }
  307. /* sign */
  308. if (sign) {
  309. if (buf < end)
  310. *buf = sign;
  311. ++buf;
  312. }
  313. /* "0x" / "0" prefix */
  314. if (need_pfx) {
  315. if (spec.base == 16 || !is_zero) {
  316. if (buf < end)
  317. *buf = '0';
  318. ++buf;
  319. }
  320. if (spec.base == 16) {
  321. if (buf < end)
  322. *buf = ('X' | locase);
  323. ++buf;
  324. }
  325. }
  326. /* zero or space padding */
  327. if (!(spec.flags & LEFT)) {
  328. char c = ' ' + (spec.flags & ZEROPAD);
  329. while (--field_width >= 0) {
  330. if (buf < end)
  331. *buf = c;
  332. ++buf;
  333. }
  334. }
  335. /* hmm even more zero padding? */
  336. while (i <= --precision) {
  337. if (buf < end)
  338. *buf = '0';
  339. ++buf;
  340. }
  341. /* actual digits of result */
  342. while (--i >= 0) {
  343. if (buf < end)
  344. *buf = tmp[i];
  345. ++buf;
  346. }
  347. /* trailing space padding */
  348. while (--field_width >= 0) {
  349. if (buf < end)
  350. *buf = ' ';
  351. ++buf;
  352. }
  353. return buf;
  354. }
  355. static noinline_for_stack char *
  356. special_hex_number(char *buf, char *end, unsigned long long num, int size)
  357. {
  358. struct printf_spec spec;
  359. spec.type = FORMAT_TYPE_PTR;
  360. spec.field_width = 2 + 2 * size; /* 0x + hex */
  361. spec.flags = SPECIAL | SMALL | ZEROPAD;
  362. spec.base = 16;
  363. spec.precision = -1;
  364. return number(buf, end, num, spec);
  365. }
  366. static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
  367. {
  368. size_t size;
  369. if (buf >= end) /* nowhere to put anything */
  370. return;
  371. size = end - buf;
  372. if (size <= spaces) {
  373. memset(buf, ' ', size);
  374. return;
  375. }
  376. if (len) {
  377. if (len > size - spaces)
  378. len = size - spaces;
  379. memmove(buf + spaces, buf, len);
  380. }
  381. memset(buf, ' ', spaces);
  382. }
  383. /*
  384. * Handle field width padding for a string.
  385. * @buf: current buffer position
  386. * @n: length of string
  387. * @end: end of output buffer
  388. * @spec: for field width and flags
  389. * Returns: new buffer position after padding.
  390. */
  391. static noinline_for_stack char *widen_string(char *buf, int n, char *end,
  392. struct printf_spec spec)
  393. {
  394. unsigned spaces;
  395. if (likely(n >= spec.field_width))
  396. return buf;
  397. /* we want to pad the sucker */
  398. spaces = spec.field_width - n;
  399. if (!(spec.flags & LEFT)) {
  400. move_right(buf - n, end, n, spaces);
  401. return buf + spaces;
  402. }
  403. while (spaces--) {
  404. if (buf < end)
  405. *buf = ' ';
  406. ++buf;
  407. }
  408. return buf;
  409. }
  410. /* Handle string from a well known address. */
  411. static char *string_nocheck(char *buf, char *end, const char *s,
  412. struct printf_spec spec)
  413. {
  414. int len = 0;
  415. int lim = spec.precision;
  416. while (lim--) {
  417. char c = *s++;
  418. if (!c)
  419. break;
  420. if (buf < end)
  421. *buf = c;
  422. ++buf;
  423. ++len;
  424. }
  425. return widen_string(buf, len, end, spec);
  426. }
  427. /* Be careful: error messages must fit into the given buffer. */
  428. static char *error_string(char *buf, char *end, const char *s,
  429. struct printf_spec spec)
  430. {
  431. /*
  432. * Hard limit to avoid a completely insane messages. It actually
  433. * works pretty well because most error messages are in
  434. * the many pointer format modifiers.
  435. */
  436. if (spec.precision == -1)
  437. spec.precision = 2 * sizeof(void *);
  438. return string_nocheck(buf, end, s, spec);
  439. }
  440. /*
  441. * Do not call any complex external code here. Nested printk()/vsprintf()
  442. * might cause infinite loops. Failures might break printk() and would
  443. * be hard to debug.
  444. */
  445. static const char *check_pointer_msg(const void *ptr)
  446. {
  447. if (!ptr)
  448. return "(null)";
  449. if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
  450. return "(efault)";
  451. return NULL;
  452. }
  453. static int check_pointer(char **buf, char *end, const void *ptr,
  454. struct printf_spec spec)
  455. {
  456. const char *err_msg;
  457. err_msg = check_pointer_msg(ptr);
  458. if (err_msg) {
  459. *buf = error_string(*buf, end, err_msg, spec);
  460. return -EFAULT;
  461. }
  462. return 0;
  463. }
  464. static noinline_for_stack char *string(char *buf, char *end, const char *s,
  465. struct printf_spec spec)
  466. {
  467. if (check_pointer(&buf, end, s, spec))
  468. return buf;
  469. return string_nocheck(buf, end, s, spec);
  470. }
  471. static char *pointer_string(char *buf, char *end, const void *ptr,
  472. struct printf_spec spec)
  473. {
  474. spec.base = 16;
  475. spec.flags |= SMALL;
  476. if (spec.field_width == -1) {
  477. spec.field_width = 2 * sizeof(ptr);
  478. spec.flags |= ZEROPAD;
  479. }
  480. return number(buf, end, (unsigned long int)ptr, spec);
  481. }
  482. static noinline_for_stack
  483. char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
  484. const char *fmt)
  485. {
  486. int i, len = 1; /* if we pass '%ph[CDN]', field width remains
  487. negative value, fallback to the default */
  488. char separator;
  489. if (spec.field_width == 0)
  490. /* nothing to print */
  491. return buf;
  492. if (check_pointer(&buf, end, addr, spec))
  493. return buf;
  494. switch (fmt[1]) {
  495. case 'C':
  496. separator = ':';
  497. break;
  498. case 'D':
  499. separator = '-';
  500. break;
  501. case 'N':
  502. separator = 0;
  503. break;
  504. default:
  505. separator = ' ';
  506. break;
  507. }
  508. if (spec.field_width > 0)
  509. len = min_t(int, spec.field_width, 64);
  510. for (i = 0; i < len; ++i) {
  511. if (buf < end)
  512. *buf = hex_asc_hi(addr[i]);
  513. ++buf;
  514. if (buf < end)
  515. *buf = hex_asc_lo(addr[i]);
  516. ++buf;
  517. if (separator && i != len - 1) {
  518. if (buf < end)
  519. *buf = separator;
  520. ++buf;
  521. }
  522. }
  523. return buf;
  524. }
  525. static noinline_for_stack
  526. char *mac_address_string(char *buf, char *end, u8 *addr,
  527. struct printf_spec spec, const char *fmt)
  528. {
  529. char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
  530. char *p = mac_addr;
  531. int i;
  532. char separator;
  533. bool reversed = false;
  534. if (check_pointer(&buf, end, addr, spec))
  535. return buf;
  536. switch (fmt[1]) {
  537. case 'F':
  538. separator = '-';
  539. break;
  540. case 'R':
  541. reversed = true;
  542. fallthrough;
  543. default:
  544. separator = ':';
  545. break;
  546. }
  547. for (i = 0; i < 6; i++) {
  548. if (reversed)
  549. p = hex_byte_pack(p, addr[5 - i]);
  550. else
  551. p = hex_byte_pack(p, addr[i]);
  552. if (fmt[0] == 'M' && i != 5)
  553. *p++ = separator;
  554. }
  555. *p = '\0';
  556. return string_nocheck(buf, end, mac_addr, spec);
  557. }
  558. static char *default_pointer(char *buf, char *end, const void *ptr,
  559. struct printf_spec spec)
  560. {
  561. return pointer_string(buf, end, ptr, spec);
  562. }
  563. static char *err_ptr(char *buf, char *end, void *ptr,
  564. struct printf_spec spec)
  565. {
  566. int err = PTR_ERR(ptr);
  567. // const char *sym = errname(err);
  568. // if (sym)
  569. // return string_nocheck(buf, end, sym, spec);
  570. /*
  571. * Somebody passed ERR_PTR(-1234) or some other non-existing
  572. * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
  573. * printing it as its decimal representation.
  574. */
  575. spec.flags |= SIGN;
  576. spec.base = 10;
  577. return number(buf, end, err, spec);
  578. }
  579. /*
  580. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  581. * by an extra set of alphanumeric characters that are extended format
  582. * specifiers.
  583. *
  584. * Please update scripts/checkpatch.pl when adding/removing conversion
  585. * characters. (Search for "check for vsprintf extension").
  586. *
  587. * Right now we handle:
  588. *
  589. * - 'S' For symbolic direct pointers (or function descriptors) with offset
  590. * - 's' For symbolic direct pointers (or function descriptors) without offset
  591. * - '[Ss]R' as above with __builtin_extract_return_addr() translation
  592. * - 'S[R]b' as above with module build ID (for use in backtraces)
  593. * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
  594. * %ps and %pS. Be careful when re-using these specifiers.
  595. * - 'B' For backtraced symbolic direct pointers with offset
  596. * - 'Bb' as above with module build ID (for use in backtraces)
  597. * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
  598. * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
  599. * - 'b[l]' For a bitmap, the number of bits is determined by the field
  600. * width which must be explicitly specified either as part of the
  601. * format string '%32b[l]' or through '%*b[l]', [l] selects
  602. * range-list format instead of hex format
  603. * - 'M' For a 6-byte MAC address, it prints the address in the
  604. * usual colon-separated hex notation
  605. * - 'm' For a 6-byte MAC address, it prints the hex address without colons
  606. * - 'MF' For a 6-byte MAC FDDI address, it prints the address
  607. * with a dash-separated hex notation
  608. * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
  609. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
  610. * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  611. * IPv6 uses colon separated network-order 16 bit hex with leading 0's
  612. * [S][pfs]
  613. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  614. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  615. * - 'i' [46] for 'raw' IPv4/IPv6 addresses
  616. * IPv6 omits the colons (01020304...0f)
  617. * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
  618. * [S][pfs]
  619. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  620. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  621. * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
  622. * - 'I[6S]c' for IPv6 addresses printed as specified by
  623. * https://tools.ietf.org/html/rfc5952
  624. * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
  625. * of the following flags (see string_escape_mem() for the
  626. * details):
  627. * a - ESCAPE_ANY
  628. * c - ESCAPE_SPECIAL
  629. * h - ESCAPE_HEX
  630. * n - ESCAPE_NULL
  631. * o - ESCAPE_OCTAL
  632. * p - ESCAPE_NP
  633. * s - ESCAPE_SPACE
  634. * By default ESCAPE_ANY_NP is used.
  635. * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
  636. * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  637. * Options for %pU are:
  638. * b big endian lower case hex (default)
  639. * B big endian UPPER case hex
  640. * l little endian lower case hex
  641. * L little endian UPPER case hex
  642. * big endian output byte order is:
  643. * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
  644. * little endian output byte order is:
  645. * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
  646. * - 'V' For a struct va_format which contains a format string * and va_list *,
  647. * call vsnprintf(->format, *->va_list).
  648. * Implements a "recursive vsnprintf".
  649. * Do not use this feature without some mechanism to verify the
  650. * correctness of the format string and va_list arguments.
  651. * - 'K' For a kernel pointer that should be hidden from unprivileged users.
  652. * Use only for procfs, sysfs and similar files, not printk(); please
  653. * read the documentation (path below) first.
  654. * - 'NF' For a netdev_features_t
  655. * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value.
  656. * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
  657. * a certain separator (' ' by default):
  658. * C colon
  659. * D dash
  660. * N no separator
  661. * The maximum supported length is 64 bytes of the input. Consider
  662. * to use print_hex_dump() for the larger input.
  663. * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
  664. * (default assumed to be phys_addr_t, passed by reference)
  665. * - 'd[234]' For a dentry name (optionally 2-4 last components)
  666. * - 'D[234]' Same as 'd' but for a struct file
  667. * - 'g' For block_device name (gendisk + partition number)
  668. * - 't[RT][dt][r][s]' For time and date as represented by:
  669. * R struct rtc_time
  670. * T time64_t
  671. * - 'C' For a clock, it prints the name (Common Clock Framework) or address
  672. * (legacy clock framework) of the clock
  673. * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
  674. * (legacy clock framework) of the clock
  675. * - 'G' For flags to be printed as a collection of symbolic strings that would
  676. * construct the specific value. Supported flags given by option:
  677. * p page flags (see struct page) given as pointer to unsigned long
  678. * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
  679. * v vma flags (VM_*) given as pointer to unsigned long
  680. * - 'OF[fnpPcCF]' For a device tree object
  681. * Without any optional arguments prints the full_name
  682. * f device node full_name
  683. * n device node name
  684. * p device node phandle
  685. * P device node path spec (name + @unit)
  686. * F device node flags
  687. * c major compatible string
  688. * C full compatible string
  689. * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
  690. * Without an option prints the full name of the node
  691. * f full name
  692. * P node name, including a possible unit address
  693. * - 'x' For printing the address unmodified. Equivalent to "%lx".
  694. * Please read the documentation (path below) before using!
  695. * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
  696. * bpf_trace_printk() where [ku] prefix specifies either kernel (k)
  697. * or user (u) memory to probe, and:
  698. * s a string, equivalent to "%s" on direct vsnprintf() use
  699. *
  700. * ** When making changes please also update:
  701. * Documentation/core-api/printk-formats.rst
  702. *
  703. * Note: The default behaviour (unadorned %p) is to hash the address,
  704. * rendering it useful as a unique identifier.
  705. *
  706. * There is also a '%pA' format specifier, but it is only intended to be used
  707. * from Rust code to format core::fmt::Arguments. Do *not* use it from C.
  708. * See rust/kernel/print.rs for details.
  709. */
  710. static noinline_for_stack char *pointer(const char *fmt, char *buf, char *end,
  711. void *ptr, struct printf_spec spec)
  712. {
  713. switch (*fmt) {
  714. // case 'S':
  715. // case 's':
  716. // ptr = dereference_symbol_descriptor(ptr);
  717. // fallthrough;
  718. // case 'B':
  719. // return symbol_string(buf, end, ptr, spec, fmt);
  720. // case 'R':
  721. // case 'r':
  722. // return resource_string(buf, end, ptr, spec, fmt);
  723. case 'h':
  724. return hex_string(buf, end, ptr, spec, fmt);
  725. // case 'b':
  726. // switch (fmt[1]) {
  727. // case 'l':
  728. // return bitmap_list_string(buf, end, ptr, spec, fmt);
  729. // default:
  730. // return bitmap_string(buf, end, ptr, spec, fmt);
  731. // }
  732. case 'M': /* Colon separated: 00:01:02:03:04:05 */
  733. case 'm': /* Contiguous: 000102030405 */
  734. /* [mM]F (FDDI) */
  735. /* [mM]R (Reverse order; Bluetooth) */
  736. return mac_address_string(buf, end, ptr, spec, fmt);
  737. // case 'I': /* Formatted IP supported
  738. // * 4: 1.2.3.4
  739. // * 6: 0001:0203:...:0708
  740. // * 6c: 1::708 or 1::1.2.3.4
  741. // */
  742. // case 'i': /* Contiguous:
  743. // * 4: 001.002.003.004
  744. // * 6: 000102...0f
  745. // */
  746. // return ip_addr_string(buf, end, ptr, spec, fmt);
  747. // case 'E':
  748. // return escaped_string(buf, end, ptr, spec, fmt);
  749. // case 'U':
  750. // return uuid_string(buf, end, ptr, spec, fmt);
  751. // case 'V':
  752. // return va_format(buf, end, ptr, spec, fmt);
  753. // case 'K':
  754. // return restricted_pointer(buf, end, ptr, spec);
  755. // case 'N':
  756. // return netdev_bits(buf, end, ptr, spec, fmt);
  757. // case '4':
  758. // return fourcc_string(buf, end, ptr, spec, fmt);
  759. // case 'a':
  760. // return address_val(buf, end, ptr, spec, fmt);
  761. // case 'd':
  762. // return dentry_name(buf, end, ptr, spec, fmt);
  763. // case 't':
  764. // return time_and_date(buf, end, ptr, spec, fmt);
  765. // case 'C':
  766. // return clock(buf, end, ptr, spec, fmt);
  767. // case 'D':
  768. // return file_dentry_name(buf, end, ptr, spec, fmt);
  769. #ifdef CONFIG_BLOCK
  770. case 'g':
  771. return bdev_name(buf, end, ptr, spec, fmt);
  772. #endif
  773. // case 'G':
  774. // return flags_string(buf, end, ptr, spec, fmt);
  775. // case 'O':
  776. // return device_node_string(buf, end, ptr, spec, fmt + 1);
  777. // case 'f':
  778. // return fwnode_string(buf, end, ptr, spec, fmt + 1);
  779. // case 'A':
  780. // if (!IS_ENABLED(CONFIG_RUST)) {
  781. // WARN_ONCE(1, "Please remove %%pA from non-Rust code\n");
  782. // return error_string(buf, end, "(%pA?)", spec);
  783. // }
  784. // return rust_fmt_argument(buf, end, ptr);
  785. case 'x':
  786. return pointer_string(buf, end, ptr, spec);
  787. case 'e':
  788. /* %pe with a non-ERR_PTR gets treated as plain %p */
  789. if (!IS_ERR(ptr))
  790. return default_pointer(buf, end, ptr, spec);
  791. return err_ptr(buf, end, ptr, spec);
  792. case 'u':
  793. case 'k':
  794. switch (fmt[1]) {
  795. case 's':
  796. return string(buf, end, ptr, spec);
  797. default:
  798. return error_string(buf, end, "(einval)", spec);
  799. }
  800. default:
  801. return default_pointer(buf, end, ptr, spec);
  802. }
  803. }
  804. /*
  805. * Helper function to decode printf style format.
  806. * Each call decode a token from the format and return the
  807. * number of characters read (or likely the delta where it wants
  808. * to go on the next call).
  809. * The decoded token is returned through the parameters
  810. *
  811. * 'h', 'l', or 'L' for integer fields
  812. * 'z' support added 23/7/1999 S.H.
  813. * 'z' changed to 'Z' --davidm 1/25/99
  814. * 'Z' changed to 'z' --adobriyan 2017-01-25
  815. * 't' added for ptrdiff_t
  816. *
  817. * @fmt: the format string
  818. * @type of the token returned
  819. * @flags: various flags such as +, -, # tokens..
  820. * @field_width: overwritten width
  821. * @base: base of the number (octal, hex, ...)
  822. * @precision: precision of a number
  823. * @qualifier: qualifier of a number (long, size_t, ...)
  824. */
  825. static noinline_for_stack int format_decode(const char *fmt,
  826. struct printf_spec *spec)
  827. {
  828. const char *start = fmt;
  829. char qualifier;
  830. /* we finished early by reading the field width */
  831. if (spec->type == FORMAT_TYPE_WIDTH) {
  832. if (spec->field_width < 0) {
  833. spec->field_width = -spec->field_width;
  834. spec->flags |= LEFT;
  835. }
  836. spec->type = FORMAT_TYPE_NONE;
  837. goto precision;
  838. }
  839. /* we finished early by reading the precision */
  840. if (spec->type == FORMAT_TYPE_PRECISION) {
  841. if (spec->precision < 0)
  842. spec->precision = 0;
  843. spec->type = FORMAT_TYPE_NONE;
  844. goto qualifier;
  845. }
  846. /* By default */
  847. spec->type = FORMAT_TYPE_NONE;
  848. for (; *fmt; ++fmt) {
  849. if (*fmt == '%')
  850. break;
  851. }
  852. /* Return the current non-format string */
  853. if (fmt != start || !*fmt)
  854. return fmt - start;
  855. /* Process flags */
  856. spec->flags = 0;
  857. while (1) { /* this also skips first '%' */
  858. bool found = true;
  859. ++fmt;
  860. switch (*fmt) {
  861. case '-':
  862. spec->flags |= LEFT;
  863. break;
  864. case '+':
  865. spec->flags |= PLUS;
  866. break;
  867. case ' ':
  868. spec->flags |= SPACE;
  869. break;
  870. case '#':
  871. spec->flags |= SPECIAL;
  872. break;
  873. case '0':
  874. spec->flags |= ZEROPAD;
  875. break;
  876. default:
  877. found = false;
  878. }
  879. if (!found)
  880. break;
  881. }
  882. /* get field width */
  883. spec->field_width = -1;
  884. if (isdigit(*fmt))
  885. spec->field_width = skip_atoi(&fmt);
  886. else if (*fmt == '*') {
  887. /* it's the next argument */
  888. spec->type = FORMAT_TYPE_WIDTH;
  889. return ++fmt - start;
  890. }
  891. precision:
  892. /* get the precision */
  893. spec->precision = -1;
  894. if (*fmt == '.') {
  895. ++fmt;
  896. if (isdigit(*fmt)) {
  897. spec->precision = skip_atoi(&fmt);
  898. if (spec->precision < 0)
  899. spec->precision = 0;
  900. } else if (*fmt == '*') {
  901. /* it's the next argument */
  902. spec->type = FORMAT_TYPE_PRECISION;
  903. return ++fmt - start;
  904. }
  905. }
  906. qualifier:
  907. /* get the conversion qualifier */
  908. qualifier = 0;
  909. if (*fmt == 'h' || _tolower(*fmt) == 'l' || *fmt == 'z' ||
  910. *fmt == 't') {
  911. qualifier = *fmt++;
  912. if (unlikely(qualifier == *fmt)) {
  913. if (qualifier == 'l') {
  914. qualifier = 'L';
  915. ++fmt;
  916. } else if (qualifier == 'h') {
  917. qualifier = 'H';
  918. ++fmt;
  919. }
  920. }
  921. }
  922. /* default base */
  923. spec->base = 10;
  924. switch (*fmt) {
  925. case 'c':
  926. spec->type = FORMAT_TYPE_CHAR;
  927. return ++fmt - start;
  928. case 's':
  929. spec->type = FORMAT_TYPE_STR;
  930. return ++fmt - start;
  931. case 'p':
  932. spec->type = FORMAT_TYPE_PTR;
  933. return ++fmt - start;
  934. case '%':
  935. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  936. return ++fmt - start;
  937. /* integer number formats - set up the flags and "break" */
  938. case 'o':
  939. spec->base = 8;
  940. break;
  941. case 'x':
  942. spec->flags |= SMALL;
  943. fallthrough;
  944. case 'X':
  945. spec->base = 16;
  946. break;
  947. case 'd':
  948. case 'i':
  949. spec->flags |= SIGN;
  950. break;
  951. case 'u':
  952. break;
  953. case 'n':
  954. /*
  955. * Since %n poses a greater security risk than
  956. * utility, treat it as any other invalid or
  957. * unsupported format specifier.
  958. */
  959. fallthrough;
  960. default:
  961. WARN_ONCE(1,
  962. "Please remove unsupported %%%c in format string\n",
  963. *fmt);
  964. spec->type = FORMAT_TYPE_INVALID;
  965. return fmt - start;
  966. }
  967. if (qualifier == 'L')
  968. spec->type = FORMAT_TYPE_LONG_LONG;
  969. else if (qualifier == 'l') {
  970. BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
  971. spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
  972. } else if (qualifier == 'z') {
  973. spec->type = FORMAT_TYPE_SIZE_T;
  974. } else if (qualifier == 't') {
  975. spec->type = FORMAT_TYPE_PTRDIFF;
  976. } else if (qualifier == 'H') {
  977. BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
  978. spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
  979. } else if (qualifier == 'h') {
  980. BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
  981. spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
  982. } else {
  983. BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
  984. spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
  985. }
  986. return ++fmt - start;
  987. }
  988. /**
  989. * vsnprintf - Format a string and place it in a buffer
  990. * @buf: The buffer to place the result into
  991. * @size: The size of the buffer, including the trailing null space
  992. * @fmt: The format string to use
  993. * @args: Arguments for the format string
  994. *
  995. * This function generally follows C99 vsnprintf, but has some
  996. * extensions and a few limitations:
  997. *
  998. * - ``%n`` is unsupported
  999. * - ``%p*`` is handled by pointer()
  1000. *
  1001. * See pointer() or Documentation/core-api/printk-formats.rst for more
  1002. * extensive description.
  1003. *
  1004. * **Please update the documentation in both places when making changes**
  1005. *
  1006. * The return value is the number of characters which would
  1007. * be generated for the given input, excluding the trailing
  1008. * '\0', as per ISO C99. If you want to have the exact
  1009. * number of characters written into @buf as return value
  1010. * (not including the trailing '\0'), use vscnprintf(). If the
  1011. * return is greater than or equal to @size, the resulting
  1012. * string is truncated.
  1013. *
  1014. * If you're not already dealing with a va_list consider using snprintf().
  1015. */
  1016. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1017. {
  1018. unsigned long long num;
  1019. char *str, *end;
  1020. struct printf_spec spec = { 0 };
  1021. /* Reject out-of-range values early. Large positive sizes are
  1022. used for unknown buffer sizes. */
  1023. if (WARN_ON_ONCE(size > INT_MAX))
  1024. return 0;
  1025. str = buf;
  1026. end = buf + size;
  1027. /* Make sure end is always >= buf */
  1028. if (end < buf) {
  1029. end = ((void *)-1);
  1030. size = end - buf;
  1031. }
  1032. while (*fmt) {
  1033. const char *old_fmt = fmt;
  1034. int read = format_decode(fmt, &spec);
  1035. fmt += read;
  1036. switch (spec.type) {
  1037. case FORMAT_TYPE_NONE: {
  1038. int copy = read;
  1039. if (str < end) {
  1040. if (copy > end - str)
  1041. copy = end - str;
  1042. memcpy(str, old_fmt, copy);
  1043. }
  1044. str += read;
  1045. break;
  1046. }
  1047. case FORMAT_TYPE_WIDTH:
  1048. set_field_width(&spec, va_arg(args, int));
  1049. break;
  1050. case FORMAT_TYPE_PRECISION:
  1051. set_precision(&spec, va_arg(args, int));
  1052. break;
  1053. case FORMAT_TYPE_CHAR: {
  1054. char c;
  1055. if (!(spec.flags & LEFT)) {
  1056. while (--spec.field_width > 0) {
  1057. if (str < end)
  1058. *str = ' ';
  1059. ++str;
  1060. }
  1061. }
  1062. c = (unsigned char)va_arg(args, int);
  1063. if (str < end)
  1064. *str = c;
  1065. ++str;
  1066. while (--spec.field_width > 0) {
  1067. if (str < end)
  1068. *str = ' ';
  1069. ++str;
  1070. }
  1071. break;
  1072. }
  1073. case FORMAT_TYPE_STR:
  1074. str = string(str, end, va_arg(args, char *), spec);
  1075. break;
  1076. case FORMAT_TYPE_PTR:
  1077. str = pointer(fmt, str, end, va_arg(args, void *),
  1078. spec);
  1079. while (isalnum(*fmt))
  1080. fmt++;
  1081. break;
  1082. case FORMAT_TYPE_PERCENT_CHAR:
  1083. if (str < end)
  1084. *str = '%';
  1085. ++str;
  1086. break;
  1087. case FORMAT_TYPE_INVALID:
  1088. /*
  1089. * Presumably the arguments passed gcc's type
  1090. * checking, but there is no safe or sane way
  1091. * for us to continue parsing the format and
  1092. * fetching from the va_list; the remaining
  1093. * specifiers and arguments would be out of
  1094. * sync.
  1095. */
  1096. goto out;
  1097. default:
  1098. switch (spec.type) {
  1099. case FORMAT_TYPE_LONG_LONG:
  1100. num = va_arg(args, long long);
  1101. break;
  1102. case FORMAT_TYPE_ULONG:
  1103. num = va_arg(args, unsigned long);
  1104. break;
  1105. case FORMAT_TYPE_LONG:
  1106. num = va_arg(args, long);
  1107. break;
  1108. case FORMAT_TYPE_SIZE_T:
  1109. if (spec.flags & SIGN)
  1110. num = va_arg(args, ssize_t);
  1111. else
  1112. num = va_arg(args, size_t);
  1113. break;
  1114. case FORMAT_TYPE_PTRDIFF:
  1115. num = va_arg(args, ptrdiff_t);
  1116. break;
  1117. case FORMAT_TYPE_UBYTE:
  1118. num = (unsigned char)va_arg(args, int);
  1119. break;
  1120. case FORMAT_TYPE_BYTE:
  1121. num = (signed char)va_arg(args, int);
  1122. break;
  1123. case FORMAT_TYPE_USHORT:
  1124. num = (unsigned short)va_arg(args, int);
  1125. break;
  1126. case FORMAT_TYPE_SHORT:
  1127. num = (short)va_arg(args, int);
  1128. break;
  1129. case FORMAT_TYPE_INT:
  1130. num = (int)va_arg(args, int);
  1131. break;
  1132. default:
  1133. num = va_arg(args, unsigned int);
  1134. }
  1135. str = number(str, end, num, spec);
  1136. }
  1137. }
  1138. out:
  1139. if (size > 0) {
  1140. if (str < end)
  1141. *str = '\0';
  1142. else
  1143. end[-1] = '\0';
  1144. }
  1145. /* the trailing null byte doesn't count towards the total */
  1146. return str - buf;
  1147. }
  1148. /**
  1149. * snprintf - Format a string and place it in a buffer
  1150. * @buf: The buffer to place the result into
  1151. * @size: The size of the buffer, including the trailing null space
  1152. * @fmt: The format string to use
  1153. * @...: Arguments for the format string
  1154. *
  1155. * The return value is the number of characters which would be
  1156. * generated for the given input, excluding the trailing null,
  1157. * as per ISO C99. If the return is greater than or equal to
  1158. * @size, the resulting string is truncated.
  1159. *
  1160. * See the vsnprintf() documentation for format string extensions over C99.
  1161. */
  1162. int snprintf(char *buf, size_t size, const char *fmt, ...)
  1163. {
  1164. va_list args;
  1165. int i;
  1166. va_start(args, fmt);
  1167. i = vsnprintf(buf, size, fmt, args);
  1168. va_end(args);
  1169. return i;
  1170. }