unistd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright (C) 2020 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _UNISTD_C
  18. #define _UNISTD_C
  19. #include <sys/utsname.h>
  20. #define NULL 0
  21. #define __PATH_MAX 4096
  22. void* malloc(unsigned size);
  23. int access(char* pathname, int mode)
  24. {
  25. asm("LOAD R0 R14 0"
  26. "LOAD R1 R14 4"
  27. "SYS_ACCESS");
  28. }
  29. int chdir(char* path)
  30. {
  31. asm("LOAD R0 R14 0"
  32. "SYS_CHDIR");
  33. }
  34. int fchdir(int fd)
  35. {
  36. asm("LOAD R0 R14 0"
  37. "SYS_FCHDIR");
  38. }
  39. void _exit(int value);
  40. int fork()
  41. {
  42. asm("SYS_FORK");
  43. }
  44. int waitpid (int pid, int* status_ptr, int options)
  45. {
  46. /* Uses wait4 with struct rusage *ru set to NULL */
  47. asm("LOAD R0 R14 0"
  48. "LOAD R1 R14 4"
  49. "LOAD R2 R14 8"
  50. "FALSE R3"
  51. "SYS_WAIT4");
  52. }
  53. int execve(char* file_name, char** argv, char** envp)
  54. {
  55. asm("LOAD R0 R14 0"
  56. "LOAD R1 R14 4"
  57. "LOAD R2 R14 8"
  58. "SYS_EXECVE");
  59. }
  60. int read(int fd, char* buf, unsigned count)
  61. {
  62. asm("LOAD R0 R14 0"
  63. "LOAD R1 R14 4"
  64. "LOAD R2 R14 8"
  65. "SYS_READ");
  66. }
  67. int write(int fd, char* buf, unsigned count)
  68. {
  69. asm("LOAD R0 R14 0"
  70. "LOAD R1 R14 4"
  71. "LOAD R2 R14 8"
  72. "SYS_WRITE");
  73. }
  74. int lseek(int fd, int offset, int whence)
  75. {
  76. asm("LOAD R0 R14 0"
  77. "LOAD R1 R14 4"
  78. "LOAD R2 R14 8"
  79. "SYS_LSEEK");
  80. }
  81. int close(int fd)
  82. {
  83. asm("LOAD R0 R14 0"
  84. "SYS_CLOSE");
  85. }
  86. int unlink (char* filename)
  87. {
  88. asm("LOAD R0 R14 0"
  89. "SYS_UNLINK");
  90. }
  91. int _getcwd(char* buf, int size)
  92. {
  93. asm("LOAD R0 R14 0"
  94. "LOAD R1 R14 4"
  95. "SYS_GETCWD");
  96. }
  97. char* getcwd(char* buf, unsigned size)
  98. {
  99. int c = _getcwd(buf, size);
  100. if(0 == c) return NULL;
  101. return buf;
  102. }
  103. char* getwd(char* buf)
  104. {
  105. return getcwd(buf, __PATH_MAX);
  106. }
  107. char* get_current_dir_name()
  108. {
  109. return getcwd(malloc(__PATH_MAX), __PATH_MAX);
  110. }
  111. /********************************************************************************
  112. * All memory past the text segment and stack are always allocated to heap *
  113. * purposes and thus no syscalls are needed for brk *
  114. ********************************************************************************/
  115. int brk(void *addr)
  116. {
  117. asm("LOAD R0 R14 0"
  118. "ADDU R0 R12 R0"
  119. "SWAP R0 R12");
  120. }
  121. int uname(struct utsname* unameData)
  122. {
  123. asm("LOAD R0 R14 0"
  124. "SYS_UNAME");
  125. }
  126. #endif