Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define BUF_LIMIT 512
- #define ec_meno1(s,m) \
- if ( (s) == -1 ) {perror(m);}
- int main( int argc, char *argv[] ){
- char *optstring = "o:";
- char buf[BUF_LIMIT];
- int opttype, i, l, j, fdin, fdout = STDOUT_FILENO, oflag=0;
- opterr = 0;
- if(argc < 2){
- printf("Syntax : mycat file1 ... file N [OPTIONS]\n");
- printf("Prints the content of file1 on stdout\n");
- printf("Option: -o fileout\n");
- printf("Prints the content of the input files on the specified output file\n");
- exit(-1);
- }
- while((opttype=getopt(argc,argv,optstring)) != -1)
- switch(opttype){
- case 'o' :
- ec_meno1((fdout = open(optarg,O_WRONLY|O_CREAT|O_APPEND)),"Couldn't open/create the output file");
- oflag=1;
- break;
- case '?' :
- if(optopt == 'o') printf("Option -o requires an argument\n");
- else printf("Illegal option\n");
- exit(-1);
- default :
- printf("Unknown error\n");
- exit(-1);
- };
- if(!oflag) j=1;
- else j=optind;
- for(i=j;i<argc;i++){
- printf("**********************\n");
- printf("Reading %s \n",argv[i]);
- printf("**********************\n");
- ec_meno1((fdin = open(argv[i],O_RDONLY)),"Couldn't open the file");
- while((l = read(fdin,buf,BUF_LIMIT))>0) ec_meno1((write(fdout,buf,l)),"Problem with the output file descriptor");
- ec_meno1(l,"Couldn't read the file");
- ec_meno1((close(fdin)),"Couldn't close the file");
- printf("\n");
- }
- if(oflag) ec_meno1((close(fdout)),"Couldn't close the file");;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement