Advertisement
sorinavancea

tema1?

Mar 28th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 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.  
  119. fd=open(path,O_RDONLY);
  120.  
  121. if(fd==-1){
  122. perror("Could not open input file!");
  123. return -1;
  124. }
  125.  
  126. lseek(fd,0,SEEK_SET);
  127. read(fd, &magic, 1);
  128.  
  129. if(magic != 'S'){
  130. ok=0;
  131. printf("\nERROR\nwrong magic");
  132. return -1;
  133. }
  134.  
  135. read(fd,&size,2);
  136. read(fd,&version,2);
  137.  
  138. if(version<126 || version >209){
  139. if(ok){
  140. ok=0;
  141. printf("\nERROR\nwrong version\n");
  142. return -1;
  143. }
  144. }
  145. read(fd,&no_of_sections,1);
  146.  
  147. if(no_of_sections<3 || no_of_sections>10){
  148. if(ok){
  149. ok=0;
  150. printf("ERROR\nwrong sect_nr\n");
  151. return -1;
  152. }
  153. }
  154.  
  155. for(int i=0;i<no_of_sections;i++){
  156. read(fd,&type,2);
  157.  
  158. //printf("%d \n",type);
  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, 11, SEEK_SET);
  172. for(int i=0; i<no_of_sections; i++){
  173. printf("section%d: %s %d %d\n", i+1, section_header, type, size);
  174. }
  175. }
  176. }
  177. close(fd);
  178. return 1;
  179. }
  180.  
  181. int main(int argc, char **argv){
  182. int list = 0, recursive = 0;
  183. int ok_name_ends = 0;
  184. char path[200];
  185. int parse1 = 0;
  186. char *name_ends_with = NULL;
  187. int size = -1;
  188. int i = 1;
  189.  
  190. if(argc >= 2){
  191. if(strcmp(argv[1], "variant") == 0){
  192. printf("95965\n");
  193. }
  194. else {
  195. while(i < argc){
  196. if(strcmp(argv[i],"parse") == 0){
  197. parse1 = 1;
  198. }
  199. else if(strcmp(argv[i],"list") == 0){
  200. list = 1;
  201. }
  202. else if(strcmp(argv[i], "recursive") == 0){
  203. recursive = 1;
  204. }
  205. else if(strstr(argv[i],"path")){
  206. strcpy(path,strstr(argv[i],"="));
  207. strcpy(path,path+1);
  208. }
  209. else if(strncmp(argv[i], "size_greater=", 13) == 0) {
  210. char *nr_str = argv[i] + 13;
  211. size = atoi(nr_str);
  212. }
  213. else if(strncmp(argv[i],"name_ends_with", 15) != 0){
  214. ok_name_ends = 1;
  215. name_ends_with = argv[i] + 15;
  216. }
  217.  
  218. i++;
  219. }
  220.  
  221. if(list == 1){
  222. if(recursive == 1){
  223. listRecursivitate( path, size, ok_name_ends, name_ends_with);
  224. }
  225. else{
  226. listRec(path, size, ok_name_ends, name_ends_with);
  227. }
  228. }
  229. else if(parse1 == 0){
  230. listRecursivitate(path, size, ok_name_ends, name_ends_with);
  231.  
  232. }
  233.  
  234. if(parse1 == 1){
  235. parse(path, 1);
  236.  
  237. }
  238.  
  239. }
  240. }
  241. return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement