library/src/filesystem/BP-Filesystem.cc
// searches an existing list for the following conditions (note: elf detection must have read permissions on file)
searches an existing list for the following conditions (note: elf detection must have read permissions on file)
searches an existing list for the following conditions ( note : elf detection must have read permissions on file )
P_BP_FILE_LIST BP_FSSearchAll(P_BP_FILE_LIST inList, char *name, bool execute, bool write, bool read, bool elf, uid_t uid, gid_t gid)
{
	if(!inList)
		return NULL;
	if(!inList->first)
		return NULL;
	P_BP_FILE_LIST elfList = NULL;
	FILE * elfFile      = NULL;
	int readBytes       = 0;
	int i               = 0;
	char elfBuff[10];
		memset(elfBuff, 0x00, 10);
	for(inList = inList->first; inList != NULL; inList = inList->next)
	{
		if(name)
		{
			#ifndef WIN32
			if(inList->pathToFile == NULL)
				continue;
			for(i = bpstrlen(inList->pathToFile); \
				inList->pathToFile[i] != '/'; i--){}
			#endif
			#ifdef WIN32
			for(i = bpstrlen(inList->pathToFile); \
				inList->pathToFile != '\\'; i--){}
			#endif
			if(i != 0)
				i++;
			if(memcmp(&inList->pathToFile[i], name, bpstrlen(name)) != 0)
				continue;
			if(memcmp(&inList->pathToFile[i], name, bpstrlen(&inList->pathToFile[i])) != 0)
				continue;
		} 
		if(execute == true && inList->execute != true)
			continue;
		if(write == true && inList->write != true)
			continue;
		if(read == true && inList->read != true)
			continue;
		if(gid != -1 && inList->attrs.st_gid != gid)
			continue;
		if(gid != -1 && inList->attrs.st_uid != uid)
			continue;
		if(elf == true && inList->read == true){
			if(inList->attrs.st_size <= 4)
				continue;
			elfFile = fopen(inList->pathToFile, "r");
			if(elfFile != NULL){
				readBytes = fread(elfBuff, 1, 4, elfFile);
				fclose(elfFile);
				if(readBytes == 4 && memcmp(elfBuff, "\x7f\x45\x4c\x46", 4) != 0)
					continue;
			} else continue;
		}
		if(elfList == NULL)
		{
			elfList = BP_FileListDupElement(inList);
			elfList->first = elfList;
			elfList->prev = NULL;
			elfList->next = NULL;
		}
		else
		{
			elfList->next = BP_FileListDupElement(inList);
			elfList->next->prev = elfList;
			elfList = elfList->next;
			elfList->first = elfList->prev->first;
			elfList->next = NULL;
		}
		memset(elfBuff, 0x00, 4);
	}
	if(elfList != NULL)
		return elfList->first;
	else
		return NULL;
}
P_BP_FILE_LIST BP_FSSearchAll ( P_BP_FILE_LIST inList , char * name , bool execute , bool write , bool read , bool elf , uid_t uid , gid_t gid ) { if ( ! inList ) return NULL ; if ( ! inList -> first ) return NULL ; P_BP_FILE_LIST elfList = NULL ; FILE * elfFile = NULL ; int readBytes = 0 ; int i = 0 ; char elfBuff [ 10 ] ; memset ( elfBuff , 0x00 , 10 ) ; for ( inList = inList -> first ; inList != NULL ; inList = inList -> next ) { if ( name ) { #ifndef WIN32 if ( inList -> pathToFile == NULL ) continue ; for ( i = bpstrlen ( inList -> pathToFile ) ; inList -> pathToFile [ i ] != ' ' ; i -- ) { } #endif #ifdef WIN32 for ( i = bpstrlen ( inList -> pathToFile ) ; inList -> pathToFile != ' \\ ' ; i -- ) { } #endif if ( i != 0 ) i ++ ; if ( memcmp ( & inList -> pathToFile [ i ] , name , bpstrlen ( name ) ) != 0 ) continue ; if ( memcmp ( & inList -> pathToFile [ i ] , name , bpstrlen ( & inList -> pathToFile [ i ] ) ) != 0 ) continue ; } if ( execute == true && inList -> execute != true ) continue ; if ( write == true && inList -> write != true ) continue ; if ( read == true && inList -> read != true ) continue ; if ( gid != -1 && inList -> attrs . st_gid != gid ) continue ; if ( gid != -1 && inList -> attrs . st_uid != uid ) continue ; if ( elf == true && inList -> read == true ) { if ( inList -> attrs . st_size <= 4 ) continue ; elfFile = fopen ( inList -> pathToFile , " " ) ; if ( elfFile != NULL ) { readBytes = fread ( elfBuff , 1 , 4 , elfFile ) ; fclose ( elfFile ) ; if ( readBytes == 4 && memcmp ( elfBuff , " \x7f \x45 \x4c \x46 " , 4 ) != 0 ) continue ; } else continue ; } if ( elfList == NULL ) { elfList = BP_FileListDupElement ( inList ) ; elfList -> first = elfList ; elfList -> prev = NULL ; elfList -> next = NULL ; } else { elfList -> next = BP_FileListDupElement ( inList ) ; elfList -> next -> prev = elfList ; elfList = elfList -> next ; elfList -> first = elfList -> prev -> first ; elfList -> next = NULL ; } memset ( elfBuff , 0x00 , 4 ) ; } if ( elfList != NULL ) return elfList -> first ; else return NULL ; }
searches an existing list for the following conditions (note: elf detection must have read permissions on file)
searches an existing list for the following conditions ( note : elf detection must have read permissions on file )
// ensure list pointers are navigatable // local vars // holds signifigant bytes // walk the list and search for values // search for name if it's set // not win32 compatible [search key: portable] // if it found the last /, iterate it to find the beginning of the // file name // compare and continue loop if not the same // continue the loop if the file is not executable // and the executable flag has been set to true. // continue the loop if the file is not writable // and the writable flag has been set to true // continue the loop if the file is not readable // and the readable flag has been set to true // continue the loop if file is unreadable // continue the loop if the uid does not match // continue the loop if the gid does not match // check to see if it's elf - note: may be possible to get false // comparissons for elf due to the inList->attrs.st_size comparisson // below.  Regardless, its pretty safe // 	if its not elf, continue // if it passes all the previous conditions assign the element to outList // assignments // reset the elf buffer