-FILE *ask_and_open(const char *filename, const char *what) {
- FILE *r;
- char *directory;
- char line[PATH_MAX];
- const char *fn;
-
- /* Check stdin and stdout */
- if(!isatty(0) || !isatty(1)) {
- /* Argh, they are running us from a script or something. Write
- the files to the current directory and let them burn in hell
- for ever. */
- fn = filename;
- } else {
- /* Ask for a file and/or directory name. */
- fprintf(stdout, "Please enter a file to save %s to [%s]: ",
- what, filename);
- fflush(stdout);
-
- fn = readline(stdin, line, sizeof line);
-
- if(!fn) {
- fprintf(stderr, "Error while reading stdin: %s\n",
- strerror(errno));
- return NULL;
+bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *config_tree, const char *name)
+{
+ char filename[PATH_MAX];
+
+ snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+ return write_config_file(config_tree, filename);
+}
+
+bool change_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
+ char filename[PATH_MAX];
+ char tmpname[PATH_MAX];
+ char buf[MAX_STRING_SIZE];
+ const int keylen = strlen(key);
+
+ snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+ snprintf(tmpname, PATH_MAX, "%s.tmp", filename);
+
+ FILE *in = fopen(filename, "r");
+ if(!in) {
+ // Hm, maybe the file does not exist? Try appending.
+ return append_config_file(mesh, name, key, value);
+ }
+
+ FILE *out = fopen(tmpname, "w");
+ if(!out) {
+ logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmpname, strerror(errno));
+ fclose(in);
+ return false;
+ }
+
+ bool ignore = false;
+
+ while(readline(in, buf, sizeof buf)) {
+ if(ignore) {
+ if(!strncmp(buf, "-----END", 8))
+ ignore = false;
+ } else {
+ if(!strncmp(buf, "-----BEGIN", 10))
+ ignore = true;
+ }
+
+ if(!ignore && !strncmp(buf, key, keylen)) {
+ if(strchr("\t =", buf[keylen])) {
+ continue;
+ }