Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- András
- Search Drive
- Drive
- .
- Folder Path
- My Drive
- Egyetem
- NEW
- Folders and views
- My Drive
- Shared with me
- Google Photos
- Recent
- Starred
- Bin
- 530 MB of 15 GB used
- Upgrade storage
- .
- Java
- .
- Google Sheets
- BA Órarend
- Text
- bead.txt
- Google Docs
- Korábbi pluszminusz
- Google Sheets
- Követelmények
- Google Docs
- pluszminusz
- Google Sheets
- Tárgyaim megléte
- Text
- bead.txt
- Details
- Activity
- YESTERDAY
- Youuploaded an item
- Fri 13:03
- Text
- bead.txt
- No recorded activity before 4 December 2015
- Get Drive for PC
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <wait.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #define CHAR_SIZE 512
- typedef enum { false, true } bool;
- typedef struct {
- char date[CHAR_SIZE];
- char name[CHAR_SIZE];
- char city[CHAR_SIZE];
- char address[CHAR_SIZE];
- char gift_name[CHAR_SIZE];
- } request_data;
- struct message {
- long mtype; //ez egy szabadon hasznalhato ertek, pl uzenetek osztalyozasara
- char mtext[CHAR_SIZE];
- };
- void write_list(request_data* requests, int number_of_request);
- int write_list_with_filter_city(char* filter, request_data* requests, int number_of_request, char* msg[], bool b);
- int write_list_with_filter_gift(char* filter, request_data* requests, int number_of_request, char* msg[], bool b);
- void create_new_request(request_data* requests, int* number_of_request);
- void write_list_to_file(request_data* requests, int number_of_request, const char* file_name);
- bool check_gift_type(const char* gift_type);
- void read_from_file(const char* file_name, request_data* requests, int* number_of_request);
- int check_request(request_data* requests, int number_of_request, request_data request);
- int send_message(int msg_id, char* msg_string);
- void deliver_gifts(char* filter, char* name, request_data* requests, int number_of_request, char* msg[], const char* argv[]);
- void write_list_to_file_mikulas(const char* file_name, char* msg);
- int main(int argc, const char * argv[]) {
- while(true) {
- char* msg[CHAR_SIZE];
- char tmp[CHAR_SIZE];
- int number_of_request;
- request_data requests[512];
- read_from_file("data.txt", requests, &number_of_request);
- printf("\nKerem valasszon az alabbi lehetosegek kozul:\n");
- printf("- Uj keres felvetele(ujkeres)\n");
- printf("- Lista kiirasa(kiiras)\n");
- printf("- Listaba kereses(keres)\n");
- printf("- Ajandekok kiszallitasa(szallit)\n");
- printf("- Kilepes (exit)\n\n");
- fgets(tmp, sizeof(tmp), stdin);
- if(strncmp(tmp, "kiiras", 6) == 0) {
- write_list(requests, number_of_request);
- }
- if(strncmp(tmp, "keres", 5) == 0) {
- printf("- Mi szerint szeretne keresni?(varos/ajandek)\n");
- fgets(tmp, sizeof(tmp), stdin);
- if(strncmp(tmp, "varos", 5) == 0) {
- printf("- Kerem a varos nevet\n");
- fgets(tmp, sizeof(tmp), stdin);
- write_list_with_filter_city(tmp, requests, number_of_request, msg, true);
- } else if(strncmp(tmp, "ajandek", 7) == 0) {
- printf("- Kerem az ajandek nevet\n");
- fgets(tmp, sizeof(tmp), stdin);
- write_list_with_filter_gift(tmp, requests, number_of_request, msg, true);
- }
- }
- if(strncmp(tmp, "szallit", 7) == 0) {
- printf("- Mi szerint szeretne szallitani?(varos/ajandek)\n");
- fgets(tmp, sizeof(tmp), stdin);
- if(strncmp(tmp, "varos", 5) == 0) {
- printf("- Kerem a varos nevet\n");
- fgets(tmp, sizeof(tmp), stdin);
- deliver_gifts("varos", tmp, requests, number_of_request, msg, argv);
- } else if(strncmp(tmp, "ajandek", 7) == 0) {
- printf("- Kerem az ajandek nevet\n");
- fgets(tmp, sizeof(tmp), stdin);
- deliver_gifts("ajandek", tmp, requests, number_of_request, msg, argv);
- }
- }
- if(strncmp(tmp, "ujkeres", 7) == 0) {
- create_new_request(requests, &number_of_request);
- write_list_to_file(requests, number_of_request, "data.txt");
- }
- if(strncmp(tmp, "exit", 4) == 0) {
- break;
- }
- }
- return 0;
- }
- void create_new_request(request_data* requests, int* number_of_request) {
- bool end = false;
- char tmp[CHAR_SIZE];
- while(!end) {
- request_data request;
- int i = 0;
- while(i < 5) {
- if(i == 0) {
- time_t timer;
- char buffer[CHAR_SIZE];
- struct tm* tm_info;
- time(&timer);
- tm_info = localtime(&timer);
- strftime(buffer, 26, "%Y.%m.%d. %H:%M:%S\n", tm_info);
- strcpy(request.date, buffer);
- } else if(i == 1) {
- printf("\nKerem a gyermek nevet!:(Jozsika)\n");
- fgets(tmp, sizeof(tmp), stdin);
- strcpy(request.name, tmp);
- } else if(i == 2) {
- printf("Kerem a ajandek nevet!:(Baba Konyv)\n");
- fgets(tmp, sizeof(tmp), stdin);
- bool gift_type = check_gift_type(tmp);
- while(gift_type == false) {
- printf("Kerem a ajandek nevet!:(Baba Konyv)\n");
- fgets(tmp, sizeof(tmp), stdin);
- gift_type = check_gift_type(tmp);
- }
- strcpy(request.gift_name, tmp);
- } else if(i == 3) {
- printf("Kerem a varos nevet!:(Budapest)\n");
- fgets(tmp, sizeof(tmp), stdin);
- strcpy(request.city, tmp);
- } else if(i == 4) {
- printf("Kerem a pontos cimet!:(Almafa utca 1)\n");
- fgets(tmp, sizeof(tmp), stdin);
- strcpy(request.address, tmp);
- int index = check_request(requests, *number_of_request, request);
- if(index == -1) {
- requests[*number_of_request] = request;
- ++(*number_of_request);
- } else {
- strcpy(requests[index].name, request.name);
- strcpy(requests[index].address, request.address);
- strcpy(requests[index].gift_name, request.gift_name);
- strcpy(requests[index].city, request.city);
- strcpy(requests[index].date, request.date);
- }
- }
- i++;
- }
- printf("Szeretne meg kerest rogziteni?(y/n)\n");
- fgets(tmp, sizeof(tmp), stdin);
- const char* yes = "y";
- if(strncmp(tmp, yes, 1) != 0) {
- end = true;
- }
- }
- }
- void deliver_gifts(char* filter, char* name, request_data* requests, int number_of_request, char* msg[], const char* argv[]) {
- int pid, fd;
- int fid = mkfifo ("pipe", 0666);
- int uzenetsor, status;
- key_t key;
- key = ftok(argv[0], 1);
- uzenetsor = msgget(key, 0600 | IPC_CREAT);
- if (fid == -1) {
- printf("Error number: %i",errno);
- exit(EXIT_FAILURE);
- }
- pid = fork();
- if (pid > 0) {
- int wait_status;
- fd = open("pipe", O_WRONLY);
- write(fd, filter, sizeof(filter));
- close(fd);
- waitpid(pid, &wait_status, -1);
- //üzenet
- struct message uz;
- status = msgrcv(uzenetsor, &uz, CHAR_SIZE, 5, 0 );
- if(status >= 0) {
- printf("A mikulas megkapta a manoktol a listat es kiirta egy fajlba!\nBoldog karacsonyt gyerekek!\n");
- if(strncmp(filter, "varos", 5) == 0) {
- write_list_to_file_mikulas("varos.txt", uz.mtext);
- } else {
- write_list_to_file_mikulas("ajandek.txt", uz.mtext);
- }
- } else {
- perror("hiba");
- }
- } else {
- char msg_from_santa[CHAR_SIZE];
- int amount;
- fd = open("pipe", O_RDONLY);
- read(fd, msg_from_santa, sizeof(msg_from_santa));
- if (strncmp("varos", msg_from_santa, 5) == 0) {
- amount = write_list_with_filter_city(name, requests, number_of_request, msg, false);
- } else if (strncmp("ajandek", msg_from_santa, 7) == 0) {
- amount = write_list_with_filter_gift(name, requests, number_of_request, msg, false);
- }
- char tmp[CHAR_SIZE];
- int i;
- for(i = 0; i < amount; i++) {
- if(i == 0) {
- strcpy(tmp, msg[i]);
- } else {
- strcat(tmp, msg[i]);
- }
- }
- send_message(uzenetsor, tmp);
- wait( NULL );
- status = msgctl( uzenetsor, IPC_RMID, NULL );
- if ( status < 0 ) {
- perror("msgctl");
- }
- close(fd);
- unlink("pipe");
- exit(0);
- }
- }
- int send_message(int msg_id, char* msg_string) {
- struct message msg;
- strcpy(msg.mtext, msg_string);
- msg.mtype = 5;
- int status;
- status = msgsnd(msg_id, &msg, strlen (msg.mtext) + 1 , 0);
- if ( status < 0 )
- perror("msgsnd");
- return 0;
- }
- void read_from_file(const char* file_name, request_data* requests, int* number_of_request) {
- FILE *file;
- *number_of_request = 0;
- char tmp[512];
- file = fopen(file_name, "r");
- int rows = 0;
- request_data request;
- while (fgets(tmp, 100, file) != NULL) {
- if(strcmp(tmp, "\n") != 0) {
- if(rows == 0) {
- strcpy(request.date, tmp);
- } else if(rows == 1) {
- strcpy(request.name, tmp);
- } else if(rows == 2) {
- strcpy(request.gift_name, tmp);
- } else if(rows == 3) {
- strcpy(request.city, tmp);
- } else if(rows == 4) {
- rows = -1;
- strcpy(request.address, tmp);
- requests[*number_of_request] = request;
- ++(*number_of_request);
- }
- rows++;
- }
- }
- fclose(file);
- }
- void write_list(request_data* requests, int number_of_request) {
- int i = 0;
- for(i = 0; i < number_of_request; i++) {
- printf("%s%s%s%s%s\n",
- requests[i].date, requests[i].name,
- requests[i].gift_name, requests[i].city, requests[i].address);
- }
- printf("A manok %d kisgyermek kivansagat rogzitettek!\n", i);
- }
- int write_list_with_filter_city(char* filter, request_data* requests, int number_of_request, char* msg[], bool b) {
- int i;
- int amount = 0;
- for(i = 0; i < number_of_request; i++) {
- if(strcmp(filter, requests[i].city) == 0) {
- char tmp[CHAR_SIZE];
- strcpy(tmp, requests[i].date);
- strcat(tmp, requests[i].name);
- strcat(tmp, requests[i].gift_name);
- strcat(tmp, requests[i].city);
- strcat(tmp, requests[i].address);
- strcat(tmp, "\n");
- msg[amount] = tmp;
- if(b) {
- printf("%s%s%s%s%s\n",
- requests[i].date, requests[i].name,
- requests[i].gift_name, requests[i].city, requests[i].address);
- }
- amount++;
- }
- }
- if(b) printf("%d kisgyermek kivansaga erkezett a varosbol!\n", amount);
- return amount;
- }
- int write_list_with_filter_gift(char* filter, request_data* requests, int number_of_request, char* msg[], bool b){
- int i;
- int amount = 0;
- for(i = 0; i < number_of_request; i++) {
- if(strcmp(filter, requests[i].gift_name) == 0) {
- char tmp[CHAR_SIZE];
- strcpy(tmp, requests[i].date);
- strcat(tmp, requests[i].name);
- strcat(tmp, requests[i].gift_name);
- strcat(tmp, requests[i].city);
- strcat(tmp, requests[i].address);
- strcat(tmp, "\n");
- msg[amount] = tmp;
- if(b) {
- printf("%s%s%s%s%s\n",
- requests[i].date, requests[i].name,
- requests[i].gift_name, requests[i].city, requests[i].address);
- }
- amount++;
- }
- }
- if(b) printf("%d darab ajandek van a listan!\n", amount);
- return amount;
- }
- bool check_gift_type(const char* gift_type) {
- if(strncmp("Baba", gift_type, 4) == 0) { return true; }
- if(strncmp("Auto", gift_type, 4) == 0) { return true; }
- if(strncmp("Labda", gift_type, 5) == 0) { return true; }
- if(strncmp("Kirako", gift_type, 6) == 0) { return true; }
- if(strncmp("Szinezo", gift_type, 7) == 0) { return true; }
- if(strncmp("Konyv", gift_type, 5) == 0) { return true; }
- return false;
- }
- void write_list_to_file(request_data* requests, int number_of_request, const char* file_name) {
- FILE *file;
- file = fopen(file_name, "w");
- int i;
- for(i = 0; i < number_of_request; i++) {
- fprintf(file, "%s%s%s%s%s\n",
- requests[i].date, requests[i].name,
- requests[i].gift_name, requests[i].city, requests[i].address);
- }
- fclose(file);
- }
- void write_list_to_file_mikulas(const char* file_name, char* msg) {
- FILE *file;
- file = fopen(file_name, "w");
- fprintf(file, "%s", msg);
- fclose(file);
- }
- int check_request(request_data* requests, int number_of_request, request_data request) {
- int i;
- for(i = 0; i < number_of_request; i++) {
- if(strcmp(requests[i].name, request.name) == 0) {
- return i;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement