cstdlib.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-15 Meco Man first version
  9. */
  10. #include <rtthread.h>
  11. #define DBG_TAG "stdlib"
  12. #define DBG_LVL DBG_INFO
  13. #include <rtdbg.h>
  14. void __rt_libc_exit(int status)
  15. {
  16. rt_thread_t self = rt_thread_self();
  17. if (self != RT_NULL)
  18. {
  19. #ifdef RT_USING_PTHREADS
  20. extern void pthread_exit(void *value);
  21. pthread_exit((void *)status);
  22. #else
  23. LOG_E("thread:%s exit:%d!", self->name, status);
  24. rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);
  25. #endif
  26. }
  27. }
  28. #ifdef RT_USING_MSH
  29. int system(const char *command)
  30. {
  31. extern int msh_exec(char *cmd, rt_size_t length);
  32. if (command)
  33. {
  34. msh_exec((char *)command, rt_strlen(command));
  35. }
  36. return 0;
  37. }
  38. RTM_EXPORT(system);
  39. #endif /* RT_USING_MSH */
  40. char *ltoa(long value, char *string, int radix)
  41. {
  42. char tmp[33];
  43. char *tp = tmp;
  44. long i;
  45. unsigned long v;
  46. int sign;
  47. char *sp;
  48. if (string == NULL)
  49. {
  50. return 0 ;
  51. }
  52. if (radix > 36 || radix <= 1)
  53. {
  54. return 0 ;
  55. }
  56. sign = (radix == 10 && value < 0);
  57. if (sign)
  58. {
  59. v = -value;
  60. }
  61. else
  62. {
  63. v = (unsigned long)value;
  64. }
  65. while (v || tp == tmp)
  66. {
  67. i = v % radix;
  68. v = v / radix;
  69. if (i < 10)
  70. *tp++ = i+'0';
  71. else
  72. *tp++ = i + 'a' - 10;
  73. }
  74. sp = string;
  75. if (sign)
  76. *sp++ = '-';
  77. while (tp > tmp)
  78. *sp++ = *--tp;
  79. *sp = 0;
  80. return string;
  81. }
  82. char *itoa(int value, char *string, int radix)
  83. {
  84. return ltoa(value, string, radix) ;
  85. }
  86. char *ultoa(unsigned long value, char *string, int radix)
  87. {
  88. char tmp[33];
  89. char *tp = tmp;
  90. long i;
  91. unsigned long v = value;
  92. char *sp;
  93. if (string == NULL)
  94. {
  95. return 0;
  96. }
  97. if (radix > 36 || radix <= 1)
  98. {
  99. return 0;
  100. }
  101. while (v || tp == tmp)
  102. {
  103. i = v % radix;
  104. v = v / radix;
  105. if (i < 10)
  106. *tp++ = i+'0';
  107. else
  108. *tp++ = i + 'a' - 10;
  109. }
  110. sp = string;
  111. while (tp > tmp)
  112. *sp++ = *--tp;
  113. *sp = 0;
  114. return string;
  115. }
  116. char *utoa(unsigned value, char *string, int radix)
  117. {
  118. return ultoa(value, string, radix) ;
  119. }