cstdlib.c 2.4 KB

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