vsprintf.c 34 KB

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