vsprintf.c 28 KB

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