vsprintf.c 18 KB

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