vsprintf.c 26 KB

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