vsprintf.c 28 KB

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