Advertisement
STANAANDREY

tpa t1pb2

Mar 3rd, 2023
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. typedef enum {
  5.   READ, WRITE, DEL, RENAME, COPY
  6. } PermsE;
  7.  
  8. void setPerm(uint8_t *perms, PermsE permNr) {
  9.   *perms |= (1 << permNr);
  10. }
  11.  
  12. void resetPerm(uint8_t *perms, PermsE permNr) {
  13.   *perms &= ~(1 << permNr);
  14. }
  15.  
  16. int hasPerm(uint8_t perms, PermsE permNr) {
  17.   return 1 & (perms >> permNr);
  18. }
  19.  
  20. void printPerms(uint8_t perms) {
  21.   for (int i = 0; i < 5; i++) {
  22.     if (!hasPerm(perms, i)) {
  23.       printf("-/");
  24.       continue;
  25.     }
  26.     switch(i) {
  27.     case READ:
  28.       printf("Read");
  29.       break;
  30.     case WRITE:
  31.       printf("Write");
  32.       break;
  33.     case DEL:
  34.       printf("Delete");
  35.       break;
  36.     case RENAME:
  37.       printf("Rename");
  38.       break;
  39.     case COPY:
  40.       printf("Copy");
  41.       break;
  42.     }
  43.     putchar('/');
  44.   }
  45.   puts("");
  46. }
  47.  
  48. int main(void) {
  49.   uint8_t perms = 0;
  50.   setPerm(&perms, WRITE);
  51.   setPerm(&perms, RENAME);
  52.   setPerm(&perms, DEL);
  53.   printPerms(perms);
  54.   resetPerm(&perms, WRITE);
  55.   setPerm(&perms, READ);
  56.   printPerms(perms);
  57.   return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement