int main(int argc, char *argv[ ]) { //recieving all arguments (options and/or input files) int c; int printMsg = 0; //option m : 0- don;t print, 1- printBytes int revDiff = 0; // option r: 0- regular, 2- apply difference by reverse FILE *toPrint; //input file: original file to fix FILE *difference; //input file: differences file, as recieved by bcmp.c while ((c = getopt(argc, argv, "hrm")) != -1) { switch(c) { case 'h': printf("\r\n=====options summery:======\r\n"); printf("-h print a summary of options and exit\r\n"); printf("-r reverse the differences\r\n"); printf("-m print a message each time a change is applied\r\n"); printf("====================\r\n"); return 0; case 'r': revDiff = 1; break; case 'm': printMsg = 1; break; } } // parsing input file into tokens (newLine delimeter) char diffLine[300]; difference = fopen(argv[optind+1], "r"); struct diff theDiff; while(fgets(diffLine,sizeof(diffLine),difference) != NULL) { // get difference (per line) parsediff(diffLine, &theDiff); // open input file again toPrint = fopen(argv[optind], "rb+"); // read bytes from file (stream) - 4 bytes per iteration, and XOR it with results (accumelator) fseek(toPrint,theDiff.offset, SEEK_SET); // in case no reverse if ( revDiff == 0 ) { if ( (char)getc(toPrint) == theDiff.old ) { fseek(toPrint,theDiff.offset,SEEK_SET); fwrite(&theDiff.new,1,1,toPrint); if (printMsg == 1) {printf("A change was applied: %s\n",diffLine);} } } // in case reverse was needed else{ if ( (char)getc(toPrint) == theDiff.new ) {
void fix(FILE *file1, char *line, int print){ struct diff diff; parsediff(line, &diff); fseek(file1, diff.offset-1, SEEK_SET); fwrite(&diff.new , 1, 1, file1); if(print) { printf("A change has been applied\n"); } }
void fixChange( char *diffLine, struct diff theDiff, FILE *toPrint, char* flgs){ // get difference (per line) parsediff(diffLine, &theDiff); // read bytes from file (stream) - 4 bytes per iteration, and XOR it with results (accumelator) fseek(toPrint,theDiff.offset, SEEK_SET); // in case no reverse if ( (strchr(flgs, 'r')) == 0 ) { if ( (char)getc(toPrint) == theDiff.old ) { fseek(toPrint,theDiff.offset,SEEK_SET); fwrite(&theDiff.new,1,1,toPrint); if (strchr(flgs, 'm')) {printf("A change was applied: %s\n",diffLine);} } }
int main (int argc, char **argv) { char v=0,help=0,r=0; char c; char flag=0; while ( (c=getopt(argc,argv,"hrm")) != -1 ) { switch (c) { case 'h': helper(); return; case 'r': r=1; break; case 'm': v=1; break; } } FILE *input,*diffFile; if (!(input = fopen(argv[optind],"r+"))) { printf("bad input file name\n"); return; } if (!(diffFile = fopen(argv[optind+1],"r"))) { printf("bad diff file name\n"); return; } char line[128]; struct diff current; while ( fgets(line,128,diffFile) ) { printf("read diff line: %s\n",line); parsediff(line,¤t); fseek(input,current.offset,SEEK_SET); char value[1]; printf("new: %i old: %i \n", current.new, current.old); if (r==1) { fputc(-current.new,input); } else
int main(int argc, char *argv[] ) { int hflag=0, mflag=0, rflag=0, optflag; while((optflag=getopt(argc,argv, "hrm"))!=-1) { switch(optarg) //NOTICE ///////////////////////////////////////////////////////// { case 'h': break; case 'r': case 'r': default: printf("unknown flag, -h for help"); } } /* while (getopt (,,)!= -1) if r - rflag=1 h - hlag=1 m - mflag=1 ? = do nothing \ dont care */ if (argc > 2) //flags input here. /* #include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); OPTIONS -h print a summary of options and exit -r reverse the differences -m print a message each time a change is applied */ { FILE *f1, *f2; f1 = fopen(argv[1],"r+"); // f1 = fopen(argv[optind],"r+"); f2 = fopen(argv[2],"r"); // f2 = fopen(argv[optind+1],"r"); struct diff diff; long i=0; while (!(feof(f2))) { char line[512]="",x[16]=""; fgets(line,512,f2); // printf("this is the line:fgets %s - strlen %d\n",line, strlen(line) ); if(strlen(line) > 10) { parsediff( line, &diff); //throws segmentation fault on an empty/small (under 7 chars) string! // printf("diff.old = %d, diff.new= %d, diff.offset= %ld\n",diff.old,diff.new,diff.offset); /* used fseek instead while(i!= (diff.offset) ) { i++; fread(&x, 1, 1,f1); // printf(" -%c i'm in da loop\n",*x); //test fread is working. it is. //fgetc(f1); } */ fseek(f1,diff.offset,SEEK_SET); //if (rflag) tmp = diff.new ,diff.new = old ,diff.old =tmp; //NOTICE //if (mflag) printf("change has been applied to the file"); fwrite(&diff.new,1,1,f1); } else {
int main(int argc, char **argv) { FILE *changes; FILE *tochange; struct diff diff; char line[128]; char ch[1]={'a'}; int message = 0; char c; int reverse=0; int pid; int count = 0; int status; while (1) { c = getopt (argc, argv, "rmh"); if (c == -1) { break; } switch (c) { case 'r': printf ("User has invoked with -r.\n"); reverse = 1; break; case 'h': printf ("OPTIONS\n -h print a summary of options and exit\n -r reverse the differences\n -m print a message each time a change is applied\n"); break; case 'm': printf ("User has invoked with -m.\n"); message = 1; break; case '?': default: printf ("Usage: %s [-a] [-b <something>].\n", argv[0]); break; } } if(argc != optind+2) printf("illigel input\n%d %d",argc,optind); else{ tochange = fopen(argv[optind], "rb+"); changes = fopen(argv[optind+1], "r"); while ( fgets ( line, 128, changes ) != NULL ) { parsediff(line, &diff); fseek(tochange, diff.offset, SEEK_SET ); ch[0] = (char)getc(tochange); if (!reverse){ if (ch[0] == diff.old){ count++; pid = fork(); if(pid == 0){ printf("%d\n",count); fixme(tochange,diff); exit(0); } } } } fclose(tochange); fclose(changes); } if (pid != 0){ while (count > 0){ wait(&status); printf("exit with status %ld\n",(long)status); count--; } } return 0; }
int main(int argc, char **argv) { struct diff diff; fputdiff(stdout, parsediff("byte 33 -174 +230", &diff)); return 0; }