stdlib.c 801 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <libc/unistd.h>
  2. #include <libc/stdlib.h>
  3. #include <libc/ctype.h>
  4. #include <libsystem/syscall.h>
  5. int abs(int i)
  6. {
  7. return i < 0 ? -i : i;
  8. }
  9. long labs(long i)
  10. {
  11. return i < 0 ? -i : i;
  12. }
  13. long long llabs(long long i)
  14. {
  15. return i < 0 ? -i : i;
  16. }
  17. int atoi(const char *str)
  18. {
  19. int n = 0, neg = 0;
  20. while (isspace(*str))
  21. {
  22. str++;
  23. }
  24. switch (*str)
  25. {
  26. case '-':
  27. neg = 1;
  28. break;
  29. case '+':
  30. str++;
  31. break;
  32. }
  33. /* Compute n as a negative number to avoid overflow on INT_MIN */
  34. while (isdigit(*str))
  35. {
  36. n = 10 * n - (*str++ - '0');
  37. }
  38. return neg ? n : -n;
  39. }
  40. /**
  41. * @brief 退出进程
  42. *
  43. * @param status
  44. */
  45. void exit(int status)
  46. {
  47. syscall_invoke(SYS_EXIT, status, 0, 0, 0, 0, 0, 0, 0);
  48. }