+int read_config(char *file, struct ma_config *config, int *macs)
+{
+ FILE *f;
+ char line[256];
+ int i;
+
+ f = fopen(file, "r");
+ if (f == NULL)
+ return errno;
+
+#define INT_OPTION(opt, var) \
+ if (strncmp(line, opt " ", sizeof(opt)) == 0) { \
+ var = atoi(&line[sizeof(opt)]); \
+ }
+#define STRING_OPTION(opt, var) \
+ if (strncmp(line, opt " ", sizeof(opt)) == 0) { \
+ var = strdup(&line[sizeof(opt)]); \
+ }
+
+ while (fgets(line, sizeof(line), f) != NULL) {
+ for (i = strlen(line) - 1; i >= 0 && isspace(line[i]); i--)
+ line[i] = '\0';
+ if (line[0] == '\0' || line[0] == '#')
+ continue;
+
+ if (strncmp(line, "mac ", 4) == 0) {
+ if (*macs >= MAX_MACS) {
+ printf("Can only accept %d MAC addresses to"
+ " watch for.\n", MAX_MACS);
+ exit(EXIT_FAILURE);
+ }
+ sscanf(&line[4],
+ "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
+ &config->macs[*macs].mac[0],
+ &config->macs[*macs].mac[1],
+ &config->macs[*macs].mac[2],
+ &config->macs[*macs].mac[3],
+ &config->macs[*macs].mac[4],
+ &config->macs[*macs].mac[5]);
+ config->macs[*macs].valid = true;
+ (*macs)++;
+ } else
+ STRING_OPTION("mqtt_host", config->mqtt_host) else
+ INT_OPTION("mqtt_port", config->mqtt_port) else
+ STRING_OPTION("mqtt_user", config->mqtt_username) else
+ STRING_OPTION("mqtt_pass", config->mqtt_password) else
+ STRING_OPTION("mqtt_topic", config->mqtt_topic) else
+ STRING_OPTION("location", config->location) else
+ STRING_OPTION("capath", config->capath)
+ }
+ fclose(f);
+
+ return 0;
+}
+