/* 5-2 * Tanel Toova * Faili operatsioonid ja vőtmete kasutamine * */ #include #include #include int Inp=0; int Prnt=0; int SizeB=0; int SizeMB=0; int Otp=0; //next procedure writes usage help to stderr and exits with eCode int usage(FILE *stream, int eCode) { fprintf(stream, "Options:\n" " -f --input Name of input file\n" " -s --sizeB Size in bytes\n" " -S --sizeMB Size in megabytes\n" " -p --print Put contence of input file to screen\n" " -o --output Name of output file\n" ); exit(eCode); } //next procedure writes contents of inputfile to stdout int FtoS(FILE *inputstream) { while(!feof(inputstream)){ fputc(fgetc(inputstream),stdout); } fprintf(stdout,"\n"); fclose(inputstream); exit(0); } //next procedure writes size of input file in bytes or megabytes to outputstream (screen of file) int Failisuurus(FILE *inputstream, FILE *outputstream) { long size; fseek(inputstream,0,SEEK_END); size=(ftell(inputstream)); if(SizeB==1){fprintf(outputstream,"The size of file is %i B\n", size);} if(SizeMB==1){fprintf(outputstream,"The size of file is %i MB\n", size/1024/1024);} if(Otp==1){fclose(outputstream);} fclose(inputstream); exit(0); } int main(int argc, char **argv) { FILE *inputfile; FILE *outputfile; char in[80]; //asked name of inputfile if key -f is not used int option; char *short_options = {"f:sSpo:"}; static struct option long_options[] = { {"input", 1, NULL, 'f'}, {"sizeB", 0, NULL, 's'}, {"sizeMB", 0, NULL, 'S'}, {"print", 0, NULL, 'p'}, {"output", 1, NULL, 'o'}, {NULL, 0, NULL, 0} }; while(1){ option = getopt_long(argc, argv, short_options, long_options, NULL); if(option == -1){break;} //if there are no more keys switch(option){ case 'f': //checks if -f has an argument and tries to open new stream Inp=1; if(!optarg){ fprintf(stderr,"-f needs an argument\n"); usage(stderr,2); }; inputfile = fopen(optarg,"r"); if(!inputfile){ fprintf(stderr,"Error opening file %s\n",optarg); usage(stderr,3); }; break; case 'p': Prnt=1; break; case 's': SizeB=1; break; case 'S': SizeMB=1; break; case 'o': //checks if -o has an argument and creates new stream(file) Otp=1; if(!optarg){ fprintf(stderr,"-o needs an argument\n"); usage(stderr,2); }; outputfile = fopen(optarg,"w"); break; default: //if unknown key is used usage(stderr,1); } //switch } //while if(!Inp){ //if -f was not used then asks user to give inputfile name and tries to open new stream fprintf(stdout,"Enter the input file: "); scanf("%s",in); inputfile = fopen(in,"r"); if(!in){ fprintf(stderr,"Error opening file %s\n",in); usage(stderr,3); } } //fallowing if-statements check the improper use of keys if(SizeB==1 && SizeMB==1){ fprintf(stderr,"Invalid use of keys -s and -S\n"); usage(stderr,4); } if(SizeB==1 && Prnt==1){ fprintf(stderr,"invalid use of keys -s and -p\n"); usage(stderr,4); } if(SizeMB==1 && Prnt==1){ fprintf(stderr,"Invalid use of keys -S and -p\n"); usage(stderr,4); } if(Otp==1 && Prnt==1){ fprintf(stderr,"Invalid use of keys -o and -p\n"); usage(stderr,4); } if(Otp==1 && SizeB==0 && SizeMB==0){ fprintf(stderr,"Invalid use of key -o, -s or -S must be used with it\n"); usage(stderr,4); } if(SizeB==0 && SizeMB==0 && Prnt==0){Prnt=1;} //if -o key is given then size of inputfile is writen into outputfile (either in B or MB) if(Otp==1){Failisuurus(inputfile,outputfile);} //if -o is not given but -s or -S is then the size of inputile is writen to stdout (either in B or MB) if(SizeB==1 || SizeMB==1){Failisuurus(inputfile,stdout);} //the default action of writing contents of inputfile to stdout if(Prnt ==1){FtoS(inputfile);} }