stdlib.c 1.0 KB

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