stat.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/umask.html
  16. mode_t umask(mode_t mask)
  17. {
  18. return syscall(SC_umask, mask);
  19. }
  20. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html
  21. int mkdir(const char* pathname, mode_t mode)
  22. {
  23. if (!pathname) {
  24. errno = EFAULT;
  25. return -1;
  26. }
  27. int rc = syscall(SC_mkdir, pathname, strlen(pathname), mode);
  28. __RETURN_WITH_ERRNO(rc, rc, -1);
  29. }
  30. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
  31. int chmod(const char* pathname, mode_t mode)
  32. {
  33. if (!pathname) {
  34. errno = EFAULT;
  35. return -1;
  36. }
  37. int rc = syscall(SC_chmod, pathname, strlen(pathname), mode);
  38. __RETURN_WITH_ERRNO(rc, rc, -1);
  39. }
  40. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmod.html
  41. int fchmod(int fd, mode_t mode)
  42. {
  43. int rc = syscall(SC_fchmod, fd, mode);
  44. __RETURN_WITH_ERRNO(rc, rc, -1);
  45. }
  46. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifo.html
  47. int mkfifo(const char* pathname, mode_t mode)
  48. {
  49. return mknod(pathname, mode | S_IFIFO, 0);
  50. }
  51. static int do_stat(int dirfd, const char* path, struct stat* statbuf, bool follow_symlinks)
  52. {
  53. if (!path) {
  54. errno = EFAULT;
  55. return -1;
  56. }
  57. Syscall::SC_stat_params params { { path, strlen(path) }, statbuf, dirfd, follow_symlinks };
  58. int rc = syscall(SC_stat, &params);
  59. __RETURN_WITH_ERRNO(rc, rc, -1);
  60. }
  61. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/lstat.html
  62. int lstat(const char* path, struct stat* statbuf)
  63. {
  64. return do_stat(AT_FDCWD, path, statbuf, false);
  65. }
  66. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
  67. int stat(const char* path, struct stat* statbuf)
  68. {
  69. return do_stat(AT_FDCWD, path, statbuf, true);
  70. }
  71. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstat.html
  72. int fstat(int fd, struct stat* statbuf)
  73. {
  74. int rc = syscall(SC_fstat, fd, statbuf);
  75. __RETURN_WITH_ERRNO(rc, rc, -1);
  76. }
  77. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatat.html
  78. int fstatat(int fd, const char* path, struct stat* statbuf, int flags)
  79. {
  80. return do_stat(fd, path, statbuf, !(flags & AT_SYMLINK_NOFOLLOW));
  81. }
  82. }