vsprintf.c 28 KB

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