18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: LGPL-2.1
28c2ecf20Sopenharmony_ci#include <sys/types.h>
38c2ecf20Sopenharmony_ci#include <sys/stat.h>
48c2ecf20Sopenharmony_ci#include <fcntl.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef O_DIRECT
78c2ecf20Sopenharmony_ci#define O_DIRECT	00040000
88c2ecf20Sopenharmony_ci#endif
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef O_DIRECTORY
118c2ecf20Sopenharmony_ci#define O_DIRECTORY	00200000
128c2ecf20Sopenharmony_ci#endif
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#ifndef O_NOATIME
158c2ecf20Sopenharmony_ci#define O_NOATIME	01000000
168c2ecf20Sopenharmony_ci#endif
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#ifndef O_TMPFILE
198c2ecf20Sopenharmony_ci#define O_TMPFILE	020000000
208c2ecf20Sopenharmony_ci#endif
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#undef O_LARGEFILE
238c2ecf20Sopenharmony_ci#define O_LARGEFILE	00100000
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cisize_t open__scnprintf_flags(unsigned long flags, char *bf, size_t size, bool show_prefix)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	const char *prefix = "O_";
288c2ecf20Sopenharmony_ci	int printed = 0;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	if ((flags & O_ACCMODE) == O_RDONLY)
318c2ecf20Sopenharmony_ci		printed = scnprintf(bf, size, "%s%s", show_prefix ? prefix : "", "RDONLY");
328c2ecf20Sopenharmony_ci	if (flags == 0)
338c2ecf20Sopenharmony_ci		return printed;
348c2ecf20Sopenharmony_ci#define	P_FLAG(n) \
358c2ecf20Sopenharmony_ci	if (flags & O_##n) { \
368c2ecf20Sopenharmony_ci		printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
378c2ecf20Sopenharmony_ci		flags &= ~O_##n; \
388c2ecf20Sopenharmony_ci	}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	P_FLAG(RDWR);
418c2ecf20Sopenharmony_ci	P_FLAG(APPEND);
428c2ecf20Sopenharmony_ci	P_FLAG(ASYNC);
438c2ecf20Sopenharmony_ci	P_FLAG(CLOEXEC);
448c2ecf20Sopenharmony_ci	P_FLAG(CREAT);
458c2ecf20Sopenharmony_ci	P_FLAG(DIRECT);
468c2ecf20Sopenharmony_ci	P_FLAG(DIRECTORY);
478c2ecf20Sopenharmony_ci	P_FLAG(EXCL);
488c2ecf20Sopenharmony_ci	P_FLAG(LARGEFILE);
498c2ecf20Sopenharmony_ci	P_FLAG(NOFOLLOW);
508c2ecf20Sopenharmony_ci	P_FLAG(TMPFILE);
518c2ecf20Sopenharmony_ci	P_FLAG(NOATIME);
528c2ecf20Sopenharmony_ci	P_FLAG(NOCTTY);
538c2ecf20Sopenharmony_ci#ifdef O_NONBLOCK
548c2ecf20Sopenharmony_ci	P_FLAG(NONBLOCK);
558c2ecf20Sopenharmony_ci#elif O_NDELAY
568c2ecf20Sopenharmony_ci	P_FLAG(NDELAY);
578c2ecf20Sopenharmony_ci#endif
588c2ecf20Sopenharmony_ci#ifdef O_PATH
598c2ecf20Sopenharmony_ci	P_FLAG(PATH);
608c2ecf20Sopenharmony_ci#endif
618c2ecf20Sopenharmony_ci#ifdef O_DSYNC
628c2ecf20Sopenharmony_ci	if ((flags & O_SYNC) == O_SYNC)
638c2ecf20Sopenharmony_ci		printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", "SYNC");
648c2ecf20Sopenharmony_ci	else {
658c2ecf20Sopenharmony_ci		P_FLAG(DSYNC);
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci#else
688c2ecf20Sopenharmony_ci	P_FLAG(SYNC);
698c2ecf20Sopenharmony_ci#endif
708c2ecf20Sopenharmony_ci	P_FLAG(TRUNC);
718c2ecf20Sopenharmony_ci	P_FLAG(WRONLY);
728c2ecf20Sopenharmony_ci#undef P_FLAG
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	if (flags)
758c2ecf20Sopenharmony_ci		printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return printed;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cisize_t syscall_arg__scnprintf_open_flags(char *bf, size_t size, struct syscall_arg *arg)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	int flags = arg->val;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (!(flags & O_CREAT))
858c2ecf20Sopenharmony_ci		arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return open__scnprintf_flags(flags, bf, size, arg->show_string_prefix);
888c2ecf20Sopenharmony_ci}
89