Advertisement
weeez

bead2 - updated kész

Dec 5th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.27 KB | None | 0 0
  1. #include <sys/ipc.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h> // for open()
  5. #include <errno.h> // for perror()
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/msg.h>
  9. #include <sys/wait.h> //waitpid
  10. #include <string.h> //for strcpy()
  11. #include <unistd.h> //for file write() / fork() /pipe()
  12. #include <time.h>
  13. #include <wait.h>
  14.  
  15. struct Kivansag {
  16.   char date[100];
  17.   char name[100];
  18.   char city[100];
  19.   char address[100];
  20.   char gift[100];
  21. };
  22.  
  23. struct uzenet {
  24.     long mtype;
  25.     char mtext [1024];
  26. };
  27.  
  28. int sending( int uzenetsor, char* to_parent )
  29. {
  30.      struct uzenet uz;
  31.      uz.mtype = 5;
  32.      int i;
  33.      for(i = 0; i < 1024; i++){
  34.          uz.mtext[i] = to_parent[i];
  35.      }
  36.      int status;
  37.      
  38.      status = msgsnd( uzenetsor, &uz, strlen ( uz.mtext ) + 1 , 0 );
  39.     // a 3. param ilyen is lehet: sizeof(uz.mtext)
  40.         // a 4. parameter gyakran IPC_NOWAIT, ez a 0-val azonos
  41.      if ( status < 0 )
  42.           perror("msgsnd");
  43.      return 0;
  44. }
  45.  
  46. int receiving( int uzenetsor)
  47. {
  48.      struct uzenet uz;
  49.      int status;
  50.      // az utolso parameter(0) az uzenet azonositoszama
  51.     // ha az 0, akkor a sor elso uzenetet vesszuk ki
  52.     // ha >0 (5), akkor az 5-os uzenetekbol a kovetkezot
  53.     // vesszuk ki a sorbol
  54.      status = msgrcv(uzenetsor, &uz, 1024, 5, 0 );  //5 helyett 0, akkor minden üzenetet olvashat
  55.      
  56.      if ( status < 0 )
  57.           perror("msgsnd");
  58.      else
  59.           printf( "A manók üzenete:  %s\n", uz.mtext );
  60.      return 0;
  61. }
  62.  
  63. const char *FILE_NAME = "kivansagok.scv";
  64. FILE *fileContents;
  65. int list_size = 0;
  66. struct Kivansag gifts[1000];  
  67.  
  68. struct Kivansag rowToKivansag(char row[1000]);
  69. char *kivansagToRow(struct Kivansag kivansag);
  70. char *swapSpace(char *row);
  71. int isCorrect(char answer[10]);
  72. void menu_print();
  73. void cutEnter(char string[100]);
  74. struct Kivansag getEmptyKivansag();
  75. int isEmptyKivansag(struct Kivansag kivansag);
  76.  
  77. int main(int argc, char* argv[]){
  78.        
  79.   char msg[200000];
  80.   //char msg2[200000];
  81.  
  82.   key_t key;
  83.   int uzenetsor, status;
  84.   key = ftok(argv[0],1);
  85.   uzenetsor = msgget ( key, 0600 | IPC_CREAT);
  86.      if ( uzenetsor < 0 ) {
  87.           perror("msgget");
  88.           return 1;
  89.      }
  90.  
  91.     if((fileContents= fopen(FILE_NAME, "rb")) == NULL){
  92.         if(errno == ENOENT){//if "kivansagok" file doesn't exists
  93.             printf("A Kivansagok lista fajlja nem letezik! \tKilepes...\n");
  94.             exit(1);
  95.         }
  96.     }else{
  97.         //file read start
  98.         fscanf(fileContents,"%d\n",&list_size); //init list_size
  99.        
  100.         if(list_size > 0){
  101.             int i; 
  102.                
  103.             for(i = 0; i < list_size; i++){ // init gifts
  104.                 struct Kivansag kivansag;
  105.                 char row[1000];
  106.                 fscanf(fileContents,"%s",row);
  107.                 kivansag = rowToKivansag(row);
  108.                 gifts[i] = kivansag;
  109.             }
  110.  
  111.             printf("TESZT*****************\n");
  112.             for(i = 0; i < list_size; i++){ // init gifts
  113.                 printf("Datum: %s Nev: %s Varos: %s Pontos cim: %s Ajandek: %s\n",gifts[i].date,gifts[i].name,gifts[i].city,gifts[i].address,gifts[i].gift);               
  114.             }
  115.             printf("***********************\n");
  116.  
  117.         }
  118.         fclose(fileContents);  
  119.         //file read ending
  120.                    
  121.        
  122.         int pipe_santa_to_child[2];
  123.         int pipe_child_to_santa[2];
  124.         int pipeee[2];
  125.        
  126.         if (pipe(pipe_santa_to_child) == -1 || pipe(pipe_child_to_santa) == -1 || pipe(pipeee) == -1) {
  127.             perror("Hiba a pipe nyitaskor!");
  128.             exit(EXIT_FAILURE);
  129.         }
  130.        
  131.         pid_t child;
  132.         child = fork();
  133.        
  134.                
  135.         if (child<0){
  136.             perror("The fork calling was not succesful\n");
  137.             exit(1);
  138.         }
  139.         else if(child > 0){ //santa session
  140.             printf("Mikulas folyamat indul...\n");
  141.            
  142.             close(pipe_santa_to_child[0]);
  143.            
  144.             char answer[10];
  145.             int check = 0;
  146.             do{  
  147.                 menu_print();
  148.                 fgets(answer,10, stdin);
  149.  
  150.                 check = isCorrect(answer);
  151.  
  152.                 if(check < 1 || check > 5){
  153.                     printf("\nHibas adatbevitel!\n");
  154.                 }else{   
  155.                     if(check == 1){        
  156.                         char city[100];
  157.                         city[0] = '\0';                
  158.                         char solution[100];
  159.                         int solInt = -1;
  160.                         int city_counter = 0;
  161.                         char citys[1000][1000];
  162.                         citys[city_counter][1000] = '\0';
  163.                         int j = -1;
  164.                         do{
  165.                             printf("\n");
  166.                             printf("Valaszon az alabbi varosok kozul: \n");                
  167.                             for(j = 0; j < list_size; j++){
  168.                                 if(city_counter > 0){
  169.                                     int k;
  170.                                     int hadBeen = 0;
  171.                                     for(k = 0; k < city_counter; ++k){
  172.                                         if(strcmp(citys[k],gifts[j].city) == 0)
  173.                                         {
  174.                                             hadBeen = 1;
  175.                                         }                          
  176.                                     }
  177.                                     if(hadBeen == 0){
  178.                                         strcat(citys[city_counter],gifts[j].city);
  179.                                         ++city_counter;
  180.                                     }
  181.                                 }else{
  182.                                     strcat(citys[city_counter],gifts[j].city);
  183.                                     ++city_counter;
  184.                                 }
  185.                             }              
  186.                             for(j = 0; j < city_counter; ++j){
  187.                                 printf("    (%d) - %s \n", j, citys[j]);
  188.                             }
  189.                             printf("    (%d) - Kilepes \n", j);
  190.                             fgets(solution,100,stdin);
  191.                             cutEnter(solution);
  192.                             solInt = isCorrect(solution);                  
  193.                             if(solInt < j+1 && solInt >= 0){
  194.                                 strcat(city,citys[solInt]);
  195.                                 solInt = j;
  196.                             }else{
  197.                                 printf("\n Hibas parameter! \n");
  198.                             }
  199.                         }while(solInt != j);
  200.                                            
  201.                        
  202.                         char toImp[100];
  203.                         toImp[0] = '\0';
  204.                         strcat(toImp,"1");
  205.                         strcat(toImp,",");
  206.                         strcat(toImp, city);
  207.  
  208.                         write(pipe_santa_to_child[1], toImp ,18);   //!!!                  
  209.                         check = 3;
  210.                     }
  211.                     if(check == 2){
  212.                         char fromListGift[100];
  213.                         fromListGift[0] = '\0';                
  214.                         char solution[100];
  215.                         int solInt = -1;
  216.                         int gift_counter = 0;
  217.                         char listgifts[1000][1000];
  218.                         listgifts[gift_counter][1000] = '\0';
  219.                         int j = -1;
  220.                         do{
  221.                             printf("\n");
  222.                             printf("Valaszon az alabbi ajandekok kozul: \n");                  
  223.                             for(j = 0; j < list_size; j++){
  224.                                 if(gift_counter > 0){
  225.                                     int k;
  226.                                     int hadBeen = 0;
  227.                                     for(k = 0; k < gift_counter; ++k){
  228.                                         if(strcmp(listgifts[k],gifts[j].gift) == 0)
  229.                                         {
  230.                                             hadBeen = 1;
  231.                                         }                          
  232.                                     }
  233.                                     if(hadBeen == 0){
  234.                                         strcat(listgifts[gift_counter],gifts[j].gift);
  235.                                         ++gift_counter;
  236.                                     }
  237.                                 }else{
  238.                                     strcat(listgifts[gift_counter],gifts[j].gift);
  239.                                     ++gift_counter;
  240.                                 }
  241.                             }              
  242.                             for(j = 0; j < gift_counter; ++j){
  243.                                 printf("    (%d) - %s \n", j, listgifts[j]);
  244.                             }
  245.                             printf("    (%d) - Kilepes \n", j);
  246.                             fgets(solution,100,stdin);
  247.                             cutEnter(solution);
  248.                             solInt = isCorrect(solution);                  
  249.                             if(solInt < j+1 && solInt >= 0){
  250.                                 strcat(fromListGift,listgifts[solInt]);
  251.                                 solInt = j;
  252.                             }else{
  253.                                 printf("\n Hibas parameter! \n");
  254.                             }
  255.                         }while(solInt != j);                                           
  256.                        
  257.                         char toImp[100];
  258.                         toImp[0] = '\0';
  259.                         strcat(toImp,"2");
  260.                         strcat(toImp,",");
  261.                         strcat(toImp, fromListGift);
  262.  
  263.                         write(pipe_santa_to_child[1], toImp ,18);   //!!!                  
  264.                         check = 3;
  265.                     }
  266.                     if(check == 3){
  267.                         write(pipe_santa_to_child[1], "3",2);                      
  268.                     }
  269.  
  270.                 }
  271.             }
  272.             while(check != 3);                                   
  273.                                
  274.             close(pipe_santa_to_child[1]);
  275.            
  276.             int valasz = receiving( uzenetsor);
  277.             if(valasz < 0){        
  278.                 perror("fogadas hiba\n");
  279.             }
  280.             fflush(NULL);
  281.             waitpid(child, &status, 0);
  282.                    
  283.            
  284.  
  285.             //close(pipe_child_to_santa[1]);
  286.             //read(pipe_child_to_santa[0], msg2, sizeof(msg2));
  287.             //printf("Ezt kuldtek a manok: %s \n", msg2 );
  288.             //close(pipe_child_to_santa[0]);
  289.                                
  290.             printf("Mikulas folyamat befejezodik...\n");
  291.        
  292.         }
  293.         else if(child == 0){ // elf session                
  294.             printf("Mano folyamat indul...\n");
  295.            
  296.             close(pipe_santa_to_child[1]);
  297.             read(pipe_santa_to_child[0], msg, sizeof(msg));
  298.             if(msg[0] == '3'){
  299.                 msg[0] = '\0';
  300.             }          
  301.            
  302.             printf("Ezt kuldte a mikulas: %s \n", msg);
  303.             close(pipe_santa_to_child[0]);
  304.            
  305.             char to_child[200][200];
  306.             to_child[0][200] = '\0';
  307.             //char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE];           
  308.             char sublogs[1000][1000];
  309.             int log_counter = 0;           
  310.             sublogs[log_counter][1000] = '\0';
  311.            
  312.            
  313.             if(msg[0] == '1'){
  314.                 int l = 2;     
  315.                 int j = 0;             
  316.                 char city[100];
  317.                 city[0] = '\0';
  318.                 while( l < strlen(msg)){                   
  319.                     city[j] = msg[l];
  320.                     ++l;               
  321.                     ++j;                   
  322.                     city[j] = '\0';
  323.                 }              
  324.  
  325.  
  326.                 char subgift[100];             
  327.                 //char *oldRows;                       
  328.                 int sub_size = list_size;
  329.                
  330.                 /*printf("Adja meg a varost nevet, ahova az ajandekokat szallitjuk!\n");
  331.                 fgets(city,100,stdin);
  332.                 cutEnter(city);*/
  333.                
  334.  
  335.                
  336.                
  337.                 int i;
  338.                 for(i = 0; i < list_size; i++){
  339.                     if(strcmp(city, gifts[i].city)==0){                            
  340.                         subgift[0] = '\0';
  341.                         strcat(subgift,gifts[i].gift); 
  342.                         strcat(sublogs[log_counter],subgift);                      
  343.                         ++log_counter;
  344.                         gifts[i] = getEmptyKivansag();                             
  345.                         --sub_size;
  346.                     }
  347.                 }
  348.                
  349.                 list_size = sub_size;
  350.                 /*
  351.                 fileContents = fopen(FILE_NAME,"wb");      
  352.                 fprintf(fileContents, "%d\n",list_size);
  353.                 for(i = 0; i < list_size; i++){
  354.                     if(isEmptyKivansag(gifts[i]) == 0){
  355.                         oldRows = kivansagToRow(gifts[i]);
  356.                         oldRows = swapSpace(oldRows);
  357.                         fprintf(fileContents, "%s",oldRows);   
  358.                     }
  359.                 }
  360.                 fclose(fileContents);    
  361.                 */
  362.                 to_child[0][200] = '\0';
  363.                 if(log_counter == 1){
  364.                     strcat(to_child[0],"\n");  
  365.                     strcat(to_child[0],"Ebbe a varosba erkezett meg az osszes ajandek: "); 
  366.                     strcat(to_child[0],city);
  367.                     strcat(to_child[0],",");
  368.                     strcat(to_child[0]," ezt az ajandekot kaptak: ");
  369.                     strcat(to_child[0], sublogs[log_counter-1]);               
  370.                     strcat(to_child[0],"\n");  
  371.                    
  372.                 }else if(log_counter > 1){
  373.                     int i;
  374.                     for(i = 0; i < log_counter; ++i){
  375.                         strcat(to_child[i],"\n");
  376.                         strcat(to_child[i],"Ebbe a varosba erkezett meg az osszes ajandek: "); 
  377.                         strcat(to_child[i],city);
  378.                         strcat(to_child[i],",");
  379.                         strcat(to_child[i]," ezt az ajandekot kaptak: ");
  380.                         strcat(to_child[i],sublogs[i]);                                    
  381.                     }
  382.                     strcat(to_child[i],"\n");
  383.                 }else if(log_counter < 1){                                 
  384.                     strcat(to_child[0],"Nem volt ilyen varos");                
  385.                     strcat(to_child[0],"\n");      
  386.                 }              
  387.                
  388.             }
  389.             if(msg[0] == '2'){
  390.                 int l = 2;     
  391.                 int j = 0;             
  392.                 char fromListGift[100];
  393.                 fromListGift[0] = '\0';
  394.                 while( l < strlen(msg)){                   
  395.                     fromListGift[j] = msg[l];
  396.                     ++l;               
  397.                     ++j;                   
  398.                     fromListGift[j] = '\0';
  399.                 }              
  400.  
  401.  
  402.                 char subCity[100];             
  403.                 //char *oldRows;                       
  404.                 int sub_size = list_size;
  405.                
  406.                 /*printf("Adja meg a varost nevet, ahova az ajandekokat szallitjuk!\n");
  407.                 fgets(city,100,stdin);
  408.                 cutEnter(city);*/
  409.                
  410.  
  411.                
  412.                
  413.                 int i;
  414.                 for(i = 0; i < list_size; i++){
  415.                     if(strcmp(fromListGift, gifts[i].gift)==0){                            
  416.                         subCity[0] = '\0';
  417.                         strcat(subCity,gifts[i].city); 
  418.                         strcat(sublogs[log_counter],subCity);                      
  419.                         ++log_counter;
  420.                         gifts[i] = getEmptyKivansag();                             
  421.                         --sub_size;
  422.                     }
  423.                 }
  424.                
  425.                 list_size = sub_size;
  426.                 /*
  427.                 fileContents = fopen(FILE_NAME,"wb");      
  428.                 fprintf(fileContents, "%d\n",list_size);
  429.                 for(i = 0; i < list_size; i++){
  430.                     if(isEmptyKivansag(gifts[i]) == 0){
  431.                         oldRows = kivansagToRow(gifts[i]);
  432.                         oldRows = swapSpace(oldRows);
  433.                         fprintf(fileContents, "%s",oldRows);   
  434.                     }
  435.                 }
  436.                 fclose(fileContents);    
  437.                 */
  438.                 to_child[0][200] = '\0';
  439.                 if(log_counter == 1){
  440.                     strcat(to_child[0],"\n");  
  441.                     strcat(to_child[0],"Ez a fajta ajandek erkezett meg: ");   
  442.                     strcat(to_child[0],fromListGift);
  443.                     strcat(to_child[0],",");
  444.                     strcat(to_child[0]," ebbe a varosba: ");   
  445.                     strcat(to_child[0], sublogs[log_counter-1]);               
  446.                     strcat(to_child[0],"\n");  
  447.                    
  448.                 }else if(log_counter > 1){
  449.                     int i;
  450.                     for(i = 0; i < log_counter; ++i){
  451.                         strcat(to_child[i],"\n");
  452.                         strcat(to_child[i],"Ez a fajta ajandek erkezett meg: ");   
  453.                         strcat(to_child[i],fromListGift);
  454.                         strcat(to_child[i],",");
  455.                         strcat(to_child[i]," ebbe a varosba: ");   
  456.                         strcat(to_child[i],sublogs[i]);                                    
  457.                     }
  458.                     strcat(to_child[i],"\n");
  459.                 }else if(log_counter < 1){                                 
  460.                     strcat(to_child[0],"Nem volt ilyen ajandek");                  
  461.                     strcat(to_child[0],"\n");      
  462.                 }              
  463.                
  464.             }          
  465.  
  466.             int sizeOfArray = log_counter;
  467.             log_counter = 0;
  468.             msg[0] = '\0';
  469.            
  470.            
  471.             if(sizeOfArray == 0){
  472.                 sizeOfArray = 1;
  473.             }
  474.             char to_parent [sizeOfArray * 200];
  475.             to_parent[0] = '\0';
  476.  
  477.             int h;
  478.             for(h = 0; h < sizeOfArray; h++){
  479.                 strcat(to_parent, to_child[h]);
  480.             }          
  481.            
  482.            
  483.             close(pipe_child_to_santa[0]);
  484.             //write(pipe_child_to_santa[1], to_parent,sizeof(to_parent));
  485.             close(pipe_child_to_santa[1]);
  486.            
  487.             sending( uzenetsor, to_parent );
  488.             wait( NULL );          
  489.             status = msgctl ( uzenetsor, IPC_RMID, NULL);
  490.            
  491.             if ( status < 0 ) {
  492.                 perror("msgctl");
  493.                 return 0;          
  494.             }
  495.            
  496.             printf("\nMano folyamat befejezodik...\n");
  497.         }
  498.        
  499.  
  500.     }      
  501.    
  502.     return 0;
  503. }
  504.  
  505. struct Kivansag getEmptyKivansag(){
  506.     struct Kivansag kivansag;
  507.     strcat(kivansag.gift,"");
  508.     return kivansag;
  509. }
  510.  
  511. int isEmptyKivansag(struct Kivansag kivansag){
  512.     return (strcmp(kivansag.gift,"") == 0);
  513. }
  514.  
  515. char *swapSpace(char *row){
  516.     int count = 0;
  517.     while(*row != '\0'){
  518.         ++count;
  519.         if(*row == ' '){
  520.             *row = '_';
  521.         }
  522.         ++row;
  523.     }
  524.     int i;
  525.     for(i = 0; i < count; i++){
  526.         --row;
  527.     }
  528.     return row;
  529. }
  530.  
  531. char *kivansagToRow(struct Kivansag kivansag){
  532.     char *row;
  533.     row = kivansag.date;   
  534.     strcat(row,",");
  535.     strcat(row,kivansag.name); 
  536.     strcat(row,",");
  537.     strcat(row,kivansag.city); 
  538.     strcat(row,",");
  539.     strcat(row,kivansag.address);
  540.     strcat(row,",");
  541.     strcat(row,kivansag.gift); 
  542.     strcat(row,"\n");
  543.     return row;
  544. }
  545.  
  546. void cutEnter(char string[100]){
  547.     char *pos;
  548.     if ((pos=strchr(string, '\n')) != NULL)
  549.         *pos = '\0';
  550. }
  551.  
  552. void menu_print(){
  553.     printf("\n*********************************************************\n");
  554.                 printf("Udv a Mikulas kezelofeluleten! Az alabbiak kozul valaszthat:\n"
  555.                 "  (1) Ajandek kuldese gyujtohelyrol, egy varosba, sok ajandekot\n"
  556.                 "  (2) Ajandek kuldese uzembol, sok varosba, egy fele ajandekot\n"                         
  557.                 "  (3) Kilepes\n");
  558. }
  559.  
  560. int isCorrect(char answer[10]){
  561.    return *answer-48;
  562. }
  563.  
  564. struct Kivansag rowToKivansag(char row[1000]){
  565.     struct Kivansag kivansag;
  566.     int i = 0;
  567.     int j = 0;
  568.     int count = 0;
  569.     int lastPos = 0;
  570.     while(row[i] != '\0'){
  571.         if(row[i] == ','){
  572.             ++count;
  573.             lastPos = j;
  574.             j = 0;
  575.         }else{
  576.             if(count == 0){        
  577.                 if(row[i] == '_'){
  578.                     kivansag.date[j] = ' ';
  579.                 }
  580.                 else{
  581.                     kivansag.date[j] = row[i];
  582.                 }
  583.                 ++j;
  584.             }
  585.             if(count == 1){
  586.                 kivansag.date[lastPos] = '\0';
  587.                 if(row[i] == '_'){
  588.                     kivansag.name[j] = ' ';
  589.                 }
  590.                 else{
  591.                     kivansag.name[j] = row[i];
  592.                 }
  593.                 ++j;
  594.             }
  595.             if(count == 2){
  596.                 kivansag.name[lastPos] = '\0';
  597.                 if(row[i] == '_'){
  598.                     kivansag.city[j] = ' ';
  599.                 }
  600.                 else{
  601.                     kivansag.city[j] = row[i];
  602.                 }
  603.                 ++j;
  604.             }
  605.             if(count == 3){
  606.                 kivansag.city[lastPos] = '\0';
  607.                 if(row[i] == '_'){
  608.                     kivansag.address[j] = ' ';
  609.                 }
  610.                 else{
  611.                     kivansag.address[j] = row[i];
  612.                 }
  613.                 ++j;
  614.             }
  615.             if(count == 4){
  616.                 kivansag.address[lastPos] = '\0';
  617.                 if(row[i] == '_'){
  618.                     kivansag.gift[j] = ' ';
  619.                 }
  620.                 else{
  621.                     kivansag.gift[j] = row[i];
  622.                 }
  623.                 ++j;
  624.             }      
  625.         }
  626.         ++i;
  627.     }          
  628.     kivansag.gift[j] = '\0';   
  629.     return kivansag;
  630. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement