#include #include #include #include #include #include #include #include #include #include unsigned char buffer[65536]; unsigned char initbytes[16]; unsigned long long int restore[8]; unsigned long long int countcount = 0; unsigned long long int outputlimit = 18446744073709551615LLU; unsigned long long int skip = 0; unsigned long long int skipped = 0; unsigned char number = 0; char sizemodifier = 'T'; unsigned long long int readcount = 0; unsigned long long int megabytes = 0; unsigned int jcount = 0; unsigned long long int bindex = 0; #define HELPLINECOUNT 38 char *helplines[HELPLINECOUNT] = { " ", " findrepeat8 reads STDIN and searches for a repeating sequence of characters.", " Usage: findrepeat8 -n -s 2) { strncpy(line, &curargv[2], 80); } else { ++curargc; if (curargc > argc) { printf("Error: Specified number requested but amount not given.\n"); return(0); } strncpy(line, argv[curargc-1], 80); } i = 0; outputlimit = 0LLU; while ((line[i] >= 48) && (line[i] <= 57) && (line[i] != 0)) { outputlimit = (outputlimit*10) + (line[i]-48); ++i; } if (line[i] == 0) { limitunits = 'B'; } else { limitunits = line[i]; } switch (limitunits) { case 'K': outputlimit *= 1024LLU; break; case 'M': outputlimit *= 1048576LLU; break; case 'G': outputlimit *= 1073741824LLU; break; case 'T': outputlimit *= 1099511627776LLU; break; case 'P': outputlimit *= 1125899906842624LLU; break; default: break; } ++curargc; continue; } if (strncmp(curargv, "-s", 2) == 0) { readcount = strlen(curargv); if (readcount > 2) { strncpy(line, &curargv[2], 80); } else { ++curargc; if (curargc > argc) { printf("Error: Specified skip number requested but amount not given.\n"); return(0); } strncpy(line, argv[curargc-1], 80); } i = 0; skip = 0; while ((line[i] >= 48) && (line[i] <= 57) && (line[i] != 0)) { skip = (skip*10) + (line[i]-48); ++i; } if (line[i] == 0) { skipunits = 'B'; } else { skipunits = line[i]; } switch (skipunits) { case 'K': skip *= 1024LLU; break; case 'M': skip *= 1048576LLU; break; case 'G': skip *= 1073741824LLU; break; case 'T': skip *= 1099511627776LLU; break; case 'P': skip *= 1125899906842624LLU; break; default: break; } skipunits = 'S'; /* this is now used as a flag that says Set.*/ ++curargc; continue; } ++curargc; continue; } printf("OUTPUTLIMIT %llu\n", outputlimit); if (skip) { printf("Skipping %llu bytes....\n", skip); } readcount = 0; skipped = skip; if (skip > 65536) { while (readcount < (skip-65536)) { readcount += fread(buffer, 1, 65536, stdin); } skip -= readcount; if (skip) { readcount += fread(buffer, 1, skip, stdin); } printf("Skipping readcount = %llu skipped = %llu\n", readcount, skipped); } else { readcount += fread(buffer, 1, skip, stdin); printf("Skipping readcount = %llu skipped = %llu\n", readcount, skipped); } readcount = fread(initbytes, 1, 8, stdin); readcount = 0; bindex = 0; countcount = 0; /* * This puts numbers that will never match in the buffer for when we * have to go back from jcount and start from next number after the * FIRST number that matched. Since readcount will be zero to start * the while loop, the first thing it does is read in the buffer. * But first we save the last 8 elements in case we need to rewind * back before the biginning of the current buffer. */ for (i=0; i<8; i++) { buffer[65528+i] = initbytes[i] - 1; } printf("Starting search looking for = %d %d %d %d %d %d %d %d\n", initbytes[0], initbytes[1], initbytes[2], initbytes[3], initbytes[4], initbytes[5], initbytes[6], initbytes[7]); jcount = 0; bindex = 8; while (1) { if (readcount == 0) { for (i=0; i<8; i++) { buffer[i] = buffer[65528+i]; } readcount = fread(&buffer[8], 1, 65528, stdin); if (readcount <= 0) { printf("Ran out of input at %llu megabytes. Exiting.\n", megabytes); exit(0); } } number = buffer[bindex]; /*printf("%d\n", number);*/ ++countcount; if (countcount > outputlimit) { megabytes = (countcount >> 20); printf("Hit outputlimit %llu megabytes. Exiting.\n", megabytes); exit(0); } if ((countcount % 1073741824) == 0) { printf("Still alive. %llu numbers read in.\n", countcount); fflush(stdout); } if (number == initbytes[jcount]) { ++jcount; if (jcount>=8) { /* printf("Found repeat starting at %llu initbytes %d %d %d %d %d %d %d %d\n", (countcount), initbytes[0], initbytes[1], initbytes[2], initbytes[3], initbytes[4], initbytes[5], initbytes[6], initbytes[7]);*/ printf("Found repeat starting at %llu numbers %d %d %d %d %d %d %d %d\n", (countcount), buffer[bindex-8], buffer[bindex-7], buffer[bindex-6], buffer[bindex-5], buffer[bindex-4], buffer[bindex-3], buffer[bindex-2], buffer[bindex-1]); fflush(stdout); exit(0); } } else { bindex -= jcount; jcount = 0; } ++bindex; if (bindex == readcount+8) { bindex = 8; readcount = 0; } } printf("Exiting now. %llu numbers read in.\n", countcount); return(0); } /* END main */