Reputation: 87
Just as in open() system call user can pass multiple flags separated by pipe ("|", like O_CREAT|O_WRONLY ) but if we look at man page of open system call it will show below given code
int open(const char *pathname, int flags);
So my question is that since this function contains one argument for flags but user can still pass multiple flags like above example. So can anyone tell me how it is working and if we want to implement similar function how to achieve this?
Upvotes: 0
Views: 1027
Reputation: 3164
The trick here is to "one-hot" encode flags as 1 << n
where 0 <= n < sizeof(int) * 8
. So for example, we could have:
int FLAG1 = 0x1;
int FLAG2 = 0x2;
int FLAG3 = 0x4;
/* ... */
And then write:
void func(int flags) {
if (flags & FLAG1)
/* FLAG1 is set ... */
if (flags & FLAG2)
/* FLAG2 ist set ... */
/* ... */
}
Upvotes: 2