]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/config.c
110251a92f36c5e2fdb71ddb95c607895ca65d52
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
29 static int parse_evaluate(struct blkid_config
*conf
, char *s
)
34 if (conf
->nevals
>= __BLKID_EVAL_LAST
)
39 if (strcmp(s
, "udev") == 0)
40 conf
->eval
[conf
->nevals
] = BLKID_EVAL_UDEV
;
41 else if (strcmp(s
, "scan") == 0)
42 conf
->eval
[conf
->nevals
] = BLKID_EVAL_SCAN
;
53 DBG(DEBUG_CONFIG
, printf(
54 "config file: unknown evaluation method '%s'.\n", s
));
58 static int parse_next(FILE *fd
, struct blkid_config
*conf
)
63 /* read the next non-blank non-comment line */
65 if (fgets (buf
, sizeof(buf
), fd
) == NULL
)
66 return feof(fd
) ? 0 : -1;
67 s
= strchr (buf
, '\n');
69 /* Missing final newline? Otherwise extremely */
70 /* long line - assume file was corrupted */
72 s
= strchr (buf
, '\0');
74 DBG(DEBUG_CONFIG
, fprintf(stderr
,
75 "libblkid: config file: missing newline at line '%s'.\n",
81 if (--s
>= buf
&& *s
== '\r')
85 while (*s
== ' ' || *s
== '\t') /* skip space */
88 } while (*s
== '\0' || *s
== '#');
90 if (!strncmp(s
, "SEND_UEVENT=", 12)) {
92 if (*s
&& !strcasecmp(s
, "yes"))
96 } else if (!strncmp(s
, "CACHE_FILE=", 11)) {
99 conf
->cachefile
= blkid_strdup(s
);
100 } else if (!strncmp(s
, "EVALUATE=", 9)) {
102 if (*s
&& parse_evaluate(conf
, s
) == -1)
105 DBG(DEBUG_CONFIG
, printf(
106 "config file: unknown option '%s'.\n", s
));
112 /* return real config data or built-in default */
113 struct blkid_config
*blkid_read_config(const char *filename
)
115 struct blkid_config
*conf
;
119 filename
= safe_getenv("BLKID_CONF");
121 filename
= BLKID_CONFIG_FILE
;
123 conf
= (struct blkid_config
*) calloc(1, sizeof(*conf
));
128 DBG(DEBUG_CONFIG
, fprintf(stderr
,
129 "reading config file: %s.\n", filename
));
131 f
= fopen(filename
, "r");
133 DBG(DEBUG_CONFIG
, fprintf(stderr
,
134 "%s: does not exist, using built-in default\n", filename
));
138 if (parse_next(f
, conf
)) {
139 DBG(DEBUG_CONFIG
, fprintf(stderr
,
140 "%s: parse error\n", filename
));
146 conf
->eval
[0] = BLKID_EVAL_UDEV
;
147 conf
->eval
[1] = BLKID_EVAL_SCAN
;
150 if (!conf
->cachefile
)
151 conf
->cachefile
= blkid_strdup(BLKID_CACHE_FILE
);
152 if (conf
->uevent
== -1)
163 void blkid_free_config(struct blkid_config
*conf
)
167 free(conf
->cachefile
);
173 * usage: tst_config [<filename>]
175 int main(int argc
, char *argv
[])
178 struct blkid_config
*conf
;
179 char *filename
= NULL
;
181 blkid_init_debug(DEBUG_ALL
);
186 conf
= blkid_read_config(filename
);
190 printf("EVALUATE: ");
191 for (i
= 0; i
< conf
->nevals
; i
++)
192 printf("%s ", conf
->eval
[i
] == BLKID_EVAL_UDEV
? "udev" : "scan");
195 printf("SEND UEVENT: %s\n", conf
->uevent
? "TRUE" : "FALSE");
196 printf("CACHE_FILE: %s\n", conf
->cachefile
);
198 blkid_free_config(conf
);