diff -urN --exclude=.svn univention-config-registry/include/univention/config.h univention-config-registry.2/include/univention/config.h --- univention-config-registry/include/univention/config.h 2011-05-12 09:25:43.305839474 +0200 +++ univention-config-registry.2/include/univention/config.h 2011-05-12 08:57:07.861839394 +0200 @@ -35,9 +35,25 @@ #include -char* univention_config_get_string ( char *value ); -int univention_config_get_int ( char *value ); -long univention_config_get_long ( char *value ); -int univention_config_set_string ( char *key, char *value); +/** + * Retrieve value of config registry entry associated with key. + * @return an allocated buffer containingt the value or NULL on errors or if not found. + */ +char *univention_config_get_string(const char *key); +/** + * Retrieve integer value of config registry entry associated with key. + * @return an integer value of -1 on errors of if not found. + */ +int univention_config_get_int (const char *key); +/** + * Retrieve integer value of config registry entry associated with key. + * @return an integer value of -1 on errors of if not found. + */ +long univention_config_get_long (const char *key); +/** + * Set config registry entry associated with key to new value. + * @return 0 on success, -1 on internal errors. + */ +int univention_config_set_string(const char *key, const char *value); #endif diff -urN --exclude=.svn univention-config-registry/lib/config.c univention-config-registry.2/lib/config.c --- univention-config-registry/lib/config.c 2011-05-12 08:59:20.661347727 +0200 +++ univention-config-registry.2/lib/config.c 2011-05-12 08:57:07.861839394 +0200 @@ -47,130 +47,89 @@ #define BASECONFIG_MAX_LINE 1024 -char* univention_config_get_string ( char *value ) +char *univention_config_get_string(const char *key) { FILE *file; char line[BASECONFIG_MAX_LINE]; char *nvalue; - int len; + size_t len; + char *ret = NULL; - if( (file=fopen(BASECONFIG_FILE,"r")) == NULL ) + if ((file = fopen(BASECONFIG_FILE, "r")) == NULL) { - univention_debug(UV_DEBUG_CONFIG,UV_DEBUG_ERROR,"Error on opening \"%s\n",BASECONFIG_FILE); + univention_debug(UV_DEBUG_CONFIG, UV_DEBUG_ERROR, "Error on opening \"%s\"", BASECONFIG_FILE); return NULL; } - - len = strlen(value); - nvalue = calloc(len + 2 /* ':' + '\0'*/, sizeof(char)); - memcpy(nvalue, value, len); - nvalue[len] = ':'; + len = strlen(key); + nvalue = malloc(len + 2 /* ':' + '\0'*/); + memcpy(nvalue, key, len); + nvalue[len++] = ':'; + nvalue[len++] = '\0'; - while( fgets(line, BASECONFIG_MAX_LINE, file) != NULL ) + while (fgets(line, BASECONFIG_MAX_LINE, file) != NULL) { - if( !strncmp(line, nvalue, strlen(nvalue) ) ) + if (!strncmp(line, nvalue, len)) { - fclose (file); - free (nvalue); - - return (char*)strndup(&(line[len+2]), strlen(line) - (len+2) - 1 ); - /* no newline */ + ret = strndup(line + len, strlen(line) - len - 1 ); /* no newline */ + goto done; } } - - fclose (file); - - univention_debug(UV_DEBUG_USERS, UV_DEBUG_INFO,"Did not find \"%s\"\n",value); - + univention_debug(UV_DEBUG_USERS, UV_DEBUG_INFO, "Did not find \"%s\"", key); +done: + fclose(file); free(nvalue); - return NULL; + return ret; } -int univention_config_get_int(char *value) +int univention_config_get_int(const char *key) { - FILE *file; - char line[BASECONFIG_MAX_LINE]; - char *s_var; - int var; - - if( (file=fopen(BASECONFIG_FILE,"r")) == NULL ) - { - univention_debug(UV_DEBUG_USERS,UV_DEBUG_ERROR,"Error on opening \"%s\n",BASECONFIG_FILE); - return -1; - } - - while( fgets(line, BASECONFIG_MAX_LINE, file) != NULL ) - { - if( !strncmp(line, value, strlen(value) ) ) - { - fclose (file); - s_var=(char*)strndup(&(line[strlen(value)+2]), strlen(line) - (strlen(value)+2) - 1 ); - return atoi(s_var); - } + int ret = -1; + char *s = univention_config_get_string(key); + if (s) { + ret = atoi(s); + free(s); } - - fclose (file); - - univention_debug(UV_DEBUG_USERS, UV_DEBUG_INFO,"Did not find \"%s\"\n",value); - - return -1; + return ret; } -long univention_config_get_long(char *value) +long univention_config_get_long(const char *key) { - FILE *file; - char line[BASECONFIG_MAX_LINE]; - char *s_var; - long var; - - if( (file=fopen(BASECONFIG_FILE,"r")) == NULL ) - { - univention_debug(UV_DEBUG_USERS,UV_DEBUG_ERROR,"Error on opening \"%s\n",BASECONFIG_FILE); - return -1; - } - - while( fgets(line, BASECONFIG_MAX_LINE, file) != NULL ) - { - if( !strncmp(line, value, strlen(value) ) ) - { - fclose (file); - s_var=(char*)strndup(&(line[strlen(value)+2]), strlen(line) - (strlen(value)+2) - 1 ); - return atol(s_var); - } + long ret = -1; + char *s = univention_config_get_string(key); + if (s) { + ret = atol(s); + free(s); } - - fclose (file); - - univention_debug(UV_DEBUG_USERS, UV_DEBUG_INFO,"Did not find \"%s\"\n",value); - - return -1; + return ret; } -int univention_config_set_string(char *key, char *value) +int univention_config_set_string(const char *key, const char *value) { + size_t len; char *str; int pid, status; - - str=malloc((strlen(key)+strlen(value)+2) * sizeof(char)); - strcpy(str, key); - strcat(str, "="); - strcat(str,value); + len = strlen(key) + strlen(value) + 2; + str = malloc(len); + if (!str) + return -1; + snprintf(str, len, "%s=%s", key, value); pid = fork(); if (pid == -1) return -1; if (pid == 0) { - char *argv[5]; - argv[0] = "sh"; - argv[1] = "-c"; - argv[2] = "univention-config-registry"; - argv[2] = "set"; - argv[3] = str; - argv[4] = 0; - execve("/bin/sh", argv, NULL); + /* child */ + char *argv[4]; + argv[0] = "univention-config-registry"; + argv[1] = "set"; + argv[2] = str; + argv[3] = NULL; + execve("/usr/sbin/univention-config-registry", argv, NULL); exit(127); } + /* parent */ do { if (waitpid(pid, &status, 0) == -1) { if (errno != EINTR) @@ -181,4 +140,3 @@ return 0; } -