+enum {
+ INDEX_DIR, INDEX_HOSTKEYS, INDEX_RANDSEED,
+ INDEX_SESSIONDIR, INDEX_SESSION,
+};
+
+static const char hex[16] = "0123456789ABCDEF";
+
+static char *mungestr(const char *in)
+{
+ 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");
+ 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_RANDSEED ? "/.putty/randomseed" :
+ "/.putty/ERROR", FILENAME_MAX - len);
+ }
+ filename[FILENAME_MAX-1] = '\0';
+}
+
+/*
+ * Read an entire line of text from a file. Return a buffer
+ * malloced to be as big as necessary (caller must free).
+ */
+static char *fgetline(FILE *fp)
+{
+ char *ret = snewn(512, char);
+ int size = 512, len = 0;
+ while (fgets(ret + len, size - len, fp)) {
+ len += strlen(ret + len);
+ if (ret[len-1] == '\n')
+ break; /* got a newline, we're done */
+ size = len + 512;
+ ret = sresize(ret, size, char);
+ }
+ if (len == 0) { /* first fgets returned NULL */
+ sfree(ret);
+ return NULL;
+ }
+ ret[len] = '\0';
+ return ret;
+}
+