Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #define BUFFERSIZE 4096
- #define COPYMODE 0644
- #define STDOUT 2
- void FilePrinter(int,int, FILE *);
- int main(int ac, char *av[])
- {
- int in_fd, out_fd, n_chars, flag;
- int i;
- char buf[BUFFERSIZE];
- FILE *fp;
- /* check arguments */
- //if (ac != 3){
- //fprintf(stderr, "usage: %s source destination\n", *av);
- //return 1;
- //}
- if (strcmp(av[1], "-e") == 0 )
- {
- flag = 1;
- }
- else if (strcmp(av[1], "-n") == 0 )
- {
- flag = 2;
- }
- else if (strcmp(av[1], "-s") == 0 )
- {
- flag = 3;
- }
- else
- {
- flag = 0;
- }
- if(flag == 0)
- {
- i = 1;
- }
- else
- {
- i = 2;
- }
- for( ; i <= ac; i++)
- {
- fp = fopen(av[i], "r");
- //if ((in_fd=open(av[i], O_RDONLY)) == -1)
- //{
- //perror("Cannot open source file");
- //return 1;
- //}
- FilePrinter(in_fd, flag, fp);
- }
- }
- void FilePrinter(int iFileHandler, int treatment, FILE * ffp)
- {
- char buf[BUFFERSIZE];
- //while ((n_chars = read(iFileHandler, buf, BUFFERSIZE)) > 0)
- while(fgets(buf, BUFFERSIZE, ffp) != NULL)
- {
- if(treatment == 0)
- {
- //if (write(STDOUT, buf, n_chars) != n_chars)
- //{
- //perror("Write error");
- //return 1;
- //}
- printf("%s", buf);
- }
- if( treatment == 1)
- {
- char tempBuf[strlen(buf) + 2];
- strncpy(tempBuf, buf, strlen(buf)+2);
- tempBuf[strlen(buf) - 1] = '$';
- tempBuf[strlen(buf)] = '\n';
- tempBuf[strlen(buf) + 1] = '\0';
- printf("%s", tempBuf);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement