vsprintf.c 19 KB

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