Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #define BUFF_SIZE 64
- char* permissionsFct(char *file){
- struct stat st;
- char *modeval = malloc(sizeof(char) * 9 + 1);
- if(stat(file, &st) == 0){
- mode_t perm = st.st_mode;
- modeval[0] = (perm & S_IRUSR) ? 'r' : '-';
- modeval[1] = (perm & S_IWUSR) ? 'w' : '-';
- modeval[2] = (perm & S_IXUSR) ? 'x' : '-';
- modeval[3] = (perm & S_IRGRP) ? 'r' : '-';
- modeval[4] = (perm & S_IWGRP) ? 'w' : '-';
- modeval[5] = (perm & S_IXGRP) ? 'x' : '-';
- modeval[6] = (perm & S_IROTH) ? 'r' : '-';
- modeval[7] = (perm & S_IWOTH) ? 'w' : '-';
- modeval[8] = (perm & S_IXOTH) ? 'x' : '-';
- modeval[9] = '\0';
- free(modeval);
- return modeval;
- }
- else{
- perror("PermissionFct error");
- }
- return 0;
- }
- int parseFile2(const char *path)
- {
- int fd=-1;
- char magic;
- //char c;
- //int ok=1;
- fd= open(path, O_RDONLY);
- if(fd == -1) {
- // perror("Could not open input file\n");
- return 0;
- }
- ////////magic
- lseek(fd,-1,SEEK_END);
- read(fd,&magic,1);
- if(magic!='D')
- {
- //printf("ERROR\n wrong magic\n");
- return 0;
- }
- ////////////headerSize
- short int headerSize=0;
- lseek(fd,-3,SEEK_END);
- read(fd,&headerSize,2);
- //printf("header size este %d\n",headerSize);
- ///////version
- int version=0;
- lseek(fd, -headerSize,SEEK_END);
- read(fd,&version,1);
- if(version>171 || version<118)
- {
- // printf("ERROR\nwrong version\n");
- return 0;
- }
- //printf("Version is %d\n",version);
- ///////nr of sections
- int nrSections=0;
- read(fd,&nrSections,1);
- //printf("%d\n",nrSections);
- if(nrSections<7 || nrSections>18)
- {
- //printf("ERROR\nwrong sect_nr\n");
- return 0;
- }
- //////sections
- char sect_name[12];
- int sect_type=0;
- int sect_offset=0;
- int sect_size=0;
- for(int i=0;i<nrSections;i++)
- {
- read(fd,sect_name,11);
- read(fd,§_type,2);
- if(sect_type!=61 && sect_type!=91 && sect_type!=67 && sect_type!=69)
- {
- {
- //printf("ERROR\nwrong sect_types\n");
- return 0;
- }
- }
- read(fd,§_offset,4);
- read(fd,§_size,4);
- }
- //printf("SUCCESS\n");
- //printf("version=%d\n",version);
- //printf("nr_sections=%d\n",nrSections);
- //lseek(fd,-nrSections*21,SEEK_CUR);
- //for(int i=0;i<nrSections;i++)
- //{
- // read(fd,sect_name,11);
- //read(fd,§_type,2);
- //read(fd,§_offset,4);
- //read(fd,§_size,4);
- // printf("section%d: %s %d %d\n",i+1,sect_name,sect_type,sect_size);
- //}
- close(fd);
- return 1;
- }
- void listDir(const char *path,int permissions,char *permissions_string,int name_starts_with,char *name_starts_with_string)
- {
- DIR *dir = NULL;
- struct dirent *entry = NULL;
- char fullPath[512];
- //struct stat statbuf;
- dir = opendir(path);
- if(dir == NULL) {
- perror("Could not open directory");
- return;
- }
- while((entry = readdir(dir)) != NULL) {
- if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
- {
- snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
- if(name_starts_with==1 && permissions==1 )// 1 1
- {
- if (strncmp (entry->d_name,name_starts_with_string,strlen(name_starts_with_string)) == 0)
- {
- char *perm=permissionsFct(fullPath);
- //printf("%s\n",perm);
- if(strcmp(perm,permissions_string)==0)
- {printf("%s\n", fullPath);
- free(perm);}
- }
- }
- else
- if(name_starts_with==1 && permissions==0 )// 1 0
- {
- if (strncmp (entry->d_name,name_starts_with_string,strlen(name_starts_with_string)) == 0)
- {
- printf("%s\n", fullPath);
- }
- }
- else
- if(name_starts_with==0 && permissions==1 )// 0 1
- {
- char *perm=permissionsFct(fullPath);
- //printf("%s\n",perm);
- if(strcmp(perm,permissions_string)==0)
- {printf("%s\n", fullPath);
- free(perm);}
- }
- else
- printf("%s\n", fullPath);//0 0
- }
- }}
- closedir(dir);
- }
- void findAll(const char *path)
- {
- DIR *dir = NULL;
- struct dirent *entry = NULL;
- char fullPath[512];
- struct stat statbuf;
- dir = opendir(path);
- if(dir == NULL) {
- perror("Could not open directory");
- return;
- }
- while((entry = readdir(dir)) != NULL) {
- if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
- snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
- if(lstat(fullPath, &statbuf) == 0) {
- if(S_ISREG(statbuf.st_mode))
- {
- int sectiuni=0;
- int fd=-1;
- fd= open(fullPath, O_RDONLY);
- if(fd == -1) {
- perror("Could not open input file\n");
- return;
- }
- if(parseFile2(fullPath)==0)
- {
- printf("ERROR\ninvalid file\n");
- return;
- }
- char magic;
- lseek(fd,-1,SEEK_END);
- read(fd,&magic,1);
- short int headerSize=0;
- lseek(fd,-3,SEEK_END);
- read(fd,&headerSize,2);
- int version=0;
- lseek(fd, -headerSize,SEEK_END);
- read(fd,&version,1);
- int nrSections=0;
- read(fd,&nrSections,1);
- //printf("\n\n\n SECTIUNI %d\n\n\n",nrSections);
- char sect_name[12];
- int sect_type=0;
- int sect_offset=0;
- int sect_size=0;
- for(int i=1;i<=nrSections;i++)
- {
- //revin tot timpul la poz subsectiunii
- lseek(fd,-headerSize+2+(i-1)*(21),SEEK_END);
- read(fd,sect_name,11);
- read(fd,§_type,2);
- read(fd,§_offset,4);
- read(fd,§_size,4);
- // printf("size %d\n",sect_size);
- //pt fiecare sectiune verific nr de linii
- char buf[900000]="";
- // printf("size %d\n",sect_size);
- lseek(fd,sect_offset,SEEK_SET);
- //printf("size %d\n",sect_size);
- read(fd,buf,sect_size);
- //printf("%s\n\n\n\n",buf);
- //printf("\n %c %c\n",buf[0],buf[sect_size-1]);
- int nrLinii=1;
- for(int i=0;i<sect_size;i++)
- {
- if(buf[i]=='\n')
- nrLinii++;
- }
- //printf("nr LINII %d\n",nrLinii);
- if(nrLinii==13)
- sectiuni++;
- }
- if(sectiuni>=3)
- printf("%s\n", fullPath);
- }
- if(S_ISDIR(statbuf.st_mode)) {
- findAll(fullPath);
- }
- }
- }
- }
- closedir(dir);
- }
- void listRec(const char *path,int permissions,char *permissions_string,int name_starts_with,char *name_starts_with_string)
- {
- DIR *dir = NULL;
- struct dirent *entry = NULL;
- char fullPath[512];
- struct stat statbuf;
- dir = opendir(path);
- if(dir == NULL) {
- perror("Could not open directory");
- return;
- }
- while((entry = readdir(dir)) != NULL) {
- if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
- snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
- if(lstat(fullPath, &statbuf) == 0) {
- if(name_starts_with==1 && permissions==1 )// 1 1
- {
- if (strncmp (entry->d_name,name_starts_with_string,strlen(name_starts_with_string)) == 0)
- {
- char *perm=permissionsFct(fullPath);
- //printf("%s\n",perm);
- if(strcmp(perm,permissions_string)==0)
- { printf("%s\n", fullPath);
- free(perm);}
- }
- }
- else
- if(name_starts_with==1 && permissions==0 )// 1 0
- {
- if (strncmp (entry->d_name,name_starts_with_string,strlen(name_starts_with_string)) == 0)
- {
- printf("%s\n", fullPath);
- }
- }
- else
- if(name_starts_with==0 && permissions==1 )// 0 1
- {
- char *perm=permissionsFct(fullPath);
- //printf("%s\n",perm);
- if(strcmp(perm,permissions_string)==0)
- {printf("%s\n", fullPath);
- free(perm);}
- }
- else
- printf("%s\n", fullPath);//0 0
- if(S_ISDIR(statbuf.st_mode)) {
- listRec(fullPath,permissions,permissions_string,name_starts_with,name_starts_with_string);
- }
- }
- }
- }
- closedir(dir);
- }
- int parseFile(const char *path)
- {
- int fd=-1;
- char magic;
- //char c;
- //int ok=1;
- fd= open(path, O_RDONLY);
- if(fd == -1) {
- perror("Could not open input file\n");
- return 0;
- }
- ////////magic
- lseek(fd,-1,SEEK_END);
- read(fd,&magic,1);
- if(magic!='D')
- {
- printf("ERROR\n wrong magic\n");
- return 0;
- }
- ////////////headerSize
- short int headerSize=0;
- lseek(fd,-3,SEEK_END);
- read(fd,&headerSize,2);
- //printf("header size este %d\n",headerSize);
- ///////version
- int version=0;
- lseek(fd, -headerSize,SEEK_END);
- read(fd,&version,1);
- if(version>171 || version<118)
- {
- printf("ERROR\nwrong version\n");
- return 0;
- }
- //printf("Version is %d\n",version);
- ///////nr of sections
- int nrSections=0;
- read(fd,&nrSections,1);
- //printf("%d\n",nrSections);
- if(nrSections<7 || nrSections>18)
- {
- printf("ERROR\nwrong sect_nr\n");
- return 0;
- }
- //////sections
- char sect_name[12];
- int sect_type=0;
- int sect_offset=0;
- int sect_size=0;
- for(int i=0;i<nrSections;i++)
- {
- read(fd,sect_name,11);
- read(fd,§_type,2);
- if(sect_type!=61 && sect_type!=91 && sect_type!=67 && sect_type!=69)
- {
- {
- printf("ERROR\nwrong sect_types\n");
- return 0;
- }
- }
- read(fd,§_offset,4);
- read(fd,§_size,4);
- }
- printf("SUCCESS\n");
- printf("version=%d\n",version);
- printf("nr_sections=%d\n",nrSections);
- lseek(fd,-nrSections*21,SEEK_CUR);
- for(int i=0;i<nrSections;i++)
- {
- read(fd,sect_name,11);
- read(fd,§_type,2);
- read(fd,§_offset,4);
- read(fd,§_size,4);
- printf("section%d: %s %d %d\n",i+1,sect_name,sect_type,sect_size);
- }
- close(fd);
- return 1;
- }
- void extractFile(const char *extractPath,int sectionNr,int lineNr)
- {
- int fd=-1;
- fd= open(extractPath, O_RDONLY);
- if(fd == -1) {
- perror("Could not open input file\n");
- return;
- }
- if(parseFile2(extractPath)==0)
- {
- printf("ERROR\ninvalid file\n");
- return;
- }
- char magic;
- lseek(fd,-1,SEEK_END);
- read(fd,&magic,1);
- short int headerSize=0;
- lseek(fd,-3,SEEK_END);
- read(fd,&headerSize,2);
- int version=0;
- lseek(fd, -headerSize,SEEK_END);
- read(fd,&version,1);
- int nrSections=0;
- read(fd,&nrSections,1);
- char sect_name[12];
- int sect_type=0;
- int sect_offset=0;
- int sect_size=0;
- if(sectionNr>nrSections)
- {
- printf("ERROR\ninvalid section\n");
- return;
- }
- for(int i=1;i<=sectionNr;i++)
- {
- read(fd,sect_name,11);
- read(fd,§_type,2);
- read(fd,§_offset,4);
- read(fd,§_size,4);
- }
- ///am citit datele despre sectiune
- lseek(fd,sect_offset,SEEK_SET);//inceputul sectiunii
- int i=0;
- char nr;
- char linie[900000];
- char inv[900000];
- while(read(fd,&nr,1)>0 && i<lineNr-1)//citesc n-1 linii
- {
- if(nr=='\n')//de fiecare data cand ajung la finalul unei linii incrementez i
- i++;
- }
- i=0;
- lseek(fd,-1,SEEK_CUR);//inceput linie;
- while(1)
- {
- int nr_caract_citite=read(fd,&linie[i],1);
- if(nr_caract_citite<0)
- {
- printf("Nu s-a citit linia\n");
- return;
- }
- if(linie[i]=='\n')
- break;// ma opresc din citit cand ajung la finalul liniei
- i++;
- }
- //final linie
- int k=0;//inversez linia
- if(linie[strlen(linie)-1]!='\n')
- linie[strlen(linie)]='\n';
- for(int j=strlen(linie)-1;j>=0;j--)
- {
- inv[k]=linie[j];
- k++;
- }
- printf("SUCCESS");
- for(int i=0;i<strlen(inv);i++)
- printf("%c",inv[i]);
- }
- int main(int argc, char **argv){
- int recursive=0;
- int name_starts_with=0;
- int permissions=0;
- char *name_starts_with_string=NULL;
- char *permissions_string=NULL;
- char *path_string=NULL;
- if(argc >= 2)
- {
- /////varianta
- if(strcmp(argv[1], "variant") == 0){
- printf("73651\n");
- }
- /////list
- if(strcmp(argv[1],"list")==0)
- {
- for(int i=2;i<argc;i++)
- {
- if(strcmp(argv[i],"recursive")==0)
- recursive=1;////daca e recursiv apelez functia de listare recursiva, altfel listare simpla
- else
- {
- if(strstr(argv[i],"name_starts_with=")!=NULL)
- {
- name_starts_with=1;
- ///extragere string
- //printf("name argv[%d]=%s\n",i,argv[i]);
- name_starts_with_string = strtok (argv[i],"=");
- name_starts_with_string = strtok (NULL, "=");
- }
- else
- { if(strstr(argv[i],"permissions=")!=NULL)
- {
- permissions=1;
- ///extragere permisiuni
- permissions_string = strtok (argv[i],"=");
- permissions_string = strtok (NULL, "=");
- }
- else
- {
- if(strstr(argv[i],"path=")!=NULL)
- {
- path_string = strtok (argv[i],"=");
- path_string = strtok (NULL, "=");
- }
- }
- }
- }
- }
- printf("SUCCESS\n");
- if(recursive==1)
- listRec(path_string,permissions,permissions_string,name_starts_with,name_starts_with_string);
- if(recursive==0)
- listDir(path_string,permissions,permissions_string,name_starts_with,name_starts_with_string);
- }
- /////////parse
- //int parse=0;
- char *parsePath=NULL;
- if(strcmp(argv[1], "parse") == 0)
- {
- //parse=1;
- if(strstr(argv[2],"path=")!=NULL)
- {
- parsePath = strtok (argv[2],"=");
- parsePath = strtok (NULL, "=");
- parseFile(parsePath);
- }
- }
- /////////////extract
- char *extractPath=NULL;
- char *section=NULL;
- char *line=NULL;
- int lineNr=0;
- int sectionNr=0;
- if(strcmp(argv[1],"extract")==0)
- {
- for(int i=2;i<argc;i++)
- {
- if(strstr(argv[i],"path=")!=NULL)
- {
- extractPath = strtok (argv[i],"=");
- extractPath = strtok (NULL, "=");
- //printf("%s\n",extractPath);
- }
- else
- if(strstr(argv[i],"section=")!=NULL)
- {
- section = strtok (argv[i],"=");
- section = strtok (NULL, "=");
- sectionNr=atoi(section);
- //printf("%d \n",sectionNr);
- }
- else
- if(strstr(argv[i],"line")!=NULL)
- {
- line = strtok (argv[i],"=");
- line = strtok (NULL, "=");
- lineNr=atoi(line);
- //printf("%d \n",lineNr);
- }
- }
- extractFile(extractPath,sectionNr,lineNr);
- }
- ///////////findall
- char *findPath=NULL;
- if(strcmp(argv[1],"findall")==0)
- {
- if(strstr(argv[2],"path=")!=NULL)
- {
- findPath = strtok (argv[2],"=");
- findPath = strtok (NULL, "=");
- printf("SUCCESS\n");
- findAll(findPath);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement