stdlib.c 1.0 KB

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