vsprintf.c 26 KB

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