2 * config.c - blkid.conf routines
4 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_STAT_H
20 #ifdef HAVE_SYS_MKDEV_H
21 #include <sys/mkdev.h>
33 static int parse_evaluate(struct blkid_config *conf, char *s)
38 if (conf->nevals >= __BLKID_EVAL_LAST)
43 if (strcmp(s, "udev") == 0)
44 conf->eval[conf->nevals] = BLKID_EVAL_UDEV;
45 else if (strcmp(s, "scan") == 0)
46 conf->eval[conf->nevals] = BLKID_EVAL_SCAN;
57 DBG(DEBUG_CONFIG, printf(
58 "config file: unknown evaluation method '%s'.\n", s));
62 static int parse_next(FILE *fd, struct blkid_config *conf)
67 /* read the next non-blank non-comment line */
69 if (fgets (buf, sizeof(buf), fd) == NULL)
70 return feof(fd) ? 0 : -1;
71 s = strchr (buf, '\n');
73 /* Missing final newline? Otherwise extremely */
74 /* long line - assume file was corrupted */
76 s = strchr (buf, '\0');
78 DBG(DEBUG_CONFIG, fprintf(stderr,
79 "libblkid: config file: missing newline at line '%s'.\n",
85 if (--s >= buf && *s == '\r')
89 while (*s == ' ' || *s == '\t') /* skip space */
92 } while (*s == '\0' || *s == '#');
94 if (!strncmp(s, "SEND_UEVENT=", 12)) {
96 if (*s && !strcasecmp(s, "yes"))
100 } else if (!strncmp(s, "CACHE_FILE=", 11)) {
103 conf->cachefile = blkid_strdup(s);
104 } else if (!strncmp(s, "EVALUATE=", 9)) {
106 if (*s && parse_evaluate(conf, s) == -1)
109 DBG(DEBUG_CONFIG, printf(
110 "config file: unknown option '%s'.\n", s));
116 /* return real config data or built-in default */
117 struct blkid_config *blkid_read_config(const char *filename)
119 struct blkid_config *conf;
123 filename = blkid_safe_getenv("BLKID_CONF");
125 filename = BLKID_CONFIG_FILE;
127 conf = (struct blkid_config *) calloc(1, sizeof(*conf));
132 DBG(DEBUG_CONFIG, fprintf(stderr,
133 "reading config file: %s.\n", filename));
135 f = fopen(filename, "r");
137 DBG(DEBUG_CONFIG, fprintf(stderr,
138 "%s: does not exist, using built-in default\n", filename));
142 if (parse_next(f, conf)) {
143 DBG(DEBUG_CONFIG, fprintf(stderr,
144 "%s: parse error\n", filename));
150 conf->eval[0] = BLKID_EVAL_UDEV;
151 conf->eval[1] = BLKID_EVAL_SCAN;
154 if (!conf->cachefile)
155 conf->cachefile = blkid_strdup(BLKID_CACHE_FILE);
156 if (conf->uevent == -1)
167 void blkid_free_config(struct blkid_config *conf)
171 free(conf->cachefile);
177 * usage: tst_config [<filename>]
179 int main(int argc, char *argv[])
182 struct blkid_config *conf;
183 char *filename = NULL;
185 blkid_init_debug(DEBUG_ALL);
190 conf = blkid_read_config(filename);
194 printf("EVALUATE: ");
195 for (i = 0; i < conf->nevals; i++)
196 printf("%s ", conf->eval[i] == BLKID_EVAL_UDEV ? "udev" : "scan");
199 printf("SEND UEVENT: %s\n", conf->uevent ? "TRUE" : "FALSE");
200 printf("CACHE_FILE: %s\n", conf->cachefile);
202 blkid_free_config(conf);