Ejemplo n.º 1
0
// 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;
}
Ejemplo n.º 2
0
//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;
}