kservice.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. * File : kservice.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-16 Bernard the first version
  13. * 2006-05-25 Bernard rewrite vsprintf
  14. * 2006-08-10 Bernard add rt_show_version
  15. * 2010-03-17 Bernard remove rt_strlcpy function
  16. * fix gcc compiling issue.
  17. * 2010-04-15 Bernard remove weak definition on ICCM16C compiler
  18. */
  19. #include <rtthread.h>
  20. #include <rthw.h>
  21. /**
  22. * @addtogroup KernelService
  23. */
  24. /*@{*/
  25. #ifndef RT_USING_NEWLIB
  26. /* global errno in RT-Thread*/
  27. int errno;
  28. #else
  29. #include <errno.h>
  30. #endif
  31. static rt_device_t _console_device = RT_NULL;
  32. /*
  33. * This function will get errno
  34. *
  35. * @return errno
  36. */
  37. rt_err_t rt_get_errno(void)
  38. {
  39. rt_thread_t tid;
  40. tid = rt_thread_self();
  41. if (tid == RT_NULL) return errno;
  42. return tid->error;
  43. }
  44. /*
  45. * This function will set errno
  46. *
  47. * @param error the errno shall be set
  48. */
  49. void rt_set_errno(rt_err_t error)
  50. {
  51. rt_thread_t tid;
  52. tid = rt_thread_self();
  53. if (tid == RT_NULL) { errno = error; return; }
  54. tid->error = error;
  55. }
  56. /**
  57. * This function will set the content of memory to specified value
  58. *
  59. * @param s the address of source memory
  60. * @param c the value shall be set in content
  61. * @param count the copied length
  62. *
  63. * @return the address of source memory
  64. *
  65. */
  66. void *rt_memset(void * s, int c, rt_ubase_t count)
  67. {
  68. #ifdef RT_TINY_SIZE
  69. char *xs = (char *) s;
  70. while (count--)
  71. *xs++ = c;
  72. return s;
  73. #else
  74. #define LBLOCKSIZE (sizeof(rt_int32_t))
  75. #define UNALIGNED(X) ((rt_int32_t)X & (LBLOCKSIZE - 1))
  76. #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
  77. int i;
  78. char *m = (char *)s;
  79. rt_uint32_t buffer;
  80. rt_uint32_t *aligned_addr;
  81. rt_uint32_t d = c & 0xff;
  82. if (!TOO_SMALL (count) && !UNALIGNED (s))
  83. {
  84. /* If we get this far, we know that n is large and m is word-aligned. */
  85. aligned_addr = (rt_uint32_t*)s;
  86. /* Store D into each char sized location in BUFFER so that
  87. * we can set large blocks quickly.
  88. */
  89. if (LBLOCKSIZE == 4)
  90. {
  91. buffer = (d << 8) | d;
  92. buffer |= (buffer << 16);
  93. }
  94. else
  95. {
  96. buffer = 0;
  97. for (i = 0; i < LBLOCKSIZE; i++)
  98. buffer = (buffer << 8) | d;
  99. }
  100. while (count >= LBLOCKSIZE*4)
  101. {
  102. *aligned_addr++ = buffer;
  103. *aligned_addr++ = buffer;
  104. *aligned_addr++ = buffer;
  105. *aligned_addr++ = buffer;
  106. count -= 4*LBLOCKSIZE;
  107. }
  108. while (count >= LBLOCKSIZE)
  109. {
  110. *aligned_addr++ = buffer;
  111. count -= LBLOCKSIZE;
  112. }
  113. /* Pick up the remainder with a bytewise loop. */
  114. m = (char*)aligned_addr;
  115. }
  116. while (count--)
  117. {
  118. *m++ = (char)d;
  119. }
  120. return s;
  121. #undef LBLOCKSIZE
  122. #undef UNALIGNED
  123. #undef TOO_SMALL
  124. #endif
  125. }
  126. /**
  127. * This function will copy memory content from source address to destination
  128. * address.
  129. *
  130. * @param dst the address of destination memory
  131. * @param src the address of source memory
  132. * @param count the copied length
  133. *
  134. * @return the address of destination memory
  135. *
  136. */
  137. void *rt_memcpy(void * dst, const void *src, rt_ubase_t count)
  138. {
  139. #ifdef RT_TINY_SIZE
  140. char *tmp = (char *) dst, *s = (char *) src;
  141. while (count--)
  142. *tmp++ = *s++;
  143. return dst;
  144. #else
  145. #define UNALIGNED(X, Y) \
  146. (((rt_int32_t)X & (sizeof (rt_int32_t) - 1)) | ((rt_int32_t)Y & (sizeof (rt_int32_t) - 1)))
  147. #define BIGBLOCKSIZE (sizeof (rt_int32_t) << 2)
  148. #define LITTLEBLOCKSIZE (sizeof (rt_int32_t))
  149. #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
  150. char *dst_ptr = (char*)dst;
  151. char *src_ptr = (char*)src;
  152. rt_int32_t *aligned_dst;
  153. rt_int32_t *aligned_src;
  154. int len = count;
  155. /* If the size is small, or either SRC or DST is unaligned,
  156. then punt into the byte copy loop. This should be rare. */
  157. if (!TOO_SMALL(len) && !UNALIGNED (src_ptr, dst_ptr))
  158. {
  159. aligned_dst = (rt_int32_t*)dst_ptr;
  160. aligned_src = (rt_int32_t*)src_ptr;
  161. /* Copy 4X long words at a time if possible. */
  162. while (len >= BIGBLOCKSIZE)
  163. {
  164. *aligned_dst++ = *aligned_src++;
  165. *aligned_dst++ = *aligned_src++;
  166. *aligned_dst++ = *aligned_src++;
  167. *aligned_dst++ = *aligned_src++;
  168. len -= BIGBLOCKSIZE;
  169. }
  170. /* Copy one long word at a time if possible. */
  171. while (len >= LITTLEBLOCKSIZE)
  172. {
  173. *aligned_dst++ = *aligned_src++;
  174. len -= LITTLEBLOCKSIZE;
  175. }
  176. /* Pick up any residual with a byte copier. */
  177. dst_ptr = (char*)aligned_dst;
  178. src_ptr = (char*)aligned_src;
  179. }
  180. while (len--)
  181. *dst_ptr++ = *src_ptr++;
  182. return dst;
  183. #undef UNALIGNED
  184. #undef BIGBLOCKSIZE
  185. #undef LITTLEBLOCKSIZE
  186. #undef TOO_SMALL
  187. #endif
  188. }
  189. /**
  190. * This function will move memory content from source address to destination
  191. * address.
  192. *
  193. * @param dest the address of destination memory
  194. * @param src the address of source memory
  195. * @param n the copied length
  196. *
  197. * @return the address of destination memory
  198. *
  199. */
  200. void* rt_memmove(void *dest, const void *src, rt_ubase_t n)
  201. {
  202. char *tmp = (char *) dest, *s = (char *) src;
  203. if (s < tmp && tmp < s + n)
  204. {
  205. tmp+=n;
  206. s+=n;
  207. while (n--)
  208. *tmp-- = *s--;
  209. }
  210. else
  211. {
  212. while (n--)
  213. *tmp++ = *s++;
  214. }
  215. return dest;
  216. }
  217. /**
  218. * memcmp - Compare two areas of memory
  219. * @param cs: One area of memory
  220. * @param ct: Another area of memory
  221. * @param count: The size of the area.
  222. */
  223. rt_int32_t rt_memcmp(const void * cs,const void * ct, rt_ubase_t count)
  224. {
  225. const unsigned char *su1, *su2;
  226. int res = 0;
  227. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  228. if ((res = *su1 - *su2) != 0)
  229. break;
  230. return res;
  231. }
  232. /**
  233. * This function will return the first occurrence of a string.
  234. *
  235. * @param s1 the source string
  236. * @param s2 the find string
  237. *
  238. * @return the first occurrence of a s2 in s1, or RT_NULL if no found.
  239. */
  240. char * rt_strstr(const char * s1,const char * s2)
  241. {
  242. int l1, l2;
  243. l2 = rt_strlen(s2);
  244. if (!l2)
  245. return (char *) s1;
  246. l1 = rt_strlen(s1);
  247. while (l1 >= l2)
  248. {
  249. l1--;
  250. if (!rt_memcmp(s1,s2,l2))
  251. return (char *) s1;
  252. s1++;
  253. }
  254. return RT_NULL;
  255. }
  256. /**
  257. * This function will compare two strings while ignoring differences in case
  258. *
  259. * @param a the string to be compared
  260. * @param b the string to be compared
  261. *
  262. * @return the result
  263. */
  264. rt_uint32_t rt_strcasecmp(const char *a, const char *b)
  265. {
  266. int ca, cb;
  267. do
  268. {
  269. ca = *a++ & 0xff;
  270. cb = *b++ & 0xff;
  271. if (ca >= 'A' && ca <= 'Z')
  272. ca += 'a' - 'A';
  273. if (cb >= 'A' && cb <= 'Z')
  274. cb += 'a' - 'A';
  275. }
  276. while (ca == cb && ca != '\0');
  277. return ca - cb;
  278. }
  279. /**
  280. * This function will copy string no more than n bytes.
  281. *
  282. * @param dest the string to copy
  283. * @param src the string to be copied
  284. * @param n the maximum copied length
  285. *
  286. * @return the result
  287. */
  288. char *rt_strncpy(char *dest, const char *src, rt_ubase_t n)
  289. {
  290. char *tmp = (char *) dest, *s = (char *) src;
  291. while(n--)
  292. *tmp++ = *s++;
  293. return dest;
  294. }
  295. /**
  296. * This function will compare two strings with specified maximum length
  297. *
  298. * @param cs the string to be compared
  299. * @param ct the string to be compared
  300. * @param count the maximum compare length
  301. *
  302. * @return the result
  303. */
  304. rt_ubase_t rt_strncmp(const char * cs, const char * ct, rt_ubase_t count)
  305. {
  306. register signed char __res = 0;
  307. while (count)
  308. {
  309. if ((__res = *cs - *ct++) != 0 || !*cs++)
  310. break;
  311. count--;
  312. }
  313. return __res;
  314. }
  315. /**
  316. * This function will compare two strings without specified length
  317. *
  318. * @param cs the string to be compared
  319. * @param ct the string to be compared
  320. *
  321. * @return the result
  322. */
  323. rt_ubase_t rt_strcmp (const char *cs, const char *ct)
  324. {
  325. while (*cs && *cs == *ct)
  326. cs++, ct++;
  327. return (*cs - *ct);
  328. }
  329. /**
  330. * This function will return the length of a string, which terminate will
  331. * null character.
  332. *
  333. * @param s the string
  334. *
  335. * @return the length of string
  336. */
  337. rt_ubase_t rt_strlen(const char *s)
  338. {
  339. const char *sc;
  340. for (sc = s; *sc != '\0'; ++sc) /* nothing */
  341. ;
  342. return sc - s;
  343. }
  344. #ifdef RT_USING_HEAP
  345. /**
  346. * This function will duplicate a string.
  347. *
  348. * @param s the string to be duplicated
  349. *
  350. * @return the duplicated string pointer
  351. */
  352. char *rt_strdup(const char *s)
  353. {
  354. rt_size_t len = rt_strlen(s) + 1;
  355. char *tmp = (char *)rt_malloc(len);
  356. if(!tmp) return RT_NULL;
  357. rt_memcpy(tmp, s, len);
  358. return tmp;
  359. }
  360. #endif
  361. /**
  362. * This function will show the version of rt-thread rtos
  363. */
  364. void rt_show_version()
  365. {
  366. rt_kprintf("\n \\ | /\n");
  367. rt_kprintf("- RT - Thread Operating System\n");
  368. rt_kprintf(" / | \\ 0.%d.%d build %s\n", RT_VERSION, RT_SUBVERSION, __DATE__);
  369. rt_kprintf(" 2006 - 2009 Copyright by rt-thread team\n");
  370. }
  371. /* private function */
  372. #define isdigit(c) ((unsigned)((c) - '0') < 10)
  373. rt_inline rt_int32_t divide(rt_int32_t *n, rt_int32_t base)
  374. {
  375. rt_int32_t res;
  376. /* optimized for processor which does not support divide instructions. */
  377. if (base == 10)
  378. {
  379. res = ((rt_uint32_t)*n) % 10U;
  380. *n = ((rt_uint32_t)*n) / 10U;
  381. }
  382. else
  383. {
  384. res = ((rt_uint32_t)*n) % 16U;
  385. *n = ((rt_uint32_t)*n) / 16U;
  386. }
  387. return res;
  388. }
  389. rt_inline int skip_atoi(const char **s)
  390. {
  391. register int i=0;
  392. while (isdigit(**s)) i = i*10 + *((*s)++) - '0';
  393. return i;
  394. }
  395. #define ZEROPAD (1 << 0) /* pad with zero */
  396. #define SIGN (1 << 1) /* unsigned/signed long */
  397. #define PLUS (1 << 2) /* show plus */
  398. #define SPACE (1 << 3) /* space if plus */
  399. #define LEFT (1 << 4) /* left justified */
  400. #define SPECIAL (1 << 5) /* 0x */
  401. #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
  402. #ifdef RT_PRINTF_PRECISION
  403. static char *print_number(char * buf, char * end, long num, int base, int s, int precision, int type)
  404. #else
  405. static char *print_number(char * buf, char * end, long num, int base, int s, int type)
  406. #endif
  407. {
  408. char c, sign;
  409. #ifdef RT_PRINTF_LONGLONG
  410. char tmp[32];
  411. #else
  412. char tmp[16];
  413. #endif
  414. const char *digits;
  415. static const char small_digits[] = "0123456789abcdef";
  416. static const char large_digits[] = "0123456789ABCDEF";
  417. register int i;
  418. register int size;
  419. size = s;
  420. digits = (type & LARGE) ? large_digits : small_digits;
  421. if (type & LEFT) type &= ~ZEROPAD;
  422. c = (type & ZEROPAD) ? '0' : ' ';
  423. /* get sign */
  424. sign = 0;
  425. if (type & SIGN)
  426. {
  427. if (num < 0)
  428. {
  429. sign = '-';
  430. num = -num;
  431. }
  432. else if (type & PLUS) sign = '+';
  433. else if (type & SPACE) sign = ' ';
  434. }
  435. #ifdef RT_PRINTF_SPECIAL
  436. if (type & SPECIAL)
  437. {
  438. if (base == 16) size -= 2;
  439. else if (base == 8) size--;
  440. }
  441. #endif
  442. i = 0;
  443. if (num == 0) tmp[i++]='0';
  444. else
  445. {
  446. while (num != 0) tmp[i++] = digits[divide(&num, base)];
  447. }
  448. #ifdef RT_PRINTF_PRECISION
  449. if (i > precision) precision = i;
  450. size -= precision;
  451. #else
  452. size -= i;
  453. #endif
  454. if (!(type&(ZEROPAD | LEFT)))
  455. {
  456. while(size-->0)
  457. {
  458. if (buf <= end) *buf = ' ';
  459. ++buf;
  460. }
  461. }
  462. if (sign)
  463. {
  464. if (buf <= end)
  465. {
  466. *buf = sign;
  467. --size;
  468. }
  469. ++buf;
  470. }
  471. #ifdef RT_PRINTF_SPECIAL
  472. if (type & SPECIAL)
  473. {
  474. if (base==8)
  475. {
  476. if (buf <= end) *buf = '0';
  477. ++buf;
  478. }
  479. else if (base==16)
  480. {
  481. if (buf <= end) *buf = '0';
  482. ++buf;
  483. if (buf <= end)
  484. {
  485. *buf = type & LARGE? 'X' : 'x';
  486. }
  487. ++buf;
  488. }
  489. }
  490. #endif
  491. /* no align to the left */
  492. if (!(type & LEFT))
  493. {
  494. while (size-- > 0)
  495. {
  496. if (buf <= end) *buf = c;
  497. ++buf;
  498. }
  499. }
  500. #ifdef RT_PRINTF_PRECISION
  501. while (i < precision--)
  502. {
  503. if (buf <= end) *buf = '0';
  504. ++buf;
  505. }
  506. #endif
  507. /* put number in the temporary buffer */
  508. while (i-- > 0)
  509. {
  510. if (buf <= end) *buf = tmp[i];
  511. ++buf;
  512. }
  513. while (size-- > 0)
  514. {
  515. if (buf <= end) *buf = ' ';
  516. ++buf;
  517. }
  518. return buf;
  519. }
  520. static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args)
  521. {
  522. #ifdef RT_PRINTF_LONGLONG
  523. unsigned long long num;
  524. #else
  525. rt_uint32_t num;
  526. #endif
  527. int i, len;
  528. char *str, *end, c;
  529. const char *s;
  530. rt_uint8_t base; /* the base of number */
  531. rt_uint8_t flags; /* flags to print number */
  532. rt_uint8_t qualifier; /* 'h', 'l', or 'L' for integer fields */
  533. rt_int32_t field_width; /* width of output field */
  534. #ifdef RT_PRINTF_PRECISION
  535. int precision; /* min. # of digits for integers and max for a string */
  536. #endif
  537. str = buf;
  538. end = buf + size - 1;
  539. /* Make sure end is always >= buf */
  540. if (end < buf)
  541. {
  542. end = ((char *)-1);
  543. size = end - buf;
  544. }
  545. for (; *fmt ; ++fmt)
  546. {
  547. if (*fmt != '%')
  548. {
  549. if (str <= end) *str = *fmt;
  550. ++str;
  551. continue;
  552. }
  553. /* process flags */
  554. flags = 0;
  555. while(1)
  556. {
  557. /* skips the first '%' also */
  558. ++fmt;
  559. if (*fmt == '-') flags |= LEFT;
  560. else if (*fmt == '+') flags |= PLUS;
  561. else if (*fmt == ' ') flags |= SPACE;
  562. else if (*fmt == '#') flags |= SPECIAL;
  563. else if (*fmt == '0') flags |= ZEROPAD;
  564. else break;
  565. }
  566. /* get field width */
  567. field_width = -1;
  568. if (isdigit(*fmt)) field_width = skip_atoi(&fmt);
  569. else if (*fmt == '*')
  570. {
  571. ++fmt;
  572. /* it's the next argument */
  573. field_width = va_arg(args, int);
  574. if (field_width < 0)
  575. {
  576. field_width = -field_width;
  577. flags |= LEFT;
  578. }
  579. }
  580. #ifdef RT_PRINTF_PRECISION
  581. /* get the precision */
  582. precision = -1;
  583. if (*fmt == '.')
  584. {
  585. ++fmt;
  586. if (isdigit(*fmt)) precision = skip_atoi(&fmt);
  587. else if (*fmt == '*')
  588. {
  589. ++fmt;
  590. /* it's the next argument */
  591. precision = va_arg(args, int);
  592. }
  593. if (precision < 0) precision = 0;
  594. }
  595. #endif
  596. /* get the conversion qualifier */
  597. qualifier = 0;
  598. if (*fmt == 'h' || *fmt == 'l'
  599. #ifdef RT_PRINTF_LONGLONG
  600. || *fmt == 'L'
  601. #endif
  602. )
  603. {
  604. qualifier = *fmt;
  605. ++fmt;
  606. #ifdef RT_PRINTF_LONGLONG
  607. if (qualifier == 'l' && *fmt == 'l')
  608. {
  609. qualifier = 'L';
  610. ++fmt;
  611. }
  612. #endif
  613. }
  614. /* the default base */
  615. base = 10;
  616. switch (*fmt)
  617. {
  618. case 'c':
  619. if (!(flags & LEFT))
  620. {
  621. while (--field_width > 0)
  622. {
  623. if (str <= end) *str = ' ';
  624. ++str;
  625. }
  626. }
  627. /* get character */
  628. c = (rt_uint8_t) va_arg(args, int);
  629. if (str <= end) *str = c;
  630. ++str;
  631. /* put width */
  632. while (--field_width > 0)
  633. {
  634. if (str <= end) *str = ' ';
  635. ++str;
  636. }
  637. continue;
  638. case 's':
  639. s = va_arg(args, char *);
  640. if (!s) s = "(NULL)";
  641. len = rt_strlen(s);
  642. #ifdef RT_PRINTF_PRECISION
  643. if (precision > 0 && len > precision) len = precision;
  644. #endif
  645. if (!(flags & LEFT))
  646. {
  647. while (len < field_width--)
  648. {
  649. if (str <= end) *str = ' ';
  650. ++str;
  651. }
  652. }
  653. for (i = 0; i < len; ++i)
  654. {
  655. if (str <= end) *str = *s;
  656. ++str;
  657. ++s;
  658. }
  659. while (len < field_width--)
  660. {
  661. if (str <= end) *str = ' ';
  662. ++str;
  663. }
  664. continue;
  665. case 'p':
  666. if (field_width == -1)
  667. {
  668. field_width = sizeof(void *) << 1;
  669. flags |= ZEROPAD;
  670. }
  671. #ifdef RT_PRINTF_PRECISION
  672. str = print_number(str, end,
  673. (long) va_arg(args, void *),
  674. 16, field_width, precision, flags);
  675. #else
  676. str = print_number(str, end,
  677. (long) va_arg(args, void *),
  678. 16, field_width, flags);
  679. #endif
  680. continue;
  681. case '%':
  682. if (str <= end) *str = '%';
  683. ++str;
  684. continue;
  685. /* integer number formats - set up the flags and "break" */
  686. case 'o':
  687. base = 8;
  688. break;
  689. case 'X':
  690. flags |= LARGE;
  691. case 'x':
  692. base = 16;
  693. break;
  694. case 'd':
  695. case 'i':
  696. flags |= SIGN;
  697. case 'u':
  698. break;
  699. default:
  700. if (str <= end) *str = '%';
  701. ++str;
  702. if (*fmt)
  703. {
  704. if (str <= end) *str = *fmt;
  705. ++str;
  706. }
  707. else
  708. {
  709. --fmt;
  710. }
  711. continue;
  712. }
  713. #ifdef RT_PRINTF_LONGLONG
  714. if (qualifier == 'L') num = va_arg(args, long long);
  715. else if (qualifier == 'l')
  716. #else
  717. if (qualifier == 'l')
  718. #endif
  719. {
  720. num = va_arg(args, rt_uint32_t);
  721. if (flags & SIGN) num = (rt_int32_t) num;
  722. }
  723. else if (qualifier == 'h')
  724. {
  725. num = (rt_uint16_t) va_arg(args, rt_int32_t);
  726. if (flags & SIGN) num = (rt_int16_t) num;
  727. }
  728. else
  729. {
  730. num = va_arg(args, rt_uint32_t);
  731. if (flags & SIGN) num = (rt_int32_t) num;
  732. }
  733. #ifdef RT_PRINTF_PRECISION
  734. str = print_number(str, end, num, base, field_width, precision, flags);
  735. #else
  736. str = print_number(str, end, num, base, field_width, flags);
  737. #endif
  738. }
  739. if (str <= end) *str = '\0';
  740. else *end = '\0';
  741. /* the trailing null byte doesn't count towards the total
  742. * ++str;
  743. */
  744. return str-buf;
  745. }
  746. /**
  747. * This function will fill a formatted string to buffer
  748. *
  749. * @param buf the buffer to save formatted string
  750. * @param size the size of buffer
  751. * @param fmt the format
  752. */
  753. rt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
  754. {
  755. rt_int32_t n;
  756. va_list args;
  757. va_start(args, fmt);
  758. n = vsnprintf(buf, size, fmt, args);
  759. va_end(args);
  760. return n;
  761. }
  762. /**
  763. * This function will fill a formatted string to buffer
  764. *
  765. * @param buf the buffer to save formatted string
  766. * @param arg_ptr the arg_ptr
  767. * @param format the format
  768. */
  769. rt_int32_t rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
  770. {
  771. return vsnprintf(buf, (rt_size_t) -1, format, arg_ptr);
  772. }
  773. /**
  774. * This function will fill a formatted string to buffer
  775. *
  776. * @param buf the buffer to save formatted string
  777. * @param format the format
  778. */
  779. rt_int32_t rt_sprintf(char *buf ,const char *format,...)
  780. {
  781. rt_int32_t n;
  782. va_list arg_ptr;
  783. va_start(arg_ptr, format);
  784. n = rt_vsprintf(buf ,format,arg_ptr);
  785. va_end (arg_ptr);
  786. return n;
  787. }
  788. /**
  789. * This function will set a device as console device.
  790. * After set a device to console, all output of rt_kprintf will be
  791. * redirected to this new device.
  792. *
  793. * @param name the name of new console device
  794. *
  795. * @return the old console device handler
  796. */
  797. rt_device_t rt_console_set_device(const char* name)
  798. {
  799. rt_device_t new, old;
  800. /* save old device */
  801. old = _console_device;
  802. /* find new console device */
  803. new = rt_device_find(name);
  804. if (new != RT_NULL)
  805. {
  806. if (_console_device != RT_NULL)
  807. {
  808. /* close old console device */
  809. rt_device_close(_console_device);
  810. }
  811. /* set new console device */
  812. _console_device = new;
  813. rt_device_open(_console_device, RT_DEVICE_OFLAG_RDWR);
  814. }
  815. return old;
  816. }
  817. #if defined(__GNUC__)
  818. void rt_hw_console_output(const char* str) __attribute__((weak));
  819. void rt_hw_console_output(const char* str)
  820. #elif defined(__CC_ARM)
  821. __weak void rt_hw_console_output(const char* str)
  822. #elif defined(__IAR_SYSTEMS_ICC__)
  823. __weak void rt_hw_console_output(const char* str)
  824. #endif
  825. {
  826. /* empty console output */
  827. }
  828. /**
  829. * This function will print a formatted string on system console
  830. *
  831. * @param fmt the format
  832. */
  833. void rt_kprintf(const char *fmt, ...)
  834. {
  835. va_list args;
  836. rt_size_t length;
  837. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  838. va_start(args, fmt);
  839. length = vsnprintf(rt_log_buf, sizeof(rt_log_buf), fmt, args);
  840. if (_console_device == RT_NULL)
  841. {
  842. rt_hw_console_output(rt_log_buf);
  843. }
  844. else
  845. {
  846. rt_device_write(_console_device, 0, rt_log_buf, length);
  847. }
  848. va_end(args);
  849. }
  850. #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__)
  851. #include <sys/types.h>
  852. void* memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memcpy")));
  853. void* memset(void *s, int c, size_t n) __attribute__((weak, alias("rt_memset")));
  854. void* memmove(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memmove")));
  855. int memcmp(const void *s1, const void *s2, size_t n) __attribute__((weak, alias("rt_memcmp")));
  856. size_t strlen(const char *s) __attribute__((weak, alias("rt_strlen")));
  857. char *strstr(const char *s1,const char *s2) __attribute__((weak, alias("rt_strstr")));
  858. int strcasecmp(const char *a, const char *b) __attribute__((weak, alias("rt_strcasecmp")));
  859. char *strncpy(char *dest, const char *src, size_t n) __attribute__((weak, alias("rt_strncpy")));
  860. int strncmp(const char *cs, const char *ct, size_t count) __attribute__((weak, alias("rt_strncmp")));
  861. #ifdef RT_USING_HEAP
  862. char *strdup(const char *s) __attribute__((weak, alias("rt_strdup")));
  863. #endif
  864. #endif
  865. /*@}*/