Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/ipc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h> // for open()
- #include <errno.h> // for perror()
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/msg.h>
- #include <sys/wait.h> //waitpid
- #include <string.h> //for strcpy()
- #include <unistd.h> //for file write() / fork() /pipe()
- #include <time.h>
- #include <wait.h>
- struct Kivansag {
- char date[100];
- char name[100];
- char city[100];
- char address[100];
- char gift[100];
- };
- struct uzenet {
- long mtype;
- char mtext [1024];
- };
- int sending( int uzenetsor, char* to_parent )
- {
- struct uzenet uz;
- uz.mtype = 5;
- int i;
- for(i = 0; i < 1024; i++){
- uz.mtext[i] = to_parent[i];
- }
- int status;
- status = msgsnd( uzenetsor, &uz, strlen ( uz.mtext ) + 1 , 0 );
- // a 3. param ilyen is lehet: sizeof(uz.mtext)
- // a 4. parameter gyakran IPC_NOWAIT, ez a 0-val azonos
- if ( status < 0 )
- perror("msgsnd");
- return 0;
- }
- int receiving( int uzenetsor)
- {
- struct uzenet uz;
- int status;
- // az utolso parameter(0) az uzenet azonositoszama
- // ha az 0, akkor a sor elso uzenetet vesszuk ki
- // ha >0 (5), akkor az 5-os uzenetekbol a kovetkezot
- // vesszuk ki a sorbol
- status = msgrcv(uzenetsor, &uz, 1024, 5, 0 ); //5 helyett 0, akkor minden üzenetet olvashat
- if ( status < 0 )
- perror("msgsnd");
- else
- printf( "A manók üzenete: %s\n", uz.mtext );
- return 0;
- }
- const char *FILE_NAME = "kivansagok.scv";
- FILE *fileContents;
- int list_size = 0;
- struct Kivansag gifts[1000];
- struct Kivansag rowToKivansag(char row[1000]);
- char *kivansagToRow(struct Kivansag kivansag);
- char *swapSpace(char *row);
- int isCorrect(char answer[10]);
- void menu_print();
- void cutEnter(char string[100]);
- struct Kivansag getEmptyKivansag();
- int isEmptyKivansag(struct Kivansag kivansag);
- int main(int argc, char* argv[]){
- char msg[200000];
- //char msg2[200000];
- key_t key;
- int uzenetsor, status;
- key = ftok(argv[0],1);
- uzenetsor = msgget ( key, 0600 | IPC_CREAT);
- if ( uzenetsor < 0 ) {
- perror("msgget");
- return 1;
- }
- if((fileContents= fopen(FILE_NAME, "rb")) == NULL){
- if(errno == ENOENT){//if "kivansagok" file doesn't exists
- printf("A Kivansagok lista fajlja nem letezik! \tKilepes...\n");
- exit(1);
- }
- }else{
- //file read start
- fscanf(fileContents,"%d\n",&list_size); //init list_size
- if(list_size > 0){
- int i;
- for(i = 0; i < list_size; i++){ // init gifts
- struct Kivansag kivansag;
- char row[1000];
- fscanf(fileContents,"%s",row);
- kivansag = rowToKivansag(row);
- gifts[i] = kivansag;
- }
- printf("TESZT*****************\n");
- for(i = 0; i < list_size; i++){ // init gifts
- 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);
- }
- printf("***********************\n");
- }
- fclose(fileContents);
- //file read ending
- int pipe_santa_to_child[2];
- int pipe_child_to_santa[2];
- int pipeee[2];
- if (pipe(pipe_santa_to_child) == -1 || pipe(pipe_child_to_santa) == -1 || pipe(pipeee) == -1) {
- perror("Hiba a pipe nyitaskor!");
- exit(EXIT_FAILURE);
- }
- pid_t child;
- child = fork();
- if (child<0){
- perror("The fork calling was not succesful\n");
- exit(1);
- }
- else if(child > 0){ //santa session
- printf("Mikulas folyamat indul...\n");
- close(pipe_santa_to_child[0]);
- char answer[10];
- int check = 0;
- do{
- menu_print();
- fgets(answer,10, stdin);
- check = isCorrect(answer);
- if(check < 1 || check > 5){
- printf("\nHibas adatbevitel!\n");
- }else{
- if(check == 1){
- char city[100];
- city[0] = '\0';
- char solution[100];
- int solInt = -1;
- int city_counter = 0;
- char citys[1000][1000];
- citys[city_counter][1000] = '\0';
- int j = -1;
- do{
- printf("\n");
- printf("Valaszon az alabbi varosok kozul: \n");
- for(j = 0; j < list_size; j++){
- if(city_counter > 0){
- int k;
- int hadBeen = 0;
- for(k = 0; k < city_counter; ++k){
- if(strcmp(citys[k],gifts[j].city) == 0)
- {
- hadBeen = 1;
- }
- }
- if(hadBeen == 0){
- strcat(citys[city_counter],gifts[j].city);
- ++city_counter;
- }
- }else{
- strcat(citys[city_counter],gifts[j].city);
- ++city_counter;
- }
- }
- for(j = 0; j < city_counter; ++j){
- printf(" (%d) - %s \n", j, citys[j]);
- }
- printf(" (%d) - Kilepes \n", j);
- fgets(solution,100,stdin);
- cutEnter(solution);
- solInt = isCorrect(solution);
- if(solInt < j+1 && solInt >= 0){
- strcat(city,citys[solInt]);
- solInt = j;
- }else{
- printf("\n Hibas parameter! \n");
- }
- }while(solInt != j);
- char toImp[100];
- toImp[0] = '\0';
- strcat(toImp,"1");
- strcat(toImp,",");
- strcat(toImp, city);
- write(pipe_santa_to_child[1], toImp ,18); //!!!
- check = 3;
- }
- if(check == 2){
- char fromListGift[100];
- fromListGift[0] = '\0';
- char solution[100];
- int solInt = -1;
- int gift_counter = 0;
- char listgifts[1000][1000];
- listgifts[gift_counter][1000] = '\0';
- int j = -1;
- do{
- printf("\n");
- printf("Valaszon az alabbi ajandekok kozul: \n");
- for(j = 0; j < list_size; j++){
- if(gift_counter > 0){
- int k;
- int hadBeen = 0;
- for(k = 0; k < gift_counter; ++k){
- if(strcmp(listgifts[k],gifts[j].gift) == 0)
- {
- hadBeen = 1;
- }
- }
- if(hadBeen == 0){
- strcat(listgifts[gift_counter],gifts[j].gift);
- ++gift_counter;
- }
- }else{
- strcat(listgifts[gift_counter],gifts[j].gift);
- ++gift_counter;
- }
- }
- for(j = 0; j < gift_counter; ++j){
- printf(" (%d) - %s \n", j, listgifts[j]);
- }
- printf(" (%d) - Kilepes \n", j);
- fgets(solution,100,stdin);
- cutEnter(solution);
- solInt = isCorrect(solution);
- if(solInt < j+1 && solInt >= 0){
- strcat(fromListGift,listgifts[solInt]);
- solInt = j;
- }else{
- printf("\n Hibas parameter! \n");
- }
- }while(solInt != j);
- char toImp[100];
- toImp[0] = '\0';
- strcat(toImp,"2");
- strcat(toImp,",");
- strcat(toImp, fromListGift);
- write(pipe_santa_to_child[1], toImp ,18); //!!!
- check = 3;
- }
- if(check == 3){
- write(pipe_santa_to_child[1], "3",2);
- }
- }
- }
- while(check != 3);
- close(pipe_santa_to_child[1]);
- int valasz = receiving( uzenetsor);
- if(valasz < 0){
- perror("fogadas hiba\n");
- }
- fflush(NULL);
- waitpid(child, &status, 0);
- //close(pipe_child_to_santa[1]);
- //read(pipe_child_to_santa[0], msg2, sizeof(msg2));
- //printf("Ezt kuldtek a manok: %s \n", msg2 );
- //close(pipe_child_to_santa[0]);
- printf("Mikulas folyamat befejezodik...\n");
- }
- else if(child == 0){ // elf session
- printf("Mano folyamat indul...\n");
- close(pipe_santa_to_child[1]);
- read(pipe_santa_to_child[0], msg, sizeof(msg));
- if(msg[0] == '3'){
- msg[0] = '\0';
- }
- printf("Ezt kuldte a mikulas: %s \n", msg);
- close(pipe_santa_to_child[0]);
- char to_child[200][200];
- to_child[0][200] = '\0';
- //char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE];
- char sublogs[1000][1000];
- int log_counter = 0;
- sublogs[log_counter][1000] = '\0';
- if(msg[0] == '1'){
- int l = 2;
- int j = 0;
- char city[100];
- city[0] = '\0';
- while( l < strlen(msg)){
- city[j] = msg[l];
- ++l;
- ++j;
- city[j] = '\0';
- }
- char subgift[100];
- //char *oldRows;
- int sub_size = list_size;
- /*printf("Adja meg a varost nevet, ahova az ajandekokat szallitjuk!\n");
- fgets(city,100,stdin);
- cutEnter(city);*/
- int i;
- for(i = 0; i < list_size; i++){
- if(strcmp(city, gifts[i].city)==0){
- subgift[0] = '\0';
- strcat(subgift,gifts[i].gift);
- strcat(sublogs[log_counter],subgift);
- ++log_counter;
- gifts[i] = getEmptyKivansag();
- --sub_size;
- }
- }
- list_size = sub_size;
- /*
- fileContents = fopen(FILE_NAME,"wb");
- fprintf(fileContents, "%d\n",list_size);
- for(i = 0; i < list_size; i++){
- if(isEmptyKivansag(gifts[i]) == 0){
- oldRows = kivansagToRow(gifts[i]);
- oldRows = swapSpace(oldRows);
- fprintf(fileContents, "%s",oldRows);
- }
- }
- fclose(fileContents);
- */
- to_child[0][200] = '\0';
- if(log_counter == 1){
- strcat(to_child[0],"\n");
- strcat(to_child[0],"Ebbe a varosba erkezett meg az osszes ajandek: ");
- strcat(to_child[0],city);
- strcat(to_child[0],",");
- strcat(to_child[0]," ezt az ajandekot kaptak: ");
- strcat(to_child[0], sublogs[log_counter-1]);
- strcat(to_child[0],"\n");
- }else if(log_counter > 1){
- int i;
- for(i = 0; i < log_counter; ++i){
- strcat(to_child[i],"\n");
- strcat(to_child[i],"Ebbe a varosba erkezett meg az osszes ajandek: ");
- strcat(to_child[i],city);
- strcat(to_child[i],",");
- strcat(to_child[i]," ezt az ajandekot kaptak: ");
- strcat(to_child[i],sublogs[i]);
- }
- strcat(to_child[i],"\n");
- }else if(log_counter < 1){
- strcat(to_child[0],"Nem volt ilyen varos");
- strcat(to_child[0],"\n");
- }
- }
- if(msg[0] == '2'){
- int l = 2;
- int j = 0;
- char fromListGift[100];
- fromListGift[0] = '\0';
- while( l < strlen(msg)){
- fromListGift[j] = msg[l];
- ++l;
- ++j;
- fromListGift[j] = '\0';
- }
- char subCity[100];
- //char *oldRows;
- int sub_size = list_size;
- /*printf("Adja meg a varost nevet, ahova az ajandekokat szallitjuk!\n");
- fgets(city,100,stdin);
- cutEnter(city);*/
- int i;
- for(i = 0; i < list_size; i++){
- if(strcmp(fromListGift, gifts[i].gift)==0){
- subCity[0] = '\0';
- strcat(subCity,gifts[i].city);
- strcat(sublogs[log_counter],subCity);
- ++log_counter;
- gifts[i] = getEmptyKivansag();
- --sub_size;
- }
- }
- list_size = sub_size;
- /*
- fileContents = fopen(FILE_NAME,"wb");
- fprintf(fileContents, "%d\n",list_size);
- for(i = 0; i < list_size; i++){
- if(isEmptyKivansag(gifts[i]) == 0){
- oldRows = kivansagToRow(gifts[i]);
- oldRows = swapSpace(oldRows);
- fprintf(fileContents, "%s",oldRows);
- }
- }
- fclose(fileContents);
- */
- to_child[0][200] = '\0';
- if(log_counter == 1){
- strcat(to_child[0],"\n");
- strcat(to_child[0],"Ez a fajta ajandek erkezett meg: ");
- strcat(to_child[0],fromListGift);
- strcat(to_child[0],",");
- strcat(to_child[0]," ebbe a varosba: ");
- strcat(to_child[0], sublogs[log_counter-1]);
- strcat(to_child[0],"\n");
- }else if(log_counter > 1){
- int i;
- for(i = 0; i < log_counter; ++i){
- strcat(to_child[i],"\n");
- strcat(to_child[i],"Ez a fajta ajandek erkezett meg: ");
- strcat(to_child[i],fromListGift);
- strcat(to_child[i],",");
- strcat(to_child[i]," ebbe a varosba: ");
- strcat(to_child[i],sublogs[i]);
- }
- strcat(to_child[i],"\n");
- }else if(log_counter < 1){
- strcat(to_child[0],"Nem volt ilyen ajandek");
- strcat(to_child[0],"\n");
- }
- }
- int sizeOfArray = log_counter;
- log_counter = 0;
- msg[0] = '\0';
- if(sizeOfArray == 0){
- sizeOfArray = 1;
- }
- char to_parent [sizeOfArray * 200];
- to_parent[0] = '\0';
- int h;
- for(h = 0; h < sizeOfArray; h++){
- strcat(to_parent, to_child[h]);
- }
- close(pipe_child_to_santa[0]);
- //write(pipe_child_to_santa[1], to_parent,sizeof(to_parent));
- close(pipe_child_to_santa[1]);
- sending( uzenetsor, to_parent );
- wait( NULL );
- status = msgctl ( uzenetsor, IPC_RMID, NULL);
- if ( status < 0 ) {
- perror("msgctl");
- return 0;
- }
- printf("\nMano folyamat befejezodik...\n");
- }
- }
- return 0;
- }
- struct Kivansag getEmptyKivansag(){
- struct Kivansag kivansag;
- strcat(kivansag.gift,"");
- return kivansag;
- }
- int isEmptyKivansag(struct Kivansag kivansag){
- return (strcmp(kivansag.gift,"") == 0);
- }
- char *swapSpace(char *row){
- int count = 0;
- while(*row != '\0'){
- ++count;
- if(*row == ' '){
- *row = '_';
- }
- ++row;
- }
- int i;
- for(i = 0; i < count; i++){
- --row;
- }
- return row;
- }
- char *kivansagToRow(struct Kivansag kivansag){
- char *row;
- row = kivansag.date;
- strcat(row,",");
- strcat(row,kivansag.name);
- strcat(row,",");
- strcat(row,kivansag.city);
- strcat(row,",");
- strcat(row,kivansag.address);
- strcat(row,",");
- strcat(row,kivansag.gift);
- strcat(row,"\n");
- return row;
- }
- void cutEnter(char string[100]){
- char *pos;
- if ((pos=strchr(string, '\n')) != NULL)
- *pos = '\0';
- }
- void menu_print(){
- printf("\n*********************************************************\n");
- printf("Udv a Mikulas kezelofeluleten! Az alabbiak kozul valaszthat:\n"
- " (1) Ajandek kuldese gyujtohelyrol, egy varosba, sok ajandekot\n"
- " (2) Ajandek kuldese uzembol, sok varosba, egy fele ajandekot\n"
- " (3) Kilepes\n");
- }
- int isCorrect(char answer[10]){
- return *answer-48;
- }
- struct Kivansag rowToKivansag(char row[1000]){
- struct Kivansag kivansag;
- int i = 0;
- int j = 0;
- int count = 0;
- int lastPos = 0;
- while(row[i] != '\0'){
- if(row[i] == ','){
- ++count;
- lastPos = j;
- j = 0;
- }else{
- if(count == 0){
- if(row[i] == '_'){
- kivansag.date[j] = ' ';
- }
- else{
- kivansag.date[j] = row[i];
- }
- ++j;
- }
- if(count == 1){
- kivansag.date[lastPos] = '\0';
- if(row[i] == '_'){
- kivansag.name[j] = ' ';
- }
- else{
- kivansag.name[j] = row[i];
- }
- ++j;
- }
- if(count == 2){
- kivansag.name[lastPos] = '\0';
- if(row[i] == '_'){
- kivansag.city[j] = ' ';
- }
- else{
- kivansag.city[j] = row[i];
- }
- ++j;
- }
- if(count == 3){
- kivansag.city[lastPos] = '\0';
- if(row[i] == '_'){
- kivansag.address[j] = ' ';
- }
- else{
- kivansag.address[j] = row[i];
- }
- ++j;
- }
- if(count == 4){
- kivansag.address[lastPos] = '\0';
- if(row[i] == '_'){
- kivansag.gift[j] = ' ';
- }
- else{
- kivansag.gift[j] = row[i];
- }
- ++j;
- }
- }
- ++i;
- }
- kivansag.gift[j] = '\0';
- return kivansag;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement