unistd.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "unistd.h"
  2. #include "string.h"
  3. #include "errno.h"
  4. #include <Kernel/Syscall.h>
  5. extern "C" {
  6. uid_t getuid()
  7. {
  8. return Syscall::invoke(Syscall::PosixGetuid);
  9. }
  10. gid_t getgid()
  11. {
  12. return Syscall::invoke(Syscall::PosixGetgid);
  13. }
  14. pid_t getpid()
  15. {
  16. return Syscall::invoke(Syscall::PosixGetpid);
  17. }
  18. int open(const char* path)
  19. {
  20. size_t length = strlen(path);
  21. int rc = Syscall::invoke(Syscall::PosixOpen, (dword)path, (dword)length);
  22. __RETURN_WITH_ERRNO(rc, rc, -1);
  23. }
  24. ssize_t read(int fd, void* buf, size_t count)
  25. {
  26. int rc = Syscall::invoke(Syscall::PosixRead, (dword)fd, (dword)buf, (dword)count);
  27. __RETURN_WITH_ERRNO(rc, rc, -1);
  28. }
  29. int close(int fd)
  30. {
  31. int rc = Syscall::invoke(Syscall::PosixClose, fd);
  32. __RETURN_WITH_ERRNO(rc, rc, -1);
  33. }
  34. pid_t waitpid(pid_t waitee)
  35. {
  36. int rc = Syscall::invoke(Syscall::PosixWaitpid, waitee);
  37. __RETURN_WITH_ERRNO(rc, rc, -1);
  38. }
  39. int lstat(const char* path, stat* statbuf)
  40. {
  41. int rc = Syscall::invoke(Syscall::PosixLstat, (dword)path, (dword)statbuf);
  42. __RETURN_WITH_ERRNO(rc, rc, -1);
  43. }
  44. char* getcwd(char* buffer, size_t size)
  45. {
  46. int rc = Syscall::invoke(Syscall::PosixGetcwd, (dword)buffer, (dword)size);
  47. __RETURN_WITH_ERRNO(rc, buffer, nullptr);
  48. }
  49. int sleep(unsigned seconds)
  50. {
  51. return Syscall::invoke(Syscall::Sleep, (dword)seconds);
  52. }
  53. int gethostname(char* buffer, size_t size)
  54. {
  55. int rc = Syscall::invoke(Syscall::PosixGethostname, (dword)buffer, (dword)size);
  56. __RETURN_WITH_ERRNO(rc, rc, -1);
  57. }
  58. }