fcntl.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <stdarg.h>
  9. #include <string.h>
  10. #include <syscall.h>
  11. extern "C" {
  12. int fcntl(int fd, int cmd, ...)
  13. {
  14. va_list ap;
  15. va_start(ap, cmd);
  16. u32 extra_arg = va_arg(ap, u32);
  17. int rc = syscall(SC_fcntl, fd, cmd, extra_arg);
  18. va_end(ap);
  19. __RETURN_WITH_ERRNO(rc, rc, -1);
  20. }
  21. int watch_file(const char* path, size_t path_length)
  22. {
  23. int rc = syscall(SC_watch_file, path, path_length);
  24. __RETURN_WITH_ERRNO(rc, rc, -1);
  25. }
  26. int creat(const char* path, mode_t mode)
  27. {
  28. return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
  29. }
  30. int open(const char* path, int options, ...)
  31. {
  32. if (!path) {
  33. errno = EFAULT;
  34. return -1;
  35. }
  36. auto path_length = strlen(path);
  37. if (path_length > INT32_MAX) {
  38. errno = EINVAL;
  39. return -1;
  40. }
  41. va_list ap;
  42. va_start(ap, options);
  43. auto mode = (mode_t)va_arg(ap, unsigned);
  44. va_end(ap);
  45. Syscall::SC_open_params params { AT_FDCWD, { path, path_length }, options, mode };
  46. int rc = syscall(SC_open, &params);
  47. __RETURN_WITH_ERRNO(rc, rc, -1);
  48. }
  49. int openat(int dirfd, const char* path, int options, ...)
  50. {
  51. if (!path) {
  52. errno = EFAULT;
  53. return -1;
  54. }
  55. auto path_length = strlen(path);
  56. if (path_length > INT32_MAX) {
  57. errno = EINVAL;
  58. return -1;
  59. }
  60. va_list ap;
  61. va_start(ap, options);
  62. auto mode = (mode_t)va_arg(ap, unsigned);
  63. va_end(ap);
  64. Syscall::SC_open_params params { dirfd, { path, path_length }, options, mode };
  65. int rc = syscall(SC_open, &params);
  66. __RETURN_WITH_ERRNO(rc, rc, -1);
  67. }
  68. }