vsprintf.c 22 KB

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