vsprintf.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /*
  2. * linux/lib/vsprintf.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
  7. /*
  8. * Wirzenius wrote this portably, Torvalds fucked it up :-)
  9. */
  10. /*
  11. * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
  12. * - changed to provide snprintf and vsnprintf functions
  13. * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
  14. * - scnprintf and vscnprintf
  15. */
  16. #include <stdarg.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/ctype.h>
  21. #include <linux/kernel.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/ioport.h>
  25. #include <asm/page.h> /* for PAGE_SIZE */
  26. #include <asm/div64.h>
  27. #include <asm/sections.h> /* for dereference_function_descriptor() */
  28. /* Works only for digits and letters, but small and fast */
  29. #define TOLOWER(x) ((x) | 0x20)
  30. static unsigned int simple_guess_base(const char *cp)
  31. {
  32. if (cp[0] == '0') {
  33. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  34. return 16;
  35. else
  36. return 8;
  37. } else {
  38. return 10;
  39. }
  40. }
  41. /**
  42. * simple_strtoul - convert a string to an unsigned long
  43. * @cp: The start of the string
  44. * @endp: A pointer to the end of the parsed string will be placed here
  45. * @base: The number base to use
  46. */
  47. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  48. {
  49. unsigned long result = 0;
  50. if (!base)
  51. base = simple_guess_base(cp);
  52. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  53. cp += 2;
  54. while (isxdigit(*cp)) {
  55. unsigned int value;
  56. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  57. if (value >= base)
  58. break;
  59. result = result * base + value;
  60. cp++;
  61. }
  62. if (endp)
  63. *endp = (char *)cp;
  64. return result;
  65. }
  66. EXPORT_SYMBOL(simple_strtoul);
  67. /**
  68. * simple_strtol - convert a string to a signed long
  69. * @cp: The start of the string
  70. * @endp: A pointer to the end of the parsed string will be placed here
  71. * @base: The number base to use
  72. */
  73. long simple_strtol(const char *cp, char **endp, unsigned int base)
  74. {
  75. if(*cp == '-')
  76. return -simple_strtoul(cp + 1, endp, base);
  77. return simple_strtoul(cp, endp, base);
  78. }
  79. EXPORT_SYMBOL(simple_strtol);
  80. /**
  81. * simple_strtoull - convert a string to an unsigned long long
  82. * @cp: The start of the string
  83. * @endp: A pointer to the end of the parsed string will be placed here
  84. * @base: The number base to use
  85. */
  86. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  87. {
  88. unsigned long long result = 0;
  89. if (!base)
  90. base = simple_guess_base(cp);
  91. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  92. cp += 2;
  93. while (isxdigit(*cp)) {
  94. unsigned int value;
  95. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  96. if (value >= base)
  97. break;
  98. result = result * base + value;
  99. cp++;
  100. }
  101. if (endp)
  102. *endp = (char *)cp;
  103. return result;
  104. }
  105. EXPORT_SYMBOL(simple_strtoull);
  106. /**
  107. * simple_strtoll - convert a string to a signed long long
  108. * @cp: The start of the string
  109. * @endp: A pointer to the end of the parsed string will be placed here
  110. * @base: The number base to use
  111. */
  112. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  113. {
  114. if(*cp=='-')
  115. return -simple_strtoull(cp + 1, endp, base);
  116. return simple_strtoull(cp, endp, base);
  117. }
  118. /**
  119. * strict_strtoul - convert a string to an unsigned long strictly
  120. * @cp: The string to be converted
  121. * @base: The number base to use
  122. * @res: The converted result value
  123. *
  124. * strict_strtoul converts a string to an unsigned long only if the
  125. * string is really an unsigned long string, any string containing
  126. * any invalid char at the tail will be rejected and -EINVAL is returned,
  127. * only a newline char at the tail is acceptible because people generally
  128. * change a module parameter in the following way:
  129. *
  130. * echo 1024 > /sys/module/e1000/parameters/copybreak
  131. *
  132. * echo will append a newline to the tail.
  133. *
  134. * It returns 0 if conversion is successful and *res is set to the converted
  135. * value, otherwise it returns -EINVAL and *res is set to 0.
  136. *
  137. * simple_strtoul just ignores the successive invalid characters and
  138. * return the converted value of prefix part of the string.
  139. */
  140. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  141. {
  142. char *tail;
  143. unsigned long val;
  144. size_t len;
  145. *res = 0;
  146. len = strlen(cp);
  147. if (len == 0)
  148. return -EINVAL;
  149. val = simple_strtoul(cp, &tail, base);
  150. if ((*tail == '\0') ||
  151. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  152. *res = val;
  153. return 0;
  154. }
  155. return -EINVAL;
  156. }
  157. EXPORT_SYMBOL(strict_strtoul);
  158. /**
  159. * strict_strtol - convert a string to a long strictly
  160. * @cp: The string to be converted
  161. * @base: The number base to use
  162. * @res: The converted result value
  163. *
  164. * strict_strtol is similiar to strict_strtoul, but it allows the first
  165. * character of a string is '-'.
  166. *
  167. * It returns 0 if conversion is successful and *res is set to the converted
  168. * value, otherwise it returns -EINVAL and *res is set to 0.
  169. */
  170. int strict_strtol(const char *cp, unsigned int base, long *res)
  171. {
  172. int ret;
  173. if (*cp == '-') {
  174. ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
  175. if (!ret)
  176. *res = -(*res);
  177. } else {
  178. ret = strict_strtoul(cp, base, (unsigned long *)res);
  179. }
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(strict_strtol);
  183. /**
  184. * strict_strtoull - convert a string to an unsigned long long strictly
  185. * @cp: The string to be converted
  186. * @base: The number base to use
  187. * @res: The converted result value
  188. *
  189. * strict_strtoull converts a string to an unsigned long long only if the
  190. * string is really an unsigned long long string, any string containing
  191. * any invalid char at the tail will be rejected and -EINVAL is returned,
  192. * only a newline char at the tail is acceptible because people generally
  193. * change a module parameter in the following way:
  194. *
  195. * echo 1024 > /sys/module/e1000/parameters/copybreak
  196. *
  197. * echo will append a newline to the tail of the string.
  198. *
  199. * It returns 0 if conversion is successful and *res is set to the converted
  200. * value, otherwise it returns -EINVAL and *res is set to 0.
  201. *
  202. * simple_strtoull just ignores the successive invalid characters and
  203. * return the converted value of prefix part of the string.
  204. */
  205. int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
  206. {
  207. char *tail;
  208. unsigned long long val;
  209. size_t len;
  210. *res = 0;
  211. len = strlen(cp);
  212. if (len == 0)
  213. return -EINVAL;
  214. val = simple_strtoull(cp, &tail, base);
  215. if ((*tail == '\0') ||
  216. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  217. *res = val;
  218. return 0;
  219. }
  220. return -EINVAL;
  221. }
  222. EXPORT_SYMBOL(strict_strtoull);
  223. /**
  224. * strict_strtoll - convert a string to a long long strictly
  225. * @cp: The string to be converted
  226. * @base: The number base to use
  227. * @res: The converted result value
  228. *
  229. * strict_strtoll is similiar to strict_strtoull, but it allows the first
  230. * character of a string is '-'.
  231. *
  232. * It returns 0 if conversion is successful and *res is set to the converted
  233. * value, otherwise it returns -EINVAL and *res is set to 0.
  234. */
  235. int strict_strtoll(const char *cp, unsigned int base, long long *res)
  236. {
  237. int ret;
  238. if (*cp == '-') {
  239. ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
  240. if (!ret)
  241. *res = -(*res);
  242. } else {
  243. ret = strict_strtoull(cp, base, (unsigned long long *)res);
  244. }
  245. return ret;
  246. }
  247. EXPORT_SYMBOL(strict_strtoll);
  248. static int skip_atoi(const char **s)
  249. {
  250. int i=0;
  251. while (isdigit(**s))
  252. i = i*10 + *((*s)++) - '0';
  253. return i;
  254. }
  255. /* Decimal conversion is by far the most typical, and is used
  256. * for /proc and /sys data. This directly impacts e.g. top performance
  257. * with many processes running. We optimize it for speed
  258. * using code from
  259. * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
  260. * (with permission from the author, Douglas W. Jones). */
  261. /* Formats correctly any integer in [0,99999].
  262. * Outputs from one to five digits depending on input.
  263. * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
  264. static char* put_dec_trunc(char *buf, unsigned q)
  265. {
  266. unsigned d3, d2, d1, d0;
  267. d1 = (q>>4) & 0xf;
  268. d2 = (q>>8) & 0xf;
  269. d3 = (q>>12);
  270. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  271. q = (d0 * 0xcd) >> 11;
  272. d0 = d0 - 10*q;
  273. *buf++ = d0 + '0'; /* least significant digit */
  274. d1 = q + 9*d3 + 5*d2 + d1;
  275. if (d1 != 0) {
  276. q = (d1 * 0xcd) >> 11;
  277. d1 = d1 - 10*q;
  278. *buf++ = d1 + '0'; /* next digit */
  279. d2 = q + 2*d2;
  280. if ((d2 != 0) || (d3 != 0)) {
  281. q = (d2 * 0xd) >> 7;
  282. d2 = d2 - 10*q;
  283. *buf++ = d2 + '0'; /* next digit */
  284. d3 = q + 4*d3;
  285. if (d3 != 0) {
  286. q = (d3 * 0xcd) >> 11;
  287. d3 = d3 - 10*q;
  288. *buf++ = d3 + '0'; /* next digit */
  289. if (q != 0)
  290. *buf++ = q + '0'; /* most sign. digit */
  291. }
  292. }
  293. }
  294. return buf;
  295. }
  296. /* Same with if's removed. Always emits five digits */
  297. static char* put_dec_full(char *buf, unsigned q)
  298. {
  299. /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
  300. /* but anyway, gcc produces better code with full-sized ints */
  301. unsigned d3, d2, d1, d0;
  302. d1 = (q>>4) & 0xf;
  303. d2 = (q>>8) & 0xf;
  304. d3 = (q>>12);
  305. /* Possible ways to approx. divide by 10 */
  306. /* gcc -O2 replaces multiply with shifts and adds */
  307. // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
  308. // (x * 0x67) >> 10: 1100111
  309. // (x * 0x34) >> 9: 110100 - same
  310. // (x * 0x1a) >> 8: 11010 - same
  311. // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
  312. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  313. q = (d0 * 0xcd) >> 11;
  314. d0 = d0 - 10*q;
  315. *buf++ = d0 + '0';
  316. d1 = q + 9*d3 + 5*d2 + d1;
  317. q = (d1 * 0xcd) >> 11;
  318. d1 = d1 - 10*q;
  319. *buf++ = d1 + '0';
  320. d2 = q + 2*d2;
  321. q = (d2 * 0xd) >> 7;
  322. d2 = d2 - 10*q;
  323. *buf++ = d2 + '0';
  324. d3 = q + 4*d3;
  325. q = (d3 * 0xcd) >> 11; /* - shorter code */
  326. /* q = (d3 * 0x67) >> 10; - would also work */
  327. d3 = d3 - 10*q;
  328. *buf++ = d3 + '0';
  329. *buf++ = q + '0';
  330. return buf;
  331. }
  332. /* No inlining helps gcc to use registers better */
  333. static noinline char* put_dec(char *buf, unsigned long long num)
  334. {
  335. while (1) {
  336. unsigned rem;
  337. if (num < 100000)
  338. return put_dec_trunc(buf, num);
  339. rem = do_div(num, 100000);
  340. buf = put_dec_full(buf, rem);
  341. }
  342. }
  343. #define ZEROPAD 1 /* pad with zero */
  344. #define SIGN 2 /* unsigned/signed long */
  345. #define PLUS 4 /* show plus */
  346. #define SPACE 8 /* space if plus */
  347. #define LEFT 16 /* left justified */
  348. #define SMALL 32 /* Must be 32 == 0x20 */
  349. #define SPECIAL 64 /* 0x */
  350. static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
  351. {
  352. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  353. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  354. char tmp[66];
  355. char sign;
  356. char locase;
  357. int need_pfx = ((type & SPECIAL) && base != 10);
  358. int i;
  359. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  360. * produces same digits or (maybe lowercased) letters */
  361. locase = (type & SMALL);
  362. if (type & LEFT)
  363. type &= ~ZEROPAD;
  364. sign = 0;
  365. if (type & SIGN) {
  366. if ((signed long long) num < 0) {
  367. sign = '-';
  368. num = - (signed long long) num;
  369. size--;
  370. } else if (type & PLUS) {
  371. sign = '+';
  372. size--;
  373. } else if (type & SPACE) {
  374. sign = ' ';
  375. size--;
  376. }
  377. }
  378. if (need_pfx) {
  379. size--;
  380. if (base == 16)
  381. size--;
  382. }
  383. /* generate full string in tmp[], in reverse order */
  384. i = 0;
  385. if (num == 0)
  386. tmp[i++] = '0';
  387. /* Generic code, for any base:
  388. else do {
  389. tmp[i++] = (digits[do_div(num,base)] | locase);
  390. } while (num != 0);
  391. */
  392. else if (base != 10) { /* 8 or 16 */
  393. int mask = base - 1;
  394. int shift = 3;
  395. if (base == 16) shift = 4;
  396. do {
  397. tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
  398. num >>= shift;
  399. } while (num);
  400. } else { /* base 10 */
  401. i = put_dec(tmp, num) - tmp;
  402. }
  403. /* printing 100 using %2d gives "100", not "00" */
  404. if (i > precision)
  405. precision = i;
  406. /* leading space padding */
  407. size -= precision;
  408. if (!(type & (ZEROPAD+LEFT))) {
  409. while(--size >= 0) {
  410. if (buf < end)
  411. *buf = ' ';
  412. ++buf;
  413. }
  414. }
  415. /* sign */
  416. if (sign) {
  417. if (buf < end)
  418. *buf = sign;
  419. ++buf;
  420. }
  421. /* "0x" / "0" prefix */
  422. if (need_pfx) {
  423. if (buf < end)
  424. *buf = '0';
  425. ++buf;
  426. if (base == 16) {
  427. if (buf < end)
  428. *buf = ('X' | locase);
  429. ++buf;
  430. }
  431. }
  432. /* zero or space padding */
  433. if (!(type & LEFT)) {
  434. char c = (type & ZEROPAD) ? '0' : ' ';
  435. while (--size >= 0) {
  436. if (buf < end)
  437. *buf = c;
  438. ++buf;
  439. }
  440. }
  441. /* hmm even more zero padding? */
  442. while (i <= --precision) {
  443. if (buf < end)
  444. *buf = '0';
  445. ++buf;
  446. }
  447. /* actual digits of result */
  448. while (--i >= 0) {
  449. if (buf < end)
  450. *buf = tmp[i];
  451. ++buf;
  452. }
  453. /* trailing space padding */
  454. while (--size >= 0) {
  455. if (buf < end)
  456. *buf = ' ';
  457. ++buf;
  458. }
  459. return buf;
  460. }
  461. static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
  462. {
  463. int len, i;
  464. if ((unsigned long)s < PAGE_SIZE)
  465. s = "<NULL>";
  466. len = strnlen(s, precision);
  467. if (!(flags & LEFT)) {
  468. while (len < field_width--) {
  469. if (buf < end)
  470. *buf = ' ';
  471. ++buf;
  472. }
  473. }
  474. for (i = 0; i < len; ++i) {
  475. if (buf < end)
  476. *buf = *s;
  477. ++buf; ++s;
  478. }
  479. while (len < field_width--) {
  480. if (buf < end)
  481. *buf = ' ';
  482. ++buf;
  483. }
  484. return buf;
  485. }
  486. static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
  487. {
  488. unsigned long value = (unsigned long) ptr;
  489. #ifdef CONFIG_KALLSYMS
  490. char sym[KSYM_SYMBOL_LEN];
  491. sprint_symbol(sym, value);
  492. return string(buf, end, sym, field_width, precision, flags);
  493. #else
  494. field_width = 2*sizeof(void *);
  495. flags |= SPECIAL | SMALL | ZEROPAD;
  496. return number(buf, end, value, 16, field_width, precision, flags);
  497. #endif
  498. }
  499. static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags)
  500. {
  501. #ifndef IO_RSRC_PRINTK_SIZE
  502. #define IO_RSRC_PRINTK_SIZE 4
  503. #endif
  504. #ifndef MEM_RSRC_PRINTK_SIZE
  505. #define MEM_RSRC_PRINTK_SIZE 8
  506. #endif
  507. /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
  508. char sym[4*sizeof(resource_size_t) + 8];
  509. char *p = sym, *pend = sym + sizeof(sym);
  510. int size = -1;
  511. if (res->flags & IORESOURCE_IO)
  512. size = IO_RSRC_PRINTK_SIZE;
  513. else if (res->flags & IORESOURCE_MEM)
  514. size = MEM_RSRC_PRINTK_SIZE;
  515. *p++ = '[';
  516. p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
  517. *p++ = '-';
  518. p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
  519. *p++ = ']';
  520. *p = 0;
  521. return string(buf, end, sym, field_width, precision, flags);
  522. }
  523. static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
  524. int precision, int flags)
  525. {
  526. char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
  527. char *p = mac_addr;
  528. int i;
  529. for (i = 0; i < 6; i++) {
  530. p = pack_hex_byte(p, addr[i]);
  531. if (!(flags & SPECIAL) && i != 5)
  532. *p++ = ':';
  533. }
  534. *p = '\0';
  535. return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
  536. }
  537. static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
  538. int precision, int flags)
  539. {
  540. char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
  541. char *p = ip6_addr;
  542. int i;
  543. for (i = 0; i < 8; i++) {
  544. p = pack_hex_byte(p, addr[2 * i]);
  545. p = pack_hex_byte(p, addr[2 * i + 1]);
  546. if (!(flags & SPECIAL) && i != 7)
  547. *p++ = ':';
  548. }
  549. *p = '\0';
  550. return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL);
  551. }
  552. static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
  553. int precision, int flags)
  554. {
  555. char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
  556. char *p = ip4_addr;
  557. int i;
  558. for (i = 0; i < 4; i++) {
  559. p = put_dec_trunc(p, addr[i]);
  560. if (i != 3)
  561. *p++ = '.';
  562. }
  563. *p = '\0';
  564. return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
  565. }
  566. /*
  567. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  568. * by an extra set of alphanumeric characters that are extended format
  569. * specifiers.
  570. *
  571. * Right now we handle:
  572. *
  573. * - 'F' For symbolic function descriptor pointers
  574. * - 'S' For symbolic direct pointers
  575. * - 'R' For a struct resource pointer, it prints the range of
  576. * addresses (not the name nor the flags)
  577. * - 'M' For a 6-byte MAC address, it prints the address in the
  578. * usual colon-separated hex notation
  579. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  580. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  581. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  582. currently the same
  583. * - '6' For a IPv6 address prints the address in network-ordered 16 bit hex
  584. * with colon separators
  585. *
  586. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  587. * function pointers are really function descriptors, which contain a
  588. * pointer to the real address.
  589. */
  590. static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
  591. {
  592. switch (*fmt) {
  593. case 'F':
  594. ptr = dereference_function_descriptor(ptr);
  595. /* Fallthrough */
  596. case 'S':
  597. return symbol_string(buf, end, ptr, field_width, precision, flags);
  598. case 'R':
  599. return resource_string(buf, end, ptr, field_width, precision, flags);
  600. case 'M':
  601. return mac_address_string(buf, end, ptr, field_width, precision, flags);
  602. case '6':
  603. return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
  604. case 'i':
  605. flags |= SPECIAL;
  606. /* Fallthrough */
  607. case 'I':
  608. if (fmt[1] == '6')
  609. return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
  610. if (fmt[1] == '4')
  611. return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
  612. flags &= ~SPECIAL;
  613. break;
  614. }
  615. flags |= SMALL;
  616. if (field_width == -1) {
  617. field_width = 2*sizeof(void *);
  618. flags |= ZEROPAD;
  619. }
  620. return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
  621. }
  622. /**
  623. * vsnprintf - Format a string and place it in a buffer
  624. * @buf: The buffer to place the result into
  625. * @size: The size of the buffer, including the trailing null space
  626. * @fmt: The format string to use
  627. * @args: Arguments for the format string
  628. *
  629. * This function follows C99 vsnprintf, but has some extensions:
  630. * %pS output the name of a text symbol
  631. * %pF output the name of a function pointer
  632. * %pR output the address range in a struct resource
  633. *
  634. * The return value is the number of characters which would
  635. * be generated for the given input, excluding the trailing
  636. * '\0', as per ISO C99. If you want to have the exact
  637. * number of characters written into @buf as return value
  638. * (not including the trailing '\0'), use vscnprintf(). If the
  639. * return is greater than or equal to @size, the resulting
  640. * string is truncated.
  641. *
  642. * Call this function if you are already dealing with a va_list.
  643. * You probably want snprintf() instead.
  644. */
  645. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  646. {
  647. unsigned long long num;
  648. int base;
  649. char *str, *end, c;
  650. int flags; /* flags to number() */
  651. int field_width; /* width of output field */
  652. int precision; /* min. # of digits for integers; max
  653. number of chars for from string */
  654. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  655. /* 'z' support added 23/7/1999 S.H. */
  656. /* 'z' changed to 'Z' --davidm 1/25/99 */
  657. /* 't' added for ptrdiff_t */
  658. /* Reject out-of-range values early. Large positive sizes are
  659. used for unknown buffer sizes. */
  660. if (unlikely((int) size < 0)) {
  661. /* There can be only one.. */
  662. static char warn = 1;
  663. WARN_ON(warn);
  664. warn = 0;
  665. return 0;
  666. }
  667. str = buf;
  668. end = buf + size;
  669. /* Make sure end is always >= buf */
  670. if (end < buf) {
  671. end = ((void *)-1);
  672. size = end - buf;
  673. }
  674. for (; *fmt ; ++fmt) {
  675. if (*fmt != '%') {
  676. if (str < end)
  677. *str = *fmt;
  678. ++str;
  679. continue;
  680. }
  681. /* process flags */
  682. flags = 0;
  683. repeat:
  684. ++fmt; /* this also skips first '%' */
  685. switch (*fmt) {
  686. case '-': flags |= LEFT; goto repeat;
  687. case '+': flags |= PLUS; goto repeat;
  688. case ' ': flags |= SPACE; goto repeat;
  689. case '#': flags |= SPECIAL; goto repeat;
  690. case '0': flags |= ZEROPAD; goto repeat;
  691. }
  692. /* get field width */
  693. field_width = -1;
  694. if (isdigit(*fmt))
  695. field_width = skip_atoi(&fmt);
  696. else if (*fmt == '*') {
  697. ++fmt;
  698. /* it's the next argument */
  699. field_width = va_arg(args, int);
  700. if (field_width < 0) {
  701. field_width = -field_width;
  702. flags |= LEFT;
  703. }
  704. }
  705. /* get the precision */
  706. precision = -1;
  707. if (*fmt == '.') {
  708. ++fmt;
  709. if (isdigit(*fmt))
  710. precision = skip_atoi(&fmt);
  711. else if (*fmt == '*') {
  712. ++fmt;
  713. /* it's the next argument */
  714. precision = va_arg(args, int);
  715. }
  716. if (precision < 0)
  717. precision = 0;
  718. }
  719. /* get the conversion qualifier */
  720. qualifier = -1;
  721. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  722. *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
  723. qualifier = *fmt;
  724. ++fmt;
  725. if (qualifier == 'l' && *fmt == 'l') {
  726. qualifier = 'L';
  727. ++fmt;
  728. }
  729. }
  730. /* default base */
  731. base = 10;
  732. switch (*fmt) {
  733. case 'c':
  734. if (!(flags & LEFT)) {
  735. while (--field_width > 0) {
  736. if (str < end)
  737. *str = ' ';
  738. ++str;
  739. }
  740. }
  741. c = (unsigned char) va_arg(args, int);
  742. if (str < end)
  743. *str = c;
  744. ++str;
  745. while (--field_width > 0) {
  746. if (str < end)
  747. *str = ' ';
  748. ++str;
  749. }
  750. continue;
  751. case 's':
  752. str = string(str, end, va_arg(args, char *), field_width, precision, flags);
  753. continue;
  754. case 'p':
  755. str = pointer(fmt+1, str, end,
  756. va_arg(args, void *),
  757. field_width, precision, flags);
  758. /* Skip all alphanumeric pointer suffixes */
  759. while (isalnum(fmt[1]))
  760. fmt++;
  761. continue;
  762. case 'n':
  763. /* FIXME:
  764. * What does C99 say about the overflow case here? */
  765. if (qualifier == 'l') {
  766. long * ip = va_arg(args, long *);
  767. *ip = (str - buf);
  768. } else if (qualifier == 'Z' || qualifier == 'z') {
  769. size_t * ip = va_arg(args, size_t *);
  770. *ip = (str - buf);
  771. } else {
  772. int * ip = va_arg(args, int *);
  773. *ip = (str - buf);
  774. }
  775. continue;
  776. case '%':
  777. if (str < end)
  778. *str = '%';
  779. ++str;
  780. continue;
  781. /* integer number formats - set up the flags and "break" */
  782. case 'o':
  783. base = 8;
  784. break;
  785. case 'x':
  786. flags |= SMALL;
  787. case 'X':
  788. base = 16;
  789. break;
  790. case 'd':
  791. case 'i':
  792. flags |= SIGN;
  793. case 'u':
  794. break;
  795. default:
  796. if (str < end)
  797. *str = '%';
  798. ++str;
  799. if (*fmt) {
  800. if (str < end)
  801. *str = *fmt;
  802. ++str;
  803. } else {
  804. --fmt;
  805. }
  806. continue;
  807. }
  808. if (qualifier == 'L')
  809. num = va_arg(args, long long);
  810. else if (qualifier == 'l') {
  811. num = va_arg(args, unsigned long);
  812. if (flags & SIGN)
  813. num = (signed long) num;
  814. } else if (qualifier == 'Z' || qualifier == 'z') {
  815. num = va_arg(args, size_t);
  816. } else if (qualifier == 't') {
  817. num = va_arg(args, ptrdiff_t);
  818. } else if (qualifier == 'h') {
  819. num = (unsigned short) va_arg(args, int);
  820. if (flags & SIGN)
  821. num = (signed short) num;
  822. } else {
  823. num = va_arg(args, unsigned int);
  824. if (flags & SIGN)
  825. num = (signed int) num;
  826. }
  827. str = number(str, end, num, base,
  828. field_width, precision, flags);
  829. }
  830. if (size > 0) {
  831. if (str < end)
  832. *str = '\0';
  833. else
  834. end[-1] = '\0';
  835. }
  836. /* the trailing null byte doesn't count towards the total */
  837. return str-buf;
  838. }
  839. EXPORT_SYMBOL(vsnprintf);
  840. /**
  841. * vscnprintf - Format a string and place it in a buffer
  842. * @buf: The buffer to place the result into
  843. * @size: The size of the buffer, including the trailing null space
  844. * @fmt: The format string to use
  845. * @args: Arguments for the format string
  846. *
  847. * The return value is the number of characters which have been written into
  848. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  849. * returns 0.
  850. *
  851. * Call this function if you are already dealing with a va_list.
  852. * You probably want scnprintf() instead.
  853. *
  854. * See the vsnprintf() documentation for format string extensions over C99.
  855. */
  856. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  857. {
  858. int i;
  859. i=vsnprintf(buf,size,fmt,args);
  860. return (i >= size) ? (size - 1) : i;
  861. }
  862. EXPORT_SYMBOL(vscnprintf);
  863. /**
  864. * snprintf - Format a string and place it in a buffer
  865. * @buf: The buffer to place the result into
  866. * @size: The size of the buffer, including the trailing null space
  867. * @fmt: The format string to use
  868. * @...: Arguments for the format string
  869. *
  870. * The return value is the number of characters which would be
  871. * generated for the given input, excluding the trailing null,
  872. * as per ISO C99. If the return is greater than or equal to
  873. * @size, the resulting string is truncated.
  874. *
  875. * See the vsnprintf() documentation for format string extensions over C99.
  876. */
  877. int snprintf(char * buf, size_t size, const char *fmt, ...)
  878. {
  879. va_list args;
  880. int i;
  881. va_start(args, fmt);
  882. i=vsnprintf(buf,size,fmt,args);
  883. va_end(args);
  884. return i;
  885. }
  886. EXPORT_SYMBOL(snprintf);
  887. /**
  888. * scnprintf - Format a string and place it in a buffer
  889. * @buf: The buffer to place the result into
  890. * @size: The size of the buffer, including the trailing null space
  891. * @fmt: The format string to use
  892. * @...: Arguments for the format string
  893. *
  894. * The return value is the number of characters written into @buf not including
  895. * the trailing '\0'. If @size is <= 0 the function returns 0.
  896. */
  897. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  898. {
  899. va_list args;
  900. int i;
  901. va_start(args, fmt);
  902. i = vsnprintf(buf, size, fmt, args);
  903. va_end(args);
  904. return (i >= size) ? (size - 1) : i;
  905. }
  906. EXPORT_SYMBOL(scnprintf);
  907. /**
  908. * vsprintf - Format a string and place it in a buffer
  909. * @buf: The buffer to place the result into
  910. * @fmt: The format string to use
  911. * @args: Arguments for the format string
  912. *
  913. * The function returns the number of characters written
  914. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  915. * buffer overflows.
  916. *
  917. * Call this function if you are already dealing with a va_list.
  918. * You probably want sprintf() instead.
  919. *
  920. * See the vsnprintf() documentation for format string extensions over C99.
  921. */
  922. int vsprintf(char *buf, const char *fmt, va_list args)
  923. {
  924. return vsnprintf(buf, INT_MAX, fmt, args);
  925. }
  926. EXPORT_SYMBOL(vsprintf);
  927. /**
  928. * sprintf - Format a string and place it in a buffer
  929. * @buf: The buffer to place the result into
  930. * @fmt: The format string to use
  931. * @...: Arguments for the format string
  932. *
  933. * The function returns the number of characters written
  934. * into @buf. Use snprintf() or scnprintf() in order to avoid
  935. * buffer overflows.
  936. *
  937. * See the vsnprintf() documentation for format string extensions over C99.
  938. */
  939. int sprintf(char * buf, const char *fmt, ...)
  940. {
  941. va_list args;
  942. int i;
  943. va_start(args, fmt);
  944. i=vsnprintf(buf, INT_MAX, fmt, args);
  945. va_end(args);
  946. return i;
  947. }
  948. EXPORT_SYMBOL(sprintf);
  949. /**
  950. * vsscanf - Unformat a buffer into a list of arguments
  951. * @buf: input buffer
  952. * @fmt: format of buffer
  953. * @args: arguments
  954. */
  955. int vsscanf(const char * buf, const char * fmt, va_list args)
  956. {
  957. const char *str = buf;
  958. char *next;
  959. char digit;
  960. int num = 0;
  961. int qualifier;
  962. int base;
  963. int field_width;
  964. int is_sign = 0;
  965. while(*fmt && *str) {
  966. /* skip any white space in format */
  967. /* white space in format matchs any amount of
  968. * white space, including none, in the input.
  969. */
  970. if (isspace(*fmt)) {
  971. while (isspace(*fmt))
  972. ++fmt;
  973. while (isspace(*str))
  974. ++str;
  975. }
  976. /* anything that is not a conversion must match exactly */
  977. if (*fmt != '%' && *fmt) {
  978. if (*fmt++ != *str++)
  979. break;
  980. continue;
  981. }
  982. if (!*fmt)
  983. break;
  984. ++fmt;
  985. /* skip this conversion.
  986. * advance both strings to next white space
  987. */
  988. if (*fmt == '*') {
  989. while (!isspace(*fmt) && *fmt)
  990. fmt++;
  991. while (!isspace(*str) && *str)
  992. str++;
  993. continue;
  994. }
  995. /* get field width */
  996. field_width = -1;
  997. if (isdigit(*fmt))
  998. field_width = skip_atoi(&fmt);
  999. /* get conversion qualifier */
  1000. qualifier = -1;
  1001. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  1002. *fmt == 'Z' || *fmt == 'z') {
  1003. qualifier = *fmt++;
  1004. if (unlikely(qualifier == *fmt)) {
  1005. if (qualifier == 'h') {
  1006. qualifier = 'H';
  1007. fmt++;
  1008. } else if (qualifier == 'l') {
  1009. qualifier = 'L';
  1010. fmt++;
  1011. }
  1012. }
  1013. }
  1014. base = 10;
  1015. is_sign = 0;
  1016. if (!*fmt || !*str)
  1017. break;
  1018. switch(*fmt++) {
  1019. case 'c':
  1020. {
  1021. char *s = (char *) va_arg(args,char*);
  1022. if (field_width == -1)
  1023. field_width = 1;
  1024. do {
  1025. *s++ = *str++;
  1026. } while (--field_width > 0 && *str);
  1027. num++;
  1028. }
  1029. continue;
  1030. case 's':
  1031. {
  1032. char *s = (char *) va_arg(args, char *);
  1033. if(field_width == -1)
  1034. field_width = INT_MAX;
  1035. /* first, skip leading white space in buffer */
  1036. while (isspace(*str))
  1037. str++;
  1038. /* now copy until next white space */
  1039. while (*str && !isspace(*str) && field_width--) {
  1040. *s++ = *str++;
  1041. }
  1042. *s = '\0';
  1043. num++;
  1044. }
  1045. continue;
  1046. case 'n':
  1047. /* return number of characters read so far */
  1048. {
  1049. int *i = (int *)va_arg(args,int*);
  1050. *i = str - buf;
  1051. }
  1052. continue;
  1053. case 'o':
  1054. base = 8;
  1055. break;
  1056. case 'x':
  1057. case 'X':
  1058. base = 16;
  1059. break;
  1060. case 'i':
  1061. base = 0;
  1062. case 'd':
  1063. is_sign = 1;
  1064. case 'u':
  1065. break;
  1066. case '%':
  1067. /* looking for '%' in str */
  1068. if (*str++ != '%')
  1069. return num;
  1070. continue;
  1071. default:
  1072. /* invalid format; stop here */
  1073. return num;
  1074. }
  1075. /* have some sort of integer conversion.
  1076. * first, skip white space in buffer.
  1077. */
  1078. while (isspace(*str))
  1079. str++;
  1080. digit = *str;
  1081. if (is_sign && digit == '-')
  1082. digit = *(str + 1);
  1083. if (!digit
  1084. || (base == 16 && !isxdigit(digit))
  1085. || (base == 10 && !isdigit(digit))
  1086. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1087. || (base == 0 && !isdigit(digit)))
  1088. break;
  1089. switch(qualifier) {
  1090. case 'H': /* that's 'hh' in format */
  1091. if (is_sign) {
  1092. signed char *s = (signed char *) va_arg(args,signed char *);
  1093. *s = (signed char) simple_strtol(str,&next,base);
  1094. } else {
  1095. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  1096. *s = (unsigned char) simple_strtoul(str, &next, base);
  1097. }
  1098. break;
  1099. case 'h':
  1100. if (is_sign) {
  1101. short *s = (short *) va_arg(args,short *);
  1102. *s = (short) simple_strtol(str,&next,base);
  1103. } else {
  1104. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  1105. *s = (unsigned short) simple_strtoul(str, &next, base);
  1106. }
  1107. break;
  1108. case 'l':
  1109. if (is_sign) {
  1110. long *l = (long *) va_arg(args,long *);
  1111. *l = simple_strtol(str,&next,base);
  1112. } else {
  1113. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  1114. *l = simple_strtoul(str,&next,base);
  1115. }
  1116. break;
  1117. case 'L':
  1118. if (is_sign) {
  1119. long long *l = (long long*) va_arg(args,long long *);
  1120. *l = simple_strtoll(str,&next,base);
  1121. } else {
  1122. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  1123. *l = simple_strtoull(str,&next,base);
  1124. }
  1125. break;
  1126. case 'Z':
  1127. case 'z':
  1128. {
  1129. size_t *s = (size_t*) va_arg(args,size_t*);
  1130. *s = (size_t) simple_strtoul(str,&next,base);
  1131. }
  1132. break;
  1133. default:
  1134. if (is_sign) {
  1135. int *i = (int *) va_arg(args, int*);
  1136. *i = (int) simple_strtol(str,&next,base);
  1137. } else {
  1138. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  1139. *i = (unsigned int) simple_strtoul(str,&next,base);
  1140. }
  1141. break;
  1142. }
  1143. num++;
  1144. if (!next)
  1145. break;
  1146. str = next;
  1147. }
  1148. /*
  1149. * Now we've come all the way through so either the input string or the
  1150. * format ended. In the former case, there can be a %n at the current
  1151. * position in the format that needs to be filled.
  1152. */
  1153. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1154. int *p = (int *)va_arg(args, int *);
  1155. *p = str - buf;
  1156. }
  1157. return num;
  1158. }
  1159. EXPORT_SYMBOL(vsscanf);
  1160. /**
  1161. * sscanf - Unformat a buffer into a list of arguments
  1162. * @buf: input buffer
  1163. * @fmt: formatting of buffer
  1164. * @...: resulting arguments
  1165. */
  1166. int sscanf(const char * buf, const char * fmt, ...)
  1167. {
  1168. va_list args;
  1169. int i;
  1170. va_start(args,fmt);
  1171. i = vsscanf(buf,fmt,args);
  1172. va_end(args);
  1173. return i;
  1174. }
  1175. EXPORT_SYMBOL(sscanf);