cstdlib.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /**
  15. * @brief This function is called when a thread exits. It can detach the thread and perform cleanup.
  16. *
  17. * @param status is the exit status of the thread.
  18. */
  19. void __rt_libc_exit(int status)
  20. {
  21. rt_thread_t self = rt_thread_self();
  22. if (self != RT_NULL)
  23. {
  24. LOG_W("thread:%s exit:%d!", self->parent.name, status);
  25. #ifdef RT_USING_PTHREADS
  26. if (self->pthread_data != RT_NULL)
  27. {
  28. extern void pthread_exit(void *value);
  29. pthread_exit((void *)status);
  30. }
  31. else
  32. #endif
  33. {
  34. rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);
  35. }
  36. }
  37. }
  38. #ifdef RT_USING_MSH
  39. /**
  40. * @brief Execute a command using the Micro-Shell (MSH) subsystem.
  41. *
  42. * @param command is the command string to execute.
  43. *
  44. * @return Returns 0 after executing the command.
  45. */
  46. int system(const char *command)
  47. {
  48. extern int msh_exec(char *cmd, rt_size_t length);
  49. if (command)
  50. {
  51. msh_exec((char *)command, rt_strlen(command));
  52. }
  53. return 0;
  54. }
  55. RTM_EXPORT(system);
  56. #endif /* RT_USING_MSH */
  57. /**
  58. * @brief Convert a long integer to a string representation with a specified radix.
  59. *
  60. * @param value is the long integer to convert.
  61. * @param string is the destination string where the result will be stored.
  62. * @param radix is the base of the number system to be used for conversion.
  63. *
  64. * @return Returns a pointer to the destination string.
  65. */
  66. char *ltoa(long value, char *string, int radix)
  67. {
  68. char tmp[33];
  69. char *tp = tmp;
  70. long i;
  71. unsigned long v;
  72. int sign;
  73. char *sp;
  74. if (string == NULL)
  75. {
  76. return 0;
  77. }
  78. if (radix > 36 || radix <= 1)
  79. {
  80. return 0;
  81. }
  82. sign = (radix == 10 && value < 0);
  83. if (sign)
  84. {
  85. v = -value;
  86. }
  87. else
  88. {
  89. v = (unsigned long)value;
  90. }
  91. while (v || tp == tmp)
  92. {
  93. i = v % radix;
  94. v = v / radix;
  95. if (i < 10)
  96. *tp++ = (char)(i + '0');
  97. else
  98. *tp++ = (char)(i + 'a' - 10);
  99. }
  100. sp = string;
  101. if (sign)
  102. *sp++ = '-';
  103. while (tp > tmp)
  104. *sp++ = *--tp;
  105. *sp = 0;
  106. return string;
  107. }
  108. /**
  109. * @brief Convert an integer to a string representation with a specified radix.
  110. *
  111. * @param value is the integer to convert.
  112. * @param string is the destination string where the result will be stored.
  113. * @param radix is the base of the number system to be used for conversion.
  114. *
  115. * @return Returns a pointer to the destination string.
  116. */
  117. char *itoa(int value, char *string, int radix)
  118. {
  119. return ltoa(value, string, radix);
  120. }
  121. /**
  122. * @brief Convert an unsigned long integer to a string representation with a specified radix.
  123. *
  124. * @param value is the unsigned long integer to convert.
  125. * @param string is the destination string where the result will be stored.
  126. * @param radix is the base of the number system to be used for conversion.
  127. *
  128. * @return Returns a pointer to the destination string.
  129. */
  130. char *ultoa(unsigned long value, char *string, int radix)
  131. {
  132. char tmp[33];
  133. char *tp = tmp;
  134. long i;
  135. unsigned long v = value;
  136. char *sp;
  137. if (string == NULL)
  138. {
  139. return 0;
  140. }
  141. if (radix > 36 || radix <= 1)
  142. {
  143. return 0;
  144. }
  145. while (v || tp == tmp)
  146. {
  147. i = v % radix;
  148. v = v / radix;
  149. if (i < 10)
  150. *tp++ = (char)(i + '0');
  151. else
  152. *tp++ = (char)(i + 'a' - 10);
  153. }
  154. sp = string;
  155. while (tp > tmp)
  156. *sp++ = *--tp;
  157. *sp = 0;
  158. return string;
  159. }
  160. /**
  161. * @brief Convert an unsigned integer to a string representation with a specified radix.
  162. *
  163. * @param value is the unsigned integer to convert.
  164. * @param string is the destination string where the result will be stored.
  165. * @param radix is the base of the number system to be used for conversion.
  166. *
  167. * @return Returns a pointer to the destination string.
  168. */
  169. char *utoa(unsigned value, char *string, int radix)
  170. {
  171. return ultoa(value, string, radix);
  172. }