#include "config.h"
#include "log.h"
-#define CONFIG_FILE_LINE_MAX 4096
+#define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
unsigned long hash; // a simple hash to speed up searching
// we first compare hashes, and only if the hashes are equal we do string comparisons
- char name[CONFIG_MAX_NAME + 1];
- char value[CONFIG_MAX_VALUE + 1];
+ char *name;
+ char *value;
uint8_t flags;
unsigned long hash; // a simple hash to speed up searching
// we first compare hashes, and only if the hashes are equal we do string comparisons
- char name[CONFIG_MAX_NAME + 1];
+ char *name;
struct config_value *values;
avl_tree values_index;
static struct config_value *config_value_index_find(struct config *co, const char *name, unsigned long hash) {
struct config_value *result = NULL, tmp;
tmp.hash = (hash)?hash:simple_hash(name);
- strncpy(tmp.name, name, CONFIG_MAX_NAME);
- tmp.name[CONFIG_MAX_NAME] = '\0';
+ tmp.name = (char *)name;
avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result);
return result;
static struct config *config_index_find(const char *name, unsigned long hash) {
struct config *result = NULL, tmp;
tmp.hash = (hash)?hash:simple_hash(name);
- strncpy(tmp.name, name, CONFIG_MAX_NAME);
- tmp.name[CONFIG_MAX_NAME] = '\0';
+ tmp.name = (char *)name;
avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
return result;
struct config_value *cv = calloc(1, sizeof(struct config_value));
if(!cv) fatal("Cannot allocate config_value");
- strncpy(cv->name, name, CONFIG_MAX_NAME);
- strncpy(cv->value, value, CONFIG_MAX_VALUE);
+ cv->name = strdup(name);
+ if(!cv->name) fatal("Cannot allocate config.name");
cv->hash = simple_hash(cv->name);
+ cv->value = strdup(value);
+ if(!cv->value) fatal("Cannot allocate config.value");
+
config_value_index_add(co, cv);
// no need for string termination, due to calloc()
struct config *co = calloc(1, sizeof(struct config));
if(!co) fatal("Cannot allocate config");
- strncpy(co->name, section, CONFIG_MAX_NAME);
+ co->name = strdup(section);
+ if(!co->name) fatal("Cannot allocate config.name");
co->hash = simple_hash(co->name);
+
co->values_index.compar = config_value_compare;
config_index_add(co);
else {
if((cv->flags & CONFIG_VALUE_USED && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
- strncpy(cv->value, value, CONFIG_MAX_VALUE);
- // termination is already there
+ free(cv->value);
+ cv->value = strdup(value);
+ if(!cv->value) fatal("Cannot allocate config.value");
}
else
debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
s = config_get(section, name, s);
if(!s) return 0;
- if(strcmp(s, "yes") == 0 || strcmp(s, "true") == 0 || strcmp(s, "1") == 0) {
- strcpy(s, "yes");
- return 1;
- }
- else {
- strcpy(s, "no");
- return 0;
- }
+ if(!strcmp(s, "yes")) return 1;
+ else return 0;
}
const char *config_set(const char *section, const char *name, const char *value)
if(strcmp(cv->value, value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
- strncpy(cv->value, value, CONFIG_MAX_VALUE);
- // termination is already there
+ free(cv->value);
+ cv->value = strdup(value);
+ if(!cv->value) fatal("Cannot allocate config.value");
pthread_rwlock_unlock(&config_rwlock);
web_buffer_printf(wb, "\n# node '%s' is not used.", co->name);
}
- web_buffer_increase(wb, CONFIG_MAX_NAME + 4);
+ web_buffer_increase(wb, CONFIG_FILE_LINE_MAX+1);
web_buffer_printf(wb, "\n[%s]\n", co->name);
for(cv = co->values; cv ; cv = cv->next) {
if(used && !(cv->flags & CONFIG_VALUE_USED)) {
- web_buffer_increase(wb, CONFIG_MAX_NAME + 200);
+ web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1);
web_buffer_printf(wb, "\n\t# option '%s' is not used.\n", cv->name);
}
- web_buffer_increase(wb, CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 5);
+ web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1);
web_buffer_printf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
}
}
void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
{
- if(file) { ; }
va_list args;
log_date(stdout);
va_start( args, fmt );
- fprintf(stdout, "DEBUG (%04lu@%-15.15s): ", line, function);
+ fprintf(stdout, "DEBUG (%04lu@%-10.10s:%-15.15s): ", line, file, function);
vfprintf( stdout, fmt, args );
va_end( args );
fprintf(stdout, "\n");
void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
{
- if(file) { ; }
va_list args;
log_date(stderr);
va_start( args, fmt );
- if(debug_flags) fprintf(stderr, "INFO (%04lu@%-15.15s): ", line, function);
+ if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): ", line, file, function);
else fprintf(stderr, "INFO: ");
vfprintf( stderr, fmt, args );
va_end( args );
void error_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
{
- if(file) { ; }
va_list args;
log_date(stderr);
va_start( args, fmt );
- if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-15.15s): ", line, function);
+ if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-10.10s:%-15.15s): ", line, file, function);
else fprintf(stderr, "ERROR: ");
vfprintf( stderr, fmt, args );
va_end( args );
void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
{
- if(file) { ; }
va_list args;
log_date(stderr);
va_start( args, fmt );
- if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-15.15s): ", line, function);
+ if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): ", line, file, function);
else fprintf(stderr, "FATAL: ");
vfprintf( stderr, fmt, args );
va_end( args );