// search matchs in file char *Search_for(char * NameFile,char *regex) { long int count=0; int match=0; char *lineBuffer=xcalloc(1,1), *buffer2=ReadLines(NameFile), *ptr= strtok(buffer2,"\n"); char tmpline[2128]; Dead_Space(ptr); while(ptr!=NULL) { match=match_test(ptr,regex); if(match) { lineBuffer=xrealloc(lineBuffer,strlen(lineBuffer)+2256); snprintf(tmpline,2127," Line: %ld - %s\n",count,ptr); strncat(lineBuffer,tmpline,2255); } ptr = strtok (NULL, "\n"); count++; } xfree((void **)&ptr); return lineBuffer; }
//read lines of file char *Search_for(char * NameFile,char *regex) { FILE * arq; int match=0,count=1; arq = fopen(NameFile, "rx"); //DEBUG("regex %s name file %s \n",regex,NameFile); // todo think implement fcntl() ,toctou mitigation... if( arq == NULL ) { // fclose(arq); DEBUG("error in to open() file"); exit(1); } char *lineBuffer=xcalloc(1,1); char line[2048],line2[2048],tmpline[2128]; while( fgets(line,2048,arq) ) { // don't need match tab \t memset(line2,0,2048); strcat(line2," "); strncat(line2,line,2047-sizeof(char)); Dead_Space(line2); //DEBUG("%s",line2); match=match_test(line2,regex); if(match) { lineBuffer=xrealloc(lineBuffer,strlen(lineBuffer)+2128); snprintf(tmpline,2127," Line: %d - %s",count,line); strncat(lineBuffer,tmpline,2127); } count++; } if( fclose(arq) == EOF ) { DEBUG("Error in close() file %s",NameFile); exit(1); } arq=NULL; lineBuffer[strlen(lineBuffer)-1]='\0'; if(lineBuffer!=NULL) free(lineBuffer); return lineBuffer; }
int main() { visit_simple(); print_visitor2( 42 ); match_test( string_or_int( std::string( "test" ) ) ); }
void mosca_start (const char * dir_name, char * extension, char * config) { DIR * d; char tmp_path[512]; d = opendir (dir_name); if ( d == NULL) { // closedir(d); DEBUG ("Cannot open directory '%s': %s\n", dir_name, strerror (errno)); exit (EXIT_FAILURE); } while (1) { struct dirent * entry; const char * d_name; entry = readdir (d); if (! entry) { break; } d_name = entry->d_name; // TODO* i need improve that extension check if(strcmp(d_name,".") && strcmp(d_name,"..") && match_test(d_name,extension)) { snprintf(tmp_path,511,"%s/%s",dir_name,d_name); printf("\n=====================================\n%s Path:%s %s \n %sUse Module:%s %s\n",YELLOW,LAST,tmp_path,YELLOW,LAST,config); fly_to_analyse(tmp_path, config); memset(tmp_path,0,511); } if (entry->d_type & DT_DIR) { if (strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) { int path_length; char path[1024]; path_length = snprintf (path, 1023, "%s/%s", dir_name, d_name); if (path_length >= 1023) { DEBUG("Path length has got too long.\n"); exit(0); } mosca_start (path,extension,config); } } } if(closedir(d)) { DEBUG("Could not close '%s': %s\n", dir_name, strerror (errno)); exit(0); } }