# HG changeset patch # User David Demelier # Date 1321472496 -3600 # Node ID f42bcb9e7b4a75354d12116664743c2942a21fc9 # Parent fd817a7dbf2f5f11e1cf49cfe6a4e29b69f9d2b5 Add const and little security fix with *nb pointer diff -r fd817a7dbf2f -r f42bcb9e7b4a ini.c --- a/ini.c Tue Nov 15 20:12:48 2011 +0100 +++ b/ini.c Wed Nov 16 20:41:36 2011 +0100 @@ -26,9 +26,9 @@ #include "ini.h" -/* -------------------------------------------------------- - * structure definitions - * -------------------------------------------------------- */ +/* -------------------------------------------------------- */ +/* structure definitions */ +/* -------------------------------------------------------- */ struct ini_option { char *key; /* option name */ @@ -61,9 +61,9 @@ static char iniError[1024 + 1]; -/* -------------------------------------------------------- - * prototypes - * -------------------------------------------------------- */ +/* -------------------------------------------------------- */ +/* prototypes */ +/* -------------------------------------------------------- */ static void *ini_read(struct ini_config *, FILE *); static int ini_getline(struct ini_config *, FILE *); @@ -83,9 +83,9 @@ if (I_VERBOSE((file))) \ fprintf(stderr, fmt, __VA_ARGS__) -/* -------------------------------------------------------- - * public functions - * -------------------------------------------------------- */ +/* -------------------------------------------------------- */ +/* public functions */ +/* -------------------------------------------------------- */ struct ini_config * ini_load(const char *path, int flags) @@ -119,7 +119,7 @@ */ char ** -ini_get_sections_names(struct ini_config *conf, int *number) +ini_get_sections_names(const struct ini_config *conf, int *number) { struct ini_section *s; char **list; @@ -129,6 +129,10 @@ STAILQ_FOREACH(s, &conf->sections, next) ++ i; + /* For safety */ + if (number != NULL) + *number = 0; + if ((list = calloc(i + 1, sizeof (char *))) == NULL) return NULL; @@ -188,7 +192,7 @@ */ struct ini_section * -ini_select_section(struct ini_config *conf, const char *section) +ini_select_section(const struct ini_config *conf, const char *section) { struct ini_section *s; @@ -207,7 +211,7 @@ */ char * -ini_option_once(struct ini_config *conf, const char *sect, const char *key) +ini_option_once(const struct ini_config *conf, const char *sect, const char *key) { struct ini_option *o; struct ini_section *s; @@ -228,7 +232,7 @@ */ char ** -ini_get_option_names(struct ini_section *section, int *nb) +ini_get_option_names(const struct ini_section *section, int *nb) { char **list; struct ini_option *o; @@ -238,6 +242,9 @@ STAILQ_FOREACH(o, §ion->options, next) ++ i; + if (nb != NULL) + *nb = i; + if ((list = calloc(i + 1, sizeof (char *))) == NULL) return NULL; @@ -258,7 +265,7 @@ */ char * -ini_get_option(struct ini_section *section, const char *key) +ini_get_option(const struct ini_section *section, const char *key) { struct ini_option *o; @@ -309,9 +316,9 @@ free(conf); } -/* -------------------------------------------------------- - * private functions - * -------------------------------------------------------- */ +/* -------------------------------------------------------- */ +/* private functions */ +/* -------------------------------------------------------- */ /* * Read file line per line and try to parse ini like file. Lines diff -r fd817a7dbf2f -r f42bcb9e7b4a ini.h --- a/ini.h Tue Nov 15 20:12:48 2011 +0100 +++ b/ini.h Wed Nov 16 20:41:36 2011 +0100 @@ -34,16 +34,16 @@ ini_load(const char *, int); char ** -ini_get_sections_names(struct ini_config *, int *); +ini_get_sections_names(const struct ini_config *, int *); struct ini_section ** ini_get_sections(const struct ini_config *, const char *, int *); struct ini_section * -ini_select_section(struct ini_config *, const char *); +ini_select_section(const struct ini_config *, const char *); char * -ini_get_option_once(struct ini_config *, const char *, const char *); +ini_get_option_once(const struct ini_config *, const char *, const char *); void ini_free(struct ini_config *, int, int); @@ -56,9 +56,9 @@ * -------------------------------------------------------- */ char ** -ini_get_option_names(struct ini_section *, int *); +ini_get_option_names(const struct ini_section *, int *); char * -ini_get_option(struct ini_section *, const char *key); +ini_get_option(const struct ini_section *, const char *key); #endif /* _INI_H_ */