vsprintf.c 27 KB

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