Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. /* Разработать программу поиска заданной пользователем комбинации из m байт (m < 25)
  2. во всех файлах текущего каталога. Задается имя каталога. Главный процесс открывает каталог и запускает для каждого
  3. файла каталога отдельный процесс поиска заданной комбинации из m байт. Каждый процесс выводит на экран свой
  4. pid, полный путь к файлу, общее число просмотренных байт и результаты(сколько раз найдена комбинация) поиска.
  5. Число одновремено работающих процессов не должно превышать N (вводится пользователем)ю Проверитть работу программы для каталога /etc
  6. и строки ifconfig.
  7. */
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <dirent.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17.  
  18. #define TRUE 1
  19. #define FALSE 0
  20. #define MAXELEM 100
  21.  
  22. char *EditArgv(char *, char *);
  23. void SearchFile(char *, char *[], FILE *);
  24. char *EditAdress(char *, char *);
  25. void PrintFile(char *, char *, int, int);
  26. void GetRights(mode_t, char *);
  27. int CheckErrors(int, char *[]);
  28. void PrintInf(char *[], int, int);
  29.  
  30. int success = FALSE;
  31. int pr_c = 0;
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35. char *parent_catalog = NULL;
  36. int i = 0, file_counter = 0;
  37. pid_t ppid;
  38. FILE *file_proc = NULL;
  39.  
  40. file_proc = fopen("/home/maxim/file_proc.txt", "w");
  41. fprintf(file_proc, "%d", i);
  42. fclose(file_proc);
  43.  
  44. ppid = getpid();
  45. /*check for errors, if found - exit program*/
  46. if (CheckErrors(argc, argv))
  47. return 1;
  48.  
  49. /*edit parent path(if the last letter == '/' - delete it)*/
  50. parent_catalog = EditArgv(parent_catalog, argv[1]);
  51.  
  52. /*recursive function of searching file. if file wasn`t found - exit*/
  53. SearchFile(parent_catalog, argv, file_proc);
  54.  
  55. return 0;
  56. }
  57.  
  58. void PrintInf(char *argv[], int catalog_counter, int file_counter)
  59. {
  60. if (!success)
  61. fprintf(stderr, "\n%s: Such file not found...", argv[1]);
  62. printf("\nTotal catalogs looked through: %d\n", catalog_counter);
  63. printf("Total files looked through: %d\n\n", file_counter);
  64. }
  65.  
  66. int CheckErrors(int argc, char *argv[])
  67. {
  68. if (argc != 4) {
  69. fprintf(stderr, "%s: Error: Wrong amount of arguments...\n", argv[0]);
  70. return 1;
  71. }
  72. if (opendir(argv[1]) == NULL) {
  73. fprintf(stderr, "%s: Error: No such catalog %s\n", argv[0], argv[1]);
  74. return 1;
  75. }
  76. return 0;
  77. }
  78.  
  79. char *EditArgv(char *parent_catalog, char *argv)
  80. {
  81. if (argv[strlen(argv) -1] != '/' || strlen(argv) == 1) {
  82. parent_catalog = (char *) malloc(strlen(argv) + 1); //1
  83. strcpy(parent_catalog, argv);
  84. }
  85. else {
  86. parent_catalog = (char *) malloc(strlen(argv) + 1); //1
  87. strcpy(parent_catalog, argv);
  88. parent_catalog[strlen(argv) - 1] = '\0';
  89. }
  90.  
  91. return parent_catalog;
  92. }
  93.  
  94. void SearchFile(char *catalog_name, char *argv[], FILE *file_proc)
  95. {
  96. DIR *current_catalog;
  97. struct dirent *current_element;
  98. char *current_way = NULL;
  99. int proc_count = atoi(argv[3]);
  100. int state;
  101. int perem;
  102. pid_t pid;
  103. errno = 0;
  104.  
  105. if(strcmp(catalog_name, "/proc") != 0) {
  106. /*if the directory can be opened*/
  107. if (current_catalog = opendir(catalog_name)) {
  108. while(TRUE) {
  109. current_element = readdir(current_catalog);
  110. if (current_element)
  111. if ((strcmp(current_element->d_name,"..") != 0) && (strcmp(current_element->d_name,".") != 0)) {
  112. /*Add new element`s name to the path*/
  113. current_way = EditAdress(catalog_name, current_element->d_name);
  114. if (current_element->d_type == 4)
  115. SearchFile(current_way, argv, file_proc);
  116. else
  117. if (current_element->d_type == 8) {
  118. state = TRUE;
  119. if (pr_c < proc_count)
  120. pr_c++;
  121. else {
  122. wait();
  123. pr_c--;
  124. }
  125. pr_c++;
  126. //current_way = EditAdress(catalog_name, current_element->d_name);
  127. pid = fork();
  128. if (pid == 0) {
  129. //sleep(1);
  130. printf("%s\n", current_way);
  131. //sleep(10);
  132. exit(0);
  133. }
  134. //current_way = EditAdress(catalog_name, current_element->d_name);
  135. success = TRUE;
  136. }
  137. }
  138. /*if the error, when openenig file, occured - pass it*/
  139. if (errno) {
  140. fprintf(stderr, "%s: %s: %s\n", argv[0], current_way, strerror(errno));
  141. errno = 0;
  142. }
  143. /*leave when reached catalog`s end*/
  144. else
  145. if (current_element == NULL)
  146. break;
  147.  
  148. }
  149. if (closedir(current_catalog) == -1)
  150. fprintf(stderr, "%s: %s: Couldn`t close catalog\n", argv[0], catalog_name);
  151.  
  152. }
  153. else
  154. fprintf(stderr, "%s: %s: Couldn`t open catalog\n", argv[0], catalog_name);
  155. }
  156.  
  157. }
  158.  
  159. char *EditAdress(char *catalog_name, char *file_name)
  160. {
  161. char *current_way = NULL;
  162.  
  163. current_way = (char *) malloc(strlen(catalog_name) + strlen(file_name) + 20);//2
  164. strcpy(current_way, catalog_name);
  165. if(strlen(catalog_name) != 1)
  166. current_way[strlen(catalog_name)] = '/';
  167. current_way[strlen(catalog_name) + 1] = '\0';
  168. strcat(current_way, file_name);
  169.  
  170. return current_way;
  171. }
  172.  
  173. void PrintFile(char *way_to_file, char *name_of_file, int catalog_counter, int file_counter)
  174. {
  175. char dat[5];
  176. struct tm *local = NULL;
  177. struct stat stats_of_file;
  178. char rights[11];
  179.  
  180. printf("\n%s", way_to_file);
  181.  
  182. stat(way_to_file, &stats_of_file);
  183. GetRights(stats_of_file.st_mode, rights);
  184. local = localtime(&stats_of_file.st_ctime);
  185.  
  186. printf(" %d %s", stats_of_file.st_size, rights);
  187. printf(" %d.0%d.%d", local->tm_mday, local->tm_mon, local->tm_year + 1900);
  188. printf(" %d", stats_of_file.st_ino);
  189. }
  190.  
  191. void GetRights(mode_t mode, char *rights)
  192. {
  193. strcpy(rights, "----------");
  194. if (mode & S_IRUSR)
  195. rights[1] = 'r';
  196. if (mode & S_IWUSR)
  197. rights[2] = 'w';
  198. if (mode & S_IXUSR)
  199. rights[3] = 'x';
  200. if (mode & S_IRGRP)
  201. rights[4] = 'r';
  202. if (mode & S_IWGRP)
  203. rights[5] = 'w';
  204. if (mode & S_IXGRP)
  205. rights[6] = 'x';
  206. if (mode & S_IROTH)
  207. rights[7] = 'r';
  208. if (mode & S_IWOTH)
  209. rights[8] = 'w';
  210. if (mode & S_IXOTH)
  211. rights[9] = 'x';
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement