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