Advertisement
Guest User

Andreea Boncut

a guest
Mar 28th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <dirent.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <string.h>
  10.  
  11. #define BUFF_SIZE 64
  12.  
  13. char* permissionsFct(char *file){
  14. struct stat st;
  15. char *modeval = malloc(sizeof(char) * 9 + 1);
  16. if(stat(file, &st) == 0){
  17. mode_t perm = st.st_mode;
  18. modeval[0] = (perm & S_IRUSR) ? 'r' : '-';
  19. modeval[1] = (perm & S_IWUSR) ? 'w' : '-';
  20. modeval[2] = (perm & S_IXUSR) ? 'x' : '-';
  21. modeval[3] = (perm & S_IRGRP) ? 'r' : '-';
  22. modeval[4] = (perm & S_IWGRP) ? 'w' : '-';
  23. modeval[5] = (perm & S_IXGRP) ? 'x' : '-';
  24. modeval[6] = (perm & S_IROTH) ? 'r' : '-';
  25. modeval[7] = (perm & S_IWOTH) ? 'w' : '-';
  26. modeval[8] = (perm & S_IXOTH) ? 'x' : '-';
  27. modeval[9] = '\0';
  28. free(modeval);
  29. return modeval;
  30. }
  31. else{
  32. perror("PermissionFct error");
  33. }
  34.  
  35. return 0;
  36. }
  37. int parseFile2(const char *path)
  38. {
  39. int fd=-1;
  40.  
  41.  
  42. char magic;
  43. //char c;
  44. //int ok=1;
  45.  
  46.  
  47. fd= open(path, O_RDONLY);
  48. if(fd == -1) {
  49. // perror("Could not open input file\n");
  50. return 0;
  51. }
  52.  
  53. ////////magic
  54. lseek(fd,-1,SEEK_END);
  55. read(fd,&magic,1);
  56. if(magic!='D')
  57. {
  58. //printf("ERROR\n wrong magic\n");
  59. return 0;
  60. }
  61.  
  62. ////////////headerSize
  63. short int headerSize=0;
  64. lseek(fd,-3,SEEK_END);
  65. read(fd,&headerSize,2);
  66. //printf("header size este %d\n",headerSize);
  67.  
  68. ///////version
  69. int version=0;
  70. lseek(fd, -headerSize,SEEK_END);
  71. read(fd,&version,1);
  72.  
  73. if(version>171 || version<118)
  74. {
  75. // printf("ERROR\nwrong version\n");
  76. return 0;
  77. }
  78. //printf("Version is %d\n",version);
  79.  
  80. ///////nr of sections
  81. int nrSections=0;
  82. read(fd,&nrSections,1);
  83. //printf("%d\n",nrSections);
  84.  
  85. if(nrSections<7 || nrSections>18)
  86. {
  87. //printf("ERROR\nwrong sect_nr\n");
  88. return 0;
  89. }
  90.  
  91. //////sections
  92. char sect_name[12];
  93. int sect_type=0;
  94. int sect_offset=0;
  95. int sect_size=0;
  96.  
  97.  
  98.  
  99. for(int i=0;i<nrSections;i++)
  100. {
  101. read(fd,sect_name,11);
  102. read(fd,&sect_type,2);
  103. if(sect_type!=61 && sect_type!=91 && sect_type!=67 && sect_type!=69)
  104. {
  105. {
  106. //printf("ERROR\nwrong sect_types\n");
  107. return 0;
  108. }
  109. }
  110. read(fd,&sect_offset,4);
  111. read(fd,&sect_size,4);
  112. }
  113.  
  114. //printf("SUCCESS\n");
  115. //printf("version=%d\n",version);
  116. //printf("nr_sections=%d\n",nrSections);
  117. //lseek(fd,-nrSections*21,SEEK_CUR);
  118. //for(int i=0;i<nrSections;i++)
  119. //{
  120. // read(fd,sect_name,11);
  121. //read(fd,&sect_type,2);
  122. //read(fd,&sect_offset,4);
  123. //read(fd,&sect_size,4);
  124. // printf("section%d: %s %d %d\n",i+1,sect_name,sect_type,sect_size);
  125. //}
  126.  
  127.  
  128. close(fd);
  129. return 1;
  130. }
  131.  
  132. void listDir(const char *path,int permissions,char *permissions_string,int name_starts_with,char *name_starts_with_string)
  133. {
  134.  
  135. DIR *dir = NULL;
  136. struct dirent *entry = NULL;
  137. char fullPath[512];
  138. //struct stat statbuf;
  139.  
  140. dir = opendir(path);
  141. if(dir == NULL) {
  142. perror("Could not open directory");
  143. return;
  144. }
  145. while((entry = readdir(dir)) != NULL) {
  146. if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
  147. {
  148. snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
  149. if(name_starts_with==1 && permissions==1 )// 1 1
  150. {
  151. if (strncmp (entry->d_name,name_starts_with_string,strlen(name_starts_with_string)) == 0)
  152. {
  153.  
  154. char *perm=permissionsFct(fullPath);
  155. //printf("%s\n",perm);
  156. if(strcmp(perm,permissions_string)==0)
  157. {printf("%s\n", fullPath);
  158. free(perm);}
  159.  
  160.  
  161.  
  162. }
  163.  
  164.  
  165. }
  166. else
  167. if(name_starts_with==1 && permissions==0 )// 1 0
  168. {
  169. if (strncmp (entry->d_name,name_starts_with_string,strlen(name_starts_with_string)) == 0)
  170. {
  171. printf("%s\n", fullPath);
  172. }
  173.  
  174. }
  175. else
  176. if(name_starts_with==0 && permissions==1 )// 0 1
  177. {
  178.  
  179. char *perm=permissionsFct(fullPath);
  180. //printf("%s\n",perm);
  181. if(strcmp(perm,permissions_string)==0)
  182. {printf("%s\n", fullPath);
  183. free(perm);}
  184.  
  185. }
  186. else
  187. printf("%s\n", fullPath);//0 0
  188.  
  189.  
  190. }
  191. }}
  192. closedir(dir);
  193.  
  194. }
  195. void findAll(const char *path)
  196. {
  197.  
  198. DIR *dir = NULL;
  199. struct dirent *entry = NULL;
  200. char fullPath[512];
  201. struct stat statbuf;
  202.  
  203. dir = opendir(path);
  204. if(dir == NULL) {
  205. perror("Could not open directory");
  206. return;
  207. }
  208.  
  209. while((entry = readdir(dir)) != NULL) {
  210. if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
  211. snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
  212. if(lstat(fullPath, &statbuf) == 0) {
  213. if(S_ISREG(statbuf.st_mode))
  214. {
  215.  
  216. int sectiuni=0;
  217. int fd=-1;
  218. fd= open(fullPath, O_RDONLY);
  219. if(fd == -1) {
  220. perror("Could not open input file\n");
  221. return;
  222. }
  223. if(parseFile2(fullPath)==0)
  224. {
  225. printf("ERROR\ninvalid file\n");
  226. return;
  227. }
  228. char magic;
  229. lseek(fd,-1,SEEK_END);
  230. read(fd,&magic,1);
  231. short int headerSize=0;
  232. lseek(fd,-3,SEEK_END);
  233. read(fd,&headerSize,2);
  234. int version=0;
  235. lseek(fd, -headerSize,SEEK_END);
  236. read(fd,&version,1);
  237. int nrSections=0;
  238. read(fd,&nrSections,1);
  239. //printf("\n\n\n SECTIUNI %d\n\n\n",nrSections);
  240.  
  241. char sect_name[12];
  242. int sect_type=0;
  243. int sect_offset=0;
  244. int sect_size=0;
  245.  
  246. for(int i=1;i<=nrSections;i++)
  247. {
  248. //revin tot timpul la poz subsectiunii
  249. lseek(fd,-headerSize+2+(i-1)*(21),SEEK_END);
  250. read(fd,sect_name,11);
  251. read(fd,&sect_type,2);
  252. read(fd,&sect_offset,4);
  253. read(fd,&sect_size,4);
  254. // printf("size %d\n",sect_size);
  255.  
  256. //pt fiecare sectiune verific nr de linii
  257. char buf[900000]="";
  258. // printf("size %d\n",sect_size);
  259. lseek(fd,sect_offset,SEEK_SET);
  260. //printf("size %d\n",sect_size);
  261. read(fd,buf,sect_size);
  262. //printf("%s\n\n\n\n",buf);
  263. //printf("\n %c %c\n",buf[0],buf[sect_size-1]);
  264. int nrLinii=1;
  265. for(int i=0;i<sect_size;i++)
  266. {
  267. if(buf[i]=='\n')
  268. nrLinii++;
  269. }
  270. //printf("nr LINII %d\n",nrLinii);
  271. if(nrLinii==13)
  272. sectiuni++;
  273.  
  274. }
  275.  
  276. if(sectiuni>=3)
  277. printf("%s\n", fullPath);
  278.  
  279. }
  280.  
  281.  
  282.  
  283. if(S_ISDIR(statbuf.st_mode)) {
  284. findAll(fullPath);
  285. }
  286. }
  287. }
  288. }
  289. closedir(dir);
  290. }
  291.  
  292.  
  293.  
  294. void listRec(const char *path,int permissions,char *permissions_string,int name_starts_with,char *name_starts_with_string)
  295. {
  296. DIR *dir = NULL;
  297. struct dirent *entry = NULL;
  298. char fullPath[512];
  299. struct stat statbuf;
  300.  
  301. dir = opendir(path);
  302. if(dir == NULL) {
  303. perror("Could not open directory");
  304. return;
  305. }
  306. while((entry = readdir(dir)) != NULL) {
  307. if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
  308. snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
  309. if(lstat(fullPath, &statbuf) == 0) {
  310. if(name_starts_with==1 && permissions==1 )// 1 1
  311. {
  312. if (strncmp (entry->d_name,name_starts_with_string,strlen(name_starts_with_string)) == 0)
  313. {
  314.  
  315. char *perm=permissionsFct(fullPath);
  316. //printf("%s\n",perm);
  317. if(strcmp(perm,permissions_string)==0)
  318. { printf("%s\n", fullPath);
  319. free(perm);}
  320.  
  321. }
  322.  
  323.  
  324. }
  325. else
  326. if(name_starts_with==1 && permissions==0 )// 1 0
  327. {
  328. if (strncmp (entry->d_name,name_starts_with_string,strlen(name_starts_with_string)) == 0)
  329. {
  330. printf("%s\n", fullPath);
  331. }
  332.  
  333. }
  334. else
  335. if(name_starts_with==0 && permissions==1 )// 0 1
  336. {
  337.  
  338. char *perm=permissionsFct(fullPath);
  339. //printf("%s\n",perm);
  340. if(strcmp(perm,permissions_string)==0)
  341. {printf("%s\n", fullPath);
  342. free(perm);}
  343.  
  344. }
  345. else
  346. printf("%s\n", fullPath);//0 0
  347.  
  348.  
  349. if(S_ISDIR(statbuf.st_mode)) {
  350. listRec(fullPath,permissions,permissions_string,name_starts_with,name_starts_with_string);
  351. }
  352. }
  353. }
  354. }
  355. closedir(dir);
  356. }
  357.  
  358.  
  359.  
  360.  
  361.  
  362. int parseFile(const char *path)
  363. {
  364. int fd=-1;
  365.  
  366.  
  367. char magic;
  368. //char c;
  369. //int ok=1;
  370.  
  371.  
  372. fd= open(path, O_RDONLY);
  373. if(fd == -1) {
  374. perror("Could not open input file\n");
  375. return 0;
  376. }
  377.  
  378. ////////magic
  379. lseek(fd,-1,SEEK_END);
  380. read(fd,&magic,1);
  381. if(magic!='D')
  382. {
  383. printf("ERROR\n wrong magic\n");
  384. return 0;
  385. }
  386.  
  387. ////////////headerSize
  388. short int headerSize=0;
  389. lseek(fd,-3,SEEK_END);
  390. read(fd,&headerSize,2);
  391. //printf("header size este %d\n",headerSize);
  392.  
  393. ///////version
  394. int version=0;
  395. lseek(fd, -headerSize,SEEK_END);
  396. read(fd,&version,1);
  397.  
  398. if(version>171 || version<118)
  399. {
  400. printf("ERROR\nwrong version\n");
  401. return 0;
  402. }
  403. //printf("Version is %d\n",version);
  404.  
  405. ///////nr of sections
  406. int nrSections=0;
  407. read(fd,&nrSections,1);
  408. //printf("%d\n",nrSections);
  409.  
  410. if(nrSections<7 || nrSections>18)
  411. {
  412. printf("ERROR\nwrong sect_nr\n");
  413. return 0;
  414. }
  415.  
  416. //////sections
  417. char sect_name[12];
  418. int sect_type=0;
  419. int sect_offset=0;
  420. int sect_size=0;
  421.  
  422.  
  423.  
  424. for(int i=0;i<nrSections;i++)
  425. {
  426. read(fd,sect_name,11);
  427. read(fd,&sect_type,2);
  428. if(sect_type!=61 && sect_type!=91 && sect_type!=67 && sect_type!=69)
  429. {
  430. {
  431. printf("ERROR\nwrong sect_types\n");
  432. return 0;
  433. }
  434. }
  435. read(fd,&sect_offset,4);
  436. read(fd,&sect_size,4);
  437. }
  438.  
  439. printf("SUCCESS\n");
  440. printf("version=%d\n",version);
  441. printf("nr_sections=%d\n",nrSections);
  442. lseek(fd,-nrSections*21,SEEK_CUR);
  443. for(int i=0;i<nrSections;i++)
  444. {
  445. read(fd,sect_name,11);
  446. read(fd,&sect_type,2);
  447. read(fd,&sect_offset,4);
  448. read(fd,&sect_size,4);
  449. printf("section%d: %s %d %d\n",i+1,sect_name,sect_type,sect_size);
  450. }
  451.  
  452.  
  453. close(fd);
  454. return 1;
  455.  
  456.  
  457. }
  458.  
  459. void extractFile(const char *extractPath,int sectionNr,int lineNr)
  460. {
  461. int fd=-1;
  462. fd= open(extractPath, O_RDONLY);
  463. if(fd == -1) {
  464. perror("Could not open input file\n");
  465. return;
  466. }
  467. if(parseFile2(extractPath)==0)
  468. {
  469. printf("ERROR\ninvalid file\n");
  470. return;
  471. }
  472. char magic;
  473. lseek(fd,-1,SEEK_END);
  474. read(fd,&magic,1);
  475. short int headerSize=0;
  476. lseek(fd,-3,SEEK_END);
  477. read(fd,&headerSize,2);
  478. int version=0;
  479. lseek(fd, -headerSize,SEEK_END);
  480. read(fd,&version,1);
  481. int nrSections=0;
  482. read(fd,&nrSections,1);
  483.  
  484. char sect_name[12];
  485. int sect_type=0;
  486. int sect_offset=0;
  487. int sect_size=0;
  488.  
  489. if(sectionNr>nrSections)
  490. {
  491. printf("ERROR\ninvalid section\n");
  492. return;
  493. }
  494.  
  495. for(int i=1;i<=sectionNr;i++)
  496. {
  497. read(fd,sect_name,11);
  498. read(fd,&sect_type,2);
  499. read(fd,&sect_offset,4);
  500. read(fd,&sect_size,4);
  501. }
  502. ///am citit datele despre sectiune
  503. lseek(fd,sect_offset,SEEK_SET);//inceputul sectiunii
  504.  
  505.  
  506.  
  507.  
  508. int i=0;
  509. char nr;
  510. char linie[900000];
  511. char inv[900000];
  512. while(read(fd,&nr,1)>0 && i<lineNr-1)//citesc n-1 linii
  513. {
  514. if(nr=='\n')//de fiecare data cand ajung la finalul unei linii incrementez i
  515. i++;
  516. }
  517. i=0;
  518. lseek(fd,-1,SEEK_CUR);//inceput linie;
  519. while(1)
  520. {
  521.  
  522. int nr_caract_citite=read(fd,&linie[i],1);
  523.  
  524. if(nr_caract_citite<0)
  525. {
  526. printf("Nu s-a citit linia\n");
  527. return;
  528. }
  529.  
  530. if(linie[i]=='\n')
  531. break;// ma opresc din citit cand ajung la finalul liniei
  532.  
  533. i++;
  534.  
  535. }
  536. //final linie
  537.  
  538. int k=0;//inversez linia
  539.  
  540.  
  541. if(linie[strlen(linie)-1]!='\n')
  542. linie[strlen(linie)]='\n';
  543. for(int j=strlen(linie)-1;j>=0;j--)
  544. {
  545. inv[k]=linie[j];
  546. k++;
  547. }
  548.  
  549. printf("SUCCESS");
  550. for(int i=0;i<strlen(inv);i++)
  551. printf("%c",inv[i]);
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560. }
  561.  
  562. int main(int argc, char **argv){
  563. int recursive=0;
  564. int name_starts_with=0;
  565. int permissions=0;
  566. char *name_starts_with_string=NULL;
  567. char *permissions_string=NULL;
  568. char *path_string=NULL;
  569. if(argc >= 2)
  570. {
  571. /////varianta
  572. if(strcmp(argv[1], "variant") == 0){
  573. printf("73651\n");
  574. }
  575. /////list
  576.  
  577. if(strcmp(argv[1],"list")==0)
  578. {
  579.  
  580.  
  581. for(int i=2;i<argc;i++)
  582. {
  583.  
  584. if(strcmp(argv[i],"recursive")==0)
  585. recursive=1;////daca e recursiv apelez functia de listare recursiva, altfel listare simpla
  586.  
  587. else
  588. {
  589. if(strstr(argv[i],"name_starts_with=")!=NULL)
  590. {
  591. name_starts_with=1;
  592. ///extragere string
  593. //printf("name argv[%d]=%s\n",i,argv[i]);
  594.  
  595. name_starts_with_string = strtok (argv[i],"=");
  596. name_starts_with_string = strtok (NULL, "=");
  597.  
  598. }
  599.  
  600. else
  601. { if(strstr(argv[i],"permissions=")!=NULL)
  602. {
  603. permissions=1;
  604. ///extragere permisiuni
  605. permissions_string = strtok (argv[i],"=");
  606. permissions_string = strtok (NULL, "=");
  607. }
  608. else
  609. {
  610. if(strstr(argv[i],"path=")!=NULL)
  611. {
  612.  
  613. path_string = strtok (argv[i],"=");
  614. path_string = strtok (NULL, "=");
  615. }
  616. }
  617. }
  618. }
  619.  
  620. }
  621. printf("SUCCESS\n");
  622. if(recursive==1)
  623. listRec(path_string,permissions,permissions_string,name_starts_with,name_starts_with_string);
  624. if(recursive==0)
  625. listDir(path_string,permissions,permissions_string,name_starts_with,name_starts_with_string);
  626.  
  627.  
  628.  
  629. }
  630. /////////parse
  631.  
  632. //int parse=0;
  633. char *parsePath=NULL;
  634. if(strcmp(argv[1], "parse") == 0)
  635. {
  636. //parse=1;
  637. if(strstr(argv[2],"path=")!=NULL)
  638. {
  639. parsePath = strtok (argv[2],"=");
  640. parsePath = strtok (NULL, "=");
  641.  
  642. parseFile(parsePath);
  643. }
  644.  
  645.  
  646. }
  647. /////////////extract
  648. char *extractPath=NULL;
  649. char *section=NULL;
  650. char *line=NULL;
  651. int lineNr=0;
  652. int sectionNr=0;
  653. if(strcmp(argv[1],"extract")==0)
  654. {
  655. for(int i=2;i<argc;i++)
  656. {
  657.  
  658. if(strstr(argv[i],"path=")!=NULL)
  659. {
  660. extractPath = strtok (argv[i],"=");
  661. extractPath = strtok (NULL, "=");
  662. //printf("%s\n",extractPath);
  663. }
  664. else
  665. if(strstr(argv[i],"section=")!=NULL)
  666. {
  667. section = strtok (argv[i],"=");
  668. section = strtok (NULL, "=");
  669. sectionNr=atoi(section);
  670. //printf("%d \n",sectionNr);
  671. }
  672. else
  673. if(strstr(argv[i],"line")!=NULL)
  674. {
  675. line = strtok (argv[i],"=");
  676. line = strtok (NULL, "=");
  677. lineNr=atoi(line);
  678. //printf("%d \n",lineNr);
  679. }
  680. }
  681.  
  682. extractFile(extractPath,sectionNr,lineNr);
  683.  
  684. }
  685. ///////////findall
  686. char *findPath=NULL;
  687. if(strcmp(argv[1],"findall")==0)
  688. {
  689. if(strstr(argv[2],"path=")!=NULL)
  690. {
  691. findPath = strtok (argv[2],"=");
  692. findPath = strtok (NULL, "=");
  693. printf("SUCCESS\n");
  694. findAll(findPath);
  695.  
  696. }
  697. }
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704. }
  705.  
  706. return 0;
  707. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement