162306a36Sopenharmony_ci// SPDX-License-Identifier: LGPL-2.1
262306a36Sopenharmony_ci#include <sys/types.h>
362306a36Sopenharmony_ci#include <sys/stat.h>
462306a36Sopenharmony_ci#include <unistd.h>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci/* From include/linux/stat.h */
762306a36Sopenharmony_ci#ifndef S_IRWXUGO
862306a36Sopenharmony_ci#define S_IRWXUGO	(S_IRWXU|S_IRWXG|S_IRWXO)
962306a36Sopenharmony_ci#endif
1062306a36Sopenharmony_ci#ifndef S_IALLUGO
1162306a36Sopenharmony_ci#define S_IALLUGO	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
1262306a36Sopenharmony_ci#endif
1362306a36Sopenharmony_ci#ifndef S_IRUGO
1462306a36Sopenharmony_ci#define S_IRUGO         (S_IRUSR|S_IRGRP|S_IROTH)
1562306a36Sopenharmony_ci#endif
1662306a36Sopenharmony_ci#ifndef S_IWUGO
1762306a36Sopenharmony_ci#define S_IWUGO         (S_IWUSR|S_IWGRP|S_IWOTH)
1862306a36Sopenharmony_ci#endif
1962306a36Sopenharmony_ci#ifndef S_IXUGO
2062306a36Sopenharmony_ci#define S_IXUGO         (S_IXUSR|S_IXGRP|S_IXOTH)
2162306a36Sopenharmony_ci#endif
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cistatic size_t syscall_arg__scnprintf_mode_t(char *bf, size_t size, struct syscall_arg *arg)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	bool show_prefix = arg->show_string_prefix;
2662306a36Sopenharmony_ci	const char *prefix = "S_";
2762306a36Sopenharmony_ci	int printed = 0, mode = arg->val;
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define	P_MODE(n) \
3062306a36Sopenharmony_ci	if ((mode & S_##n) == S_##n) { \
3162306a36Sopenharmony_ci		printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
3262306a36Sopenharmony_ci		mode &= ~S_##n; \
3362306a36Sopenharmony_ci	}
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	P_MODE(IALLUGO);
3662306a36Sopenharmony_ci	P_MODE(IRWXUGO);
3762306a36Sopenharmony_ci	P_MODE(IRUGO);
3862306a36Sopenharmony_ci	P_MODE(IWUGO);
3962306a36Sopenharmony_ci	P_MODE(IXUGO);
4062306a36Sopenharmony_ci	P_MODE(IFMT);
4162306a36Sopenharmony_ci	P_MODE(IFSOCK);
4262306a36Sopenharmony_ci	P_MODE(IFLNK);
4362306a36Sopenharmony_ci	P_MODE(IFREG);
4462306a36Sopenharmony_ci	P_MODE(IFBLK);
4562306a36Sopenharmony_ci	P_MODE(IFDIR);
4662306a36Sopenharmony_ci	P_MODE(IFCHR);
4762306a36Sopenharmony_ci	P_MODE(IFIFO);
4862306a36Sopenharmony_ci	P_MODE(ISUID);
4962306a36Sopenharmony_ci	P_MODE(ISGID);
5062306a36Sopenharmony_ci	P_MODE(ISVTX);
5162306a36Sopenharmony_ci	P_MODE(IRWXU);
5262306a36Sopenharmony_ci	P_MODE(IRUSR);
5362306a36Sopenharmony_ci	P_MODE(IWUSR);
5462306a36Sopenharmony_ci	P_MODE(IXUSR);
5562306a36Sopenharmony_ci	P_MODE(IRWXG);
5662306a36Sopenharmony_ci	P_MODE(IRGRP);
5762306a36Sopenharmony_ci	P_MODE(IWGRP);
5862306a36Sopenharmony_ci	P_MODE(IXGRP);
5962306a36Sopenharmony_ci	P_MODE(IRWXO);
6062306a36Sopenharmony_ci	P_MODE(IROTH);
6162306a36Sopenharmony_ci	P_MODE(IWOTH);
6262306a36Sopenharmony_ci	P_MODE(IXOTH);
6362306a36Sopenharmony_ci#undef P_MODE
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	if (mode)
6662306a36Sopenharmony_ci		printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", mode);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	return printed;
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#define SCA_MODE_T syscall_arg__scnprintf_mode_t
72