Advertisement
weeez

vki

Nov 22nd, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.95 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/wait.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include <signal.h>
  12.  
  13. // CONSTANTS
  14. const int MAX_SIZE = 1280;
  15. const char *GIFT[] = {"puppet", "car", "ball", "puzzle", "colouring", "book"};
  16. const char *filename = "letters.txt";
  17. int GIFT_LENGTH = sizeof(GIFT)/sizeof(char*);
  18. const char* FIFONAME = "FIFO";
  19.  
  20. struct letter
  21. {
  22.         char date[256];
  23.         char name[256];
  24.         char city[256];
  25.         char address[256];
  26.         char gift[256];
  27. };
  28.  
  29. void run();
  30. void list();
  31. void write_letter();
  32. void deliver(pid_t sledge);
  33. void search();
  34. struct letter create_from_data();
  35. int is_correct_gift(char* gift);
  36.  
  37. int main(int argc, char** argv)
  38. {      
  39.         run();
  40.        
  41.         return 0;
  42. }
  43.  
  44. void run()
  45. {
  46.         FILE* file = fopen(filename, "a");
  47.         fclose(file);
  48.        
  49.         pid_t sledge;
  50.        
  51.         char option[3];
  52.         while(1)
  53.         {
  54.                 printf("<l> Letters\n");
  55.                 printf("<w> Write letter\n");
  56.                 printf("<d> Deliver\n");
  57.                 printf("<q> Quit\n");
  58.                 printf("\noption~> ");
  59.                 fgets(option, sizeof(option), stdin);
  60.                 option[strlen(option) - 1] = 0;
  61.                 if( strcmp(option,"l") == 0 )
  62.                 {
  63.                         list();
  64.                 }
  65.                 else if( strcmp(option,"w") == 0 )
  66.                 {
  67.                         write_letter();
  68.                 }
  69.                 else if( strcmp(option,"d") == 0 )
  70.                 {
  71.                         deliver(sledge);
  72.                         wait(&sledge);
  73.                 }
  74.                 else if( strcmp(option,"q") == 0 )
  75.                 {
  76.                         exit(0);
  77.                 }
  78.                 else{
  79.                         perror("Invalid option.\n");
  80.                 }
  81.                
  82.         }      
  83. }
  84.  
  85. void list()
  86. {
  87.         FILE* file = fopen(filename, "r");
  88.         char line[MAX_SIZE];
  89.         int count = 0;
  90.         while(fgets(line,sizeof(line), file))
  91.         {
  92.                 printf("%s", line);
  93.                 ++count;
  94.         }
  95.         printf("Total letters: %d\n", count);
  96.         fclose(file);
  97. }
  98.  
  99. void write_letter()
  100. {              
  101.         struct letter let = create_from_data();
  102.        
  103.         FILE* file = fopen(filename, "a+");
  104.        
  105.         char line[MAX_SIZE];
  106.         int line_number = 0;
  107.         int exists = 0;
  108.        
  109.         while(!feof(file) && exists == 0)
  110.         {              
  111.                 fgets(line, sizeof(line), file);
  112.                 ++line_number;
  113.                 if((strstr(line, let.name) != NULL) && (strstr(line, let.city) != NULL) && (strstr(line, let.address) != NULL))
  114.                 {
  115.                         exists = 1;
  116.                 }
  117.         }
  118.        
  119.         fclose(file);
  120.        
  121.         if(exists == 1)
  122.         {
  123.                 file = fopen(filename, "r");
  124.                 FILE* tmpfile = fopen("tmpfile", "a+");
  125.                 int count = 0;
  126.                 while(fgets(line, sizeof(line), file) != NULL)
  127.                 {
  128.                         ++count;
  129.                         if(count != line_number ) fputs(line, tmpfile);
  130.                         else fprintf(tmpfile, "%s %s %s %s %s\n",let.date, let.name, let.city, let.address, let.gift);
  131.                 }
  132.                
  133.                 fclose(file);
  134.                 fclose(tmpfile);
  135.        
  136.                 remove(filename);
  137.                 rename("tmpfile", filename);
  138.         }
  139.         else
  140.         {
  141.                 file = fopen(filename, "a+");
  142.                 fprintf(file, "%s %s %s %s %s\n",let.date, let.name, let.city, let.address, let.gift);
  143.                 fclose(file);
  144.         }
  145. }
  146.  
  147. void deliver(pid_t sledge)
  148. {      
  149.         int fd;
  150.        
  151.         int fid = mkfifo(FIFONAME, S_IRUSR|S_IWUSR);
  152.        
  153.         if(fid == -1)
  154.         {
  155.                 printf("Pipe error number: %i", errno);
  156.                 exit(1);
  157.         }
  158.        
  159.         char option[3];
  160.         printf("On city or gift? <c/g> ");
  161.         fgets(option, sizeof(option), stdin);
  162.         option[strlen(option) - 1] = 0;
  163.        
  164.         char str[MAX_SIZE];
  165.         if( strcmp(option,"c") == 0 )
  166.         {
  167.                
  168.                 printf("City> ");
  169.         }
  170.         else if( strcmp(option,"g") == 0 )
  171.         {
  172.                 printf("Gift> ");
  173.         }
  174.        
  175.         fgets(str, sizeof(str), stdin);
  176.         str[strlen(str) - 1] = 0;
  177.        
  178.         write(fd, &str, sizeof(str));
  179.         printf("Beleírtam a csobe: %s\n", str);
  180.        
  181.         // SLEDGE PROCESS
  182.         switch((sledge = fork()))
  183.         {
  184.                 case -1:
  185.             printf("Error forking sledge!\n");
  186.             exit(EXIT_FAILURE);
  187.  
  188.         case 0:
  189.             printf("Sledge is ready.\n");
  190.                        
  191.                         char string[MAX_SIZE];
  192.                         read(fd, &string, sizeof(string));
  193.                         printf("Telaputol kapott string: %s\n", string);
  194.                        
  195.                         sleep(1);
  196.                         printf("Sledge is returned.\n");
  197.                         exit(EXIT_SUCCESS);
  198.         }
  199.        
  200.         unlink(FIFONAME);
  201. }
  202.  
  203. void search()
  204. {
  205.         FILE* file = fopen(filename, "r");
  206.         char option[3];
  207.        
  208.         printf("On city or gift? <c/g> ");
  209.         fgets(option, sizeof(option), stdin);
  210.         option[strlen(option) - 1] = 0;
  211.        
  212.         char pattern[MAX_SIZE];
  213.         char line[MAX_SIZE];
  214.         if( strcmp(option,"c") == 0 )
  215.         {
  216.                
  217.                 printf("City> ");
  218.         }
  219.         else if( strcmp(option,"g") == 0 )
  220.         {
  221.                 printf("Gift> ");
  222.         }
  223.        
  224.         fgets(pattern, sizeof(pattern), stdin);
  225.         pattern[strlen(pattern) - 1] = 0;
  226.        
  227.         while(fgets(line,sizeof(line), file))
  228.         {
  229.                 if(strstr(line, pattern) != NULL)
  230.                 {
  231.                         printf("%s", line);
  232.                 }
  233.         }
  234.        
  235.         fclose(file);
  236. }
  237.  
  238. struct letter create_from_data()
  239. {
  240.         struct letter letter;
  241.         int correct;
  242.         time_t time_raw_format;
  243.     struct tm * ptr_time;
  244.        
  245.         printf("\nWrite letter for Santa. Format: <Name> <City> <Address> <Gift>\n");
  246.  
  247.     time ( &time_raw_format );
  248.     ptr_time = localtime ( &time_raw_format );
  249.     if(strftime(letter.date,sizeof(letter.date),"%Y.%m.%d",ptr_time) == 0){
  250.         printf("YYYY.MM.DD> ");
  251.                 fgets(letter.date, sizeof(letter.date), stdin);
  252.                 letter.date[strlen(letter.city) - 1] = 0;
  253.     }
  254.        
  255.         printf("Name> ");
  256.         fgets(letter.name, sizeof(letter.name), stdin);
  257.         letter.name[strlen(letter.name) - 1] = 0;
  258.         printf("City> ");
  259.         fgets(letter.city, sizeof(letter.city), stdin);
  260.         letter.city[strlen(letter.city) - 1] = 0;
  261.         printf("Address> ");
  262.         fgets(letter.address, sizeof(letter.address), stdin);
  263.         letter.address[strlen(letter.address) - 1] = 0;
  264.         do
  265.         {
  266.                 //char gift[MAX_SIZE];
  267.                 printf("Gift> ");
  268.                 fgets(letter.gift, sizeof(letter.gift), stdin);
  269.                 letter.gift[strlen(letter.gift) - 1] = 0;
  270.                 correct = is_correct_gift(letter.gift);
  271.                 if(correct != 0) printf("Invalid gift: %s\n", letter.gift);    
  272.         } while (correct != 0);
  273.        
  274.         return letter;
  275. }
  276.  
  277. int is_correct_gift(char *gift)
  278. {
  279.         int correct = 1;
  280.         int gift_index;
  281.         for(gift_index = 0; gift_index < GIFT_LENGTH; ++gift_index)
  282.         {
  283.                 if(strcmp(GIFT[gift_index], gift) == 0)
  284.                 {
  285.                         correct = 0;
  286.                         break;
  287.                 }
  288.         }
  289.         return correct;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement