stat.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <stdio.h>
  9. #include <string.h>
  10. #include <sys/stat.h>
  11. #include <syscall.h>
  12. #include <unistd.h>
  13. extern "C" {
  14. mode_t umask(mode_t mask)
  15. {
  16. return syscall(SC_umask, mask);
  17. }
  18. int mkdir(const char* pathname, mode_t mode)
  19. {
  20. if (!pathname) {
  21. errno = EFAULT;
  22. return -1;
  23. }
  24. int rc = syscall(SC_mkdir, pathname, strlen(pathname), mode);
  25. __RETURN_WITH_ERRNO(rc, rc, -1);
  26. }
  27. int chmod(const char* pathname, mode_t mode)
  28. {
  29. if (!pathname) {
  30. errno = EFAULT;
  31. return -1;
  32. }
  33. int rc = syscall(SC_chmod, pathname, strlen(pathname), mode);
  34. __RETURN_WITH_ERRNO(rc, rc, -1);
  35. }
  36. int fchmod(int fd, mode_t mode)
  37. {
  38. int rc = syscall(SC_fchmod, fd, mode);
  39. __RETURN_WITH_ERRNO(rc, rc, -1);
  40. }
  41. int mkfifo(const char* pathname, mode_t mode)
  42. {
  43. return mknod(pathname, mode | S_IFIFO, 0);
  44. }
  45. static int do_stat(const char* path, struct stat* statbuf, bool follow_symlinks)
  46. {
  47. if (!path) {
  48. errno = EFAULT;
  49. return -1;
  50. }
  51. Syscall::SC_stat_params params { { path, strlen(path) }, statbuf, follow_symlinks };
  52. int rc = syscall(SC_stat, &params);
  53. __RETURN_WITH_ERRNO(rc, rc, -1);
  54. }
  55. int lstat(const char* path, struct stat* statbuf)
  56. {
  57. return do_stat(path, statbuf, false);
  58. }
  59. int stat(const char* path, struct stat* statbuf)
  60. {
  61. return do_stat(path, statbuf, true);
  62. }
  63. int fstat(int fd, struct stat* statbuf)
  64. {
  65. int rc = syscall(SC_fstat, fd, statbuf);
  66. __RETURN_WITH_ERRNO(rc, rc, -1);
  67. }
  68. }