/** \brief Retrieve all server definitions from a file. * \param filename The full path to the file which contains the server definitions. * \param count The value of this pointer will be set to the amount of servers which could be parsed. * \param memory The value of this pointer will be set to the total amount of allocated memory across all servers. * \return A serverinfo array, containing all servers that could be parsed from the given file. */ static serverinfo* read_server_definitions( char* filename, int* count, unsigned long* memory ) { serverinfo* slist = NULL; unsigned int lineno = 0; int numservers = 0; unsigned long memtotal = 0; serverinfo server; FILE* fi = fopen( filename, "r" ); while ( fi && !feof( fi ) ) { char sline[128]; char* line = fgets( sline, sizeof(sline), fi ); if (line == NULL) { break; } lineno++; if ( strlen( sline ) < 2 || sline[0] == '#' ) continue; read_server_line( sline, &server ); if ( server.memory > 0 && strlen( server.addr ) > 0 ) { slist = (serverinfo*)realloc( slist, sizeof( serverinfo ) * ( numservers + 1 ) ); memcpy( &slist[numservers], &server, sizeof( serverinfo ) ); numservers++; memtotal += server.memory; } else { // This kind of tells the parent code that "there were servers but not really" *count = 1; free( slist ); snprintf( k_error, sizeof(k_error), "Ketama: Server %s with memory %lu could not be loaded.\n", server.addr, server.memory ); syslog1( LOG_INFO, k_error ); if (fi != NULL) { fclose( fi ); } return 0; } } if ( !fi ) { snprintf( k_error, sizeof(k_error), "Ketama: File %s doesn't exist!", filename ); syslog1( LOG_INFO, k_error ); *count = 0; return 0; } else { fclose( fi ); } // sort the server list qsort( (void *) slist, numservers, sizeof( serverinfo ), (compfn)serverinfo_compare ); *count = numservers; *memory = memtotal; return slist; }
/** \brief Retrieve all server definitions from a file. * \param filename The full path to the file which contains the server definitions. * \param count The value of this pointer will be set to the amount of servers which could be parsed. * \param memory The value of this pointer will be set to the total amount of allocated memory across all servers. * \return A serverinfo array, containing all servers that could be parsed from the given file. */ static serverinfo* read_server_definitions( char* filename, unsigned int* count, unsigned long* memory ) { serverinfo* slist = 0; unsigned int lineno = 0; unsigned int numservers = 0; unsigned long memtotal = 0; FILE* fi = fopen( filename, "r" ); while ( fi && !feof( fi ) ) { char sline[128] = ""; if (fgets( sline, 127, fi ) == NULL) continue; lineno++; if ( strlen( sline ) < 2 || sline[0] == '#' ) continue; serverinfo server = read_server_line( sline ); if ( server.memory > 0 && strlen( server.addr ) ) { slist = (serverinfo*)realloc( slist, sizeof( serverinfo ) * ( numservers + 1 ) ); memcpy( &slist[numservers], &server, sizeof( serverinfo ) ); numservers++; memtotal += server.memory; } else { /* This kind of tells the parent code that * "there were servers but not really" */ *count = 1; free( slist ); set_error( "%s (line %d in %s)", k_error, lineno, filename ); return 0; } } if ( !fi ) { set_error( "File %s doesn't exist!", filename ); *count = 0; return 0; } else { fclose( fi ); } *count = numservers; *memory = memtotal; return slist; }