Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <time.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- struct msg_buffer {
- long msg_type;
- char msg[50];
- } message;
- const char* locations[] = {"Bali", "Mali", "Cook islands", "Bahamas", "Iceland"};
- int location_count[] = {0, 0, 0, 0, 0};
- int location_min[] = {2, 5, 5, 5, 5};
- const char* transports[] = {"Airplane", "Ship", "Bus"};
- const char* DATAFILENAME = "data.txt";
- const char* TEMPFILENAME = "data_temp.txt";
- int locations_len = sizeof(locations) / sizeof(locations[0]);
- int transports_len = sizeof(transports) / sizeof(transports[0]);
- const int RECORD_SIZE = 60 + sizeof(int) + 20 + sizeof(int);
- // char* people[1024][4];
- char stuckNamesAtLoc[60][100];
- // enum Transport{Airplane, Ship, Bus};
- // struct PersonInfo{
- // char name[60];
- // char location[20];
- // char phoneNumber[20];
- // char transport[20];
- // // enum Transport transport;
- // };
- void handler(int s) {}
- void printSeparator()
- {
- printf("\n###############################\n\n");
- }
- void printAllData()
- {
- printSeparator();
- int fp = open(DATAFILENAME, O_RDONLY);
- if(fp < 0) {
- printf("no data stored!\n");
- return;
- }
- int chosenLocation;
- int chosenTransport;
- char name[60];
- char phoneNumber[20];
- size_t end = lseek(fp, 0, SEEK_END);
- lseek(fp, 0, SEEK_SET);
- int i = 0;
- while(lseek(fp, 0, SEEK_CUR) != end)
- {
- read(fp, name, sizeof(name));
- read(fp, &chosenLocation, sizeof(chosenLocation));
- read(fp, phoneNumber, sizeof(phoneNumber));
- read(fp, &chosenTransport, sizeof(chosenTransport));
- printf("%d) name: %s, location: %s, phone: %s, transport: %s\n", i, name, locations[chosenLocation], phoneNumber, transports[chosenTransport]);
- ++i;
- }
- printSeparator();
- close(fp);
- }
- int getIntUserInput()
- {
- int input;
- char ch;
- while (scanf("%d", &input) != 1)
- {
- while ((ch = getchar()) != '\n')
- {
- putchar(ch);
- }
- printf(" is not a number.\nPlease enter a number!\n");
- }
- return input;
- }
- int chooseLocationNum()
- {
- int selectedLocationNum;
- printf("The available locations are the following:\n");
- printSeparator();
- for (int i = 0; i < locations_len; ++i)
- {
- printf("%d: %s\n", i, locations[i]);
- }
- printSeparator();
- while(1)
- {
- selectedLocationNum = getIntUserInput();
- if (selectedLocationNum >= 0 && selectedLocationNum < locations_len)
- {
- break;
- }
- printf("Invalid number!\nSelect the number of location you need information about!\n");
- printSeparator();
- for (int i = 0; i < locations_len; ++i)
- {
- printf("%d: %s\n", i, locations[i]);
- }
- printSeparator();
- }
- return selectedLocationNum;
- }
- int chooseTransportNum()
- {
- int selectedTransportNum;
- printf("The available transports are the following:\n");
- printSeparator();
- for (int i = 0; i < transports_len; ++i)
- {
- printf("%d: %s\n", i, transports[i]);
- }
- printSeparator();
- while(1)
- {
- selectedTransportNum = getIntUserInput();
- if (selectedTransportNum >= 0 && selectedTransportNum < transports_len)
- {
- break;
- }
- printf("Invalid number!\nSelect the number of location you need information about!\n");
- printSeparator();
- for (int i = 0; i < transports_len; ++i)
- {
- printf("%d: %s\n", i, transports[i]);
- }
- printSeparator();
- }
- return selectedTransportNum;
- }
- int getChosenMenuPoint(void)
- {
- printf("0) exit the program\n");
- printf("1) input data\n");
- printf("2) delete data\n");
- printf("3) modify existing data\n");
- printf("4) list data by location\n");
- printf("5) list all data\n");
- return getIntUserInput();
- }
- void enterNewData()
- {
- char name[60];
- char phoneNumber[20];
- printf("[Enter name]\n");
- getchar();
- scanf("%[^\n]", name);
- printf("[Enter location numer]\n");
- int chosenLocation = chooseLocationNum();
- printf("[Enter phone number]\n");
- scanf("%s", phoneNumber);
- printf("[Enter transport]\n");
- int chosenTransport = chooseTransportNum();
- int fp = open(DATAFILENAME, O_CREAT | O_RDWR | O_APPEND);
- if(fp < 0) {
- perror("Error opening file.");
- exit(1);
- }
- write(fp, name, sizeof(name));
- write(fp, &chosenLocation, sizeof(chosenLocation));
- write(fp, phoneNumber, sizeof(phoneNumber));
- write(fp, &chosenTransport, sizeof(chosenTransport));
- close(fp);
- location_count[chosenLocation] += 1;
- }
- int deleteData()
- {
- printAllData();
- printf("choose the index of the line you wanna modify/delete!\n");
- int lineIndexToDelete;
- int fp = open(DATAFILENAME, O_RDONLY);
- if (fp < 0)
- {
- printf("no data stored yet!\n");
- return -1;
- }
- int end = lseek(fp, 0, SEEK_END) / RECORD_SIZE;
- lseek(fp, 0, SEEK_SET);
- while(1)
- {
- lineIndexToDelete = getIntUserInput();
- if (lineIndexToDelete >= 0 && lineIndexToDelete < end)
- {
- break;
- }
- printf("Invalid number!\nSelect a valid number!\n");
- }
- int fp2 = open(TEMPFILENAME, O_CREAT | O_RDWR);
- if (fp2 < 0)
- {
- printf("oh shit here we go again");
- return -1;
- }
- int chosenLocation;
- int chosenTransport;
- char name[60];
- char phoneNumber[20];
- char record[RECORD_SIZE];
- for (int i = 0; i < end; ++i)
- {
- if (lineIndexToDelete != i)
- {
- read(fp, record, RECORD_SIZE);
- write(fp2, record, RECORD_SIZE);
- }
- else
- {
- read(fp, name, sizeof(name));
- read(fp, &chosenLocation, sizeof(chosenLocation));
- read(fp, phoneNumber, sizeof(phoneNumber));
- read(fp, &chosenTransport, sizeof(chosenTransport));
- location_count[chosenLocation] -= 1;
- }
- }
- close(fp);
- close(fp2);
- remove(DATAFILENAME);
- rename(TEMPFILENAME, DATAFILENAME);
- return 0;
- }
- int rescuePeopleFrom(int locationIndex = -1)
- {
- if (locationIndex < 0 || locationIndex > 5)
- {
- return -1;
- }
- int fp = open(DATAFILENAME, O_RDONLY);
- //should never be the case
- if (fp < 0)
- {
- printf("no data stored yet!\n");
- return -1;
- }
- int end = lseek(fp, 0, SEEK_END) / RECORD_SIZE;
- lseek(fp, 0, SEEK_SET);
- int fp2 = open(TEMPFILENAME, O_CREAT | O_RDWR);
- if (fp2 < 0)
- {
- printf("oh shit here we go again");
- return -1;
- }
- int storedLocationIndex;
- int chosenTransport;
- char name[60];
- char phoneNumber[20];
- char record[RECORD_SIZE];
- for (int i = 0; i < end; ++i)
- {
- read(fp, name, sizeof(name));
- read(fp, &storedLocationIndex, sizeof(storedLocationIndex));
- read(fp, phoneNumber, sizeof(phoneNumber));
- read(fp, &chosenTransport, sizeof(chosenTransport));
- if (locationIndex != storedLocationIndex)
- {
- write(fp2, name, sizeof(name));
- write(fp2, &storedLocationIndex, sizeof(storedLocationIndex));
- write(fp2, phoneNumber, sizeof(phoneNumber));
- write(fp2, &chosenTransport, sizeof(chosenTransport));
- // read(fp, record, RECORD_SIZE);
- // write(fp2, record, RECORD_SIZE);
- }
- }
- close(fp);
- close(fp2);
- remove(DATAFILENAME);
- rename(TEMPFILENAME, DATAFILENAME);
- return 0;
- }
- void modifyData()
- {
- if (!deleteData())
- {
- enterNewData();
- }
- else
- {
- printf("there is no data stored yet! Nothing to modify!\n");
- }
- }
- void getNamesAt(int selectedLocationNum)
- {
- int fp = open(DATAFILENAME, O_RDONLY);
- if(fp < 0) {
- printf("no data stored yet!\n");
- return;
- }
- int chosenLocation;
- int chosenTransport;
- char name[60];
- char phoneNumber[20];
- size_t end = lseek(fp, 0, SEEK_END);
- lseek(fp, 0, SEEK_SET);
- int i = 0;
- while(lseek(fp, 0, SEEK_CUR) != end)
- {
- read(fp, name, sizeof(name));
- read(fp, &chosenLocation, sizeof(chosenLocation));
- read(fp, phoneNumber, sizeof(phoneNumber));
- read(fp, &chosenTransport, sizeof(chosenTransport));
- if (chosenLocation == selectedLocationNum)
- {
- i += 1;
- strcpy(stuckNamesAtLoc[i], name);
- printf("faszkivan::::::%d + %s + %s\n", i, stuckNamesAtLoc[i], name);
- }
- }
- sprintf(name, "%d", i);
- strcpy(stuckNamesAtLoc[0], name);
- for (int j = 0; j < 4; ++j)
- {
- printf("------%s\n", stuckNamesAtLoc[j]);
- }
- }
- void listData(bool silentInit = false)
- {
- int selectedLocationNum = -1;
- if (!silentInit && selectedLocationNum == -1)
- {
- printf("[Select the number of location you need information about]\n");
- selectedLocationNum = chooseLocationNum();
- printf("selected location is: %s. The following people are stuck there:\n", locations[selectedLocationNum]);
- printSeparator();
- }
- int fp = open(DATAFILENAME, O_RDONLY);
- if(fp < 0) {
- printf("no data stored yet!\n");
- return;
- }
- int chosenLocation;
- int chosenTransport;
- char name[60];
- char phoneNumber[20];
- size_t end = lseek(fp, 0, SEEK_END);
- lseek(fp, 0, SEEK_SET);
- while(lseek(fp, 0, SEEK_CUR) != end)
- {
- read(fp, name, sizeof(name));
- read(fp, &chosenLocation, sizeof(chosenLocation));
- read(fp, phoneNumber, sizeof(phoneNumber));
- read(fp, &chosenTransport, sizeof(chosenTransport));
- if (silentInit)
- {
- location_count[chosenLocation] += 1;
- }
- else if (chosenLocation == selectedLocationNum)
- {
- printf("name: %s, location: %s, phone: %s, transport: %s\n", name, locations[chosenLocation], phoneNumber, transports[chosenTransport]);
- }
- }
- if (!silentInit)
- {
- printSeparator();
- }
- close(fp);
- }
- void rescue(int locIndex)
- {
- int pfd[2];
- if (pipe(pfd) == -1)
- {
- printf("pipe error!");
- exit(1);
- }
- signal(SIGUSR1, handler);
- if (fork() == 0) //child
- {
- printf("[CHI] Preparation for rescue has started...\n");
- sleep(2);
- printf("[CHI] Rescue team ready...\n");
- kill(getppid(), SIGUSR1);
- char peopleToSave[60][100];
- read(pfd[0], peopleToSave, sizeof(char) * 60 * 100);
- int numOfPeople = atoi(peopleToSave[0]);
- for (int i = 1; i < numOfPeople + 1; ++i)
- {
- printf("[CHI] STUCK EMBERKENEV: %s\n", peopleToSave[i]);
- }
- sleep(2);
- rescuePeopleFrom(locIndex);
- printf("[CHI] Rescue team got home...\n");
- key_t key;
- int msgId;
- printf("------------00\n");
- key = ftok("progfile", 65);
- msgId = msgget(key, 0666 | IPC_CREAT); //0666 -> readwrite
- printf("------------1\n");
- message.msg_type = 1;
- printf("------------2\n");
- char str[50];
- sprintf(str, "%d people saved from: %s..", numOfPeople, locations[locIndex]);
- strcpy(message.msg, str);
- printf("------------%s3\n", message.msg);
- kill(getppid(), SIGUSR1);
- msgsnd(msgId, &message, sizeof(message), 0);
- printf("------------4\n");
- exit(0);
- }
- else // parent
- {
- printf("[PAR] Preparing information for rescue team...\n");
- getNamesAt(locIndex);
- // printf("[PAR] wtf: %s\n", stuckNamesAtLoc[0]);
- // printf("[PAR] wtf: %s\n", stuckNamesAtLoc[1]);
- printf("[PAR] locindex: %d\n", locIndex);
- printf("[PAR] Information for rescue team is ready...\n");
- printf("[PAR] Waiting for rescue team to be ready...\n");
- pause();
- printf("[PAR] starting information sharing...\n");
- write(pfd[1], stuckNamesAtLoc, sizeof(char) * 60 * 100);
- printf("[PAR] Waiting for rescue to happen...\n");
- pause();
- printf("[PAR] Starting msg process\n");
- key_t key;
- int msgId;
- key = ftok("progfile", 65);
- msgId = msgget(key, 0666 | IPC_CREAT);
- msgrcv(msgId, &message, sizeof(message), 1, 0);
- printf("[PAR] %s\n", message);
- msgctl(msgId, IPC_RMID, NULL);
- }
- }
- void checkForRescue()
- {
- for (int i = 0; i < 5; ++i)
- {
- if (location_count[i] >= location_min[i])
- {
- printf("Let's rescue some people from: %s!\n", locations[i]);
- rescue(i);
- }
- }
- }
- int main()
- {
- if (access(DATAFILENAME, F_OK) == -1) {
- printf("file does not exist.. creating data.txt\n");
- FILE* fp = fopen(DATAFILENAME, "w");
- fclose(fp);
- }
- listData(1); //init number of stuck people per location
- checkForRescue();
- int n = -1;
- while (n = getChosenMenuPoint()) //it will exit if n is 0
- {
- switch (n)
- {
- case 1:
- enterNewData();
- checkForRescue();
- break;
- case 2:
- deleteData();
- checkForRescue();
- break;
- case 3:
- modifyData();
- checkForRescue();
- break;
- case 4:
- listData();
- break;
- case 5:
- printAllData();
- break;
- case 6:
- for (int i = 0; i < 5; ++i)
- {
- printf("%d, ", location_count[i]);
- }
- printf("\n");
- default:
- printf("Please input a valid menu point number!\n");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement