Read a .config file
Find whether an option is built in, a module, a value, or not set. An easy embedded C problem (Linux build systems), run and graded in the browser.
easy · Linux build systems · linux · kconfig · parsing
The Linux kernel, Buildroot and Zephyr all store their configuration in a .config file written by Kconfig:
CONFIG_SPI=y
CONFIG_SPI_SPIDEV=m
# CONFIG_SOUND is not set
CONFIG_HZ=250
CONFIG_LOCALVERSION="-board"
Write kconfig_get(config, option, out, size): find the line for option (the full name, CONFIG_ included) and return:
- 1 for
=y(built in), 2 for=m(module), 3 for any other value, copying the value (without surrounding quotes) toout, - 0 for
# CONFIG_X is not set, or if the option does not appear at all.
The option name must match the whole name: CONFIG_SPI must not match CONFIG_SPI_SPIDEV.
Starter code
int kconfig_get(const char *config, const char *option, char *out, int size) {
const char *p = strstr(config, option);
if (!p) return 0;
p += strlen(option) + 1;
if (*p == 'y') return 1;
if (*p == 'm') return 2;
int n = 0;
while (*p && *p != '\n' && n < size - 1) out[n++] = *p++;
out[n] = 0;
return 3;
}Example tests
Built in and modules
show("CONFIG_SPI");
show("CONFIG_SPI_SPIDEV");Expected output:
CONFIG_SPI: built in
CONFIG_SPI_SPIDEV: moduleLearn it first
- Linux build systems · Yocto, Buildroot and OpenWrt: layers, recipes and images.