cstdlib.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. LOG_W("thread:%s exit:%d!", self->parent.name, status);
  20. #ifdef RT_USING_PTHREADS
  21. if(self->pthread_data != RT_NULL)
  22. {
  23. extern void pthread_exit(void *value);
  24. pthread_exit((void *)status);
  25. }
  26. else
  27. #endif
  28. {
  29. rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);
  30. }
  31. }
  32. }
  33. #ifdef RT_USING_MSH
  34. int system(const char *command)
  35. {
  36. extern int msh_exec(char *cmd, rt_size_t length);
  37. if (command)
  38. {
  39. msh_exec((char *)command, rt_strlen(command));
  40. }
  41. return 0;
  42. }
  43. RTM_EXPORT(system);
  44. #endif /* RT_USING_MSH */
  45. char *ltoa(long value, char *string, int radix)
  46. {
  47. char tmp[33];
  48. char *tp = tmp;
  49. long i;
  50. unsigned long v;
  51. int sign;
  52. char *sp;
  53. if (string == NULL)
  54. {
  55. return 0 ;
  56. }
  57. if (radix > 36 || radix <= 1)
  58. {
  59. return 0 ;
  60. }
  61. sign = (radix == 10 && value < 0);
  62. if (sign)
  63. {
  64. v = -value;
  65. }
  66. else
  67. {
  68. v = (unsigned long)value;
  69. }
  70. while (v || tp == tmp)
  71. {
  72. i = v % radix;
  73. v = v / radix;
  74. if (i < 10)
  75. *tp++ = (char)(i+'0');
  76. else
  77. *tp++ = (char)(i + 'a' - 10);
  78. }
  79. sp = string;
  80. if (sign)
  81. *sp++ = '-';
  82. while (tp > tmp)
  83. *sp++ = *--tp;
  84. *sp = 0;
  85. return string;
  86. }
  87. char *itoa(int value, char *string, int radix)
  88. {
  89. return ltoa(value, string, radix) ;
  90. }
  91. char *ultoa(unsigned long value, char *string, int radix)
  92. {
  93. char tmp[33];
  94. char *tp = tmp;
  95. long i;
  96. unsigned long v = value;
  97. char *sp;
  98. if (string == NULL)
  99. {
  100. return 0;
  101. }
  102. if (radix > 36 || radix <= 1)
  103. {
  104. return 0;
  105. }
  106. while (v || tp == tmp)
  107. {
  108. i = v % radix;
  109. v = v / radix;
  110. if (i < 10)
  111. *tp++ = (char)(i+'0');
  112. else
  113. *tp++ = (char)(i + 'a' - 10);
  114. }
  115. sp = string;
  116. while (tp > tmp)
  117. *sp++ = *--tp;
  118. *sp = 0;
  119. return string;
  120. }
  121. char *utoa(unsigned value, char *string, int radix)
  122. {
  123. return ultoa(value, string, radix) ;
  124. }