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