kservice.c 19 KB

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