1 #include <ctype.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <libsystem/syscall.h>
5 #include <signal.h>
6
7 extern void _fini();
8
abs(int i)9 int abs(int i)
10 {
11 return i < 0 ? -i : i;
12 }
13
labs(long i)14 long labs(long i)
15 {
16 return i < 0 ? -i : i;
17 }
18
llabs(long long i)19 long long llabs(long long i)
20 {
21 return i < 0 ? -i : i;
22 }
23
atoi(const char * str)24 int atoi(const char *str)
25 {
26 int n = 0, neg = 0;
27
28 while (isspace(*str))
29 {
30 str++;
31 }
32
33 switch (*str)
34 {
35 case '-':
36 neg = 1;
37 break;
38 case '+':
39 str++;
40 break;
41 }
42
43 /* Compute n as a negative number to avoid overflow on INT_MIN */
44 while (isdigit(*str))
45 {
46 n = 10 * n - (*str++ - '0');
47 }
48
49 return neg ? n : -n;
50 }
51
52 /**
53 * @brief 退出进程
54 *
55 * @param status
56 */
exit(int status)57 void exit(int status)
58 {
59 _fini();
60 syscall_invoke(SYS_EXIT, status, 0, 0, 0, 0, 0, 0, 0);
61 }
62
63 /**
64 * @brief 通过发送SIGABRT,从而退出当前进程
65 *
66 */
abort()67 void abort()
68 {
69 // step1:设置SIGABRT的处理函数为SIG_DFL
70 signal(SIGABRT, SIG_DFL);
71 raise(SIGABRT);
72 }