unistd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <libc/unistd.h>
  2. #include <libsystem/syscall.h>
  3. #include <libc/errno.h>
  4. #include <libc/stdio.h>
  5. #include <libc/stddef.h>
  6. /**
  7. * @brief 关闭文件接口
  8. *
  9. * @param fd 文件描述符
  10. * @return int
  11. */
  12. int close(int fd)
  13. {
  14. return syscall_invoke(SYS_CLOSE, fd, 0, 0, 0, 0, 0, 0, 0);
  15. }
  16. /**
  17. * @brief 从文件读取数据的接口
  18. *
  19. * @param fd 文件描述符
  20. * @param buf 缓冲区
  21. * @param count 待读取数据的字节数
  22. * @return ssize_t 成功读取的字节数
  23. */
  24. ssize_t read(int fd, void *buf, size_t count)
  25. {
  26. return (ssize_t)syscall_invoke(SYS_READ, fd, (uint64_t)buf, count, 0, 0, 0, 0, 0);
  27. }
  28. /**
  29. * @brief 向文件写入数据的接口
  30. *
  31. * @param fd 文件描述符
  32. * @param buf 缓冲区
  33. * @param count 待写入数据的字节数
  34. * @return ssize_t 成功写入的字节数
  35. */
  36. ssize_t write(int fd, void const *buf, size_t count)
  37. {
  38. return (ssize_t)syscall_invoke(SYS_WRITE, fd, (uint64_t)buf, count, 0, 0, 0, 0, 0);
  39. }
  40. /**
  41. * @brief 调整文件的访问位置
  42. *
  43. * @param fd 文件描述符号
  44. * @param offset 偏移量
  45. * @param whence 调整模式
  46. * @return uint64_t 调整结束后的文件访问位置
  47. */
  48. off_t lseek(int fd, off_t offset, int whence)
  49. {
  50. return (off_t)syscall_invoke(SYS_LSEEK, fd, offset, whence, 0, 0, 0, 0, 0);
  51. }
  52. /**
  53. * @brief fork当前进程
  54. *
  55. * @return pid_t
  56. */
  57. pid_t fork(void)
  58. {
  59. return (pid_t)syscall_invoke(SYS_FORK, 0, 0, 0, 0, 0, 0, 0, 0);
  60. }
  61. /**
  62. * @brief fork当前进程,但是与父进程共享VM、flags、fd
  63. *
  64. * @return pid_t
  65. */
  66. pid_t vfork(void)
  67. {
  68. return (pid_t)syscall_invoke(SYS_VFORK, 0, 0, 0, 0, 0, 0, 0, 0);
  69. }
  70. /**
  71. * @brief 将堆内存调整为end_brk
  72. *
  73. * @param end_brk 新的堆区域的结束地址
  74. * end_brk=-1 ===> 返回堆区域的起始地址
  75. * end_brk=-2 ===> 返回堆区域的结束地址
  76. * @return uint64_t 错误码
  77. *
  78. */
  79. uint64_t brk(uint64_t end_brk)
  80. {
  81. uint64_t x = (uint64_t)syscall_invoke(SYS_BRK, (uint64_t)end_brk, 0, 0, 0, 0, 0, 0, 0);
  82. // printf("brk(): end_brk=%#018lx x=%#018lx", (uint64_t)end_brk, x);
  83. return x;
  84. }
  85. /**
  86. * @brief 将堆内存空间加上offset(注意,该系统调用只应在普通进程中调用,而不能是内核线程)
  87. *
  88. * @param increment offset偏移量
  89. * @return uint64_t the previous program break
  90. */
  91. void *sbrk(int64_t increment)
  92. {
  93. void *retval = (void *)syscall_invoke(SYS_SBRK, (uint64_t)increment, 0, 0, 0, 0, 0, 0, 0);
  94. if (retval == (void *)-ENOMEM)
  95. return (void *)(-1);
  96. else
  97. {
  98. errno = 0;
  99. return (void *)retval;
  100. }
  101. }
  102. /**
  103. * @brief 切换当前工作目录
  104. *
  105. * @param dest_path 目标目录
  106. * @return int64_t 成功:0,失败:负值(错误码)
  107. */
  108. int64_t chdir(char *dest_path)
  109. {
  110. if (dest_path == NULL)
  111. {
  112. errno = -EFAULT;
  113. return -1;
  114. }
  115. else
  116. {
  117. return syscall_invoke(SYS_CHDIR, (uint64_t)dest_path, 0, 0, 0, 0, 0, 0, 0);
  118. }
  119. }
  120. /**
  121. * @brief 执行新的程序
  122. *
  123. * @param path 文件路径
  124. * @param argv 参数列表
  125. * @return int
  126. */
  127. int execv(const char *path, char *const argv[])
  128. {
  129. if (path == NULL)
  130. {
  131. errno = -ENOENT;
  132. return -1;
  133. }
  134. int retval = syscall_invoke(SYS_EXECVE, (uint64_t)path, (uint64_t)argv, 0, 0, 0, 0, 0, 0);
  135. if (retval != 0)
  136. return -1;
  137. else
  138. return 0;
  139. }
  140. /**
  141. * @brief 删除文件夹
  142. *
  143. * @param path 绝对路径
  144. * @return int 错误码
  145. */
  146. int rmdir(const char *path)
  147. {
  148. return syscall_invoke(SYS_RMDIR, (uint64_t)path, 0, 0, 0, 0, 0, 0, 0);
  149. }