if(!fp) {
logger(mesh, MESHLINK_ERROR, "Cannot open config file %s: %s", filename, strerror(errno));
- } else {
- fprintf(fp, "%s = %s\n", key, value);
- fclose(fp);
+ return false;
+ }
+
+ // Check if we don't add a duplicate entry
+
+ char entry[MAX_STRING_SIZE];
+ snprintf(entry, sizeof entry, "%s = %s", key, value);
+
+ char buffer[MAX_STRING_SIZE];
+ bool found = false;
+
+ while(readline(fp, buffer, sizeof buffer)) {
+ if(!strcmp(buffer, entry)) {
+ found = true;
+ break;
+ }
}
- return fp != NULL;
+ // If not, append the new entry
+
+ if(!found)
+ fprintf(fp, "%s\n", entry);
+
+ fclose(fp);
+ return true;
}