open_flags.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #ifndef O_DIRECT
  6. #define O_DIRECT 00040000
  7. #endif
  8. #ifndef O_DIRECTORY
  9. #define O_DIRECTORY 00200000
  10. #endif
  11. #ifndef O_NOATIME
  12. #define O_NOATIME 01000000
  13. #endif
  14. #ifndef O_TMPFILE
  15. #define O_TMPFILE 020000000
  16. #endif
  17. #undef O_LARGEFILE
  18. #define O_LARGEFILE 00100000
  19. size_t open__scnprintf_flags(unsigned long flags, char *bf, size_t size)
  20. {
  21. int printed = 0;
  22. if (flags == 0)
  23. return scnprintf(bf, size, "RDONLY");
  24. #define P_FLAG(n) \
  25. if (flags & O_##n) { \
  26. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  27. flags &= ~O_##n; \
  28. }
  29. P_FLAG(RDWR);
  30. P_FLAG(APPEND);
  31. P_FLAG(ASYNC);
  32. P_FLAG(CLOEXEC);
  33. P_FLAG(CREAT);
  34. P_FLAG(DIRECT);
  35. P_FLAG(DIRECTORY);
  36. P_FLAG(EXCL);
  37. P_FLAG(LARGEFILE);
  38. P_FLAG(NOFOLLOW);
  39. P_FLAG(TMPFILE);
  40. P_FLAG(NOATIME);
  41. P_FLAG(NOCTTY);
  42. #ifdef O_NONBLOCK
  43. P_FLAG(NONBLOCK);
  44. #elif O_NDELAY
  45. P_FLAG(NDELAY);
  46. #endif
  47. #ifdef O_PATH
  48. P_FLAG(PATH);
  49. #endif
  50. #ifdef O_DSYNC
  51. if ((flags & O_SYNC) == O_SYNC)
  52. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
  53. else {
  54. P_FLAG(DSYNC);
  55. }
  56. #else
  57. P_FLAG(SYNC);
  58. #endif
  59. P_FLAG(TRUNC);
  60. P_FLAG(WRONLY);
  61. #undef P_FLAG
  62. if (flags)
  63. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  64. return printed;
  65. }
  66. size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size, struct syscall_arg *arg)
  67. {
  68. int flags = arg->val;
  69. if (!(flags & O_CREAT))
  70. arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
  71. return open__scnprintf_flags(flags, bf, size);
  72. }