- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <limits.h>
- #include <string.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <stdbool.h>
- #include <sys/stat.h>
- enum class
- {
- CLASS_OWNER,
- CLASS_GROUP,
- CLASS_OTHER
- };
- enum permission
- {
- PERMISSION_READ,
- PERMISSION_WRITE,
- PERMISSION_EXECUTE
- };
- const mode_t EMPTY_MODE = 0;
- mode_t perm(enum class c, enum permission p) { return 1 << ((2 - p) + (2 - c) * 3); }
- mode_t mode_add(mode_t mode, enum class c, enum permission p) { return mode | perm(c, p); }
- mode_t mode_rm(mode_t mode, enum class c, enum permission p) { return mode & ~perm(c, p); }
- void afiseazaDescriere();
- int cmd;
- int setMasc[13];
- int main(int argc, char **argv)
- {
- if (argc < 1)
- {
- printf("Introdu numele fisierului/fisierelor.\n");
- exit(1);
- }
- while (1)
- {
- afiseazaDescriere();
- scanf("%i", &cmd);
- if (cmd == 0)
- break;
- else
- {
- int cmdSet;
- printf("Introduceti 1(true) sau -1(false) in functie de preferinta dumneavoastra\n");
- scanf("%i", &cmdSet);
- if (cmdSet != 1 && cmdSet != -1)
- printf("Input invalid\n");
- else
- setMasc[cmd] = cmdSet;
- }
- }
- for (int i = 1; i < argc; i++)
- {
- struct stat st;
- if (stat(argv[i], &st) != 0)
- {
- printf("Eroare la stat pentru %s .\n", argv[i]);
- continue;
- }
- if (setMasc[1] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_OWNER, PERMISSION_READ);
- else if (setMasc[1] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_OWNER, PERMISSION_READ);
- if (setMasc[2] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_OWNER, PERMISSION_WRITE);
- else if (setMasc[2] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_OWNER, PERMISSION_WRITE);
- if (setMasc[3] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_OWNER, PERMISSION_EXECUTE);
- else if (setMasc[3] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_OWNER, PERMISSION_EXECUTE);
- if (setMasc[4] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_GROUP, PERMISSION_READ);
- else if (setMasc[4] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_GROUP, PERMISSION_READ);
- if (setMasc[5] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_GROUP, PERMISSION_WRITE);
- else if (setMasc[5] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_GROUP, PERMISSION_WRITE);
- if (setMasc[6] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_GROUP, PERMISSION_EXECUTE);
- else if (setMasc[6] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_GROUP, PERMISSION_EXECUTE);
- if (setMasc[7] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_OTHER, PERMISSION_READ);
- else if (setMasc[7] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_OTHER, PERMISSION_READ);
- if (setMasc[8] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_OTHER, PERMISSION_WRITE);
- else if (setMasc[8] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_OTHER, PERMISSION_WRITE);
- if (setMasc[9] == 1)
- st.st_mode = mode_add(st.st_mode, CLASS_OTHER, PERMISSION_EXECUTE);
- else if (setMasc[9] == -1)
- st.st_mode = mode_rm(st.st_mode, CLASS_OTHER, PERMISSION_EXECUTE);
- chmod(argv[i],st.st_mode);
- }
- printf("Done!");
- }
- void afiseazaDescriere()
- {
- printf("Seteaza permisiune pentru fisier/fisiere\n");
- printf("Selecteaza o optiune dintre cele de mai jos:\n");
- printf("0.Exit\n");
- printf("1.Read by owner\n");
- printf("2.Write by owner\n");
- printf("3.Execute/search by owner\n");
- printf("4.Read by group\n");
- printf("5.Write by group\n");
- printf("6.Execute/search by group\n");
- printf("7.Read by others\n");
- printf("8.Write by others\n");
- printf("9.Execute/search by others\n");
- }