Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <signal.h>
- // CONSTANTS
- const int MAX_SIZE = 1280;
- const char *GIFT[] = {"puppet", "car", "ball", "puzzle", "colouring", "book"};
- const char *filename = "letters.txt";
- int GIFT_LENGTH = sizeof(GIFT)/sizeof(char*);
- const char* FIFONAME = "FIFO";
- struct letter
- {
- char date[256];
- char name[256];
- char city[256];
- char address[256];
- char gift[256];
- };
- void run();
- void list();
- void write_letter();
- void deliver(pid_t sledge);
- void search();
- struct letter create_from_data();
- int is_correct_gift(char* gift);
- int main(int argc, char** argv)
- {
- run();
- return 0;
- }
- void run()
- {
- FILE* file = fopen(filename, "a");
- fclose(file);
- pid_t sledge;
- char option[3];
- while(1)
- {
- printf("<l> Letters\n");
- printf("<w> Write letter\n");
- printf("<d> Deliver\n");
- printf("<q> Quit\n");
- printf("\noption~> ");
- fgets(option, sizeof(option), stdin);
- option[strlen(option) - 1] = 0;
- if( strcmp(option,"l") == 0 )
- {
- list();
- }
- else if( strcmp(option,"w") == 0 )
- {
- write_letter();
- }
- else if( strcmp(option,"d") == 0 )
- {
- deliver(sledge);
- wait(&sledge);
- }
- else if( strcmp(option,"q") == 0 )
- {
- exit(0);
- }
- else{
- perror("Invalid option.\n");
- }
- }
- }
- void list()
- {
- FILE* file = fopen(filename, "r");
- char line[MAX_SIZE];
- int count = 0;
- while(fgets(line,sizeof(line), file))
- {
- printf("%s", line);
- ++count;
- }
- printf("Total letters: %d\n", count);
- fclose(file);
- }
- void write_letter()
- {
- struct letter let = create_from_data();
- FILE* file = fopen(filename, "a+");
- char line[MAX_SIZE];
- int line_number = 0;
- int exists = 0;
- while(!feof(file) && exists == 0)
- {
- fgets(line, sizeof(line), file);
- ++line_number;
- if((strstr(line, let.name) != NULL) && (strstr(line, let.city) != NULL) && (strstr(line, let.address) != NULL))
- {
- exists = 1;
- }
- }
- fclose(file);
- if(exists == 1)
- {
- file = fopen(filename, "r");
- FILE* tmpfile = fopen("tmpfile", "a+");
- int count = 0;
- while(fgets(line, sizeof(line), file) != NULL)
- {
- ++count;
- if(count != line_number ) fputs(line, tmpfile);
- else fprintf(tmpfile, "%s %s %s %s %s\n",let.date, let.name, let.city, let.address, let.gift);
- }
- fclose(file);
- fclose(tmpfile);
- remove(filename);
- rename("tmpfile", filename);
- }
- else
- {
- file = fopen(filename, "a+");
- fprintf(file, "%s %s %s %s %s\n",let.date, let.name, let.city, let.address, let.gift);
- fclose(file);
- }
- }
- void deliver(pid_t sledge)
- {
- int fd;
- int fid = mkfifo(FIFONAME, S_IRUSR|S_IWUSR);
- if(fid == -1)
- {
- printf("Pipe error number: %i", errno);
- exit(1);
- }
- char option[3];
- printf("On city or gift? <c/g> ");
- fgets(option, sizeof(option), stdin);
- option[strlen(option) - 1] = 0;
- char str[MAX_SIZE];
- if( strcmp(option,"c") == 0 )
- {
- printf("City> ");
- }
- else if( strcmp(option,"g") == 0 )
- {
- printf("Gift> ");
- }
- fgets(str, sizeof(str), stdin);
- str[strlen(str) - 1] = 0;
- write(fd, &str, sizeof(str));
- printf("Beleírtam a csobe: %s\n", str);
- // SLEDGE PROCESS
- switch((sledge = fork()))
- {
- case -1:
- printf("Error forking sledge!\n");
- exit(EXIT_FAILURE);
- case 0:
- printf("Sledge is ready.\n");
- char string[MAX_SIZE];
- read(fd, &string, sizeof(string));
- printf("Telaputol kapott string: %s\n", string);
- sleep(1);
- printf("Sledge is returned.\n");
- exit(EXIT_SUCCESS);
- }
- unlink(FIFONAME);
- }
- void search()
- {
- FILE* file = fopen(filename, "r");
- char option[3];
- printf("On city or gift? <c/g> ");
- fgets(option, sizeof(option), stdin);
- option[strlen(option) - 1] = 0;
- char pattern[MAX_SIZE];
- char line[MAX_SIZE];
- if( strcmp(option,"c") == 0 )
- {
- printf("City> ");
- }
- else if( strcmp(option,"g") == 0 )
- {
- printf("Gift> ");
- }
- fgets(pattern, sizeof(pattern), stdin);
- pattern[strlen(pattern) - 1] = 0;
- while(fgets(line,sizeof(line), file))
- {
- if(strstr(line, pattern) != NULL)
- {
- printf("%s", line);
- }
- }
- fclose(file);
- }
- struct letter create_from_data()
- {
- struct letter letter;
- int correct;
- time_t time_raw_format;
- struct tm * ptr_time;
- printf("\nWrite letter for Santa. Format: <Name> <City> <Address> <Gift>\n");
- time ( &time_raw_format );
- ptr_time = localtime ( &time_raw_format );
- if(strftime(letter.date,sizeof(letter.date),"%Y.%m.%d",ptr_time) == 0){
- printf("YYYY.MM.DD> ");
- fgets(letter.date, sizeof(letter.date), stdin);
- letter.date[strlen(letter.city) - 1] = 0;
- }
- printf("Name> ");
- fgets(letter.name, sizeof(letter.name), stdin);
- letter.name[strlen(letter.name) - 1] = 0;
- printf("City> ");
- fgets(letter.city, sizeof(letter.city), stdin);
- letter.city[strlen(letter.city) - 1] = 0;
- printf("Address> ");
- fgets(letter.address, sizeof(letter.address), stdin);
- letter.address[strlen(letter.address) - 1] = 0;
- do
- {
- //char gift[MAX_SIZE];
- printf("Gift> ");
- fgets(letter.gift, sizeof(letter.gift), stdin);
- letter.gift[strlen(letter.gift) - 1] = 0;
- correct = is_correct_gift(letter.gift);
- if(correct != 0) printf("Invalid gift: %s\n", letter.gift);
- } while (correct != 0);
- return letter;
- }
- int is_correct_gift(char *gift)
- {
- int correct = 1;
- int gift_index;
- for(gift_index = 0; gift_index < GIFT_LENGTH; ++gift_index)
- {
- if(strcmp(GIFT[gift_index], gift) == 0)
- {
- correct = 0;
- break;
- }
- }
- return correct;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement