Facebook
From Blush Dove, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 129
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <limits.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <stdbool.h>
  10. #include <sys/stat.h>
  11.  
  12. enum class
  13. {
  14.     CLASS_OWNER,
  15.     CLASS_GROUP,
  16.     CLASS_OTHER
  17. };
  18. enum permission
  19. {
  20.     PERMISSION_READ,
  21.     PERMISSION_WRITE,
  22.     PERMISSION_EXECUTE
  23. };
  24.  
  25. const mode_t EMPTY_MODE = 0;
  26. mode_t perm(enum class c, enum permission p) { return 1 << ((2 - p) + (2 - c) * 3); }
  27. mode_t mode_add(mode_t mode, enum class c, enum permission p) { return mode | perm(c, p); }
  28. mode_t mode_rm(mode_t mode, enum class c, enum permission p) { return mode & ~perm(c, p); }
  29.  
  30. void afiseazaDescriere();
  31.  
  32. int cmd;
  33.  
  34. int setMasc[13];
  35.  
  36. int main(int argc, char **argv)
  37. {
  38.     if (argc < 1)
  39.     {
  40.         printf("Introdu numele fisierului/fisierelor.\n");
  41.         exit(1);
  42.     }
  43.  
  44.     while (1)
  45.     {
  46.         afiseazaDescriere();
  47.         scanf("%i", &cmd);
  48.  
  49.         if (cmd == 0)
  50.             break;
  51.         else
  52.         {
  53.             int cmdSet;
  54.             printf("Introduceti 1(true) sau -1(false) in functie de preferinta dumneavoastra\n");
  55.  
  56.             scanf("%i", &cmdSet);
  57.  
  58.             if (cmdSet != 1 && cmdSet != -1)
  59.                 printf("Input invalid\n");
  60.             else
  61.                 setMasc[cmd] = cmdSet;
  62.         }
  63.     }
  64.  
  65.     for (int i = 1; i < argc; i++)
  66.     {
  67.         struct stat st;
  68.  
  69.         if (stat(argv[i], &st) != 0)
  70.         {
  71.             printf("Eroare la stat pentru %s .\n", argv[i]);
  72.             continue;
  73.         }
  74.  
  75.         if (setMasc[1] == 1)
  76.             st.st_mode = mode_add(st.st_mode, CLASS_OWNER, PERMISSION_READ);
  77.         else if (setMasc[1] == -1)
  78.             st.st_mode = mode_rm(st.st_mode, CLASS_OWNER, PERMISSION_READ);
  79.  
  80.         if (setMasc[2] == 1)
  81.             st.st_mode = mode_add(st.st_mode, CLASS_OWNER, PERMISSION_WRITE);
  82.         else if (setMasc[2] == -1)
  83.             st.st_mode = mode_rm(st.st_mode, CLASS_OWNER, PERMISSION_WRITE);
  84.  
  85.         if (setMasc[3] == 1)
  86.             st.st_mode = mode_add(st.st_mode, CLASS_OWNER, PERMISSION_EXECUTE);
  87.         else if (setMasc[3] == -1)
  88.             st.st_mode = mode_rm(st.st_mode, CLASS_OWNER, PERMISSION_EXECUTE);
  89.  
  90.         if (setMasc[4] == 1)
  91.             st.st_mode = mode_add(st.st_mode, CLASS_GROUP, PERMISSION_READ);
  92.         else if (setMasc[4] == -1)
  93.             st.st_mode = mode_rm(st.st_mode, CLASS_GROUP, PERMISSION_READ);
  94.  
  95.         if (setMasc[5] == 1)
  96.             st.st_mode = mode_add(st.st_mode, CLASS_GROUP, PERMISSION_WRITE);
  97.         else if (setMasc[5] == -1)
  98.             st.st_mode = mode_rm(st.st_mode, CLASS_GROUP, PERMISSION_WRITE);
  99.  
  100.         if (setMasc[6] == 1)
  101.             st.st_mode = mode_add(st.st_mode, CLASS_GROUP, PERMISSION_EXECUTE);
  102.         else if (setMasc[6] == -1)
  103.             st.st_mode = mode_rm(st.st_mode, CLASS_GROUP, PERMISSION_EXECUTE);
  104.  
  105.         if (setMasc[7] == 1)
  106.             st.st_mode = mode_add(st.st_mode, CLASS_OTHER, PERMISSION_READ);
  107.         else if (setMasc[7] == -1)
  108.             st.st_mode = mode_rm(st.st_mode, CLASS_OTHER, PERMISSION_READ);
  109.  
  110.         if (setMasc[8] == 1)
  111.             st.st_mode = mode_add(st.st_mode, CLASS_OTHER, PERMISSION_WRITE);
  112.         else if (setMasc[8] == -1)
  113.             st.st_mode = mode_rm(st.st_mode, CLASS_OTHER, PERMISSION_WRITE);
  114.  
  115.         if (setMasc[9] == 1)
  116.             st.st_mode = mode_add(st.st_mode, CLASS_OTHER, PERMISSION_EXECUTE);
  117.         else if (setMasc[9] == -1)
  118.             st.st_mode = mode_rm(st.st_mode, CLASS_OTHER, PERMISSION_EXECUTE);
  119.  
  120.         chmod(argv[i],st.st_mode);
  121.     }
  122.     printf("Done!");
  123. }
  124.  
  125. void afiseazaDescriere()
  126. {
  127.     printf("Seteaza permisiune pentru fisier/fisiere\n");
  128.     printf("Selecteaza o optiune dintre cele de mai jos:\n");
  129.  
  130.     printf("0.Exit\n");
  131.     printf("1.Read by owner\n");
  132.     printf("2.Write by owner\n");
  133.     printf("3.Execute/search by owner\n");
  134.     printf("4.Read by group\n");
  135.     printf("5.Write by group\n");
  136.     printf("6.Execute/search by group\n");
  137.     printf("7.Read by others\n");
  138.     printf("8.Write by others\n");
  139.     printf("9.Execute/search by others\n");
  140. }