static struct rcsection * rc_addsect(struct rcfile *rcp, const char *sectname) { struct rcsection *p; int id = 0; p = rc_findsect(rcp, sectname, 0); if (p) { /* * If section with that name already exists -- add one more, * same named, but with different id (higher by one) */ while (p != NULL) { id = p->rs_id + 1; p = rc_findsect(rcp, sectname, id); } } p = malloc(sizeof(*p)); if (!p) return (NULL); p->rs_name = strdup(sectname); p->rs_id = id; SLIST_INIT(&p->rs_keys); SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next); return (p); }
/* Count how many sections with given name exists in configuration. */ int rc_getsectionscount(struct rcfile *f, const char *sectname) { struct rcsection *p; int count = 0; p = rc_findsect(f, sectname, 0); if (p) { while (p != NULL) { count = p->rs_id + 1; p = rc_findsect(f, sectname, count); } return (count); } else return (0); }
/* * 1,yes,true * 0,no,false */ int rc_getbool(struct rcfile *rcp, const char *section, int sect_id, const char *key, int *value) { struct rcsection *rsp; struct rckey *rkp; char *p; rsp = rc_findsect(rcp, section, sect_id); if (!rsp) return (ENOENT); rkp = rc_sect_findkey(rsp,key); if (!rkp) return (ENOENT); p = rkp->rk_value; while (*p && isspace(*p)) p++; if (*p == '0' || strcasecmp(p,"no") == 0 || strcasecmp(p, "false") == 0 || strcasecmp(p, "off") == 0) { *value = 0; return (0); } if (*p == '1' || strcasecmp(p,"yes") == 0 || strcasecmp(p, "true") == 0 || strcasecmp(p, "on") == 0) { *value = 1; return (0); } fprintf(stderr, "invalid boolean value '%s' for key '%s' in section " "'%s' \n",p, key, section); return (EINVAL); }
int rc_getstringptr(struct rcfile *rcp,char *section, char *key,char **dest) { struct rcsection *rsp; struct rckey *rkp; *dest = NULL; rsp = rc_findsect(rcp, section); if (!rsp) return ENOENT; rkp = rc_sect_findkey(rsp,key); if (!rkp) return ENOENT; *dest = rkp->rk_value; return 0; }
static struct rcsection * rc_addsect(struct rcfile *rcp, char *sectname) { struct rcsection *p; p = rc_findsect(rcp, sectname); if (p) return p; p = malloc(sizeof(*p)); if (!p) return NULL; p->rs_name = strdup(sectname); SLIST_INIT(&p->rs_keys); SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next); return p; }
int rc_getstringptr(struct rcfile *rcp, const char *section, int sect_id, const char *key, char **dest) { struct rcsection *rsp; struct rckey *rkp; *dest = NULL; rsp = rc_findsect(rcp, section, sect_id); if (!rsp) return (ENOENT); rkp = rc_sect_findkey(rsp,key); if (!rkp) return (ENOENT); *dest = rkp->rk_value; return (0); }
int rc_getint(struct rcfile *rcp,char *section, char *key,int *value) { struct rcsection *rsp; struct rckey *rkp; rsp = rc_findsect(rcp, section); if (!rsp) return ENOENT; rkp = rc_sect_findkey(rsp,key); if (!rkp) return ENOENT; errno = 0; *value = strtol(rkp->rk_value,NULL,0); if (errno) { fprintf(stderr, "invalid int value '%s' for key '%s' in section '%s'\n",rkp->rk_value,key,section); return errno; } return 0; }