vsprintf.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
  445. {
  446. int len, i;
  447. if ((unsigned long)s < PAGE_SIZE)
  448. s = "<NULL>";
  449. len = strnlen(s, precision);
  450. if (!(flags & LEFT)) {
  451. while (len < field_width--) {
  452. if (buf < end)
  453. *buf = ' ';
  454. ++buf;
  455. }
  456. }
  457. for (i = 0; i < len; ++i) {
  458. if (buf < end)
  459. *buf = *s;
  460. ++buf; ++s;
  461. }
  462. while (len < field_width--) {
  463. if (buf < end)
  464. *buf = ' ';
  465. ++buf;
  466. }
  467. return buf;
  468. }
  469. /**
  470. * vsnprintf - Format a string and place it in a buffer
  471. * @buf: The buffer to place the result into
  472. * @size: The size of the buffer, including the trailing null space
  473. * @fmt: The format string to use
  474. * @args: Arguments for the format string
  475. *
  476. * The return value is the number of characters which would
  477. * be generated for the given input, excluding the trailing
  478. * '\0', as per ISO C99. If you want to have the exact
  479. * number of characters written into @buf as return value
  480. * (not including the trailing '\0'), use vscnprintf(). If the
  481. * return is greater than or equal to @size, the resulting
  482. * string is truncated.
  483. *
  484. * Call this function if you are already dealing with a va_list.
  485. * You probably want snprintf() instead.
  486. */
  487. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  488. {
  489. unsigned long long num;
  490. int base;
  491. char *str, *end, c;
  492. int flags; /* flags to number() */
  493. int field_width; /* width of output field */
  494. int precision; /* min. # of digits for integers; max
  495. number of chars for from string */
  496. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  497. /* 'z' support added 23/7/1999 S.H. */
  498. /* 'z' changed to 'Z' --davidm 1/25/99 */
  499. /* 't' added for ptrdiff_t */
  500. /* Reject out-of-range values early. Large positive sizes are
  501. used for unknown buffer sizes. */
  502. if (unlikely((int) size < 0)) {
  503. /* There can be only one.. */
  504. static char warn = 1;
  505. WARN_ON(warn);
  506. warn = 0;
  507. return 0;
  508. }
  509. str = buf;
  510. end = buf + size;
  511. /* Make sure end is always >= buf */
  512. if (end < buf) {
  513. end = ((void *)-1);
  514. size = end - buf;
  515. }
  516. for (; *fmt ; ++fmt) {
  517. if (*fmt != '%') {
  518. if (str < end)
  519. *str = *fmt;
  520. ++str;
  521. continue;
  522. }
  523. /* process flags */
  524. flags = 0;
  525. repeat:
  526. ++fmt; /* this also skips first '%' */
  527. switch (*fmt) {
  528. case '-': flags |= LEFT; goto repeat;
  529. case '+': flags |= PLUS; goto repeat;
  530. case ' ': flags |= SPACE; goto repeat;
  531. case '#': flags |= SPECIAL; goto repeat;
  532. case '0': flags |= ZEROPAD; goto repeat;
  533. }
  534. /* get field width */
  535. field_width = -1;
  536. if (isdigit(*fmt))
  537. field_width = skip_atoi(&fmt);
  538. else if (*fmt == '*') {
  539. ++fmt;
  540. /* it's the next argument */
  541. field_width = va_arg(args, int);
  542. if (field_width < 0) {
  543. field_width = -field_width;
  544. flags |= LEFT;
  545. }
  546. }
  547. /* get the precision */
  548. precision = -1;
  549. if (*fmt == '.') {
  550. ++fmt;
  551. if (isdigit(*fmt))
  552. precision = skip_atoi(&fmt);
  553. else if (*fmt == '*') {
  554. ++fmt;
  555. /* it's the next argument */
  556. precision = va_arg(args, int);
  557. }
  558. if (precision < 0)
  559. precision = 0;
  560. }
  561. /* get the conversion qualifier */
  562. qualifier = -1;
  563. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  564. *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
  565. qualifier = *fmt;
  566. ++fmt;
  567. if (qualifier == 'l' && *fmt == 'l') {
  568. qualifier = 'L';
  569. ++fmt;
  570. }
  571. }
  572. /* default base */
  573. base = 10;
  574. switch (*fmt) {
  575. case 'c':
  576. if (!(flags & LEFT)) {
  577. while (--field_width > 0) {
  578. if (str < end)
  579. *str = ' ';
  580. ++str;
  581. }
  582. }
  583. c = (unsigned char) va_arg(args, int);
  584. if (str < end)
  585. *str = c;
  586. ++str;
  587. while (--field_width > 0) {
  588. if (str < end)
  589. *str = ' ';
  590. ++str;
  591. }
  592. continue;
  593. case 's':
  594. str = string(str, end, va_arg(args, char *), field_width, precision, flags);
  595. continue;
  596. case 'p':
  597. flags |= SMALL;
  598. if (field_width == -1) {
  599. field_width = 2*sizeof(void *);
  600. flags |= ZEROPAD;
  601. }
  602. str = number(str, end,
  603. (unsigned long) va_arg(args, void *),
  604. 16, field_width, precision, flags);
  605. continue;
  606. case 'n':
  607. /* FIXME:
  608. * What does C99 say about the overflow case here? */
  609. if (qualifier == 'l') {
  610. long * ip = va_arg(args, long *);
  611. *ip = (str - buf);
  612. } else if (qualifier == 'Z' || qualifier == 'z') {
  613. size_t * ip = va_arg(args, size_t *);
  614. *ip = (str - buf);
  615. } else {
  616. int * ip = va_arg(args, int *);
  617. *ip = (str - buf);
  618. }
  619. continue;
  620. case '%':
  621. if (str < end)
  622. *str = '%';
  623. ++str;
  624. continue;
  625. /* integer number formats - set up the flags and "break" */
  626. case 'o':
  627. base = 8;
  628. break;
  629. case 'x':
  630. flags |= SMALL;
  631. case 'X':
  632. base = 16;
  633. break;
  634. case 'd':
  635. case 'i':
  636. flags |= SIGN;
  637. case 'u':
  638. break;
  639. default:
  640. if (str < end)
  641. *str = '%';
  642. ++str;
  643. if (*fmt) {
  644. if (str < end)
  645. *str = *fmt;
  646. ++str;
  647. } else {
  648. --fmt;
  649. }
  650. continue;
  651. }
  652. if (qualifier == 'L')
  653. num = va_arg(args, long long);
  654. else if (qualifier == 'l') {
  655. num = va_arg(args, unsigned long);
  656. if (flags & SIGN)
  657. num = (signed long) num;
  658. } else if (qualifier == 'Z' || qualifier == 'z') {
  659. num = va_arg(args, size_t);
  660. } else if (qualifier == 't') {
  661. num = va_arg(args, ptrdiff_t);
  662. } else if (qualifier == 'h') {
  663. num = (unsigned short) va_arg(args, int);
  664. if (flags & SIGN)
  665. num = (signed short) num;
  666. } else {
  667. num = va_arg(args, unsigned int);
  668. if (flags & SIGN)
  669. num = (signed int) num;
  670. }
  671. str = number(str, end, num, base,
  672. field_width, precision, flags);
  673. }
  674. if (size > 0) {
  675. if (str < end)
  676. *str = '\0';
  677. else
  678. end[-1] = '\0';
  679. }
  680. /* the trailing null byte doesn't count towards the total */
  681. return str-buf;
  682. }
  683. EXPORT_SYMBOL(vsnprintf);
  684. /**
  685. * vscnprintf - Format a string and place it in a buffer
  686. * @buf: The buffer to place the result into
  687. * @size: The size of the buffer, including the trailing null space
  688. * @fmt: The format string to use
  689. * @args: Arguments for the format string
  690. *
  691. * The return value is the number of characters which have been written into
  692. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  693. * returns 0.
  694. *
  695. * Call this function if you are already dealing with a va_list.
  696. * You probably want scnprintf() instead.
  697. */
  698. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  699. {
  700. int i;
  701. i=vsnprintf(buf,size,fmt,args);
  702. return (i >= size) ? (size - 1) : i;
  703. }
  704. EXPORT_SYMBOL(vscnprintf);
  705. /**
  706. * snprintf - Format a string and place it in a buffer
  707. * @buf: The buffer to place the result into
  708. * @size: The size of the buffer, including the trailing null space
  709. * @fmt: The format string to use
  710. * @...: Arguments for the format string
  711. *
  712. * The return value is the number of characters which would be
  713. * generated for the given input, excluding the trailing null,
  714. * as per ISO C99. If the return is greater than or equal to
  715. * @size, the resulting string is truncated.
  716. */
  717. int snprintf(char * buf, size_t size, const char *fmt, ...)
  718. {
  719. va_list args;
  720. int i;
  721. va_start(args, fmt);
  722. i=vsnprintf(buf,size,fmt,args);
  723. va_end(args);
  724. return i;
  725. }
  726. EXPORT_SYMBOL(snprintf);
  727. /**
  728. * scnprintf - Format a string and place it in a buffer
  729. * @buf: The buffer to place the result into
  730. * @size: The size of the buffer, including the trailing null space
  731. * @fmt: The format string to use
  732. * @...: Arguments for the format string
  733. *
  734. * The return value is the number of characters written into @buf not including
  735. * the trailing '\0'. If @size is <= 0 the function returns 0.
  736. */
  737. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  738. {
  739. va_list args;
  740. int i;
  741. va_start(args, fmt);
  742. i = vsnprintf(buf, size, fmt, args);
  743. va_end(args);
  744. return (i >= size) ? (size - 1) : i;
  745. }
  746. EXPORT_SYMBOL(scnprintf);
  747. /**
  748. * vsprintf - Format a string and place it in a buffer
  749. * @buf: The buffer to place the result into
  750. * @fmt: The format string to use
  751. * @args: Arguments for the format string
  752. *
  753. * The function returns the number of characters written
  754. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  755. * buffer overflows.
  756. *
  757. * Call this function if you are already dealing with a va_list.
  758. * You probably want sprintf() instead.
  759. */
  760. int vsprintf(char *buf, const char *fmt, va_list args)
  761. {
  762. return vsnprintf(buf, INT_MAX, fmt, args);
  763. }
  764. EXPORT_SYMBOL(vsprintf);
  765. /**
  766. * sprintf - Format a string and place it in a buffer
  767. * @buf: The buffer to place the result into
  768. * @fmt: The format string to use
  769. * @...: Arguments for the format string
  770. *
  771. * The function returns the number of characters written
  772. * into @buf. Use snprintf() or scnprintf() in order to avoid
  773. * buffer overflows.
  774. */
  775. int sprintf(char * buf, const char *fmt, ...)
  776. {
  777. va_list args;
  778. int i;
  779. va_start(args, fmt);
  780. i=vsnprintf(buf, INT_MAX, fmt, args);
  781. va_end(args);
  782. return i;
  783. }
  784. EXPORT_SYMBOL(sprintf);
  785. /**
  786. * vsscanf - Unformat a buffer into a list of arguments
  787. * @buf: input buffer
  788. * @fmt: format of buffer
  789. * @args: arguments
  790. */
  791. int vsscanf(const char * buf, const char * fmt, va_list args)
  792. {
  793. const char *str = buf;
  794. char *next;
  795. char digit;
  796. int num = 0;
  797. int qualifier;
  798. int base;
  799. int field_width;
  800. int is_sign = 0;
  801. while(*fmt && *str) {
  802. /* skip any white space in format */
  803. /* white space in format matchs any amount of
  804. * white space, including none, in the input.
  805. */
  806. if (isspace(*fmt)) {
  807. while (isspace(*fmt))
  808. ++fmt;
  809. while (isspace(*str))
  810. ++str;
  811. }
  812. /* anything that is not a conversion must match exactly */
  813. if (*fmt != '%' && *fmt) {
  814. if (*fmt++ != *str++)
  815. break;
  816. continue;
  817. }
  818. if (!*fmt)
  819. break;
  820. ++fmt;
  821. /* skip this conversion.
  822. * advance both strings to next white space
  823. */
  824. if (*fmt == '*') {
  825. while (!isspace(*fmt) && *fmt)
  826. fmt++;
  827. while (!isspace(*str) && *str)
  828. str++;
  829. continue;
  830. }
  831. /* get field width */
  832. field_width = -1;
  833. if (isdigit(*fmt))
  834. field_width = skip_atoi(&fmt);
  835. /* get conversion qualifier */
  836. qualifier = -1;
  837. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  838. *fmt == 'Z' || *fmt == 'z') {
  839. qualifier = *fmt++;
  840. if (unlikely(qualifier == *fmt)) {
  841. if (qualifier == 'h') {
  842. qualifier = 'H';
  843. fmt++;
  844. } else if (qualifier == 'l') {
  845. qualifier = 'L';
  846. fmt++;
  847. }
  848. }
  849. }
  850. base = 10;
  851. is_sign = 0;
  852. if (!*fmt || !*str)
  853. break;
  854. switch(*fmt++) {
  855. case 'c':
  856. {
  857. char *s = (char *) va_arg(args,char*);
  858. if (field_width == -1)
  859. field_width = 1;
  860. do {
  861. *s++ = *str++;
  862. } while (--field_width > 0 && *str);
  863. num++;
  864. }
  865. continue;
  866. case 's':
  867. {
  868. char *s = (char *) va_arg(args, char *);
  869. if(field_width == -1)
  870. field_width = INT_MAX;
  871. /* first, skip leading white space in buffer */
  872. while (isspace(*str))
  873. str++;
  874. /* now copy until next white space */
  875. while (*str && !isspace(*str) && field_width--) {
  876. *s++ = *str++;
  877. }
  878. *s = '\0';
  879. num++;
  880. }
  881. continue;
  882. case 'n':
  883. /* return number of characters read so far */
  884. {
  885. int *i = (int *)va_arg(args,int*);
  886. *i = str - buf;
  887. }
  888. continue;
  889. case 'o':
  890. base = 8;
  891. break;
  892. case 'x':
  893. case 'X':
  894. base = 16;
  895. break;
  896. case 'i':
  897. base = 0;
  898. case 'd':
  899. is_sign = 1;
  900. case 'u':
  901. break;
  902. case '%':
  903. /* looking for '%' in str */
  904. if (*str++ != '%')
  905. return num;
  906. continue;
  907. default:
  908. /* invalid format; stop here */
  909. return num;
  910. }
  911. /* have some sort of integer conversion.
  912. * first, skip white space in buffer.
  913. */
  914. while (isspace(*str))
  915. str++;
  916. digit = *str;
  917. if (is_sign && digit == '-')
  918. digit = *(str + 1);
  919. if (!digit
  920. || (base == 16 && !isxdigit(digit))
  921. || (base == 10 && !isdigit(digit))
  922. || (base == 8 && (!isdigit(digit) || digit > '7'))
  923. || (base == 0 && !isdigit(digit)))
  924. break;
  925. switch(qualifier) {
  926. case 'H': /* that's 'hh' in format */
  927. if (is_sign) {
  928. signed char *s = (signed char *) va_arg(args,signed char *);
  929. *s = (signed char) simple_strtol(str,&next,base);
  930. } else {
  931. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  932. *s = (unsigned char) simple_strtoul(str, &next, base);
  933. }
  934. break;
  935. case 'h':
  936. if (is_sign) {
  937. short *s = (short *) va_arg(args,short *);
  938. *s = (short) simple_strtol(str,&next,base);
  939. } else {
  940. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  941. *s = (unsigned short) simple_strtoul(str, &next, base);
  942. }
  943. break;
  944. case 'l':
  945. if (is_sign) {
  946. long *l = (long *) va_arg(args,long *);
  947. *l = simple_strtol(str,&next,base);
  948. } else {
  949. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  950. *l = simple_strtoul(str,&next,base);
  951. }
  952. break;
  953. case 'L':
  954. if (is_sign) {
  955. long long *l = (long long*) va_arg(args,long long *);
  956. *l = simple_strtoll(str,&next,base);
  957. } else {
  958. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  959. *l = simple_strtoull(str,&next,base);
  960. }
  961. break;
  962. case 'Z':
  963. case 'z':
  964. {
  965. size_t *s = (size_t*) va_arg(args,size_t*);
  966. *s = (size_t) simple_strtoul(str,&next,base);
  967. }
  968. break;
  969. default:
  970. if (is_sign) {
  971. int *i = (int *) va_arg(args, int*);
  972. *i = (int) simple_strtol(str,&next,base);
  973. } else {
  974. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  975. *i = (unsigned int) simple_strtoul(str,&next,base);
  976. }
  977. break;
  978. }
  979. num++;
  980. if (!next)
  981. break;
  982. str = next;
  983. }
  984. /*
  985. * Now we've come all the way through so either the input string or the
  986. * format ended. In the former case, there can be a %n at the current
  987. * position in the format that needs to be filled.
  988. */
  989. if (*fmt == '%' && *(fmt + 1) == 'n') {
  990. int *p = (int *)va_arg(args, int *);
  991. *p = str - buf;
  992. }
  993. return num;
  994. }
  995. EXPORT_SYMBOL(vsscanf);
  996. /**
  997. * sscanf - Unformat a buffer into a list of arguments
  998. * @buf: input buffer
  999. * @fmt: formatting of buffer
  1000. * @...: resulting arguments
  1001. */
  1002. int sscanf(const char * buf, const char * fmt, ...)
  1003. {
  1004. va_list args;
  1005. int i;
  1006. va_start(args,fmt);
  1007. i = vsscanf(buf,fmt,args);
  1008. va_end(args);
  1009. return i;
  1010. }
  1011. EXPORT_SYMBOL(sscanf);