Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #include <signal.h>
  8. #include <time.h>
  9. #include <sys/ipc.h>
  10. #include <sys/msg.h>
  11.  
  12. struct msg_buffer {
  13.    long msg_type;
  14.    char msg[50];
  15. } message;
  16.  
  17. const char* locations[] = {"Bali", "Mali", "Cook islands", "Bahamas", "Iceland"};
  18.  
  19. int location_count[] = {0, 0, 0, 0, 0};
  20. int location_min[] = {2, 5, 5, 5, 5};
  21. const char* transports[] = {"Airplane", "Ship", "Bus"};
  22.  
  23. const char* DATAFILENAME = "data.txt";
  24. const char* TEMPFILENAME = "data_temp.txt";
  25.  
  26. int locations_len = sizeof(locations) / sizeof(locations[0]);
  27. int transports_len = sizeof(transports) / sizeof(transports[0]);
  28.  
  29. const int RECORD_SIZE = 60 + sizeof(int) + 20 + sizeof(int);
  30.  
  31. // char* people[1024][4];
  32. char stuckNamesAtLoc[60][100];
  33.  
  34. // enum Transport{Airplane, Ship, Bus};
  35.  
  36. // struct PersonInfo{
  37. //   char name[60];
  38. //   char location[20];
  39. //   char phoneNumber[20];
  40. //   char transport[20];
  41. //   // enum Transport transport;
  42. // };
  43.  
  44. void handler(int s) {}
  45.  
  46. void printSeparator()
  47. {
  48.   printf("\n###############################\n\n");
  49. }
  50.  
  51. void printAllData()
  52. {
  53.   printSeparator();
  54.  
  55.   int fp = open(DATAFILENAME, O_RDONLY);
  56.   if(fp < 0) {
  57.     printf("no data stored!\n");
  58.     return;
  59.   }
  60.  
  61.   int chosenLocation;
  62.   int chosenTransport;
  63.  
  64.   char name[60];
  65.   char phoneNumber[20];
  66.  
  67.   size_t end = lseek(fp, 0, SEEK_END);
  68.   lseek(fp, 0, SEEK_SET);
  69.  
  70.   int i = 0;
  71.   while(lseek(fp, 0, SEEK_CUR) != end)
  72.   {    
  73.     read(fp, name, sizeof(name));
  74.     read(fp, &chosenLocation, sizeof(chosenLocation));
  75.     read(fp, phoneNumber, sizeof(phoneNumber));
  76.     read(fp, &chosenTransport, sizeof(chosenTransport));
  77.  
  78.     printf("%d) name: %s, location: %s, phone: %s, transport: %s\n", i, name, locations[chosenLocation], phoneNumber, transports[chosenTransport]);
  79.     ++i;
  80.   }
  81.  
  82.   printSeparator();
  83.   close(fp);
  84. }
  85.  
  86. int getIntUserInput()
  87. {
  88.   int input;
  89.   char ch;
  90.  
  91.   while (scanf("%d", &input) != 1)
  92.   {
  93.     while ((ch = getchar()) != '\n')
  94.     {
  95.       putchar(ch);
  96.     }
  97.     printf(" is not a number.\nPlease enter a number!\n");
  98.   }
  99.   return input;
  100. }
  101.  
  102.  
  103. int chooseLocationNum()
  104. {
  105.   int selectedLocationNum;
  106.  
  107.   printf("The available locations are the following:\n");
  108.  
  109.   printSeparator();
  110.   for (int i = 0; i < locations_len; ++i)
  111.   {
  112.     printf("%d: %s\n", i, locations[i]);
  113.   }
  114.   printSeparator();
  115.  
  116.   while(1)
  117.   {
  118.     selectedLocationNum = getIntUserInput();
  119.     if (selectedLocationNum >= 0 && selectedLocationNum < locations_len)
  120.     {
  121.       break;
  122.     }
  123.  
  124.     printf("Invalid number!\nSelect the number of location you need information about!\n");
  125.     printSeparator();
  126.     for (int i = 0; i < locations_len; ++i)
  127.     {
  128.       printf("%d: %s\n", i, locations[i]);
  129.     }
  130.     printSeparator();
  131.   }
  132.  
  133.   return selectedLocationNum;
  134. }
  135.  
  136. int chooseTransportNum()
  137. {
  138.   int selectedTransportNum;
  139.  
  140.   printf("The available transports are the following:\n");
  141.  
  142.   printSeparator();
  143.   for (int i = 0; i < transports_len; ++i)
  144.   {
  145.     printf("%d: %s\n", i, transports[i]);
  146.   }
  147.   printSeparator();
  148.  
  149.   while(1)
  150.   {
  151.     selectedTransportNum = getIntUserInput();
  152.     if (selectedTransportNum >= 0 && selectedTransportNum < transports_len)
  153.     {
  154.       break;
  155.     }
  156.  
  157.     printf("Invalid number!\nSelect the number of location you need information about!\n");
  158.     printSeparator();
  159.     for (int i = 0; i < transports_len; ++i)
  160.     {
  161.       printf("%d: %s\n", i, transports[i]);
  162.     }
  163.     printSeparator();
  164.   }
  165.  
  166.   return selectedTransportNum;
  167. }
  168.  
  169.  
  170. int getChosenMenuPoint(void)
  171. {
  172.   printf("0) exit the program\n");
  173.   printf("1) input data\n");
  174.   printf("2) delete data\n");
  175.   printf("3) modify existing data\n");
  176.   printf("4) list data by location\n");
  177.   printf("5) list all data\n");
  178.  
  179.   return getIntUserInput();
  180. }
  181.  
  182. void enterNewData()
  183. {
  184.   char name[60];
  185.   char phoneNumber[20];
  186.  
  187.   printf("[Enter name]\n");
  188.   getchar();
  189.   scanf("%[^\n]", name);
  190.  
  191.   printf("[Enter location numer]\n");
  192.   int chosenLocation = chooseLocationNum();
  193.  
  194.   printf("[Enter phone number]\n");
  195.   scanf("%s", phoneNumber);
  196.  
  197.   printf("[Enter transport]\n");
  198.   int chosenTransport = chooseTransportNum();
  199.  
  200.   int fp = open(DATAFILENAME, O_CREAT | O_RDWR | O_APPEND);
  201.  
  202.   if(fp < 0) {
  203.     perror("Error opening file.");
  204.     exit(1);
  205.   }
  206.  
  207.   write(fp, name, sizeof(name));
  208.   write(fp, &chosenLocation, sizeof(chosenLocation));
  209.   write(fp, phoneNumber, sizeof(phoneNumber));
  210.   write(fp, &chosenTransport, sizeof(chosenTransport));
  211.  
  212.   close(fp);
  213.   location_count[chosenLocation] += 1;
  214. }
  215.  
  216. int deleteData()
  217. {
  218.   printAllData();
  219.   printf("choose the index of the line you wanna modify/delete!\n");
  220.   int lineIndexToDelete;
  221.  
  222.   int fp = open(DATAFILENAME, O_RDONLY);
  223.  
  224.   if (fp < 0)
  225.   {
  226.     printf("no data stored yet!\n");
  227.     return -1;
  228.   }
  229.  
  230.   int end = lseek(fp, 0, SEEK_END) / RECORD_SIZE;
  231.   lseek(fp, 0, SEEK_SET);
  232.  
  233.   while(1)
  234.   {
  235.     lineIndexToDelete = getIntUserInput();
  236.     if (lineIndexToDelete >= 0 && lineIndexToDelete < end)
  237.     {
  238.       break;
  239.     }
  240.  
  241.     printf("Invalid number!\nSelect a valid number!\n");
  242.   }
  243.  
  244.   int fp2 = open(TEMPFILENAME, O_CREAT | O_RDWR);
  245.  
  246.   if (fp2 < 0)
  247.   {
  248.     printf("oh shit here we go again");
  249.     return -1;
  250.   }
  251.  
  252.   int chosenLocation;
  253.   int chosenTransport;
  254.  
  255.   char name[60];
  256.   char phoneNumber[20];
  257.  
  258.   char record[RECORD_SIZE];
  259.  
  260.   for (int i = 0; i < end; ++i)
  261.   {
  262.     if (lineIndexToDelete != i)
  263.     {
  264.       read(fp, record, RECORD_SIZE);
  265.       write(fp2, record, RECORD_SIZE);
  266.      
  267.     }
  268.     else
  269.     {
  270.       read(fp, name, sizeof(name));
  271.       read(fp, &chosenLocation, sizeof(chosenLocation));
  272.       read(fp, phoneNumber, sizeof(phoneNumber));
  273.       read(fp, &chosenTransport, sizeof(chosenTransport));
  274.       location_count[chosenLocation] -= 1;
  275.     }
  276.   }
  277.  
  278.   close(fp);
  279.   close(fp2);
  280.  
  281.   remove(DATAFILENAME);
  282.   rename(TEMPFILENAME, DATAFILENAME);
  283.   return 0;
  284. }
  285.  
  286. int rescuePeopleFrom(int locationIndex = -1)
  287. {
  288.   if (locationIndex < 0 || locationIndex > 5)
  289.   {
  290.     return -1;
  291.   }
  292.  
  293.   int fp = open(DATAFILENAME, O_RDONLY);
  294.  
  295.   //should never be the case
  296.   if (fp < 0)
  297.   {
  298.     printf("no data stored yet!\n");
  299.     return -1;
  300.   }
  301.  
  302.   int end = lseek(fp, 0, SEEK_END) / RECORD_SIZE;
  303.   lseek(fp, 0, SEEK_SET);
  304.  
  305.   int fp2 = open(TEMPFILENAME, O_CREAT | O_RDWR);
  306.  
  307.   if (fp2 < 0)
  308.   {
  309.     printf("oh shit here we go again");
  310.     return -1;
  311.   }
  312.  
  313.   int storedLocationIndex;
  314.   int chosenTransport;
  315.  
  316.   char name[60];
  317.   char phoneNumber[20];
  318.  
  319.   char record[RECORD_SIZE];
  320.  
  321.   for (int i = 0; i < end; ++i)
  322.   {
  323.     read(fp, name, sizeof(name));
  324.     read(fp, &storedLocationIndex, sizeof(storedLocationIndex));
  325.     read(fp, phoneNumber, sizeof(phoneNumber));
  326.     read(fp, &chosenTransport, sizeof(chosenTransport));
  327.  
  328.     if (locationIndex != storedLocationIndex)
  329.     {
  330.       write(fp2, name, sizeof(name));
  331.       write(fp2, &storedLocationIndex, sizeof(storedLocationIndex));
  332.       write(fp2, phoneNumber, sizeof(phoneNumber));
  333.       write(fp2, &chosenTransport, sizeof(chosenTransport));
  334.       // read(fp, record, RECORD_SIZE);
  335.       // write(fp2, record, RECORD_SIZE);
  336.      
  337.     }
  338.   }
  339.  
  340.   close(fp);
  341.   close(fp2);
  342.  
  343.   remove(DATAFILENAME);
  344.   rename(TEMPFILENAME, DATAFILENAME);
  345.   return 0;
  346. }
  347.  
  348. void modifyData()
  349. {
  350.   if (!deleteData())
  351.   {
  352.     enterNewData();
  353.   }
  354.   else
  355.   {
  356.     printf("there is no data stored yet! Nothing to modify!\n");
  357.   }
  358.  
  359. }
  360.  
  361. void getNamesAt(int selectedLocationNum)
  362. {
  363.   int fp = open(DATAFILENAME, O_RDONLY);
  364.   if(fp < 0) {
  365.     printf("no data stored yet!\n");
  366.     return;
  367.   }
  368.  
  369.   int chosenLocation;
  370.   int chosenTransport;
  371.  
  372.   char name[60];
  373.   char phoneNumber[20];
  374.  
  375.   size_t end = lseek(fp, 0, SEEK_END);
  376.   lseek(fp, 0, SEEK_SET);
  377.   int i = 0;
  378.   while(lseek(fp, 0, SEEK_CUR) != end)
  379.   {    
  380.     read(fp, name, sizeof(name));
  381.     read(fp, &chosenLocation, sizeof(chosenLocation));
  382.     read(fp, phoneNumber, sizeof(phoneNumber));
  383.     read(fp, &chosenTransport, sizeof(chosenTransport));
  384.     if (chosenLocation == selectedLocationNum)
  385.     {
  386.       i += 1;
  387.       strcpy(stuckNamesAtLoc[i], name);
  388.       printf("faszkivan::::::%d + %s + %s\n", i, stuckNamesAtLoc[i], name);
  389.     }
  390.   }
  391.   sprintf(name, "%d", i);
  392.   strcpy(stuckNamesAtLoc[0], name);
  393.   for (int j = 0; j < 4; ++j)
  394.   {
  395.     printf("------%s\n", stuckNamesAtLoc[j]);
  396.   }
  397. }
  398.  
  399. void listData(bool silentInit = false)
  400. {
  401.   int selectedLocationNum = -1;
  402.   if (!silentInit && selectedLocationNum == -1)
  403.   {
  404.     printf("[Select the number of location you need information about]\n");
  405.  
  406.     selectedLocationNum = chooseLocationNum();
  407.  
  408.     printf("selected location is: %s.  The following people are stuck there:\n", locations[selectedLocationNum]);
  409.     printSeparator();
  410.   }
  411.  
  412.   int fp = open(DATAFILENAME, O_RDONLY);
  413.   if(fp < 0) {
  414.     printf("no data stored yet!\n");
  415.     return;
  416.   }
  417.  
  418.   int chosenLocation;
  419.   int chosenTransport;
  420.  
  421.   char name[60];
  422.   char phoneNumber[20];
  423.  
  424.   size_t end = lseek(fp, 0, SEEK_END);
  425.   lseek(fp, 0, SEEK_SET);
  426.  
  427.   while(lseek(fp, 0, SEEK_CUR) != end)
  428.   {    
  429.     read(fp, name, sizeof(name));
  430.     read(fp, &chosenLocation, sizeof(chosenLocation));
  431.     read(fp, phoneNumber, sizeof(phoneNumber));
  432.     read(fp, &chosenTransport, sizeof(chosenTransport));
  433.  
  434.     if (silentInit)
  435.     {
  436.       location_count[chosenLocation] += 1;
  437.     }
  438.     else if (chosenLocation == selectedLocationNum)
  439.     {
  440.       printf("name: %s, location: %s, phone: %s, transport: %s\n", name, locations[chosenLocation], phoneNumber, transports[chosenTransport]);
  441.     }
  442.   }
  443.  
  444.   if (!silentInit)
  445.   {
  446.     printSeparator();
  447.   }
  448.   close(fp);
  449. }
  450.  
  451. void rescue(int locIndex)
  452. {
  453.   int pfd[2];
  454.   if (pipe(pfd) == -1)
  455.   {
  456.     printf("pipe error!");
  457.     exit(1);
  458.   }
  459.   signal(SIGUSR1, handler);
  460.   if (fork() == 0) //child
  461.   {
  462.     printf("[CHI] Preparation for rescue has started...\n");
  463.     sleep(2);
  464.     printf("[CHI] Rescue team ready...\n");
  465.     kill(getppid(), SIGUSR1);
  466.     char peopleToSave[60][100];
  467.     read(pfd[0], peopleToSave, sizeof(char) * 60 * 100);
  468.     int numOfPeople = atoi(peopleToSave[0]);
  469.     for (int i = 1; i < numOfPeople + 1; ++i)
  470.     {
  471.       printf("[CHI] STUCK EMBERKENEV: %s\n", peopleToSave[i]);
  472.     }
  473.     sleep(2);
  474.     rescuePeopleFrom(locIndex);
  475.     printf("[CHI] Rescue team got home...\n");
  476.  
  477.     key_t key;
  478.     int msgId;
  479.  
  480.     printf("------------00\n");
  481.     key = ftok("progfile", 65);
  482.     msgId = msgget(key, 0666 | IPC_CREAT); //0666 -> readwrite
  483.     printf("------------1\n");
  484.     message.msg_type = 1;
  485.     printf("------------2\n");
  486.  
  487.     char str[50];
  488.     sprintf(str, "%d people saved from: %s..", numOfPeople, locations[locIndex]);
  489.     strcpy(message.msg, str);
  490.  
  491.     printf("------------%s3\n", message.msg);
  492.     kill(getppid(), SIGUSR1);
  493.     msgsnd(msgId, &message, sizeof(message), 0);
  494.     printf("------------4\n");
  495.     exit(0);
  496.   }
  497.   else // parent
  498.   {
  499.     printf("[PAR] Preparing information for rescue team...\n");
  500.     getNamesAt(locIndex);
  501.     // printf("[PAR] wtf: %s\n", stuckNamesAtLoc[0]);
  502.     // printf("[PAR] wtf: %s\n", stuckNamesAtLoc[1]);
  503.     printf("[PAR] locindex: %d\n", locIndex);
  504.     printf("[PAR] Information for rescue team is ready...\n");
  505.     printf("[PAR] Waiting for rescue team to be ready...\n");
  506.     pause();
  507.     printf("[PAR] starting information sharing...\n");
  508.     write(pfd[1], stuckNamesAtLoc, sizeof(char) * 60 * 100);
  509.     printf("[PAR] Waiting for rescue to happen...\n");
  510.     pause();
  511.     printf("[PAR] Starting msg process\n");
  512.  
  513.     key_t key;
  514.     int msgId;
  515.  
  516.     key = ftok("progfile", 65);
  517.     msgId = msgget(key, 0666 | IPC_CREAT);
  518.  
  519.     msgrcv(msgId, &message, sizeof(message), 1, 0);
  520.  
  521.     printf("[PAR] %s\n", message);
  522.     msgctl(msgId, IPC_RMID, NULL);
  523.   }
  524.  
  525. }
  526.  
  527. void checkForRescue()
  528. {
  529.   for (int i = 0; i < 5; ++i)
  530.   {
  531.     if (location_count[i] >= location_min[i])
  532.     {
  533.       printf("Let's rescue some people from: %s!\n", locations[i]);
  534.       rescue(i);
  535.     }
  536.   }
  537. }
  538.  
  539. int main()
  540. {
  541.   if (access(DATAFILENAME, F_OK) == -1) {
  542.     printf("file does not exist.. creating data.txt\n");
  543.  
  544.     FILE* fp = fopen(DATAFILENAME, "w");
  545.     fclose(fp);
  546.   }
  547.  
  548.   listData(1); //init number of stuck people per location
  549.   checkForRescue();
  550.  
  551.   int n = -1;
  552.   while (n = getChosenMenuPoint()) //it will exit if n is 0
  553.   {
  554.     switch (n)
  555.     {
  556.       case 1:
  557.         enterNewData();
  558.         checkForRescue();
  559.         break;
  560.       case 2:
  561.         deleteData();
  562.         checkForRescue();
  563.         break;
  564.       case 3:
  565.         modifyData();
  566.         checkForRescue();
  567.         break;
  568.       case 4:
  569.         listData();
  570.         break;
  571.       case 5:
  572.         printAllData();
  573.       break;
  574.       case 6:
  575.         for (int i = 0; i < 5; ++i)
  576.           {
  577.             printf("%d, ", location_count[i]);
  578.           }
  579.           printf("\n");
  580.       default:
  581.         printf("Please input a valid menu point number!\n");
  582.         break;
  583.     }
  584.   }
  585.  
  586.   return 0;
  587. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement