ronyal

Untitled

Nov 20th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8.  
  9.  
  10. #include <stdbool.h>
  11. #define BUF_SIZE 2
  12. #define minus "-1"
  13. #define enter "\n"
  14.  
  15.  
  16. // gets 4 elements, filename ,in1, in2,out
  17. int main(int argc, char* argv[])
  18. {
  19.  
  20.     int input1_fd,input2_fd, output_fd,curfile = 2 , enOrMinus=0;
  21.     char buffer[BUF_SIZE];
  22.     bool flag = true ,eof1 = true, eof2 = true ,lflag = true;
  23.  
  24.             /* Number of bytes returned by read() and write() */
  25.     /* Character buffer */
  26.     /* Are src and dest file name arguments missing */
  27.  
  28.     if(argc != 4)
  29.     {
  30.         printf ("arguments missing");
  31.         return 1;
  32.     }
  33.  
  34.     /* Input and output file
  35.     /*Create input file descriptor*/
  36.     input1_fd = open (argv[1],O_RDONLY);
  37.     if (input1_fd == -1)
  38.     {
  39.       perror("open");
  40.       return 1;
  41.     }
  42.     input2_fd = open (argv[2],O_RDONLY);
  43.     if (input2_fd == -1)
  44.     {
  45.       perror("open");
  46.       return 2;
  47.     }
  48.  
  49.  
  50.  
  51.     /*Create output file descriptor*/
  52.  
  53.     output_fd = open (argv[3],O_WRONLY | O_CREAT,0644);
  54.     if (output_fd == -1)
  55.     {
  56.       perror("open");
  57.       return 3;
  58.     }
  59.  
  60.  
  61.  
  62.     while(flag)         // while we still have tx1 and tx2
  63.     {
  64.         switch (curfile)            //checks what file we are in
  65.         {
  66.             case 1 :
  67.             {
  68.                 //printf("curfile is = %d\n",curfile);
  69.                 read(input1_fd, buffer,1); // reads 1 char to buffer
  70.                 if(strcmp(buffer,enter) == 0) // checks if enter
  71.                 enOrMinus=1;
  72.                 if(strcmp(buffer,minus) == 0) // checks if enter
  73.                 enOrMinus=2;
  74.  
  75.                 switch(enOrMinus)           //  checks if enter
  76.                     {
  77.                                         case 1 :
  78.  
  79.                                                     if(eof2)            // enter and tx2 not empty
  80.                                                         {
  81.                                                             curfile = 2;
  82.                                                             lflag = false;
  83.                                                             write(output_fd,buffer,1); // writes enter to output
  84.                                                         }
  85.                                         break; // break enter
  86.  
  87.  
  88.                                         case 2 :            // end of for file 1
  89.                                         {
  90.                                                 eof1 = false;
  91.                                                 if(eof2)        //checks stile have file 2
  92.                                                 {
  93.                                                     curfile = 2;
  94.                                                     lflag = false;
  95.                                                 }
  96.  
  97.                                         break; // break minus
  98.                                     }
  99.                     }// end switch(buffer)
  100.                 }// end case 1
  101.                 break; // break case 1
  102.             case 2 :
  103.             {
  104.             //  printf("%d\n",curfile);
  105.                 read(input1_fd, buffer,1); // reads 1 char to buffer
  106.                 if(strcmp(buffer,enter) == 0) // checks if enter
  107.                 enOrMinus=1;
  108.                 if(strcmp(buffer,minus) == 0) // checks if enter
  109.                 enOrMinus=2;
  110.                 switch(enOrMinus)           //  checks if enter
  111.                     {
  112.                                         case 1 :
  113.  
  114.                                                     if(eof1)            // enter and tx1 not empty
  115.                                                         {
  116.                                                             curfile = 1;
  117.                                                             lflag = false;
  118.                                                             write(output_fd,buffer,1); // writes enter to output
  119.                                                         }
  120.                                         break; // end enter
  121.  
  122.  
  123.                                         case 2 :            // end of for file 2
  124.                                         {
  125.                                                 eof2 = false;
  126.                                                 if(eof1)        //checks stile have file 1
  127.                                                 {
  128.                                                     curfile = 1;
  129.                                                     lflag = false;
  130.                                                 }
  131.                                         }
  132.                                         break; // end minus
  133.                             }// end switch(buffer)
  134.                     } // end case2
  135.  
  136.             break; // end case 2
  137.  
  138.         }// end switch(curfile)
  139.  
  140.         // chesks if a letter
  141.         if((strcmp(buffer,enter) !=0 && ( strcmp(buffer,minus) !=0)))
  142.         {
  143.             write(output_fd,buffer,1); // writes enter to output
  144.         }
  145.  
  146.         if (eof1 == false && eof2 == false)
  147.             {
  148.                 flag=false;
  149.                 printf("output file generated succssesfuly");
  150.                 close(input1_fd);
  151.                 close(input2_fd);
  152.                 close(output_fd);
  153.             }
  154.         } // end while loop
  155. }// end main
Advertisement
Add Comment
Please, Sign In to add comment