Advertisement
Guest User

cat

a guest
Sep 25th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5.  
  6. #define BUFFERSIZE 4096
  7. #define COPYMODE 0644
  8. #define STDOUT 2
  9. void FilePrinter(int,int, FILE *);
  10.  
  11. int main(int ac, char *av[])
  12. {
  13.     int  in_fd, out_fd, n_chars, flag;
  14.     int i;
  15.     char buf[BUFFERSIZE];
  16.     FILE *fp;
  17.                        
  18.     /* check arguments */
  19.     //if (ac != 3){
  20.         //fprintf(stderr, "usage: %s source destination\n", *av);
  21.         //return 1;
  22.     //}
  23.    
  24.     if (strcmp(av[1], "-e") == 0 )
  25.     {
  26.         flag = 1;
  27.     }
  28.     else if (strcmp(av[1], "-n") == 0 )
  29.     {
  30.         flag = 2;
  31.     }
  32.     else if (strcmp(av[1], "-s") == 0 )
  33.     {
  34.         flag = 3;
  35.     }
  36.     else
  37.     {
  38.         flag = 0;
  39.     }
  40.    
  41.     if(flag == 0)
  42.     {
  43.         i = 1;
  44.     }
  45.     else
  46.     {
  47.         i = 2;
  48.     }
  49.     for( ; i <= ac; i++)
  50.     {
  51.         fp = fopen(av[i], "r");
  52.         //if ((in_fd=open(av[i], O_RDONLY)) == -1)
  53.         //{
  54.             //perror("Cannot open source file");
  55.             //return 1;
  56.         //}
  57.         FilePrinter(in_fd, flag, fp);
  58.     }
  59.    
  60.  
  61. }
  62.  
  63. void FilePrinter(int iFileHandler, int treatment, FILE * ffp)
  64. {
  65.     char buf[BUFFERSIZE];
  66.     //while ((n_chars = read(iFileHandler, buf, BUFFERSIZE)) > 0)
  67.     while(fgets(buf, BUFFERSIZE, ffp) != NULL)
  68.     {
  69.         if(treatment == 0)
  70.         {
  71.             //if (write(STDOUT, buf, n_chars) != n_chars)
  72.             //{
  73.                 //perror("Write error");  
  74.                 //return 1;
  75.             //}
  76.             printf("%s", buf);
  77.         }
  78.         if( treatment == 1)
  79.         {
  80.             char tempBuf[strlen(buf) + 2];
  81.             strncpy(tempBuf, buf, strlen(buf)+2);
  82.             tempBuf[strlen(buf) - 1] = '$';
  83.             tempBuf[strlen(buf)] = '\n';
  84.             tempBuf[strlen(buf) + 1] = '\0';
  85.             printf("%s", tempBuf);
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement