Advertisement
sorinavancea

incercare1

Mar 28th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <sys/stat.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9.  
  10. int first_test = 1;
  11.  
  12. void listRec(const char *path, int size, int ok_name_ends, const char* name_ends_with)
  13. {
  14. DIR *dir = NULL;
  15. struct dirent *entry = NULL;
  16. char fullPath[512];
  17. struct stat statbuf;
  18. dir = opendir(path);
  19.  
  20. if(dir == NULL) {
  21. printf("ERROR\n");
  22. perror("invalid directory path");
  23. return ;
  24. }
  25.  
  26. printf("SUCCESS\n");
  27.  
  28. while((entry = readdir(dir)) != NULL) {
  29. if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
  30. snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
  31. if(lstat(fullPath, &statbuf) == 0) {
  32. if(ok_name_ends==1){
  33. if(strstr(entry->d_name,name_ends_with))
  34. {
  35. char aux[500] = "";
  36. strcpy(aux,entry->d_name + 17);
  37. char aux1[500] = "";
  38. strcpy(aux1,entry->d_name + 5);
  39. printf("%s\n", fullPath);
  40. }
  41. }
  42. else{
  43. if(size == -1){
  44. printf("%s\n", fullPath);
  45. }
  46. else if(statbuf.st_size > size && S_ISREG(statbuf.st_mode)){
  47. printf("%s\n", fullPath);
  48.  
  49. }
  50. }
  51.  
  52. }
  53. }
  54. }
  55. closedir(dir);
  56. }
  57.  
  58. void listRecursivitate( const char *path, int size, int ok_name_ends, const char* name_ends_with)
  59. {
  60. DIR *dir = NULL;
  61. struct dirent *entry = NULL;
  62. char fullPath[512];
  63. struct stat statbuf;
  64.  
  65. dir = opendir(path);
  66. if(dir == NULL) {
  67. printf("ERROR\n");
  68. perror("invalid directory path");
  69. return ;
  70. }
  71. if(first_test){
  72. printf("SUCCESS\n");
  73. }
  74. while((entry = readdir(dir)) != NULL) {
  75.  
  76. if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
  77. snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
  78. if(lstat(fullPath, &statbuf) == 0) {
  79. first_test = 0;
  80.  
  81. if(ok_name_ends == 1){
  82. if(strstr(entry->d_name,name_ends_with))
  83. {
  84. char aux[500] = "";
  85. strcpy(aux,entry->d_name + 15);
  86. char aux1[500] = "";
  87. strcpy(aux1,entry->d_name + 5);
  88. printf("%s\n", fullPath);
  89. }
  90. }
  91. else{
  92. if(size == -1){
  93. printf("%s\n", fullPath);
  94. }
  95. else if(statbuf.st_size > size && S_ISREG(statbuf.st_mode)){
  96. printf("%s\n", fullPath);
  97. }
  98. if(S_ISDIR(statbuf.st_mode)) {
  99. listRecursivitate(fullPath,size, ok_name_ends, name_ends_with);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. closedir(dir);
  106. }
  107.  
  108.  
  109.  
  110.  
  111. int parse(const char *path, int afiseaza)
  112. {
  113. int size=0,version=0,fd=-1,ok=1;
  114. unsigned short no_of_sections = 0;
  115. char magic;
  116. char section_header[13]={0};
  117. //unsigned short type=0;
  118. //char *type=(char*)malloc(2*sizeof(char)+1);
  119. int type = 0;
  120. fd=open(path,O_RDONLY);
  121.  
  122. if(fd==-1){
  123. perror("Could not open input file!");
  124. return -1;
  125. }
  126.  
  127. lseek(fd,0,SEEK_SET);
  128. read(fd, &magic, 1);
  129.  
  130. if(magic != 'S'){
  131. ok=0;
  132. printf("\nERROR\nwrong magic");
  133. return -1;
  134. }
  135.  
  136. read(fd,&size,2);
  137. read(fd,&version,2);
  138.  
  139. if(version<126 || version >209){
  140. if(ok){
  141. ok=0;
  142. printf("\nERROR\nwrong version\n");
  143. return -1;
  144. }
  145. }
  146. read(fd,&no_of_sections,1);
  147.  
  148. if(no_of_sections<3 || no_of_sections>10){
  149. if(ok){
  150. ok=0;
  151. printf("ERROR\nwrong sect_nr\n");
  152. return -1;
  153. }
  154. }
  155.  
  156. for(int i = 0; i < no_of_sections; i++){
  157. read(fd,&type,2);
  158.  
  159. if(type != 50 && type != 81){
  160. if(ok){
  161. ok=0;
  162. printf("ERROR\nwrong sect_types\n");
  163. return -1;
  164. }
  165. }
  166. }
  167.  
  168. if(ok == 1){
  169. if(afiseaza == 1){
  170. printf("SUCCESS\n version=%d\n nr_sections=%d\n", version,no_of_sections);
  171. lseek(fd, 14, SEEK_SET);
  172.  
  173. for(int i=0; i<no_of_sections; i++){
  174. read(fd,section_header, 12);
  175. section_header[12]=0;
  176. printf("section%d: %s %d %d\n", i+1, section_header, type, size);
  177. }
  178. }
  179. }
  180. close(fd);
  181. return 1;
  182. }
  183.  
  184. int main(int argc, char **argv){
  185. int list = 0, recursive = 0;
  186. int ok_name_ends = 0;
  187. char path[200];
  188. int parse1 = 0;
  189. char *name_ends_with = NULL;
  190. int size = -1;
  191. int i = 1;
  192.  
  193. if(argc >= 2){
  194. if(strcmp(argv[1], "variant") == 0){
  195. printf("95965\n");
  196. }
  197. else {
  198. while(i < argc){
  199. if(strcmp(argv[i],"parse") == 0){
  200. parse1 = 1;
  201. }
  202. else if(strcmp(argv[i],"list") == 0){
  203. list = 1;
  204. }
  205. else if(strcmp(argv[i], "recursive") == 0){
  206. recursive = 1;
  207. }
  208. else if(strstr(argv[i],"path")){
  209. strcpy(path,strstr(argv[i],"="));
  210. strcpy(path,path+1);
  211. }
  212. else if(strncmp(argv[i], "size_greater=", 13) == 0) {
  213. char *nr_str = argv[i] + 13;
  214. size = atoi(nr_str);
  215. }
  216. else if(strncmp(argv[i],"name_ends_with", 15) != 0){
  217. ok_name_ends = 1;
  218. name_ends_with = argv[i] + 15;
  219. }
  220.  
  221. i++;
  222. }
  223.  
  224. if(list == 1){
  225. if(recursive == 1){
  226. listRecursivitate( path, size, ok_name_ends, name_ends_with);
  227. }
  228. else{
  229. listRec(path, size, ok_name_ends, name_ends_with);
  230. }
  231. }
  232. else if(parse1 == 0){
  233. listRecursivitate(path, size, ok_name_ends, name_ends_with);
  234.  
  235. }
  236.  
  237. if(parse1 == 1){
  238. parse(path, 1);
  239.  
  240. }
  241.  
  242. }
  243. }
  244. return 0;
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement