Simplify the random-seed-saving code: There's no need to take great care to
[sgt/putty] / mac / macstore.c
index af3503c..910b6c7 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macstore.c,v 1.7 2003/01/08 22:46:12 ben Exp $ */
+/* $Id: macstore.c,v 1.9 2003/01/18 12:03:28 ben Exp $ */
 
 /*
  * macstore.c: Macintosh-specific impementation of the interface
@@ -101,7 +101,7 @@ struct write_settings {
     FSSpec dstfile;
 };
 
-void *open_settings_w(char *sessionname) {
+void *open_settings_w(char const *sessionname) {
     short sessVRefNum, tmpVRefNum;
     long sessDirID, tmpDirID;
     OSErr error;
@@ -144,7 +144,7 @@ void *open_settings_w(char *sessionname) {
     fatalbox("Failed to open session for write (%d)", error);
 }
 
-void write_setting_s(void *handle, char *key, char *value) {
+void write_setting_s(void *handle, char const *key, char const *value) {
     int fd = *(int *)handle;
     Handle h;
     int id;
@@ -166,7 +166,7 @@ void write_setting_s(void *handle, char *key, char *value) {
        fatalbox("Failed to add resource %s (%d)", key, ResError());
 }
 
-void write_setting_i(void *handle, char *key, int value) {
+void write_setting_i(void *handle, char const *key, int value) {
     int fd = *(int *)handle;
     Handle h;
     int id;
@@ -208,7 +208,7 @@ void close_settings_w(void *handle) {
     safefree(handle);
 }
 
-void *open_settings_r(char *sessionname)
+void *open_settings_r(char const *sessionname)
 {
     short sessVRefNum;
     long sessDirID;
@@ -244,7 +244,7 @@ void *open_settings_r_fsp(FSSpec *sessfile)
     return NULL;
 }
 
-char *read_setting_s(void *handle, char *key, char *buffer, int buflen) {
+char *read_setting_s(void *handle, char const *key, char *buffer, int buflen) {
     int fd;
     Handle h;
     size_t len;
@@ -269,7 +269,7 @@ char *read_setting_s(void *handle, char *key, char *buffer, int buflen) {
     return NULL;
 }
 
-int read_setting_i(void *handle, char *key, int defvalue) {
+int read_setting_i(void *handle, char const *key, int defvalue) {
     int fd;
     Handle h;
     int value;
@@ -300,7 +300,7 @@ void close_settings_r(void *handle) {
     safefree(handle);
 }
 
-void del_settings(char *sessionname) {
+void del_settings(char const *sessionname) {
     OSErr error;
     FSSpec sessfile;
     short sessVRefNum;
@@ -390,12 +390,16 @@ void read_random_seed(noise_consumer_t consumer)
     FSClose(refnum);
 }
 
+/*
+ * We don't bother with the usual FSpExchangeFiles dance here because
+ * it doesn't really matter if the old random seed gets lost.
+ */
 void write_random_seed(void *data, int len)
 {
-    short puttyVRefNum, tmpVRefNum;
-    long puttyDirID, tmpDirID;
+    short puttyVRefNum;
+    long puttyDirID;
     OSErr error;
-    FSSpec dstfile, tmpfile;
+    FSSpec dstfile;
     short refnum;
     long count = len;
 
@@ -404,36 +408,16 @@ void write_random_seed(void *data, int len)
 
     error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed",
                         &dstfile);
-    if (error != noErr && error != fnfErr) return;
-
-    /* Create a temporary file to save to first. */
-    error = FindFolder(puttyVRefNum, kTemporaryFolderType, kCreateFolder,
-                      &tmpVRefNum, &tmpDirID);
+    if (error == fnfErr)
+       error = FSpCreate(&dstfile, PUTTY_CREATOR, SEED_TYPE, smRoman);
     if (error != noErr) return;
-    error = FSMakeFSSpec(tmpVRefNum, tmpDirID, "\pPuTTY Random Seed",
-                        &tmpfile);
-    if (error != noErr && error != fnfErr) return;
-    if (error == noErr) {
-       error = FSpDelete(&tmpfile);
-       if (error != noErr) return;
-    }
-    error = FSpCreate(&tmpfile, PUTTY_CREATOR, SEED_TYPE, smRoman);
-    if (error != noErr) return;
-
-    if (FSpOpenDF(&tmpfile, fsWrPerm, &refnum) != noErr) goto fail;
 
-    if (FSWrite(refnum, &count, data) != noErr) goto fail2;
-    if (FSClose(refnum) != noErr) goto fail;
+    if (FSpOpenDF(&dstfile, fsWrPerm, &refnum) != noErr) return;
 
-    if (FSpExchangeFiles(&tmpfile, &dstfile) != noErr) goto fail;
-    if (FSpDelete(&tmpfile) != noErr) return;
+    FSWrite(refnum, &count, data);
 
-    return;
-    
-  fail2:
     FSClose(refnum);
-  fail:
-    FSpDelete(&tmpfile);
+    return;
 }
 
 /*