vsprintf.c 18 KB

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