#include #include #include #include #include #include #include #include #define CONFFILE "hello.conf" int main(int argc, char *argv[]) { cfg_opt_t opts[] = { CFG_STR("target", "World", CFGF_NONE), CFG_FUNC("include", &cfg_include), CFG_END() }; cfg_t *cfg; char *buff; struct stat statbuf; int fd; cfg = cfg_init(opts, CFGF_NONE); if (argc > 1) { if (strcmp(argv[1], "--buffer") == 0) { if (stat (CONFFILE, &statbuf) == 0) { buff = calloc(statbuf.st_size + 1, 1); fd = open (CONFFILE, O_RDONLY); read (fd, buff, statbuf.st_size); close(fd); if (cfg_parse_buf(cfg, buff) == CFG_PARSE_ERROR) { printf ("ERROR: cfg_parse_buf failed\n"); return 1; } } else { printf ("ERROR: missing %s\n", CONFFILE); return 1; } } else { printf("USAGE: %s [OPTIONS]\n", argv[0]); printf("\n"); printf("\t-h, --help\tPrint help and exit\n"); printf("\t--buffer\tRead configuration into a buffer\n"); printf("\n"); return 0; } } else { if (cfg_parse(cfg, CONFFILE) == CFG_PARSE_ERROR) { printf ("ERROR: cfg_parse failed\n"); return 1; } } printf("Hello, %s!\n", cfg_getstr(cfg, "target")); cfg_free(cfg); return 0; }