vsprintf.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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 temp[3]; /* hold each IP quad in reverse order */
  557. char *p = ip4_addr;
  558. int i, digits;
  559. for (i = 0; i < 4; i++) {
  560. digits = put_dec_trunc(temp, addr[i]) - temp;
  561. /* reverse the digits in the quad */
  562. while (digits--)
  563. *p++ = temp[digits];
  564. if (i != 3)
  565. *p++ = '.';
  566. }
  567. *p = '\0';
  568. return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
  569. }
  570. /*
  571. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  572. * by an extra set of alphanumeric characters that are extended format
  573. * specifiers.
  574. *
  575. * Right now we handle:
  576. *
  577. * - 'F' For symbolic function descriptor pointers
  578. * - 'S' For symbolic direct pointers
  579. * - 'R' For a struct resource pointer, it prints the range of
  580. * addresses (not the name nor the flags)
  581. * - 'M' For a 6-byte MAC address, it prints the address in the
  582. * usual colon-separated hex notation
  583. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  584. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  585. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  586. * currently the same
  587. *
  588. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  589. * function pointers are really function descriptors, which contain a
  590. * pointer to the real address.
  591. */
  592. static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
  593. {
  594. if (!ptr)
  595. return string(buf, end, "(null)", field_width, precision, flags);
  596. switch (*fmt) {
  597. case 'F':
  598. ptr = dereference_function_descriptor(ptr);
  599. /* Fallthrough */
  600. case 'S':
  601. return symbol_string(buf, end, ptr, field_width, precision, flags);
  602. case 'R':
  603. return resource_string(buf, end, ptr, field_width, precision, flags);
  604. case 'm':
  605. flags |= SPECIAL;
  606. /* Fallthrough */
  607. case 'M':
  608. return mac_address_string(buf, end, ptr, field_width, precision, flags);
  609. case 'i':
  610. flags |= SPECIAL;
  611. /* Fallthrough */
  612. case 'I':
  613. if (fmt[1] == '6')
  614. return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
  615. if (fmt[1] == '4')
  616. return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
  617. flags &= ~SPECIAL;
  618. break;
  619. }
  620. flags |= SMALL;
  621. if (field_width == -1) {
  622. field_width = 2*sizeof(void *);
  623. flags |= ZEROPAD;
  624. }
  625. return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
  626. }
  627. /**
  628. * vsnprintf - Format a string and place it in a buffer
  629. * @buf: The buffer to place the result into
  630. * @size: The size of the buffer, including the trailing null space
  631. * @fmt: The format string to use
  632. * @args: Arguments for the format string
  633. *
  634. * This function follows C99 vsnprintf, but has some extensions:
  635. * %pS output the name of a text symbol
  636. * %pF output the name of a function pointer
  637. * %pR output the address range in a struct resource
  638. *
  639. * The return value is the number of characters which would
  640. * be generated for the given input, excluding the trailing
  641. * '\0', as per ISO C99. If you want to have the exact
  642. * number of characters written into @buf as return value
  643. * (not including the trailing '\0'), use vscnprintf(). If the
  644. * return is greater than or equal to @size, the resulting
  645. * string is truncated.
  646. *
  647. * Call this function if you are already dealing with a va_list.
  648. * You probably want snprintf() instead.
  649. */
  650. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  651. {
  652. unsigned long long num;
  653. int base;
  654. char *str, *end, c;
  655. int flags; /* flags to number() */
  656. int field_width; /* width of output field */
  657. int precision; /* min. # of digits for integers; max
  658. number of chars for from string */
  659. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  660. /* 'z' support added 23/7/1999 S.H. */
  661. /* 'z' changed to 'Z' --davidm 1/25/99 */
  662. /* 't' added for ptrdiff_t */
  663. /* Reject out-of-range values early. Large positive sizes are
  664. used for unknown buffer sizes. */
  665. if (unlikely((int) size < 0)) {
  666. /* There can be only one.. */
  667. static char warn = 1;
  668. WARN_ON(warn);
  669. warn = 0;
  670. return 0;
  671. }
  672. str = buf;
  673. end = buf + size;
  674. /* Make sure end is always >= buf */
  675. if (end < buf) {
  676. end = ((void *)-1);
  677. size = end - buf;
  678. }
  679. for (; *fmt ; ++fmt) {
  680. if (*fmt != '%') {
  681. if (str < end)
  682. *str = *fmt;
  683. ++str;
  684. continue;
  685. }
  686. /* process flags */
  687. flags = 0;
  688. repeat:
  689. ++fmt; /* this also skips first '%' */
  690. switch (*fmt) {
  691. case '-': flags |= LEFT; goto repeat;
  692. case '+': flags |= PLUS; goto repeat;
  693. case ' ': flags |= SPACE; goto repeat;
  694. case '#': flags |= SPECIAL; goto repeat;
  695. case '0': flags |= ZEROPAD; goto repeat;
  696. }
  697. /* get field width */
  698. field_width = -1;
  699. if (isdigit(*fmt))
  700. field_width = skip_atoi(&fmt);
  701. else if (*fmt == '*') {
  702. ++fmt;
  703. /* it's the next argument */
  704. field_width = va_arg(args, int);
  705. if (field_width < 0) {
  706. field_width = -field_width;
  707. flags |= LEFT;
  708. }
  709. }
  710. /* get the precision */
  711. precision = -1;
  712. if (*fmt == '.') {
  713. ++fmt;
  714. if (isdigit(*fmt))
  715. precision = skip_atoi(&fmt);
  716. else if (*fmt == '*') {
  717. ++fmt;
  718. /* it's the next argument */
  719. precision = va_arg(args, int);
  720. }
  721. if (precision < 0)
  722. precision = 0;
  723. }
  724. /* get the conversion qualifier */
  725. qualifier = -1;
  726. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  727. *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
  728. qualifier = *fmt;
  729. ++fmt;
  730. if (qualifier == 'l' && *fmt == 'l') {
  731. qualifier = 'L';
  732. ++fmt;
  733. }
  734. }
  735. /* default base */
  736. base = 10;
  737. switch (*fmt) {
  738. case 'c':
  739. if (!(flags & LEFT)) {
  740. while (--field_width > 0) {
  741. if (str < end)
  742. *str = ' ';
  743. ++str;
  744. }
  745. }
  746. c = (unsigned char) va_arg(args, int);
  747. if (str < end)
  748. *str = c;
  749. ++str;
  750. while (--field_width > 0) {
  751. if (str < end)
  752. *str = ' ';
  753. ++str;
  754. }
  755. continue;
  756. case 's':
  757. str = string(str, end, va_arg(args, char *), field_width, precision, flags);
  758. continue;
  759. case 'p':
  760. str = pointer(fmt+1, str, end,
  761. va_arg(args, void *),
  762. field_width, precision, flags);
  763. /* Skip all alphanumeric pointer suffixes */
  764. while (isalnum(fmt[1]))
  765. fmt++;
  766. continue;
  767. case 'n':
  768. /* FIXME:
  769. * What does C99 say about the overflow case here? */
  770. if (qualifier == 'l') {
  771. long * ip = va_arg(args, long *);
  772. *ip = (str - buf);
  773. } else if (qualifier == 'Z' || qualifier == 'z') {
  774. size_t * ip = va_arg(args, size_t *);
  775. *ip = (str - buf);
  776. } else {
  777. int * ip = va_arg(args, int *);
  778. *ip = (str - buf);
  779. }
  780. continue;
  781. case '%':
  782. if (str < end)
  783. *str = '%';
  784. ++str;
  785. continue;
  786. /* integer number formats - set up the flags and "break" */
  787. case 'o':
  788. base = 8;
  789. break;
  790. case 'x':
  791. flags |= SMALL;
  792. case 'X':
  793. base = 16;
  794. break;
  795. case 'd':
  796. case 'i':
  797. flags |= SIGN;
  798. case 'u':
  799. break;
  800. default:
  801. if (str < end)
  802. *str = '%';
  803. ++str;
  804. if (*fmt) {
  805. if (str < end)
  806. *str = *fmt;
  807. ++str;
  808. } else {
  809. --fmt;
  810. }
  811. continue;
  812. }
  813. if (qualifier == 'L')
  814. num = va_arg(args, long long);
  815. else if (qualifier == 'l') {
  816. num = va_arg(args, unsigned long);
  817. if (flags & SIGN)
  818. num = (signed long) num;
  819. } else if (qualifier == 'Z' || qualifier == 'z') {
  820. num = va_arg(args, size_t);
  821. } else if (qualifier == 't') {
  822. num = va_arg(args, ptrdiff_t);
  823. } else if (qualifier == 'h') {
  824. num = (unsigned short) va_arg(args, int);
  825. if (flags & SIGN)
  826. num = (signed short) num;
  827. } else {
  828. num = va_arg(args, unsigned int);
  829. if (flags & SIGN)
  830. num = (signed int) num;
  831. }
  832. str = number(str, end, num, base,
  833. field_width, precision, flags);
  834. }
  835. if (size > 0) {
  836. if (str < end)
  837. *str = '\0';
  838. else
  839. end[-1] = '\0';
  840. }
  841. /* the trailing null byte doesn't count towards the total */
  842. return str-buf;
  843. }
  844. EXPORT_SYMBOL(vsnprintf);
  845. /**
  846. * vscnprintf - Format a string and place it in a buffer
  847. * @buf: The buffer to place the result into
  848. * @size: The size of the buffer, including the trailing null space
  849. * @fmt: The format string to use
  850. * @args: Arguments for the format string
  851. *
  852. * The return value is the number of characters which have been written into
  853. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  854. * returns 0.
  855. *
  856. * Call this function if you are already dealing with a va_list.
  857. * You probably want scnprintf() instead.
  858. *
  859. * See the vsnprintf() documentation for format string extensions over C99.
  860. */
  861. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  862. {
  863. int i;
  864. i=vsnprintf(buf,size,fmt,args);
  865. return (i >= size) ? (size - 1) : i;
  866. }
  867. EXPORT_SYMBOL(vscnprintf);
  868. /**
  869. * snprintf - Format a string and place it in a buffer
  870. * @buf: The buffer to place the result into
  871. * @size: The size of the buffer, including the trailing null space
  872. * @fmt: The format string to use
  873. * @...: Arguments for the format string
  874. *
  875. * The return value is the number of characters which would be
  876. * generated for the given input, excluding the trailing null,
  877. * as per ISO C99. If the return is greater than or equal to
  878. * @size, the resulting string is truncated.
  879. *
  880. * See the vsnprintf() documentation for format string extensions over C99.
  881. */
  882. int snprintf(char * buf, size_t size, const char *fmt, ...)
  883. {
  884. va_list args;
  885. int i;
  886. va_start(args, fmt);
  887. i=vsnprintf(buf,size,fmt,args);
  888. va_end(args);
  889. return i;
  890. }
  891. EXPORT_SYMBOL(snprintf);
  892. /**
  893. * scnprintf - Format a string and place it in a buffer
  894. * @buf: The buffer to place the result into
  895. * @size: The size of the buffer, including the trailing null space
  896. * @fmt: The format string to use
  897. * @...: Arguments for the format string
  898. *
  899. * The return value is the number of characters written into @buf not including
  900. * the trailing '\0'. If @size is <= 0 the function returns 0.
  901. */
  902. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  903. {
  904. va_list args;
  905. int i;
  906. va_start(args, fmt);
  907. i = vsnprintf(buf, size, fmt, args);
  908. va_end(args);
  909. return (i >= size) ? (size - 1) : i;
  910. }
  911. EXPORT_SYMBOL(scnprintf);
  912. /**
  913. * vsprintf - Format a string and place it in a buffer
  914. * @buf: The buffer to place the result into
  915. * @fmt: The format string to use
  916. * @args: Arguments for the format string
  917. *
  918. * The function returns the number of characters written
  919. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  920. * buffer overflows.
  921. *
  922. * Call this function if you are already dealing with a va_list.
  923. * You probably want sprintf() instead.
  924. *
  925. * See the vsnprintf() documentation for format string extensions over C99.
  926. */
  927. int vsprintf(char *buf, const char *fmt, va_list args)
  928. {
  929. return vsnprintf(buf, INT_MAX, fmt, args);
  930. }
  931. EXPORT_SYMBOL(vsprintf);
  932. /**
  933. * sprintf - Format a string and place it in a buffer
  934. * @buf: The buffer to place the result into
  935. * @fmt: The format string to use
  936. * @...: Arguments for the format string
  937. *
  938. * The function returns the number of characters written
  939. * into @buf. Use snprintf() or scnprintf() in order to avoid
  940. * buffer overflows.
  941. *
  942. * See the vsnprintf() documentation for format string extensions over C99.
  943. */
  944. int sprintf(char * buf, const char *fmt, ...)
  945. {
  946. va_list args;
  947. int i;
  948. va_start(args, fmt);
  949. i=vsnprintf(buf, INT_MAX, fmt, args);
  950. va_end(args);
  951. return i;
  952. }
  953. EXPORT_SYMBOL(sprintf);
  954. /**
  955. * vsscanf - Unformat a buffer into a list of arguments
  956. * @buf: input buffer
  957. * @fmt: format of buffer
  958. * @args: arguments
  959. */
  960. int vsscanf(const char * buf, const char * fmt, va_list args)
  961. {
  962. const char *str = buf;
  963. char *next;
  964. char digit;
  965. int num = 0;
  966. int qualifier;
  967. int base;
  968. int field_width;
  969. int is_sign = 0;
  970. while(*fmt && *str) {
  971. /* skip any white space in format */
  972. /* white space in format matchs any amount of
  973. * white space, including none, in the input.
  974. */
  975. if (isspace(*fmt)) {
  976. while (isspace(*fmt))
  977. ++fmt;
  978. while (isspace(*str))
  979. ++str;
  980. }
  981. /* anything that is not a conversion must match exactly */
  982. if (*fmt != '%' && *fmt) {
  983. if (*fmt++ != *str++)
  984. break;
  985. continue;
  986. }
  987. if (!*fmt)
  988. break;
  989. ++fmt;
  990. /* skip this conversion.
  991. * advance both strings to next white space
  992. */
  993. if (*fmt == '*') {
  994. while (!isspace(*fmt) && *fmt)
  995. fmt++;
  996. while (!isspace(*str) && *str)
  997. str++;
  998. continue;
  999. }
  1000. /* get field width */
  1001. field_width = -1;
  1002. if (isdigit(*fmt))
  1003. field_width = skip_atoi(&fmt);
  1004. /* get conversion qualifier */
  1005. qualifier = -1;
  1006. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  1007. *fmt == 'Z' || *fmt == 'z') {
  1008. qualifier = *fmt++;
  1009. if (unlikely(qualifier == *fmt)) {
  1010. if (qualifier == 'h') {
  1011. qualifier = 'H';
  1012. fmt++;
  1013. } else if (qualifier == 'l') {
  1014. qualifier = 'L';
  1015. fmt++;
  1016. }
  1017. }
  1018. }
  1019. base = 10;
  1020. is_sign = 0;
  1021. if (!*fmt || !*str)
  1022. break;
  1023. switch(*fmt++) {
  1024. case 'c':
  1025. {
  1026. char *s = (char *) va_arg(args,char*);
  1027. if (field_width == -1)
  1028. field_width = 1;
  1029. do {
  1030. *s++ = *str++;
  1031. } while (--field_width > 0 && *str);
  1032. num++;
  1033. }
  1034. continue;
  1035. case 's':
  1036. {
  1037. char *s = (char *) va_arg(args, char *);
  1038. if(field_width == -1)
  1039. field_width = INT_MAX;
  1040. /* first, skip leading white space in buffer */
  1041. while (isspace(*str))
  1042. str++;
  1043. /* now copy until next white space */
  1044. while (*str && !isspace(*str) && field_width--) {
  1045. *s++ = *str++;
  1046. }
  1047. *s = '\0';
  1048. num++;
  1049. }
  1050. continue;
  1051. case 'n':
  1052. /* return number of characters read so far */
  1053. {
  1054. int *i = (int *)va_arg(args,int*);
  1055. *i = str - buf;
  1056. }
  1057. continue;
  1058. case 'o':
  1059. base = 8;
  1060. break;
  1061. case 'x':
  1062. case 'X':
  1063. base = 16;
  1064. break;
  1065. case 'i':
  1066. base = 0;
  1067. case 'd':
  1068. is_sign = 1;
  1069. case 'u':
  1070. break;
  1071. case '%':
  1072. /* looking for '%' in str */
  1073. if (*str++ != '%')
  1074. return num;
  1075. continue;
  1076. default:
  1077. /* invalid format; stop here */
  1078. return num;
  1079. }
  1080. /* have some sort of integer conversion.
  1081. * first, skip white space in buffer.
  1082. */
  1083. while (isspace(*str))
  1084. str++;
  1085. digit = *str;
  1086. if (is_sign && digit == '-')
  1087. digit = *(str + 1);
  1088. if (!digit
  1089. || (base == 16 && !isxdigit(digit))
  1090. || (base == 10 && !isdigit(digit))
  1091. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1092. || (base == 0 && !isdigit(digit)))
  1093. break;
  1094. switch(qualifier) {
  1095. case 'H': /* that's 'hh' in format */
  1096. if (is_sign) {
  1097. signed char *s = (signed char *) va_arg(args,signed char *);
  1098. *s = (signed char) simple_strtol(str,&next,base);
  1099. } else {
  1100. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  1101. *s = (unsigned char) simple_strtoul(str, &next, base);
  1102. }
  1103. break;
  1104. case 'h':
  1105. if (is_sign) {
  1106. short *s = (short *) va_arg(args,short *);
  1107. *s = (short) simple_strtol(str,&next,base);
  1108. } else {
  1109. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  1110. *s = (unsigned short) simple_strtoul(str, &next, base);
  1111. }
  1112. break;
  1113. case 'l':
  1114. if (is_sign) {
  1115. long *l = (long *) va_arg(args,long *);
  1116. *l = simple_strtol(str,&next,base);
  1117. } else {
  1118. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  1119. *l = simple_strtoul(str,&next,base);
  1120. }
  1121. break;
  1122. case 'L':
  1123. if (is_sign) {
  1124. long long *l = (long long*) va_arg(args,long long *);
  1125. *l = simple_strtoll(str,&next,base);
  1126. } else {
  1127. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  1128. *l = simple_strtoull(str,&next,base);
  1129. }
  1130. break;
  1131. case 'Z':
  1132. case 'z':
  1133. {
  1134. size_t *s = (size_t*) va_arg(args,size_t*);
  1135. *s = (size_t) simple_strtoul(str,&next,base);
  1136. }
  1137. break;
  1138. default:
  1139. if (is_sign) {
  1140. int *i = (int *) va_arg(args, int*);
  1141. *i = (int) simple_strtol(str,&next,base);
  1142. } else {
  1143. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  1144. *i = (unsigned int) simple_strtoul(str,&next,base);
  1145. }
  1146. break;
  1147. }
  1148. num++;
  1149. if (!next)
  1150. break;
  1151. str = next;
  1152. }
  1153. /*
  1154. * Now we've come all the way through so either the input string or the
  1155. * format ended. In the former case, there can be a %n at the current
  1156. * position in the format that needs to be filled.
  1157. */
  1158. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1159. int *p = (int *)va_arg(args, int *);
  1160. *p = str - buf;
  1161. }
  1162. return num;
  1163. }
  1164. EXPORT_SYMBOL(vsscanf);
  1165. /**
  1166. * sscanf - Unformat a buffer into a list of arguments
  1167. * @buf: input buffer
  1168. * @fmt: formatting of buffer
  1169. * @...: resulting arguments
  1170. */
  1171. int sscanf(const char * buf, const char * fmt, ...)
  1172. {
  1173. va_list args;
  1174. int i;
  1175. va_start(args,fmt);
  1176. i = vsscanf(buf,fmt,args);
  1177. va_end(args);
  1178. return i;
  1179. }
  1180. EXPORT_SYMBOL(sscanf);