Advertisement
sorinavancea

60p

Mar 27th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 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. void parse(char *path){
  109. char magic = "";
  110. unsigned short header = 0;
  111. int version = 0;
  112. unsigned short no_of_section = 0;
  113. char name[13];
  114. unsigned short type = 0;
  115. int offset = 0;
  116. int size = 0;
  117. int fd = -1;
  118.  
  119. fd = open(path, O_RDONLY);
  120. if(fd == -1){
  121. perror("Could not open input file!");
  122. return -1;
  123. }
  124. lseek(fd,-3,SEEK_SET);
  125. read(fd, &size, 2);
  126. read(fd, &magic, 1);
  127.  
  128. if (magic != 'S')
  129. {
  130. printf("ERROR\nwrong magic\n");
  131. return;
  132. }
  133.  
  134. lseek(fd, -size, SEEK_CUR);
  135.  
  136. read(fd, &version, 2);
  137. if (version<126 || version>209)
  138. {
  139.  
  140. printf("ERROR\nwrong version\n");
  141. return;
  142. }
  143. }
  144. */
  145.  
  146. int parse(const char *path, int afiseaza)
  147. {
  148. int i=0,nr=0,size=0,version=0,fd=-1,ok=1,er=0;//1- magic,2-version 3-sect_nr 4-sect_type
  149. char magic[2]={0};
  150. char name[13]={0};
  151. int type=0;
  152. int offset=0;
  153. fd=open(path,O_RDONLY);
  154.  
  155. if(fd==-1)
  156. {
  157. perror("Could not open input file!");
  158. return -1;
  159. }
  160.  
  161. lseek(fd,0,SEEK_SET);
  162. if(read(fd,magic,1)==1)
  163. {
  164. //magic[0]=0;
  165. //printf("%d\n", strcmp(magic,"S"));
  166. if(strcmp(magic,"S")!=0)
  167. {
  168. ok=0;
  169. er=1;
  170. }
  171. }
  172. else {
  173. perror("Could not read from input file");
  174. close(fd);
  175. return -1;
  176. }
  177. if(read(fd,&size,2)!=2)
  178. {
  179. perror("Could not read from input file");
  180. close(fd);
  181. return -1;
  182. }
  183. if(read(fd,&version,2)==2)
  184. {
  185. if(version<126 || version >209)
  186. {
  187. if(ok){
  188. er=2;
  189. ok=0;
  190. }
  191. }
  192. }
  193. else {
  194. perror("Could not read from input file");
  195. close(fd);
  196. return -1;
  197. }
  198. if(read(fd,&nr,1)==1)
  199. {
  200. if(nr<3 || nr>10)
  201. {
  202. if(ok){
  203. er=3;
  204. ok=0;
  205. }
  206. }
  207. }
  208. else {
  209. perror("Could not read from input file");
  210. close(fd);
  211. return -1;
  212. }
  213. for(i=0;i<nr;i++)
  214. {
  215. //if(i==0)lseek(fd,9,SEEK_CUR);
  216. //else lseek(fd,17,SEEK_CUR);
  217.  
  218. if(read(fd,&type,2)!=2)
  219. {
  220. perror("Could not read from input file");
  221. close(fd);
  222. return -1;
  223. }
  224. else
  225. {
  226. //printf("%d \n",type);
  227. if(type!=50 && type!=81)
  228. {
  229. {
  230. if(ok){
  231. er=4;
  232. ok=0;
  233. break;
  234. }
  235. }
  236. }
  237. }
  238.  
  239. }
  240. if(er)
  241. { if(afiseaza){
  242. switch(er){
  243. case 1: printf("\nERROR\nwrong magic"); break;
  244. case 2: printf("\nERROR\nwrong version\n"); break;
  245. case 3: printf("ERROR\nwrong sect_nr\n");break;
  246. default: printf("ERROR\nwrong sect_types");
  247. }
  248. }
  249. close(fd);
  250. return -1;
  251. }
  252. else {
  253. if(afiseaza==1)
  254. {printf("SUCCESS\nversion=%d\nnr_sections=%d\n",version,nr);
  255. lseek(fd,11,SEEK_SET);
  256. for(i=0;i<nr;i++)
  257. {
  258. if(read(fd,name,12)!=12)
  259. {
  260. perror("Could not read from input file");
  261. close(fd);
  262. return -1;
  263. }
  264. name[12]=0;
  265. if(read(fd,&type,2)!=2)
  266. {
  267. perror("Could not read from input file");
  268. close(fd);
  269. return -1;
  270. }
  271. if(read(fd,&offset,4)!=4)
  272. {
  273. perror("Could not read from input file");
  274. close(fd);
  275. return -1;
  276. }
  277. if(read(fd,&size,4)!=4)
  278. {
  279. perror("Could not read from input file");
  280. close(fd);
  281. return -1;
  282. }
  283. printf("section%d: %s %d %d\n",i+1,name,type, size);
  284. }
  285. }
  286. close(fd);
  287. return 1;
  288. }
  289. }
  290.  
  291. int main(int argc, char **argv){
  292. int list = 0, recursive = 0;
  293. int ok_name_ends = 0;
  294. char path[200];
  295. int parse1 = 0;
  296. char *name_ends_with = NULL;
  297. int size = -1;
  298. int i = 1;
  299.  
  300. if(argc >= 2){
  301. if(strcmp(argv[1], "variant") == 0){
  302. printf("95965\n");
  303. }
  304. else {
  305. while(i < argc){
  306. if(strcmp(argv[i],"parse") == 0){
  307. parse1 = 1;
  308.  
  309. }
  310. else if(strcmp(argv[i],"list") == 0){
  311. list = 1;
  312. }
  313. else if(strcmp(argv[i], "recursive") == 0){
  314. recursive = 1;
  315. }
  316. else if(strstr(argv[i],"path")){
  317. strcpy(path,strstr(argv[i],"="));
  318. strcpy(path,path+1);
  319. }
  320. else if(strncmp(argv[i], "size_greater=", 13) == 0) {
  321. char *nr_str = argv[i] + 13;
  322. size = atoi(nr_str);
  323. }
  324. else if(strncmp(argv[i],"name_ends_with", 15) != 0){
  325. ok_name_ends = 1;
  326. name_ends_with = argv[i] + 15;
  327. }
  328.  
  329. i++;
  330. }
  331.  
  332. if(list == 1){
  333. if(recursive == 1){
  334. listRecursivitate( path, size, ok_name_ends, name_ends_with);
  335. }
  336. else{
  337. listRec(path, size, ok_name_ends, name_ends_with);
  338. }
  339. }
  340. else if(parse1 == 0){
  341. listRec(path, size, ok_name_ends, name_ends_with);
  342.  
  343. }
  344.  
  345. if(parse1 == 1){
  346. parse(path, 1);
  347.  
  348. }
  349.  
  350. }
  351. }
  352. return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement