vsprintf.c 28 KB

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