unistd.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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("!4 R0 SUB R12 ARITH_ALWAYS"
  26. "!0 R0 LOAD32 R0 MEMORY"
  27. "!8 R1 SUB R12 ARITH_ALWAYS"
  28. "!0 R1 LOAD32 R1 MEMORY"
  29. "!33 R7 LOADI8_ALWAYS"
  30. "SYSCALL_ALWAYS");
  31. }
  32. int chdir(char* path)
  33. {
  34. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  35. "!0 R0 LOAD32 R0 MEMORY"
  36. "!12 R7 LOADI8_ALWAYS"
  37. "SYSCALL_ALWAYS");
  38. }
  39. int fchdir(int fd)
  40. {
  41. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  42. "!0 R0 LOAD32 R0 MEMORY"
  43. "!133 R7 LOADI8_ALWAYS"
  44. "SYSCALL_ALWAYS");
  45. }
  46. void _exit(int value);
  47. int fork()
  48. {
  49. asm("!2 R7 LOADI8_ALWAYS"
  50. "SYSCALL_ALWAYS");
  51. }
  52. int waitpid (int pid, int* status_ptr, int options)
  53. {
  54. asm("!114 R7 LOADI8_ALWAYS"
  55. "!12 R2 SUB R12 ARITH_ALWAYS"
  56. "!0 R2 LOAD32 R2 MEMORY"
  57. "!8 R1 SUB R12 ARITH_ALWAYS"
  58. "!0 R1 LOAD32 R1 MEMORY"
  59. "!4 R0 SUB R12 ARITH_ALWAYS"
  60. "!0 R0 LOAD32 R0 MEMORY"
  61. "SYSCALL_ALWAYS");
  62. }
  63. int execve(char* file_name, char** argv, char** envp)
  64. {
  65. asm("!11 R7 LOADI8_ALWAYS"
  66. "!12 R2 SUB R12 ARITH_ALWAYS"
  67. "!0 R2 LOAD32 R2 MEMORY"
  68. "!8 R1 SUB R12 ARITH_ALWAYS"
  69. "!0 R1 LOAD32 R1 MEMORY"
  70. "!4 R0 SUB R12 ARITH_ALWAYS"
  71. "!0 R0 LOAD32 R0 MEMORY"
  72. "SYSCALL_ALWAYS");
  73. }
  74. int read(int fd, char* buf, unsigned count)
  75. {
  76. asm("!3 R7 LOADI8_ALWAYS"
  77. "!12 R2 SUB R12 ARITH_ALWAYS"
  78. "!0 R2 LOAD32 R2 MEMORY"
  79. "!8 R1 SUB R12 ARITH_ALWAYS"
  80. "!0 R1 LOAD32 R1 MEMORY"
  81. "!4 R0 SUB R12 ARITH_ALWAYS"
  82. "!0 R0 LOAD32 R0 MEMORY"
  83. "SYSCALL_ALWAYS");
  84. }
  85. int write(int fd, char* buf, unsigned count)
  86. {
  87. asm("!4 R7 LOADI8_ALWAYS"
  88. "!12 R2 SUB R12 ARITH_ALWAYS"
  89. "!0 R2 LOAD32 R2 MEMORY"
  90. "!8 R1 SUB R12 ARITH_ALWAYS"
  91. "!0 R1 LOAD32 R1 MEMORY"
  92. "!4 R0 SUB R12 ARITH_ALWAYS"
  93. "!0 R0 LOAD32 R0 MEMORY"
  94. "SYSCALL_ALWAYS");
  95. }
  96. int lseek(int fd, int offset, int whence)
  97. {
  98. asm("!19 R7 LOADI8_ALWAYS"
  99. "!12 R2 SUB R12 ARITH_ALWAYS"
  100. "!0 R2 LOAD32 R2 MEMORY"
  101. "!8 R1 SUB R12 ARITH_ALWAYS"
  102. "!0 R1 LOAD32 R1 MEMORY"
  103. "!4 R0 SUB R12 ARITH_ALWAYS"
  104. "!0 R0 LOAD32 R0 MEMORY"
  105. "SYSCALL_ALWAYS");
  106. }
  107. int close(int fd)
  108. {
  109. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  110. "!6 R7 LOADI8_ALWAYS"
  111. "SYSCALL_ALWAYS");
  112. }
  113. int unlink (char* filename)
  114. {
  115. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  116. "!10 R7 LOADI8_ALWAYS"
  117. "SYSCALL_ALWAYS");
  118. }
  119. int symlink(char *path1, char *path2)
  120. {
  121. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  122. "!0 R0 LOAD32 R0 MEMORY"
  123. "!8 R1 SUB R12 ARITH_ALWAYS"
  124. "!0 R1 LOAD32 R1 MEMORY"
  125. "!21 R7 LOADI8_ALWAYS"
  126. "SYSCALL_ALWAYS");
  127. }
  128. int _getcwd(char* buf, int size)
  129. {
  130. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  131. "!0 R0 LOAD32 R0 MEMORY"
  132. "!8 R1 SUB R12 ARITH_ALWAYS"
  133. "!0 R1 LOAD32 R1 MEMORY"
  134. "!183 R7 LOADI8_ALWAYS"
  135. "SYSCALL_ALWAYS");
  136. }
  137. char* getcwd(char* buf, unsigned size)
  138. {
  139. int c = _getcwd(buf, size);
  140. if(0 == c) return NULL;
  141. return buf;
  142. }
  143. char* getwd(char* buf)
  144. {
  145. return getcwd(buf, __PATH_MAX);
  146. }
  147. char* get_current_dir_name()
  148. {
  149. return getcwd(malloc(__PATH_MAX), __PATH_MAX);
  150. }
  151. int brk(void *addr)
  152. {
  153. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  154. "!0 R0 LOAD32 R0 MEMORY"
  155. "!45 R7 LOADI8_ALWAYS"
  156. "SYSCALL_ALWAYS");
  157. }
  158. int uname(struct utsname* unameData)
  159. {
  160. asm("!122 R7 LOADI8_ALWAYS"
  161. "!4 R0 SUB R12 ARITH_ALWAYS"
  162. "!0 R0 LOAD32 R0 MEMORY"
  163. "SYSCALL_ALWAYS");
  164. }
  165. int unshare(int flags)
  166. {
  167. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  168. "!0 R0 LOAD32 R0 MEMORY"
  169. /* because 337 can't fit in 1 byte */
  170. "!0 R7 LOAD32 R15 MEMORY"
  171. "~0 JUMP_ALWAYS"
  172. "%337"
  173. "SYSCALL_ALWAYS");
  174. }
  175. int geteuid()
  176. {
  177. asm("!201 R7 LOADI8_ALWAYS"
  178. "SYSCALL_ALWAYS");
  179. }
  180. int getegid()
  181. {
  182. asm("!202 R7 LOADI8_ALWAYS"
  183. "SYSCALL_ALWAYS");
  184. }
  185. int chroot(char const *path)
  186. {
  187. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  188. "!61 R7 LOADI8_ALWAYS"
  189. "SYSCALL_ALWAYS");
  190. }
  191. int mount(char const *source, char const *target, char const *filesystemtype, SCM mountflags, void const *data)
  192. {
  193. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  194. "!0 R0 LOAD32 R0 MEMORY"
  195. "!8 R1 SUB R12 ARITH_ALWAYS"
  196. "!0 R1 LOAD32 R1 MEMORY"
  197. "!12 R2 SUB R12 ARITH_ALWAYS"
  198. "!0 R2 LOAD32 R2 MEMORY"
  199. "!16 R3 SUB R12 ARITH_ALWAYS"
  200. "!0 R3 LOAD32 R3 MEMORY"
  201. "!20 R4 SUB R12 ARITH_ALWAYS"
  202. "!0 R4 LOAD32 R4 MEMORY"
  203. "!31 R7 LOADI8_ALWAYS"
  204. "SYSCALL_ALWAYS");
  205. }
  206. #endif