vsprintf.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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 <linux/kallsyms.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/page.h> /* for PAGE_SIZE */
  25. #include <asm/div64.h>
  26. #include <asm/sections.h> /* for dereference_function_descriptor() */
  27. /* Works only for digits and letters, but small and fast */
  28. #define TOLOWER(x) ((x) | 0x20)
  29. static unsigned int simple_guess_base(const char *cp)
  30. {
  31. if (cp[0] == '0') {
  32. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  33. return 16;
  34. else
  35. return 8;
  36. } else {
  37. return 10;
  38. }
  39. }
  40. /**
  41. * simple_strtoul - convert a string to an unsigned long
  42. * @cp: The start of the string
  43. * @endp: A pointer to the end of the parsed string will be placed here
  44. * @base: The number base to use
  45. */
  46. unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
  47. {
  48. unsigned long result = 0;
  49. if (!base)
  50. base = simple_guess_base(cp);
  51. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  52. cp += 2;
  53. while (isxdigit(*cp)) {
  54. unsigned int value;
  55. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  56. if (value >= base)
  57. break;
  58. result = result * base + value;
  59. cp++;
  60. }
  61. if (endp)
  62. *endp = (char *)cp;
  63. return result;
  64. }
  65. EXPORT_SYMBOL(simple_strtoul);
  66. /**
  67. * simple_strtol - convert a string to a signed long
  68. * @cp: The start of the string
  69. * @endp: A pointer to the end of the parsed string will be placed here
  70. * @base: The number base to use
  71. */
  72. long simple_strtol(const char *cp,char **endp,unsigned int base)
  73. {
  74. if(*cp=='-')
  75. return -simple_strtoul(cp+1,endp,base);
  76. return simple_strtoul(cp,endp,base);
  77. }
  78. EXPORT_SYMBOL(simple_strtol);
  79. /**
  80. * simple_strtoull - convert a string to an unsigned long long
  81. * @cp: The start of the string
  82. * @endp: A pointer to the end of the parsed string will be placed here
  83. * @base: The number base to use
  84. */
  85. unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
  86. {
  87. unsigned long long result = 0;
  88. if (!base)
  89. base = simple_guess_base(cp);
  90. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  91. cp += 2;
  92. while (isxdigit(*cp)) {
  93. unsigned int value;
  94. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  95. if (value >= base)
  96. break;
  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_strtou##type(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. static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
  470. {
  471. unsigned long value = (unsigned long) ptr;
  472. #ifdef CONFIG_KALLSYMS
  473. char sym[KSYM_SYMBOL_LEN];
  474. sprint_symbol(sym, value);
  475. return string(buf, end, sym, field_width, precision, flags);
  476. #else
  477. field_width = 2*sizeof(void *);
  478. flags |= SPECIAL | SMALL | ZEROPAD;
  479. return number(buf, end, value, 16, field_width, precision, flags);
  480. #endif
  481. }
  482. /*
  483. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  484. * by an extra set of alphanumeric characters that are extended format
  485. * specifiers.
  486. *
  487. * Right now we just handle 'F' (for symbolic Function descriptor pointers)
  488. * and 'S' (for Symbolic direct pointers), but this can easily be
  489. * extended in the future (network address types etc).
  490. *
  491. * The difference between 'S' and 'F' is that on ia64 and ppc64 function
  492. * pointers are really function descriptors, which contain a pointer the
  493. * real address.
  494. */
  495. static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
  496. {
  497. switch (*fmt) {
  498. case 'F':
  499. ptr = dereference_function_descriptor(ptr);
  500. /* Fallthrough */
  501. case 'S':
  502. return symbol_string(buf, end, ptr, field_width, precision, flags);
  503. }
  504. flags |= SMALL;
  505. if (field_width == -1) {
  506. field_width = 2*sizeof(void *);
  507. flags |= ZEROPAD;
  508. }
  509. return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
  510. }
  511. /**
  512. * vsnprintf - Format a string and place it in a buffer
  513. * @buf: The buffer to place the result into
  514. * @size: The size of the buffer, including the trailing null space
  515. * @fmt: The format string to use
  516. * @args: Arguments for the format string
  517. *
  518. * This function follows C99 vsnprintf, but has some extensions:
  519. * %pS output the name of a text symbol
  520. * %pF output the name of a function pointer
  521. *
  522. * The return value is the number of characters which would
  523. * be generated for the given input, excluding the trailing
  524. * '\0', as per ISO C99. If you want to have the exact
  525. * number of characters written into @buf as return value
  526. * (not including the trailing '\0'), use vscnprintf(). If the
  527. * return is greater than or equal to @size, the resulting
  528. * string is truncated.
  529. *
  530. * Call this function if you are already dealing with a va_list.
  531. * You probably want snprintf() instead.
  532. */
  533. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  534. {
  535. unsigned long long num;
  536. int base;
  537. char *str, *end, c;
  538. int flags; /* flags to number() */
  539. int field_width; /* width of output field */
  540. int precision; /* min. # of digits for integers; max
  541. number of chars for from string */
  542. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  543. /* 'z' support added 23/7/1999 S.H. */
  544. /* 'z' changed to 'Z' --davidm 1/25/99 */
  545. /* 't' added for ptrdiff_t */
  546. /* Reject out-of-range values early. Large positive sizes are
  547. used for unknown buffer sizes. */
  548. if (unlikely((int) size < 0)) {
  549. /* There can be only one.. */
  550. static char warn = 1;
  551. WARN_ON(warn);
  552. warn = 0;
  553. return 0;
  554. }
  555. str = buf;
  556. end = buf + size;
  557. /* Make sure end is always >= buf */
  558. if (end < buf) {
  559. end = ((void *)-1);
  560. size = end - buf;
  561. }
  562. for (; *fmt ; ++fmt) {
  563. if (*fmt != '%') {
  564. if (str < end)
  565. *str = *fmt;
  566. ++str;
  567. continue;
  568. }
  569. /* process flags */
  570. flags = 0;
  571. repeat:
  572. ++fmt; /* this also skips first '%' */
  573. switch (*fmt) {
  574. case '-': flags |= LEFT; goto repeat;
  575. case '+': flags |= PLUS; goto repeat;
  576. case ' ': flags |= SPACE; goto repeat;
  577. case '#': flags |= SPECIAL; goto repeat;
  578. case '0': flags |= ZEROPAD; goto repeat;
  579. }
  580. /* get field width */
  581. field_width = -1;
  582. if (isdigit(*fmt))
  583. field_width = skip_atoi(&fmt);
  584. else if (*fmt == '*') {
  585. ++fmt;
  586. /* it's the next argument */
  587. field_width = va_arg(args, int);
  588. if (field_width < 0) {
  589. field_width = -field_width;
  590. flags |= LEFT;
  591. }
  592. }
  593. /* get the precision */
  594. precision = -1;
  595. if (*fmt == '.') {
  596. ++fmt;
  597. if (isdigit(*fmt))
  598. precision = skip_atoi(&fmt);
  599. else if (*fmt == '*') {
  600. ++fmt;
  601. /* it's the next argument */
  602. precision = va_arg(args, int);
  603. }
  604. if (precision < 0)
  605. precision = 0;
  606. }
  607. /* get the conversion qualifier */
  608. qualifier = -1;
  609. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  610. *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
  611. qualifier = *fmt;
  612. ++fmt;
  613. if (qualifier == 'l' && *fmt == 'l') {
  614. qualifier = 'L';
  615. ++fmt;
  616. }
  617. }
  618. /* default base */
  619. base = 10;
  620. switch (*fmt) {
  621. case 'c':
  622. if (!(flags & LEFT)) {
  623. while (--field_width > 0) {
  624. if (str < end)
  625. *str = ' ';
  626. ++str;
  627. }
  628. }
  629. c = (unsigned char) va_arg(args, int);
  630. if (str < end)
  631. *str = c;
  632. ++str;
  633. while (--field_width > 0) {
  634. if (str < end)
  635. *str = ' ';
  636. ++str;
  637. }
  638. continue;
  639. case 's':
  640. str = string(str, end, va_arg(args, char *), field_width, precision, flags);
  641. continue;
  642. case 'p':
  643. str = pointer(fmt+1, str, end,
  644. va_arg(args, void *),
  645. field_width, precision, flags);
  646. /* Skip all alphanumeric pointer suffixes */
  647. while (isalnum(fmt[1]))
  648. fmt++;
  649. continue;
  650. case 'n':
  651. /* FIXME:
  652. * What does C99 say about the overflow case here? */
  653. if (qualifier == 'l') {
  654. long * ip = va_arg(args, long *);
  655. *ip = (str - buf);
  656. } else if (qualifier == 'Z' || qualifier == 'z') {
  657. size_t * ip = va_arg(args, size_t *);
  658. *ip = (str - buf);
  659. } else {
  660. int * ip = va_arg(args, int *);
  661. *ip = (str - buf);
  662. }
  663. continue;
  664. case '%':
  665. if (str < end)
  666. *str = '%';
  667. ++str;
  668. continue;
  669. /* integer number formats - set up the flags and "break" */
  670. case 'o':
  671. base = 8;
  672. break;
  673. case 'x':
  674. flags |= SMALL;
  675. case 'X':
  676. base = 16;
  677. break;
  678. case 'd':
  679. case 'i':
  680. flags |= SIGN;
  681. case 'u':
  682. break;
  683. default:
  684. if (str < end)
  685. *str = '%';
  686. ++str;
  687. if (*fmt) {
  688. if (str < end)
  689. *str = *fmt;
  690. ++str;
  691. } else {
  692. --fmt;
  693. }
  694. continue;
  695. }
  696. if (qualifier == 'L')
  697. num = va_arg(args, long long);
  698. else if (qualifier == 'l') {
  699. num = va_arg(args, unsigned long);
  700. if (flags & SIGN)
  701. num = (signed long) num;
  702. } else if (qualifier == 'Z' || qualifier == 'z') {
  703. num = va_arg(args, size_t);
  704. } else if (qualifier == 't') {
  705. num = va_arg(args, ptrdiff_t);
  706. } else if (qualifier == 'h') {
  707. num = (unsigned short) va_arg(args, int);
  708. if (flags & SIGN)
  709. num = (signed short) num;
  710. } else {
  711. num = va_arg(args, unsigned int);
  712. if (flags & SIGN)
  713. num = (signed int) num;
  714. }
  715. str = number(str, end, num, base,
  716. field_width, precision, flags);
  717. }
  718. if (size > 0) {
  719. if (str < end)
  720. *str = '\0';
  721. else
  722. end[-1] = '\0';
  723. }
  724. /* the trailing null byte doesn't count towards the total */
  725. return str-buf;
  726. }
  727. EXPORT_SYMBOL(vsnprintf);
  728. /**
  729. * vscnprintf - Format a string and place it in a buffer
  730. * @buf: The buffer to place the result into
  731. * @size: The size of the buffer, including the trailing null space
  732. * @fmt: The format string to use
  733. * @args: Arguments for the format string
  734. *
  735. * The return value is the number of characters which have been written into
  736. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  737. * returns 0.
  738. *
  739. * Call this function if you are already dealing with a va_list.
  740. * You probably want scnprintf() instead.
  741. *
  742. * See the vsnprintf() documentation for format string extensions over C99.
  743. */
  744. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  745. {
  746. int i;
  747. i=vsnprintf(buf,size,fmt,args);
  748. return (i >= size) ? (size - 1) : i;
  749. }
  750. EXPORT_SYMBOL(vscnprintf);
  751. /**
  752. * snprintf - Format a string and place it in a buffer
  753. * @buf: The buffer to place the result into
  754. * @size: The size of the buffer, including the trailing null space
  755. * @fmt: The format string to use
  756. * @...: Arguments for the format string
  757. *
  758. * The return value is the number of characters which would be
  759. * generated for the given input, excluding the trailing null,
  760. * as per ISO C99. If the return is greater than or equal to
  761. * @size, the resulting string is truncated.
  762. *
  763. * See the vsnprintf() documentation for format string extensions over C99.
  764. */
  765. int snprintf(char * buf, size_t size, const char *fmt, ...)
  766. {
  767. va_list args;
  768. int i;
  769. va_start(args, fmt);
  770. i=vsnprintf(buf,size,fmt,args);
  771. va_end(args);
  772. return i;
  773. }
  774. EXPORT_SYMBOL(snprintf);
  775. /**
  776. * scnprintf - Format a string and place it in a buffer
  777. * @buf: The buffer to place the result into
  778. * @size: The size of the buffer, including the trailing null space
  779. * @fmt: The format string to use
  780. * @...: Arguments for the format string
  781. *
  782. * The return value is the number of characters written into @buf not including
  783. * the trailing '\0'. If @size is <= 0 the function returns 0.
  784. */
  785. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  786. {
  787. va_list args;
  788. int i;
  789. va_start(args, fmt);
  790. i = vsnprintf(buf, size, fmt, args);
  791. va_end(args);
  792. return (i >= size) ? (size - 1) : i;
  793. }
  794. EXPORT_SYMBOL(scnprintf);
  795. /**
  796. * vsprintf - Format a string and place it in a buffer
  797. * @buf: The buffer to place the result into
  798. * @fmt: The format string to use
  799. * @args: Arguments for the format string
  800. *
  801. * The function returns the number of characters written
  802. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  803. * buffer overflows.
  804. *
  805. * Call this function if you are already dealing with a va_list.
  806. * You probably want sprintf() instead.
  807. *
  808. * See the vsnprintf() documentation for format string extensions over C99.
  809. */
  810. int vsprintf(char *buf, const char *fmt, va_list args)
  811. {
  812. return vsnprintf(buf, INT_MAX, fmt, args);
  813. }
  814. EXPORT_SYMBOL(vsprintf);
  815. /**
  816. * sprintf - Format a string and place it in a buffer
  817. * @buf: The buffer to place the result into
  818. * @fmt: The format string to use
  819. * @...: Arguments for the format string
  820. *
  821. * The function returns the number of characters written
  822. * into @buf. Use snprintf() or scnprintf() in order to avoid
  823. * buffer overflows.
  824. *
  825. * See the vsnprintf() documentation for format string extensions over C99.
  826. */
  827. int sprintf(char * buf, const char *fmt, ...)
  828. {
  829. va_list args;
  830. int i;
  831. va_start(args, fmt);
  832. i=vsnprintf(buf, INT_MAX, fmt, args);
  833. va_end(args);
  834. return i;
  835. }
  836. EXPORT_SYMBOL(sprintf);
  837. /**
  838. * vsscanf - Unformat a buffer into a list of arguments
  839. * @buf: input buffer
  840. * @fmt: format of buffer
  841. * @args: arguments
  842. */
  843. int vsscanf(const char * buf, const char * fmt, va_list args)
  844. {
  845. const char *str = buf;
  846. char *next;
  847. char digit;
  848. int num = 0;
  849. int qualifier;
  850. int base;
  851. int field_width;
  852. int is_sign = 0;
  853. while(*fmt && *str) {
  854. /* skip any white space in format */
  855. /* white space in format matchs any amount of
  856. * white space, including none, in the input.
  857. */
  858. if (isspace(*fmt)) {
  859. while (isspace(*fmt))
  860. ++fmt;
  861. while (isspace(*str))
  862. ++str;
  863. }
  864. /* anything that is not a conversion must match exactly */
  865. if (*fmt != '%' && *fmt) {
  866. if (*fmt++ != *str++)
  867. break;
  868. continue;
  869. }
  870. if (!*fmt)
  871. break;
  872. ++fmt;
  873. /* skip this conversion.
  874. * advance both strings to next white space
  875. */
  876. if (*fmt == '*') {
  877. while (!isspace(*fmt) && *fmt)
  878. fmt++;
  879. while (!isspace(*str) && *str)
  880. str++;
  881. continue;
  882. }
  883. /* get field width */
  884. field_width = -1;
  885. if (isdigit(*fmt))
  886. field_width = skip_atoi(&fmt);
  887. /* get conversion qualifier */
  888. qualifier = -1;
  889. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  890. *fmt == 'Z' || *fmt == 'z') {
  891. qualifier = *fmt++;
  892. if (unlikely(qualifier == *fmt)) {
  893. if (qualifier == 'h') {
  894. qualifier = 'H';
  895. fmt++;
  896. } else if (qualifier == 'l') {
  897. qualifier = 'L';
  898. fmt++;
  899. }
  900. }
  901. }
  902. base = 10;
  903. is_sign = 0;
  904. if (!*fmt || !*str)
  905. break;
  906. switch(*fmt++) {
  907. case 'c':
  908. {
  909. char *s = (char *) va_arg(args,char*);
  910. if (field_width == -1)
  911. field_width = 1;
  912. do {
  913. *s++ = *str++;
  914. } while (--field_width > 0 && *str);
  915. num++;
  916. }
  917. continue;
  918. case 's':
  919. {
  920. char *s = (char *) va_arg(args, char *);
  921. if(field_width == -1)
  922. field_width = INT_MAX;
  923. /* first, skip leading white space in buffer */
  924. while (isspace(*str))
  925. str++;
  926. /* now copy until next white space */
  927. while (*str && !isspace(*str) && field_width--) {
  928. *s++ = *str++;
  929. }
  930. *s = '\0';
  931. num++;
  932. }
  933. continue;
  934. case 'n':
  935. /* return number of characters read so far */
  936. {
  937. int *i = (int *)va_arg(args,int*);
  938. *i = str - buf;
  939. }
  940. continue;
  941. case 'o':
  942. base = 8;
  943. break;
  944. case 'x':
  945. case 'X':
  946. base = 16;
  947. break;
  948. case 'i':
  949. base = 0;
  950. case 'd':
  951. is_sign = 1;
  952. case 'u':
  953. break;
  954. case '%':
  955. /* looking for '%' in str */
  956. if (*str++ != '%')
  957. return num;
  958. continue;
  959. default:
  960. /* invalid format; stop here */
  961. return num;
  962. }
  963. /* have some sort of integer conversion.
  964. * first, skip white space in buffer.
  965. */
  966. while (isspace(*str))
  967. str++;
  968. digit = *str;
  969. if (is_sign && digit == '-')
  970. digit = *(str + 1);
  971. if (!digit
  972. || (base == 16 && !isxdigit(digit))
  973. || (base == 10 && !isdigit(digit))
  974. || (base == 8 && (!isdigit(digit) || digit > '7'))
  975. || (base == 0 && !isdigit(digit)))
  976. break;
  977. switch(qualifier) {
  978. case 'H': /* that's 'hh' in format */
  979. if (is_sign) {
  980. signed char *s = (signed char *) va_arg(args,signed char *);
  981. *s = (signed char) simple_strtol(str,&next,base);
  982. } else {
  983. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  984. *s = (unsigned char) simple_strtoul(str, &next, base);
  985. }
  986. break;
  987. case 'h':
  988. if (is_sign) {
  989. short *s = (short *) va_arg(args,short *);
  990. *s = (short) simple_strtol(str,&next,base);
  991. } else {
  992. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  993. *s = (unsigned short) simple_strtoul(str, &next, base);
  994. }
  995. break;
  996. case 'l':
  997. if (is_sign) {
  998. long *l = (long *) va_arg(args,long *);
  999. *l = simple_strtol(str,&next,base);
  1000. } else {
  1001. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  1002. *l = simple_strtoul(str,&next,base);
  1003. }
  1004. break;
  1005. case 'L':
  1006. if (is_sign) {
  1007. long long *l = (long long*) va_arg(args,long long *);
  1008. *l = simple_strtoll(str,&next,base);
  1009. } else {
  1010. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  1011. *l = simple_strtoull(str,&next,base);
  1012. }
  1013. break;
  1014. case 'Z':
  1015. case 'z':
  1016. {
  1017. size_t *s = (size_t*) va_arg(args,size_t*);
  1018. *s = (size_t) simple_strtoul(str,&next,base);
  1019. }
  1020. break;
  1021. default:
  1022. if (is_sign) {
  1023. int *i = (int *) va_arg(args, int*);
  1024. *i = (int) simple_strtol(str,&next,base);
  1025. } else {
  1026. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  1027. *i = (unsigned int) simple_strtoul(str,&next,base);
  1028. }
  1029. break;
  1030. }
  1031. num++;
  1032. if (!next)
  1033. break;
  1034. str = next;
  1035. }
  1036. /*
  1037. * Now we've come all the way through so either the input string or the
  1038. * format ended. In the former case, there can be a %n at the current
  1039. * position in the format that needs to be filled.
  1040. */
  1041. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1042. int *p = (int *)va_arg(args, int *);
  1043. *p = str - buf;
  1044. }
  1045. return num;
  1046. }
  1047. EXPORT_SYMBOL(vsscanf);
  1048. /**
  1049. * sscanf - Unformat a buffer into a list of arguments
  1050. * @buf: input buffer
  1051. * @fmt: formatting of buffer
  1052. * @...: resulting arguments
  1053. */
  1054. int sscanf(const char * buf, const char * fmt, ...)
  1055. {
  1056. va_list args;
  1057. int i;
  1058. va_start(args,fmt);
  1059. i = vsscanf(buf,fmt,args);
  1060. va_end(args);
  1061. return i;
  1062. }
  1063. EXPORT_SYMBOL(sscanf);