file.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <syscall.h>
  2. #include <bits/stat.h>
  3. #include <bits/types.h>
  4. #include <bits/fcntl.h>
  5. #include <bits/stdio.h>
  6. /* Common file ops. */
  7. inline static long sys_open(const char* name, int flags)
  8. {
  9. return syscall3(NR_openat, AT_FDCWD, (long)name, flags);
  10. }
  11. inline static long sys_open3(const char* name, int flags, int mode)
  12. {
  13. return syscall4(NR_openat, AT_FDCWD, (long)name, flags, mode);
  14. }
  15. inline static long sys_openat(int at, const char* path, int flags)
  16. {
  17. return syscall3(NR_openat, at, (long)path, flags);
  18. }
  19. inline static long sys_openat4(int at, const char* path, int flags, int mode)
  20. {
  21. return syscall4(NR_openat, at, (long)path, flags, mode);
  22. }
  23. inline static long sys_close(int fd)
  24. {
  25. return syscall1(NR_close, fd);
  26. }
  27. inline static long sys_read(int fd, void* buf, unsigned long len)
  28. {
  29. return syscall3(NR_read, fd, (long)buf, len);
  30. }
  31. inline static long sys_write(int fd, const void* buf, int len)
  32. {
  33. return syscall3(NR_write, fd, (long)buf, len);
  34. }
  35. inline static long sys_fcntl(int fd, int cmd)
  36. {
  37. return syscall2(NR_fcntl, fd, cmd);
  38. }
  39. inline static long sys_fcntl3(int fd, int cmd, int arg)
  40. {
  41. return syscall3(NR_fcntl, fd, cmd, arg);
  42. }
  43. inline static long sys_flock(int fd, int op)
  44. {
  45. return syscall2(NR_flock, fd, op);
  46. }
  47. inline static long sys_dup(int fd)
  48. {
  49. return syscall1(NR_dup, fd);
  50. }
  51. inline static long sys_dup2(int fda, int fdb)
  52. {
  53. return syscall3(NR_dup3, fda, fdb, 0);
  54. }
  55. #define SEEK_SET 0
  56. #define SEEK_CUR 1
  57. #define SEEK_END 2
  58. #define SEEK_DATA 3
  59. #define SEEK_HOLE 4
  60. /* There's probably no good way to implement lseek in a way that would
  61. work reasonably well on both 32 and 64 bit targets. The llseek code below
  62. is the best I can come up with. The compiler should be able to eliminate
  63. the pointer mess in 64-bit case. */
  64. #ifdef NR__llseek
  65. inline static long sys_seek(int fd, int64_t off)
  66. {
  67. int64_t pos;
  68. int32_t hi = (off >> 32);
  69. int32_t lo = (int32_t)off;
  70. return syscall5(NR__llseek, fd, hi, lo, (long)&pos, SEEK_SET);
  71. }
  72. #else
  73. inline static long sys_seek(int fd, int64_t off)
  74. {
  75. long ret;
  76. if((ret = syscall3(NR_lseek, fd, off, SEEK_SET)) < 0)
  77. return ret;
  78. return 0;
  79. }
  80. #endif
  81. /* The prototype for this accidentally matches the man page for _llseek,
  82. so let's call it sys_llseek. */
  83. #ifdef NR__llseek
  84. inline static long sys_llseek(int fd, int64_t off, int64_t* pos, int whence)
  85. {
  86. int32_t hi = (off >> 32);
  87. int32_t lo = (int32_t)off;
  88. return syscall5(NR__llseek, fd, hi, lo, (long)pos, whence);
  89. }
  90. #else
  91. inline static long sys_llseek(int fd, int64_t off, int64_t* pos, int whence)
  92. {
  93. long ret;
  94. if((ret = syscall3(NR_lseek, fd, off, whence)) < 0)
  95. return ret;
  96. *pos = ret;
  97. return 0;
  98. }
  99. #endif
  100. #ifndef NR_fstatat
  101. # ifdef NR_newfstatat
  102. # define NR_fstatat NR_newfstatat
  103. # else
  104. # define NR_fstatat NR_fstatat64
  105. # endif
  106. #endif
  107. inline static long sys_stat(const char *path, struct stat *st)
  108. {
  109. return syscall4(NR_fstatat, AT_FDCWD, (long)path, (long)st, 0);
  110. }
  111. inline static long sys_fstat(int fd, struct stat* st)
  112. {
  113. #ifdef NR_fstat64
  114. return syscall2(NR_fstat64, fd, (long)st);
  115. #else
  116. return syscall2(NR_fstat, fd, (long)st);
  117. #endif
  118. }
  119. inline static long sys_fstatat(int dirfd, const char *path,
  120. struct stat* st, int flags)
  121. {
  122. return syscall4(NR_fstatat, dirfd, (long)path, (long)st, flags);
  123. }
  124. inline static long sys_lstat(const char *path, struct stat *st)
  125. {
  126. return syscall4(NR_fstatat, AT_FDCWD, (long)path, (long)st,
  127. AT_SYMLINK_NOFOLLOW);
  128. }
  129. inline static long sys_pipe(int* fds)
  130. {
  131. return syscall2(NR_pipe2, (long)fds, 0);
  132. }
  133. inline static long sys_pipe2(int* fds, int flags)
  134. {
  135. return syscall2(NR_pipe2, (long)fds, flags);
  136. }
  137. #ifdef NR_pread64
  138. #define NR_pread NR_pread64
  139. #define NR_pwrite NR_pwrite64
  140. #endif
  141. inline static long sys_pread(int fd, void* buf, ulong len, uint64_t off)
  142. {
  143. #if BITS == 32
  144. union { uint64_t ll; long l[2]; } x = { .ll = off };
  145. return syscall5(NR_pread, fd, (long)buf, len, x.l[0], x.l[1]);
  146. #else
  147. return syscall4(NR_pread, fd, (long)buf, (long)len, off);
  148. #endif
  149. }
  150. inline static long sys_pwrite(int fd, void* buf, ulong len, uint64_t off)
  151. {
  152. #if BITS == 32
  153. union { uint64_t ll; long l[2]; } x = { .ll = off };
  154. return syscall5(NR_pwrite, fd, (long)buf, len, x.l[0], x.l[1]);
  155. #else
  156. return syscall4(NR_pwrite, fd, (long)buf, (long)len, off);
  157. #endif
  158. }