Guest User

Untitled

a guest
Dec 6th, 2019
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5. #include <unistd.h>
  6.  
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9. #include <sys/stat.h>
  10.  
  11. #define DIM_BUFFER 1024
  12. #define P 100
  13.  
  14. typedef enum{
  15. CMD_LIST,
  16. CMD_SIZE,
  17. CMD_SEARCH,
  18. CMD_ERROR,
  19. RPL_DATA,
  20. RPL_DATA_STOP
  21. }cmd;
  22.  
  23. typedef struct{
  24. long dest;
  25. char nome_file[DIM_BUFFER];
  26. char stringa[DIM_BUFFER];
  27. int dim;
  28. cmd comando;
  29. }message;
  30.  
  31. void child(int msg, char *path, int num){
  32. message message;
  33. char buffer[DIM_BUFFER];
  34. int ricorrenze = 0;
  35. struct dirent *entry;
  36. struct stat statbuf;
  37. FILE *file;
  38. DIR *dir;
  39.  
  40. while(1){
  41. msgrcv(msg, &message, sizeof(message) - sizeof(long), num, 0);
  42.  
  43. switch(message.comando){
  44. case CMD_LIST:
  45. chdir(path);
  46. if((dir = opendir(path)) == NULL){
  47. perror("dir");
  48. exit(0);
  49. }
  50. while((entry = readdir(dir)) != NULL){
  51. lstat(entry->d_name, &statbuf);
  52.  
  53. if(S_ISREG(statbuf.st_mode)){
  54. strcpy(message.nome_file, entry->d_name);
  55. message.comando = RPL_DATA;
  56. message.dest = P;
  57. msgsnd(msg, &message, sizeof(message) - sizeof(long), 0);
  58. }
  59. }
  60. message.comando = RPL_DATA_STOP;
  61. message.dest = P;
  62. msgsnd(msg, &message, sizeof(message) - sizeof(long), 0);
  63. break;
  64.  
  65. case CMD_SIZE:
  66. chdir(path);
  67. lstat(message.nome_file, &statbuf);
  68. if(S_ISREG(statbuf.st_mode)){
  69. message.dest = P;
  70. message.dim = statbuf.st_size;
  71. message.comando = RPL_DATA;
  72. strcpy(message.nome_file, path);
  73. msgsnd(msg, &message, sizeof(message) - sizeof(long), 0);
  74. }
  75. break;
  76.  
  77. case CMD_SEARCH:
  78. chdir(path);
  79. if((file = fopen(message.nome_file, "r")) == NULL){
  80. perror("file");
  81. exit(0);
  82. }
  83. while(fgets(buffer, DIM_BUFFER, file)){
  84. if (buffer[strlen(buffer)-1] == '\n')
  85. buffer[strlen(buffer)-1] = '\0';
  86.  
  87. if(strcmp(buffer, message.stringa) == 0){
  88. ricorrenze++;
  89. }
  90. }
  91. message.dest = P;
  92. message.dim = ricorrenze;
  93. message.comando = RPL_DATA;
  94. msgsnd(msg, &message, sizeof(message) - sizeof(long), 0);
  95. break;
  96.  
  97. default:
  98. break;
  99. }
  100. }
  101. }
  102.  
  103. int main(int argc, char *argv[]){
  104. int msg, tok, len;
  105. int quit = 0;
  106. int child_ = 0;
  107. char *token;
  108. char buffer[DIM_BUFFER];
  109. message message;
  110.  
  111. if((msg = msgget(IPC_PRIVATE, IPC_CREAT|0600)) == -1){
  112. perror("msg");
  113. exit(0);
  114. }
  115.  
  116. for(int i = 1; i < argc; i++){
  117. child_++;
  118. if(fork() == 0){
  119. child(msg, argv[i], i);
  120. }
  121. }
  122.  
  123. sleep(1);
  124. do{
  125. printf("file-shell> ");
  126.  
  127. fgets(buffer, DIM_BUFFER, stdin);
  128.  
  129. len = strlen(buffer);
  130. if (buffer[len-1] == '\n')
  131. buffer[len-1] = '\0';
  132.  
  133. token = strtok(buffer, " ");
  134. if(strcmp(token, "list") == 0){
  135. message.comando = CMD_LIST;
  136. token = strtok(NULL, " ");
  137. tok = atoi(token);
  138. if(tok > child_ || tok < 0){
  139. printf("cartella non trovata! \n");
  140. }else{
  141. message.dest = tok;
  142. }
  143. msgsnd(msg, &message, sizeof(message) - sizeof(long), 0);
  144.  
  145. while(1){
  146. msgrcv(msg, &message, sizeof(message) - sizeof(long), P, 0);
  147. if(message.comando == RPL_DATA){
  148. printf("file: %s \n", message.nome_file);
  149. }else if(message.comando == RPL_DATA_STOP){
  150. break;
  151. }
  152. }
  153.  
  154. }else if(strcmp(token, "size") == 0){
  155. message.comando = CMD_SIZE;
  156. token = strtok(NULL, " ");
  157. tok = atoi(token);
  158. if(tok > child_ || tok < 0){
  159. printf("cartella non trovata! \n");
  160. }else{
  161. message.dest = tok;
  162. token = strtok(NULL, " ");
  163. strcpy(message.nome_file, token);
  164. }
  165. msgsnd(msg, &message, sizeof(message) - sizeof(long), 0);
  166.  
  167. while(1){
  168. msgrcv(msg, &message, sizeof(message) - sizeof(long), P, 0);
  169. if(message.comando == RPL_DATA){
  170. printf("dimensione %s: %d \n", message.nome_file, message.dim);
  171. break;
  172. }
  173. }
  174.  
  175. }else if(strcmp(token, "search") == 0){
  176. message.comando = CMD_SEARCH;
  177. token = strtok(NULL, " ");
  178. tok = atoi(token);
  179. if(tok > child_ || tok < 0){
  180. printf("cartella non trovata! \n");
  181. }else{
  182. message.dest = tok;
  183. token = strtok(NULL, " ");
  184. strcpy(message.nome_file, token);
  185. token = strtok(NULL, " ");
  186. strcpy(message.stringa, token);
  187. }
  188. msgsnd(msg, &message, sizeof(message) - sizeof(long), 0);
  189.  
  190. while(1){
  191. msgrcv(msg, &message, sizeof(message) - sizeof(long), P, 0);
  192. if(message.comando == RPL_DATA){
  193. printf("ricorrenze: %d \n", message.dim);
  194. break;
  195. }
  196. }
  197.  
  198. }else if(strcmp(token, "quit") == 0){
  199. quit = 1;
  200. }else{
  201. printf("comando errato! \n");
  202. }
  203.  
  204. }while(quit == 0);
  205.  
  206. exit(0);
  207. }
RAW Paste Data