Skip to content

Instantly share code, notes, and snippets.

@theasder
Created October 6, 2013 16:39
Show Gist options
  • Save theasder/6856169 to your computer and use it in GitHub Desktop.
Save theasder/6856169 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
int cmp(int p, int perm)
{
if((p & 1) && !(perm & 1))
return 0;
if(((p >> 1) & 1) && !((perm >> 1) & 1))
return 0;
if((p >> 2) && !(perm >> 2))
return 0;
return 1;
}
int identify(int uid_flag, int gid_flag, int oper, int perm)
{
int p;
if(uid_flag) {
p = perm / 100;
if(cmp(oper, p)) return 1;
else return 0;
}
if(gid_flag) {
p = perm / 10 - (perm / 100) * 10;
if(cmp(oper, p)) return 1;
else return 0;
}
p = perm % 10;
if(cmp(oper, p)) return 1;
else return 0;
}
int in(int g, int gid[], int n)
{
int i;
for(i = 0; i < n; i++)
if(g == gid[i]) return 1;
return 0;
}
int main(void)
{
unsigned int uid, gid[33] = {0}, oper, uid_flag = 0, gid_flag = 0;
unsigned int u, g, perm, i, len;
char buf[1020], *fixed;
scanf("%u\n", &uid);
fgets(buf, 1020, stdin);
while(sscanf(buf, "%u", &gid[i]) > 0)
i++;
int n = i;
for(i = 0; i < n; i++)
printf("%u ", gid[i]);
scanf("%u", &oper);
while(scanf("%u %u %u ", &u, &g, &perm) != -1 && fgets(buf, 1020, stdin)) {
uid_flag = gid_flag = 0;
if(uid == u) uid_flag = 1;
if(in(g, gid, n)) gid_flag = 1;
if(identify(uid_flag, gid_flag, oper, perm)) {
for(i = 0; buf[i] == ' '; i++)
;
len = strlen(buf);
fixed = &buf[i];
for(i = len - 1; buf[i] == ' ' || buf[i] == '\n'; i--)
;
buf[i + 1] = '\0';
printf("%s\n", fixed);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment