static int config_from_file(struct ini_cfgobj *cfgctx) { struct ini_cfgfile *fctx = NULL; char **errors = NULL; int ret; ret = ini_config_file_open(IPACONFFILE, 0, &fctx); if (ret) { fprintf(stderr, _("Failed to open config file %s\n"), IPACONFFILE); return ret; } ret = ini_config_parse(fctx, INI_STOP_ON_ANY, INI_MS_MERGE | INI_MV1S_ALLOW | INI_MV2S_ALLOW, INI_PARSE_NOWRAP, cfgctx); if (ret) { fprintf(stderr, _("Failed to parse config file %s\n"), IPACONFFILE); if (ini_config_error_count(cfgctx)) { ini_config_get_errors(cfgctx, &errors); if (errors) { ini_config_print_errors(stderr, errors); ini_config_free_errors(errors); } } ini_config_file_destroy(fctx); return ret; } ini_config_file_destroy(fctx); return 0; }
static int gp_config_from_file(const char *config_file, struct gp_ini_context *ctx, struct ini_cfgobj *ini_config, const uint32_t collision_flags) { struct ini_cfgfile *file_ctx = NULL; int ret; ret = ini_config_file_open(config_file, 0, /* metadata_flags, FIXME */ &file_ctx); if (ret) { GPDEBUG("Failed to open config file: %d (%s)\n", ret, gp_strerror(ret)); ini_config_destroy(ini_config); return ret; } ret = ini_config_parse(file_ctx, INI_STOP_ON_ANY, /* error_level */ collision_flags, INI_PARSE_NOWRAP, /* parse_flags */ ini_config); if (ret) { char **errors = NULL; /* we had a parsing failure */ GPDEBUG("Failed to parse config file: %d (%s)\n", ret, gp_strerror(ret)); if (ini_config_error_count(ini_config)) { ini_config_get_errors(ini_config, &errors); if (errors) { ini_config_print_errors(stderr, errors); ini_config_free_errors(errors); } } ini_config_file_destroy(file_ctx); ini_config_destroy(ini_config); return ret; } ini_config_file_destroy(file_ctx); return 0; }
/* * This function parses the GPT_INI file stored in the gpo_cache, and uses the * results to populate the output parameters ... */ static errno_t ad_gpo_parse_ini_file(const char *smb_path, int *_gpt_version) { struct ini_cfgfile *file_ctx = NULL; struct ini_cfgobj *ini_config = NULL; const char *ini_filename; int ret; int gpt_version = -1; TALLOC_CTX *tmp_ctx = NULL; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { ret = ENOMEM; goto done; } ini_filename = talloc_asprintf(tmp_ctx, GPO_CACHE_PATH"%s%s", smb_path, GPT_INI); if (ini_filename == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed.\n"); ret = ENOMEM; goto done; } DEBUG(SSSDBG_TRACE_FUNC, "ini_filename:%s\n", ini_filename); ret = ini_config_create(&ini_config); if (ret != 0) { DEBUG(SSSDBG_CRIT_FAILURE, "ini_config_create failed [%d][%s]\n", ret, strerror(ret)); goto done; } ret = ini_config_file_open(ini_filename, 0, &file_ctx); if (ret != 0) { DEBUG(SSSDBG_CRIT_FAILURE, "ini_config_file_open failed [%d][%s]\n", ret, strerror(ret)); goto done; } ret = ini_config_parse(file_ctx, INI_STOP_ON_NONE, 0, 0, ini_config); if (ret != 0) { DEBUG(SSSDBG_CRIT_FAILURE, "ini_config_parse failed [%d][%s]\n", ret, strerror(ret)); goto done; } ret = parse_ini_file_with_libini(ini_config, &gpt_version); if (ret != 0) { DEBUG(SSSDBG_CRIT_FAILURE, "parse_ini_file_with_libini failed [%d][%s]\n", ret, strerror(ret)); goto done; } *_gpt_version = gpt_version; done: if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Error encountered: %d.\n", ret); } ini_config_file_destroy(file_ctx); ini_config_destroy(ini_config); talloc_free(tmp_ctx); return ret; }
int main (int argc, char **argv) { struct ini_cfgobj *cfg_ctx = NULL; struct ini_cfgfile *file_ctx = NULL; char **section_list = NULL; int section_count = 0; char **attribute_list = NULL; int attribute_count = 0; struct value_obj *vo = NULL; int result = 0; char *value = NULL; char *path = "/usr/lib/systemd/system/docker.service"; printf("%s\n", path); // This throws an error as incomplete because the struct def is within the .so //file_ctx = malloc(sizeof(struct ini_cfgfile)); if ((result = ini_config_create(&cfg_ctx)) != 0) { printf("Error occured %d\n", result); goto cleanup; } //I suspect that file_ctx may be a member of struct cfg_ctx if ((result = ini_config_file_open(path, 0, &file_ctx)) != 0) { printf("Error occured %d\n", result); goto cleanup; } // This sets INI_MV2S_OVERWRITE by default, so when we parse the /etc version, it overwrites the matching section->attr // Should test this .... if ((result = ini_config_parse(file_ctx, 0, INI_MV1S_ALLOW, 0, cfg_ctx)) != 0) { printf("Error occured during config parsing %d\n", result); } result = 0; section_list = ini_get_section_list(cfg_ctx, §ion_count, &result); if (result != 0) { printf("Error while parsing section list %d\n", result); } for (int i = 0; i < section_count; ++i) { printf("Section\n%s\n ------\n", section_list[i]); attribute_list = NULL; result = 0; attribute_list = ini_get_attribute_list(cfg_ctx, section_list[i], &attribute_count, &result); if (result != 0) { printf("Error parsing attribute list\n"); goto next; } for (int j = 0; j < attribute_count; ++j) { // So we end up with MANY elements of the same attr name in attribute list ... How do we handle this? printf("attr: %s -> ", attribute_list[j]); if ((result = ini_get_config_valueobj(section_list[i], attribute_list[j], cfg_ctx, INI_GET_NEXT_VALUE, &vo)) != 0) { printf("\n Error retriving attribute value"); goto next; } if (vo == NULL) { goto nextattr; } result = 0; value = ini_get_string_config_value(vo, &result); if (result != 0) { printf("\n Error parsing attribute value"); goto next; } printf("%s ; ", value); nextattr: printf("\n"); } next: if (value != NULL) { free(value); value = NULL; } if (vo != NULL) { //How do we free this? //ini_ vo = NULL; } if (attribute_list != NULL) { ini_free_attribute_list(attribute_list); attribute_list = NULL; } } cleanup: if (section_list != NULL) { ini_free_section_list(section_list); } if (file_ctx != NULL) { // Is there a memory leak here .... wooooeeeeeoooooooo ini_config_file_close(file_ctx); } if (cfg_ctx != NULL) { ini_config_destroy(cfg_ctx); } return result; }