vsprintf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. #define ZEROPAD 1 /* pad with zero */
  123. #define SIGN 2 /* unsigned/signed long */
  124. #define PLUS 4 /* show plus */
  125. #define SPACE 8 /* space if plus */
  126. #define LEFT 16 /* left justified */
  127. #define SPECIAL 32 /* 0x */
  128. #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
  129. static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
  130. {
  131. char sign,tmp[66];
  132. const char *digits;
  133. /* we are called with base 8, 10 or 16, only, thus don't need "g..." */
  134. static const char small_digits[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */
  135. static const char large_digits[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  136. int need_pfx = ((type & SPECIAL) && base != 10);
  137. int i;
  138. digits = (type & LARGE) ? large_digits : small_digits;
  139. if (type & LEFT)
  140. type &= ~ZEROPAD;
  141. if (base < 2 || base > 36)
  142. return NULL;
  143. sign = 0;
  144. if (type & SIGN) {
  145. if ((signed long long) num < 0) {
  146. sign = '-';
  147. num = - (signed long long) num;
  148. size--;
  149. } else if (type & PLUS) {
  150. sign = '+';
  151. size--;
  152. } else if (type & SPACE) {
  153. sign = ' ';
  154. size--;
  155. }
  156. }
  157. if (need_pfx) {
  158. size--;
  159. if (base == 16)
  160. size--;
  161. }
  162. /* generate full string in tmp[], in reverse order */
  163. i = 0;
  164. if (num == 0)
  165. tmp[i++] = '0';
  166. else if (base != 10) { /* 8 or 16 */
  167. int mask = base - 1;
  168. int shift = 3;
  169. if (base == 16) shift = 4;
  170. do {
  171. tmp[i++] = digits[((unsigned char)num) & mask];
  172. num >>= shift;
  173. } while (num);
  174. } else do { /* generic code, works for any base */
  175. tmp[i++] = digits[do_div(num,10 /*base*/)];
  176. } while (num);
  177. /* printing 100 using %2d gives "100", not "00" */
  178. if (i > precision)
  179. precision = i;
  180. /* leading space padding */
  181. size -= precision;
  182. if (!(type & (ZEROPAD+LEFT))) {
  183. while(--size >= 0) {
  184. if (buf < end)
  185. *buf = ' ';
  186. ++buf;
  187. }
  188. }
  189. /* sign */
  190. if (sign) {
  191. if (buf < end)
  192. *buf = sign;
  193. ++buf;
  194. }
  195. /* "0x" / "0" prefix */
  196. if (need_pfx) {
  197. if (buf < end)
  198. *buf = '0';
  199. ++buf;
  200. if (base == 16) {
  201. if (buf < end)
  202. *buf = digits[16]; /* for arbitrary base: digits[33]; */
  203. ++buf;
  204. }
  205. }
  206. /* zero or space padding */
  207. if (!(type & LEFT)) {
  208. char c = (type & ZEROPAD) ? '0' : ' ';
  209. while (--size >= 0) {
  210. if (buf < end)
  211. *buf = c;
  212. ++buf;
  213. }
  214. }
  215. /* hmm even more zero padding? */
  216. while (i <= --precision) {
  217. if (buf < end)
  218. *buf = '0';
  219. ++buf;
  220. }
  221. /* actual digits of result */
  222. while (--i >= 0) {
  223. if (buf < end)
  224. *buf = tmp[i];
  225. ++buf;
  226. }
  227. /* trailing space padding */
  228. while (--size >= 0) {
  229. if (buf < end)
  230. *buf = ' ';
  231. ++buf;
  232. }
  233. return buf;
  234. }
  235. /**
  236. * vsnprintf - Format a string and place it in a buffer
  237. * @buf: The buffer to place the result into
  238. * @size: The size of the buffer, including the trailing null space
  239. * @fmt: The format string to use
  240. * @args: Arguments for the format string
  241. *
  242. * The return value is the number of characters which would
  243. * be generated for the given input, excluding the trailing
  244. * '\0', as per ISO C99. If you want to have the exact
  245. * number of characters written into @buf as return value
  246. * (not including the trailing '\0'), use vscnprintf(). If the
  247. * return is greater than or equal to @size, the resulting
  248. * string is truncated.
  249. *
  250. * Call this function if you are already dealing with a va_list.
  251. * You probably want snprintf() instead.
  252. */
  253. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  254. {
  255. int len;
  256. unsigned long long num;
  257. int i, base;
  258. char *str, *end, c;
  259. const char *s;
  260. int flags; /* flags to number() */
  261. int field_width; /* width of output field */
  262. int precision; /* min. # of digits for integers; max
  263. number of chars for from string */
  264. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  265. /* 'z' support added 23/7/1999 S.H. */
  266. /* 'z' changed to 'Z' --davidm 1/25/99 */
  267. /* 't' added for ptrdiff_t */
  268. /* Reject out-of-range values early. Large positive sizes are
  269. used for unknown buffer sizes. */
  270. if (unlikely((int) size < 0)) {
  271. /* There can be only one.. */
  272. static char warn = 1;
  273. WARN_ON(warn);
  274. warn = 0;
  275. return 0;
  276. }
  277. str = buf;
  278. end = buf + size;
  279. /* Make sure end is always >= buf */
  280. if (end < buf) {
  281. end = ((void *)-1);
  282. size = end - buf;
  283. }
  284. for (; *fmt ; ++fmt) {
  285. if (*fmt != '%') {
  286. if (str < end)
  287. *str = *fmt;
  288. ++str;
  289. continue;
  290. }
  291. /* process flags */
  292. flags = 0;
  293. repeat:
  294. ++fmt; /* this also skips first '%' */
  295. switch (*fmt) {
  296. case '-': flags |= LEFT; goto repeat;
  297. case '+': flags |= PLUS; goto repeat;
  298. case ' ': flags |= SPACE; goto repeat;
  299. case '#': flags |= SPECIAL; goto repeat;
  300. case '0': flags |= ZEROPAD; goto repeat;
  301. }
  302. /* get field width */
  303. field_width = -1;
  304. if (isdigit(*fmt))
  305. field_width = skip_atoi(&fmt);
  306. else if (*fmt == '*') {
  307. ++fmt;
  308. /* it's the next argument */
  309. field_width = va_arg(args, int);
  310. if (field_width < 0) {
  311. field_width = -field_width;
  312. flags |= LEFT;
  313. }
  314. }
  315. /* get the precision */
  316. precision = -1;
  317. if (*fmt == '.') {
  318. ++fmt;
  319. if (isdigit(*fmt))
  320. precision = skip_atoi(&fmt);
  321. else if (*fmt == '*') {
  322. ++fmt;
  323. /* it's the next argument */
  324. precision = va_arg(args, int);
  325. }
  326. if (precision < 0)
  327. precision = 0;
  328. }
  329. /* get the conversion qualifier */
  330. qualifier = -1;
  331. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  332. *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
  333. qualifier = *fmt;
  334. ++fmt;
  335. if (qualifier == 'l' && *fmt == 'l') {
  336. qualifier = 'L';
  337. ++fmt;
  338. }
  339. }
  340. /* default base */
  341. base = 10;
  342. switch (*fmt) {
  343. case 'c':
  344. if (!(flags & LEFT)) {
  345. while (--field_width > 0) {
  346. if (str < end)
  347. *str = ' ';
  348. ++str;
  349. }
  350. }
  351. c = (unsigned char) va_arg(args, int);
  352. if (str < end)
  353. *str = c;
  354. ++str;
  355. while (--field_width > 0) {
  356. if (str < end)
  357. *str = ' ';
  358. ++str;
  359. }
  360. continue;
  361. case 's':
  362. s = va_arg(args, char *);
  363. if ((unsigned long)s < PAGE_SIZE)
  364. s = "<NULL>";
  365. len = strnlen(s, precision);
  366. if (!(flags & LEFT)) {
  367. while (len < field_width--) {
  368. if (str < end)
  369. *str = ' ';
  370. ++str;
  371. }
  372. }
  373. for (i = 0; i < len; ++i) {
  374. if (str < end)
  375. *str = *s;
  376. ++str; ++s;
  377. }
  378. while (len < field_width--) {
  379. if (str < end)
  380. *str = ' ';
  381. ++str;
  382. }
  383. continue;
  384. case 'p':
  385. if (field_width == -1) {
  386. field_width = 2*sizeof(void *);
  387. flags |= ZEROPAD;
  388. }
  389. str = number(str, end,
  390. (unsigned long) va_arg(args, void *),
  391. 16, field_width, precision, flags);
  392. continue;
  393. case 'n':
  394. /* FIXME:
  395. * What does C99 say about the overflow case here? */
  396. if (qualifier == 'l') {
  397. long * ip = va_arg(args, long *);
  398. *ip = (str - buf);
  399. } else if (qualifier == 'Z' || qualifier == 'z') {
  400. size_t * ip = va_arg(args, size_t *);
  401. *ip = (str - buf);
  402. } else {
  403. int * ip = va_arg(args, int *);
  404. *ip = (str - buf);
  405. }
  406. continue;
  407. case '%':
  408. if (str < end)
  409. *str = '%';
  410. ++str;
  411. continue;
  412. /* integer number formats - set up the flags and "break" */
  413. case 'o':
  414. base = 8;
  415. break;
  416. case 'X':
  417. flags |= LARGE;
  418. case 'x':
  419. base = 16;
  420. break;
  421. case 'd':
  422. case 'i':
  423. flags |= SIGN;
  424. case 'u':
  425. break;
  426. default:
  427. if (str < end)
  428. *str = '%';
  429. ++str;
  430. if (*fmt) {
  431. if (str < end)
  432. *str = *fmt;
  433. ++str;
  434. } else {
  435. --fmt;
  436. }
  437. continue;
  438. }
  439. if (qualifier == 'L')
  440. num = va_arg(args, long long);
  441. else if (qualifier == 'l') {
  442. num = va_arg(args, unsigned long);
  443. if (flags & SIGN)
  444. num = (signed long) num;
  445. } else if (qualifier == 'Z' || qualifier == 'z') {
  446. num = va_arg(args, size_t);
  447. } else if (qualifier == 't') {
  448. num = va_arg(args, ptrdiff_t);
  449. } else if (qualifier == 'h') {
  450. num = (unsigned short) va_arg(args, int);
  451. if (flags & SIGN)
  452. num = (signed short) num;
  453. } else {
  454. num = va_arg(args, unsigned int);
  455. if (flags & SIGN)
  456. num = (signed int) num;
  457. }
  458. str = number(str, end, num, base,
  459. field_width, precision, flags);
  460. }
  461. if (size > 0) {
  462. if (str < end)
  463. *str = '\0';
  464. else
  465. end[-1] = '\0';
  466. }
  467. /* the trailing null byte doesn't count towards the total */
  468. return str-buf;
  469. }
  470. EXPORT_SYMBOL(vsnprintf);
  471. /**
  472. * vscnprintf - Format a string and place it in a buffer
  473. * @buf: The buffer to place the result into
  474. * @size: The size of the buffer, including the trailing null space
  475. * @fmt: The format string to use
  476. * @args: Arguments for the format string
  477. *
  478. * The return value is the number of characters which have been written into
  479. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  480. * returns 0.
  481. *
  482. * Call this function if you are already dealing with a va_list.
  483. * You probably want scnprintf() instead.
  484. */
  485. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  486. {
  487. int i;
  488. i=vsnprintf(buf,size,fmt,args);
  489. return (i >= size) ? (size - 1) : i;
  490. }
  491. EXPORT_SYMBOL(vscnprintf);
  492. /**
  493. * snprintf - Format a string and place it in a buffer
  494. * @buf: The buffer to place the result into
  495. * @size: The size of the buffer, including the trailing null space
  496. * @fmt: The format string to use
  497. * @...: Arguments for the format string
  498. *
  499. * The return value is the number of characters which would be
  500. * generated for the given input, excluding the trailing null,
  501. * as per ISO C99. If the return is greater than or equal to
  502. * @size, the resulting string is truncated.
  503. */
  504. int snprintf(char * buf, size_t size, const char *fmt, ...)
  505. {
  506. va_list args;
  507. int i;
  508. va_start(args, fmt);
  509. i=vsnprintf(buf,size,fmt,args);
  510. va_end(args);
  511. return i;
  512. }
  513. EXPORT_SYMBOL(snprintf);
  514. /**
  515. * scnprintf - 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. * @...: Arguments for the format string
  520. *
  521. * The return value is the number of characters written into @buf not including
  522. * the trailing '\0'. If @size is <= 0 the function returns 0.
  523. */
  524. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  525. {
  526. va_list args;
  527. int i;
  528. va_start(args, fmt);
  529. i = vsnprintf(buf, size, fmt, args);
  530. va_end(args);
  531. return (i >= size) ? (size - 1) : i;
  532. }
  533. EXPORT_SYMBOL(scnprintf);
  534. /**
  535. * vsprintf - Format a string and place it in a buffer
  536. * @buf: The buffer to place the result into
  537. * @fmt: The format string to use
  538. * @args: Arguments for the format string
  539. *
  540. * The function returns the number of characters written
  541. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  542. * buffer overflows.
  543. *
  544. * Call this function if you are already dealing with a va_list.
  545. * You probably want sprintf() instead.
  546. */
  547. int vsprintf(char *buf, const char *fmt, va_list args)
  548. {
  549. return vsnprintf(buf, INT_MAX, fmt, args);
  550. }
  551. EXPORT_SYMBOL(vsprintf);
  552. /**
  553. * sprintf - Format a string and place it in a buffer
  554. * @buf: The buffer to place the result into
  555. * @fmt: The format string to use
  556. * @...: Arguments for the format string
  557. *
  558. * The function returns the number of characters written
  559. * into @buf. Use snprintf() or scnprintf() in order to avoid
  560. * buffer overflows.
  561. */
  562. int sprintf(char * buf, const char *fmt, ...)
  563. {
  564. va_list args;
  565. int i;
  566. va_start(args, fmt);
  567. i=vsnprintf(buf, INT_MAX, fmt, args);
  568. va_end(args);
  569. return i;
  570. }
  571. EXPORT_SYMBOL(sprintf);
  572. /**
  573. * vsscanf - Unformat a buffer into a list of arguments
  574. * @buf: input buffer
  575. * @fmt: format of buffer
  576. * @args: arguments
  577. */
  578. int vsscanf(const char * buf, const char * fmt, va_list args)
  579. {
  580. const char *str = buf;
  581. char *next;
  582. char digit;
  583. int num = 0;
  584. int qualifier;
  585. int base;
  586. int field_width;
  587. int is_sign = 0;
  588. while(*fmt && *str) {
  589. /* skip any white space in format */
  590. /* white space in format matchs any amount of
  591. * white space, including none, in the input.
  592. */
  593. if (isspace(*fmt)) {
  594. while (isspace(*fmt))
  595. ++fmt;
  596. while (isspace(*str))
  597. ++str;
  598. }
  599. /* anything that is not a conversion must match exactly */
  600. if (*fmt != '%' && *fmt) {
  601. if (*fmt++ != *str++)
  602. break;
  603. continue;
  604. }
  605. if (!*fmt)
  606. break;
  607. ++fmt;
  608. /* skip this conversion.
  609. * advance both strings to next white space
  610. */
  611. if (*fmt == '*') {
  612. while (!isspace(*fmt) && *fmt)
  613. fmt++;
  614. while (!isspace(*str) && *str)
  615. str++;
  616. continue;
  617. }
  618. /* get field width */
  619. field_width = -1;
  620. if (isdigit(*fmt))
  621. field_width = skip_atoi(&fmt);
  622. /* get conversion qualifier */
  623. qualifier = -1;
  624. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  625. *fmt == 'Z' || *fmt == 'z') {
  626. qualifier = *fmt++;
  627. if (unlikely(qualifier == *fmt)) {
  628. if (qualifier == 'h') {
  629. qualifier = 'H';
  630. fmt++;
  631. } else if (qualifier == 'l') {
  632. qualifier = 'L';
  633. fmt++;
  634. }
  635. }
  636. }
  637. base = 10;
  638. is_sign = 0;
  639. if (!*fmt || !*str)
  640. break;
  641. switch(*fmt++) {
  642. case 'c':
  643. {
  644. char *s = (char *) va_arg(args,char*);
  645. if (field_width == -1)
  646. field_width = 1;
  647. do {
  648. *s++ = *str++;
  649. } while (--field_width > 0 && *str);
  650. num++;
  651. }
  652. continue;
  653. case 's':
  654. {
  655. char *s = (char *) va_arg(args, char *);
  656. if(field_width == -1)
  657. field_width = INT_MAX;
  658. /* first, skip leading white space in buffer */
  659. while (isspace(*str))
  660. str++;
  661. /* now copy until next white space */
  662. while (*str && !isspace(*str) && field_width--) {
  663. *s++ = *str++;
  664. }
  665. *s = '\0';
  666. num++;
  667. }
  668. continue;
  669. case 'n':
  670. /* return number of characters read so far */
  671. {
  672. int *i = (int *)va_arg(args,int*);
  673. *i = str - buf;
  674. }
  675. continue;
  676. case 'o':
  677. base = 8;
  678. break;
  679. case 'x':
  680. case 'X':
  681. base = 16;
  682. break;
  683. case 'i':
  684. base = 0;
  685. case 'd':
  686. is_sign = 1;
  687. case 'u':
  688. break;
  689. case '%':
  690. /* looking for '%' in str */
  691. if (*str++ != '%')
  692. return num;
  693. continue;
  694. default:
  695. /* invalid format; stop here */
  696. return num;
  697. }
  698. /* have some sort of integer conversion.
  699. * first, skip white space in buffer.
  700. */
  701. while (isspace(*str))
  702. str++;
  703. digit = *str;
  704. if (is_sign && digit == '-')
  705. digit = *(str + 1);
  706. if (!digit
  707. || (base == 16 && !isxdigit(digit))
  708. || (base == 10 && !isdigit(digit))
  709. || (base == 8 && (!isdigit(digit) || digit > '7'))
  710. || (base == 0 && !isdigit(digit)))
  711. break;
  712. switch(qualifier) {
  713. case 'H': /* that's 'hh' in format */
  714. if (is_sign) {
  715. signed char *s = (signed char *) va_arg(args,signed char *);
  716. *s = (signed char) simple_strtol(str,&next,base);
  717. } else {
  718. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  719. *s = (unsigned char) simple_strtoul(str, &next, base);
  720. }
  721. break;
  722. case 'h':
  723. if (is_sign) {
  724. short *s = (short *) va_arg(args,short *);
  725. *s = (short) simple_strtol(str,&next,base);
  726. } else {
  727. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  728. *s = (unsigned short) simple_strtoul(str, &next, base);
  729. }
  730. break;
  731. case 'l':
  732. if (is_sign) {
  733. long *l = (long *) va_arg(args,long *);
  734. *l = simple_strtol(str,&next,base);
  735. } else {
  736. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  737. *l = simple_strtoul(str,&next,base);
  738. }
  739. break;
  740. case 'L':
  741. if (is_sign) {
  742. long long *l = (long long*) va_arg(args,long long *);
  743. *l = simple_strtoll(str,&next,base);
  744. } else {
  745. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  746. *l = simple_strtoull(str,&next,base);
  747. }
  748. break;
  749. case 'Z':
  750. case 'z':
  751. {
  752. size_t *s = (size_t*) va_arg(args,size_t*);
  753. *s = (size_t) simple_strtoul(str,&next,base);
  754. }
  755. break;
  756. default:
  757. if (is_sign) {
  758. int *i = (int *) va_arg(args, int*);
  759. *i = (int) simple_strtol(str,&next,base);
  760. } else {
  761. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  762. *i = (unsigned int) simple_strtoul(str,&next,base);
  763. }
  764. break;
  765. }
  766. num++;
  767. if (!next)
  768. break;
  769. str = next;
  770. }
  771. /*
  772. * Now we've come all the way through so either the input string or the
  773. * format ended. In the former case, there can be a %n at the current
  774. * position in the format that needs to be filled.
  775. */
  776. if (*fmt == '%' && *(fmt + 1) == 'n') {
  777. int *p = (int *)va_arg(args, int *);
  778. *p = str - buf;
  779. }
  780. return num;
  781. }
  782. EXPORT_SYMBOL(vsscanf);
  783. /**
  784. * sscanf - Unformat a buffer into a list of arguments
  785. * @buf: input buffer
  786. * @fmt: formatting of buffer
  787. * @...: resulting arguments
  788. */
  789. int sscanf(const char * buf, const char * fmt, ...)
  790. {
  791. va_list args;
  792. int i;
  793. va_start(args,fmt);
  794. i = vsscanf(buf,fmt,args);
  795. va_end(args);
  796. return i;
  797. }
  798. EXPORT_SYMBOL(sscanf);
  799. /* Simplified asprintf. */
  800. char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
  801. {
  802. unsigned int len;
  803. char *p;
  804. va_list aq;
  805. va_copy(aq, ap);
  806. len = vsnprintf(NULL, 0, fmt, aq);
  807. va_end(aq);
  808. p = kmalloc(len+1, gfp);
  809. if (!p)
  810. return NULL;
  811. vsnprintf(p, len+1, fmt, ap);
  812. return p;
  813. }
  814. EXPORT_SYMBOL(kvasprintf);
  815. char *kasprintf(gfp_t gfp, const char *fmt, ...)
  816. {
  817. va_list ap;
  818. char *p;
  819. va_start(ap, fmt);
  820. p = kvasprintf(gfp, fmt, ap);
  821. va_end(ap);
  822. return p;
  823. }
  824. EXPORT_SYMBOL(kasprintf);