+ char *out, *ret;
+
+ if (!in || !*in)
+ in = "Default Settings";
+
+ ret = out = snewn(3*strlen(in)+1, char);
+
+ while (*in) {
+ /*
+ * There are remarkably few punctuation characters that
+ * aren't shell-special in some way or likely to be used as
+ * separators in some file format or another! Hence we use
+ * opt-in for safe characters rather than opt-out for
+ * specific unsafe ones...
+ */
+ if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' &&
+ !(*in >= '0' && *in <= '9') &&
+ !(*in >= 'A' && *in <= 'Z') &&
+ !(*in >= 'a' && *in <= 'z')) {
+ *out++ = '%';
+ *out++ = hex[((unsigned char) *in) >> 4];
+ *out++ = hex[((unsigned char) *in) & 15];
+ } else
+ *out++ = *in;
+ in++;
+ }
+ *out = '\0';
+ return ret;
+}
+
+static char *unmungestr(const char *in)
+{
+ char *out, *ret;
+ out = ret = snewn(strlen(in)+1, char);
+ while (*in) {
+ if (*in == '%' && in[1] && in[2]) {
+ int i, j;
+
+ i = in[1] - '0';
+ i -= (i > 9 ? 7 : 0);
+ j = in[2] - '0';
+ j -= (j > 9 ? 7 : 0);
+
+ *out++ = (i << 4) + j;
+ in += 3;
+ } else {
+ *out++ = *in++;
+ }
+ }
+ *out = '\0';
+ return ret;
+}
+
+static void make_filename(char *filename, int index, const char *subname)
+{
+ char *home;
+ int len;
+ home = getenv("HOME");
+ if (!home)
+ home="/";
+ strncpy(filename, home, FILENAME_MAX);
+ len = strlen(filename);
+ if (index == INDEX_SESSION) {
+ char *munged = mungestr(subname);
+ char *fn = dupprintf("/.putty/sessions/%s", munged);
+ strncpy(filename + len, fn, FILENAME_MAX - len);
+ sfree(fn);
+ sfree(munged);
+ } else {
+ strncpy(filename + len,
+ index == INDEX_DIR ? "/.putty" :
+ index == INDEX_SESSIONDIR ? "/.putty/sessions" :
+ index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" :
+ index == INDEX_HOSTKEYS_TMP ? "/.putty/sshhostkeys.tmp" :
+ index == INDEX_RANDSEED ? "/.putty/randomseed" :
+ "/.putty/ERROR", FILENAME_MAX - len);
+ }
+ filename[FILENAME_MAX-1] = '\0';
+}
+
+void *open_settings_w(const char *sessionname, char **errmsg)
+{
+ char filename[FILENAME_MAX];
+ FILE *fp;
+
+ *errmsg = NULL;
+
+ /*
+ * Start by making sure the .putty directory and its sessions
+ * subdir actually exist. Ignore error returns from mkdir since
+ * they're perfectly likely to be `already exists', and any
+ * other error will trip us up later on so there's no real need
+ * to catch it now.
+ */
+ make_filename(filename, INDEX_DIR, sessionname);
+ mkdir(filename, 0700);
+ make_filename(filename, INDEX_SESSIONDIR, sessionname);
+ mkdir(filename, 0700);
+
+ make_filename(filename, INDEX_SESSION, sessionname);
+ fp = fopen(filename, "w");
+ if (!fp) {
+ *errmsg = dupprintf("Unable to create %s: %s",
+ filename, strerror(errno));
+ return NULL; /* can't open */
+ }
+ return fp;