unistd.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #define NULL 0
  18. #define __PATH_MAX 4096
  19. void* malloc(unsigned size);
  20. int access(char* pathname, int mode)
  21. {
  22. asm("LOAD_EFFECTIVE_ADDRESS_ebx %8"
  23. "LOAD_INTEGER_ebx"
  24. "LOAD_EFFECTIVE_ADDRESS_ecx %4"
  25. "LOAD_INTEGER_ecx"
  26. "LOAD_IMMEDIATE_eax %33"
  27. "INT_80");
  28. }
  29. int chdir(char* path)
  30. {
  31. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  32. "LOAD_INTEGER_ebx"
  33. "LOAD_IMMEDIATE_eax %12"
  34. "INT_80");
  35. }
  36. int fchdir(int fd)
  37. {
  38. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  39. "LOAD_INTEGER_ebx"
  40. "LOAD_IMMEDIATE_eax %133"
  41. "INT_80");
  42. }
  43. void _exit(int value)
  44. {
  45. asm("POP_ebx"
  46. "POP_ebx"
  47. "LOAD_IMMEDIATE_eax %1"
  48. "INT_80");
  49. }
  50. int fork()
  51. {
  52. asm("LOAD_IMMEDIATE_eax %2"
  53. "LOAD_IMMEDIATE_ebx %0"
  54. "INT_80");
  55. }
  56. int waitpid (int pid, int* status_ptr, int options)
  57. {
  58. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  59. "LOAD_INTEGER_ebx"
  60. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  61. "LOAD_INTEGER_ecx"
  62. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  63. "LOAD_INTEGER_edx"
  64. "LOAD_IMMEDIATE_eax %7"
  65. "INT_80");
  66. }
  67. int execve(char* file_name, char** argv, char** envp)
  68. {
  69. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  70. "LOAD_INTEGER_ebx"
  71. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  72. "LOAD_INTEGER_ecx"
  73. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  74. "LOAD_INTEGER_edx"
  75. "LOAD_IMMEDIATE_eax %11"
  76. "INT_80");
  77. }
  78. int read(int fd, char* buf, unsigned count) {
  79. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  80. "LOAD_INTEGER_ebx"
  81. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  82. "LOAD_INTEGER_ecx"
  83. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  84. "LOAD_INTEGER_edx"
  85. "LOAD_IMMEDIATE_eax %3"
  86. "INT_80");
  87. }
  88. int write(int fd, char* buf, unsigned count) {
  89. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  90. "LOAD_INTEGER_ebx"
  91. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  92. "LOAD_INTEGER_ecx"
  93. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  94. "LOAD_INTEGER_edx"
  95. "LOAD_IMMEDIATE_eax %4"
  96. "INT_80");
  97. }
  98. int lseek(int fd, int offset, int whence)
  99. {
  100. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  101. "LOAD_INTEGER_ebx"
  102. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  103. "LOAD_INTEGER_ecx"
  104. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  105. "LOAD_INTEGER_edx"
  106. "LOAD_IMMEDIATE_eax %19"
  107. "INT_80");
  108. }
  109. int close(int fd)
  110. {
  111. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  112. "LOAD_INTEGER_ebx"
  113. "LOAD_IMMEDIATE_eax %6"
  114. "INT_80");
  115. }
  116. int _getcwd(char* buf, int size)
  117. {
  118. asm("LOAD_EFFECTIVE_ADDRESS_ebx %8"
  119. "LOAD_INTEGER_ebx"
  120. "LOAD_EFFECTIVE_ADDRESS_ecx %4"
  121. "LOAD_INTEGER_ecx"
  122. "LOAD_IMMEDIATE_eax %183"
  123. "INT_80");
  124. }
  125. char* getcwd(char* buf, unsigned size)
  126. {
  127. int c = _getcwd(buf, size);
  128. if(0 == c) return NULL;
  129. return buf;
  130. }
  131. char* getwd(char* buf)
  132. {
  133. return getcwd(buf, __PATH_MAX);
  134. }
  135. char* get_current_dir_name()
  136. {
  137. return getcwd(malloc(__PATH_MAX), __PATH_MAX);
  138. }
  139. int brk(void *addr)
  140. {
  141. asm("LOAD_ESP_IMMEDIATE_into_eax %4"
  142. "PUSH_eax"
  143. "LOAD_IMMEDIATE_eax %45"
  144. "POP_ebx"
  145. "INT_80");
  146. }
  147. int chmod(char *pathname, int mode)
  148. {
  149. asm("LOAD_EFFECTIVE_ADDRESS_ebx %8"
  150. "LOAD_INTEGER_ebx"
  151. "LOAD_EFFECTIVE_ADDRESS_ecx %4"
  152. "LOAD_INTEGER_ecx"
  153. "LOAD_IMMEDIATE_eax %15"
  154. "INT_80");
  155. }
  156. struct utsname
  157. {
  158. char sysname[65]; /* Operating system name (e.g., "Linux") */
  159. char nodename[65]; /* Name within "some implementation-defined network" */
  160. char release[65]; /* Operating system release (e.g., "2.6.28") */
  161. char version[65]; /* Operating system version */
  162. char machine[65]; /* Hardware identifier */
  163. };
  164. int uname(struct utsname* unameData)
  165. {
  166. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  167. "LOAD_INTEGER_ebx"
  168. "LOAD_IMMEDIATE_eax %109"
  169. "INT_80");
  170. }