// text search utility by 110983@lycos.com // http://change.to/opensource // version 0.1.0(beta) for DOS //TODO: //add more features #include #include #include #include #include #include main() { FILE *fn; DIR *dir; struct dirent *ent; char dirname[100], search[30], *ptr_success, buffer[31], *dirp, ch; //the pointer "ptr_success" will hold a number value returned //by strstr() //it is only an internal variable to let a loop know //if the search was successful int i, j, end_buffer, status, search_option; dirp = dirname; system("CLS"); //If compiling on Linux, this should be taken out printf("Text Serach Utility (beta)0.1.0\nBy Jonathan Harm(110983@lycos.com)\n"); printf("\nEnter the directory to search\n"); gets(dirname); printf("\nEnter string to search\n"); gets(search); fflush(stdin); printf("\nDo you want your search to be case insensitive?\n(y/n)"); search_option = getc(stdin); switch( search_option ) { //switch is to store 1 or 0 case 'y': //to the search_option variable case 'Y': search_option = 1; break; case 'n': case 'N': search_option = 0; break; default: printf("You did not enter \"y\" or \"n\"."); } dir = opendir( dirp ); //opens the directory while ((ent = readdir(dir)) != NULL) { if ( strcmp(ent->d_name,"..") != 0 && strcmp(ent->d_name,".") != 0 ) //thanks to Stee(http://www.stevec.plus.com/) for the line of code above { chdir(dirname); //change to the directory if ( (fn = fopen( ent->d_name , "r")) == NULL) { fprintf(stderr, "Error opening file %s\\%s", dirname, ent->d_name); exit(1); } for(i = 0; i < 30; i++) //first fill up the buffer. { status = fscanf(fn, "%c", &ch); buffer[i] = ch; //if the EOF is found before 30 characters if(status == EOF) //then break the loop { break; } } buffer[i] = '\0'; //put the null character in the end of the buffer end_buffer = i; //sets the end_buffer to '\0' or EOF, which ever //occurs first //this changes everything to lowercase to do //case insensitive searches if(search_option == 1) { for(i = 0; i < end_buffer; i++) { buffer[i] = tolower(buffer[i]); } for(i = 0; i < 30; i++) { search[i] = tolower(search[i]); if(search[i] == NULL) { break; } } if(i == 30) //this will truncate the search at { //30 characters if it is too long search[i] = '\0'; } } ptr_success = strstr(buffer, search); while(ptr_success == NULL) { if(ptr_success == NULL && end_buffer < 30) //dont go through the process { //of incrementing the buffer break; //if it isnt big enough } //end_buffer should always be either //NULL while(buffer[end_buffer] == NULL) { //if status == EOF, then NULL(which is what end_buffer //should be) is one less than status if(status == EOF) { --end_buffer; } if(end_buffer == 0) //don't let the program get stuck in the while loop { break; } ptr_success = strstr(buffer, search); //search if(ptr_success != NULL) //if the search was successful { //then it doesnt need to increment the buffer break; } for(i = 0, j = 1; j < end_buffer; i++, j++) { buffer[i] = buffer[j]; //move characters in buffer down one //to make room to increment to the next } status = buffer[--j] = fgetc(fn); //this line puts the next char in the buffer //and gets the status to check for EOF buffer[++j] = '\0'; //make sure end of buffer is NULL if(search_option == 1) { for(i = 0; i < end_buffer; i++) { buffer[i] = tolower(buffer[i]); } } } //end of "while(status == EOF || buffer[end_buffer] == NULL)" } // <-end of "while(ptr_success == NULL)" if (ptr_success != NULL) //if strstr returns NULL, then there was //no match found. { printf("Found match for your search in %s\\%s\n", dirname, ent->d_name); } ptr_success = NULL; //need to set to NULL or it will //think the search was successful when it isn't fclose(fn); } //end of "while ((ent = readdir(dir)) != NULL)" } //end of if ( strcmp(ent->d_name,"..") != 0 && strcmp(ent->d_name,".") != 0 ) closedir(dir); printf("\n\nThank you for using The Text Search Utility.\n"); printf("You can find the latest updates at http://change.to/opensource"); return 0; }