stat.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #include <syscall.h>
  13. #include <unistd.h>
  14. extern "C" {
  15. mode_t umask(mode_t mask)
  16. {
  17. return syscall(SC_umask, mask);
  18. }
  19. int mkdir(const char* pathname, mode_t mode)
  20. {
  21. if (!pathname) {
  22. errno = EFAULT;
  23. return -1;
  24. }
  25. int rc = syscall(SC_mkdir, pathname, strlen(pathname), mode);
  26. __RETURN_WITH_ERRNO(rc, rc, -1);
  27. }
  28. int chmod(const char* pathname, mode_t mode)
  29. {
  30. if (!pathname) {
  31. errno = EFAULT;
  32. return -1;
  33. }
  34. int rc = syscall(SC_chmod, pathname, strlen(pathname), mode);
  35. __RETURN_WITH_ERRNO(rc, rc, -1);
  36. }
  37. int fchmod(int fd, mode_t mode)
  38. {
  39. int rc = syscall(SC_fchmod, fd, mode);
  40. __RETURN_WITH_ERRNO(rc, rc, -1);
  41. }
  42. int mkfifo(const char* pathname, mode_t mode)
  43. {
  44. return mknod(pathname, mode | S_IFIFO, 0);
  45. }
  46. static int do_stat(int dirfd, const char* path, struct stat* statbuf, bool follow_symlinks)
  47. {
  48. if (!path) {
  49. errno = EFAULT;
  50. return -1;
  51. }
  52. Syscall::SC_stat_params params { dirfd, { path, strlen(path) }, statbuf, follow_symlinks };
  53. int rc = syscall(SC_stat, &params);
  54. __RETURN_WITH_ERRNO(rc, rc, -1);
  55. }
  56. int lstat(const char* path, struct stat* statbuf)
  57. {
  58. return do_stat(AT_FDCWD, path, statbuf, false);
  59. }
  60. int stat(const char* path, struct stat* statbuf)
  61. {
  62. return do_stat(AT_FDCWD, path, statbuf, true);
  63. }
  64. int fstat(int fd, struct stat* statbuf)
  65. {
  66. int rc = syscall(SC_fstat, fd, statbuf);
  67. __RETURN_WITH_ERRNO(rc, rc, -1);
  68. }
  69. int fstatat(int fd, const char* path, struct stat* statbuf, int flags)
  70. {
  71. return do_stat(fd, path, statbuf, !(flags & AT_SYMLINK_NOFOLLOW));
  72. }
  73. }