1
0

cstdlib.c 4.3 KB

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