746dbe2c |
1 | /* $Id: macstore.c,v 1.17 2003/03/27 22:46:28 ben Exp $ */ |
d082ac49 |
2 | |
3 | /* |
4 | * macstore.c: Macintosh-specific impementation of the interface |
5 | * defined in storage.h |
6 | */ |
7 | |
8 | #include <MacTypes.h> |
9 | #include <Folders.h> |
10 | #include <Memory.h> |
11 | #include <Resources.h> |
12 | #include <TextUtils.h> |
13 | |
b537dd42 |
14 | #include <stdio.h> |
d082ac49 |
15 | #include <string.h> |
16 | |
17 | #include "putty.h" |
18 | #include "storage.h" |
ce283213 |
19 | #include "mac.h" |
1d4cb74b |
20 | #include "macresid.h" |
d082ac49 |
21 | |
d082ac49 |
22 | |
d082ac49 |
23 | OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit); |
24 | |
25 | /* |
26 | * We store each session as a file in the "PuTTY" sub-directory of the |
27 | * preferences folder. Each (key,value) pair is stored as a resource. |
28 | */ |
29 | |
0c4b7799 |
30 | OSErr get_putty_dir(Boolean makeit, short *pVRefNum, long *pDirID) |
31 | { |
d082ac49 |
32 | OSErr error = noErr; |
33 | short prefVRefNum; |
0c4b7799 |
34 | FSSpec puttydir; |
35 | long prefDirID, puttyDirID; |
d082ac49 |
36 | |
37 | error = FindFolder(kOnSystemDisk, kPreferencesFolderType, makeit, |
38 | &prefVRefNum, &prefDirID); |
39 | if (error != noErr) goto out; |
40 | |
41 | error = FSMakeFSSpec(prefVRefNum, prefDirID, "\pPuTTY", &puttydir); |
42 | if (error != noErr && error != fnfErr) goto out; |
43 | error = FSpGetDirID(&puttydir, &puttyDirID, makeit); |
44 | if (error != noErr) goto out; |
45 | |
0c4b7799 |
46 | *pVRefNum = prefVRefNum; |
47 | *pDirID = puttyDirID; |
48 | |
49 | out: |
50 | return error; |
51 | } |
52 | |
53 | OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID) { |
54 | OSErr error = noErr; |
55 | short puttyVRefNum; |
56 | FSSpec sessdir; |
57 | long puttyDirID, sessDirID; |
58 | |
59 | error = get_putty_dir(makeit, &puttyVRefNum, &puttyDirID); |
60 | if (error != noErr) goto out; |
61 | error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pSaved Sessions", |
d082ac49 |
62 | &sessdir); |
63 | if (error != noErr && error != fnfErr) goto out; |
64 | error = FSpGetDirID(&sessdir, &sessDirID, makeit); |
65 | if (error != noErr) goto out; |
66 | |
0c4b7799 |
67 | *pVRefNum = puttyVRefNum; |
d082ac49 |
68 | *pDirID = sessDirID; |
69 | |
70 | out: |
71 | return error; |
72 | } |
73 | |
74 | OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit) { |
75 | CInfoPBRec pb; |
76 | OSErr error = noErr; |
77 | |
78 | pb.dirInfo.ioNamePtr = f->name; |
79 | pb.dirInfo.ioVRefNum = f->vRefNum; |
80 | pb.dirInfo.ioDrDirID = f->parID; |
81 | pb.dirInfo.ioFDirIndex = 0; |
82 | error = PBGetCatInfoSync(&pb); |
83 | if (error == fnfErr && makeit) |
84 | return FSpDirCreate(f, smSystemScript, idp); |
85 | if (error != noErr) goto out; |
86 | if ((pb.dirInfo.ioFlAttrib & ioDirMask) == 0) { |
87 | error = dirNFErr; |
88 | goto out; |
89 | } |
90 | *idp = pb.dirInfo.ioDrDirID; |
91 | |
92 | out: |
93 | return error; |
94 | } |
95 | |
1d4cb74b |
96 | /* Copy a resource into the current resource file */ |
97 | static OSErr copy_resource(ResType restype, short resid) |
98 | { |
99 | Handle h; |
100 | Str255 resname; |
101 | |
1d4cb74b |
102 | h = GetResource(restype, resid); |
103 | if (h != NULL) { |
104 | GetResInfo(h, &resid, &restype, resname); |
105 | DetachResource(h); |
106 | AddResource(h, restype, resid, resname); |
107 | if (ResError() == noErr) |
108 | WriteResource(h); |
109 | } |
1d4cb74b |
110 | return ResError(); |
111 | } |
112 | |
d082ac49 |
113 | struct write_settings { |
114 | int fd; |
115 | FSSpec tmpfile; |
116 | FSSpec dstfile; |
117 | }; |
118 | |
f5d2d791 |
119 | void *open_settings_w(char const *sessionname) { |
b537dd42 |
120 | short sessVRefNum; |
121 | long sessDirID; |
d082ac49 |
122 | OSErr error; |
123 | Str255 psessionname; |
b537dd42 |
124 | FSSpec dstfile; |
d082ac49 |
125 | |
d082ac49 |
126 | error = get_session_dir(kCreateFolder, &sessVRefNum, &sessDirID); |
b537dd42 |
127 | if (error != noErr) return NULL; |
d082ac49 |
128 | |
746dbe2c |
129 | if (!sessionname || !*sessionname) |
130 | sessionname = "Default Settings"; |
d082ac49 |
131 | c2pstrcpy(psessionname, sessionname); |
b537dd42 |
132 | error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &dstfile); |
d082ac49 |
133 | if (error == fnfErr) { |
b537dd42 |
134 | FSpCreateResFile(&dstfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript); |
135 | if ((error = ResError()) != noErr) return NULL; |
136 | } else if (error != noErr) return NULL; |
137 | |
138 | return open_settings_w_fsp(&dstfile); |
139 | } |
140 | |
141 | /* |
142 | * NB: Destination file must exist. |
143 | */ |
144 | void *open_settings_w_fsp(FSSpec *dstfile) |
145 | { |
146 | short tmpVRefNum; |
147 | long tmpDirID; |
148 | struct write_settings *ws; |
149 | OSErr error; |
150 | Str255 tmpname; |
151 | |
152 | ws = smalloc(sizeof *ws); |
153 | ws->dstfile = *dstfile; |
d082ac49 |
154 | |
155 | /* Create a temporary file to save to first. */ |
b537dd42 |
156 | error = FindFolder(ws->dstfile.vRefNum, kTemporaryFolderType, |
157 | kCreateFolder, &tmpVRefNum, &tmpDirID); |
d082ac49 |
158 | if (error != noErr) goto out; |
b537dd42 |
159 | c2pstrcpy(tmpname, tmpnam(NULL)); |
160 | error = FSMakeFSSpec(tmpVRefNum, tmpDirID, tmpname, &ws->tmpfile); |
d082ac49 |
161 | if (error != noErr && error != fnfErr) goto out; |
162 | if (error == noErr) { |
163 | error = FSpDelete(&ws->tmpfile); |
164 | if (error != noErr) goto out; |
165 | } |
166 | FSpCreateResFile(&ws->tmpfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript); |
167 | if ((error = ResError()) != noErr) goto out; |
168 | |
169 | ws->fd = FSpOpenResFile(&ws->tmpfile, fsWrPerm); |
170 | if (ws->fd == -1) {error = ResError(); goto out;} |
171 | |
1d4cb74b |
172 | /* Set up standard resources. Doesn't matter if these fail. */ |
173 | copy_resource('STR ', -16396); |
174 | copy_resource('TMPL', TMPL_Int); |
175 | |
d082ac49 |
176 | return ws; |
177 | |
178 | out: |
179 | safefree(ws); |
180 | fatalbox("Failed to open session for write (%d)", error); |
181 | } |
182 | |
f5d2d791 |
183 | void write_setting_s(void *handle, char const *key, char const *value) { |
d082ac49 |
184 | int fd = *(int *)handle; |
185 | Handle h; |
186 | int id; |
803efcdf |
187 | OSErr error; |
a7459895 |
188 | Str255 pkey; |
d082ac49 |
189 | |
190 | UseResFile(fd); |
191 | if (ResError() != noErr) |
192 | fatalbox("Failed to open saved session (%d)", ResError()); |
193 | |
194 | error = PtrToHand(value, &h, strlen(value)); |
195 | if (error != noErr) |
196 | fatalbox("Failed to allocate memory"); |
197 | /* Put the data in a resource. */ |
198 | id = Unique1ID(FOUR_CHAR_CODE('TEXT')); |
199 | if (ResError() != noErr) |
200 | fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); |
a7459895 |
201 | c2pstrcpy(pkey, key); |
202 | AddResource(h, FOUR_CHAR_CODE('TEXT'), id, pkey); |
d082ac49 |
203 | if (ResError() != noErr) |
204 | fatalbox("Failed to add resource %s (%d)", key, ResError()); |
205 | } |
206 | |
f5d2d791 |
207 | void write_setting_i(void *handle, char const *key, int value) { |
d082ac49 |
208 | int fd = *(int *)handle; |
209 | Handle h; |
210 | int id; |
211 | OSErr error; |
a7459895 |
212 | Str255 pkey; |
d082ac49 |
213 | |
214 | UseResFile(fd); |
215 | if (ResError() != noErr) |
216 | fatalbox("Failed to open saved session (%d)", ResError()); |
217 | |
218 | /* XXX assume all systems have the same "int" format */ |
219 | error = PtrToHand(&value, &h, sizeof(int)); |
220 | if (error != noErr) |
221 | fatalbox("Failed to allocate memory (%d)", error); |
222 | |
223 | /* Put the data in a resource. */ |
224 | id = Unique1ID(FOUR_CHAR_CODE('Int ')); |
225 | if (ResError() != noErr) |
226 | fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); |
a7459895 |
227 | c2pstrcpy(pkey, key); |
228 | AddResource(h, FOUR_CHAR_CODE('Int '), id, pkey); |
d082ac49 |
229 | if (ResError() != noErr) |
230 | fatalbox("Failed to add resource %s (%d)", key, ResError()); |
231 | } |
232 | |
233 | void close_settings_w(void *handle) { |
234 | struct write_settings *ws = handle; |
235 | OSErr error; |
236 | |
237 | CloseResFile(ws->fd); |
238 | if ((error = ResError()) != noErr) |
239 | goto out; |
240 | error = FSpExchangeFiles(&ws->tmpfile, &ws->dstfile); |
241 | if (error != noErr) goto out; |
242 | error = FSpDelete(&ws->tmpfile); |
243 | if (error != noErr) goto out; |
244 | return; |
245 | |
246 | out: |
247 | fatalbox("Close of saved session failed (%d)", error); |
248 | safefree(handle); |
249 | } |
250 | |
f5d2d791 |
251 | void *open_settings_r(char const *sessionname) |
ce283213 |
252 | { |
d082ac49 |
253 | short sessVRefNum; |
254 | long sessDirID; |
255 | FSSpec sessfile; |
256 | OSErr error; |
257 | Str255 psessionname; |
d082ac49 |
258 | |
259 | error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID); |
260 | |
746dbe2c |
261 | if (!sessionname || !*sessionname) |
262 | sessionname = "Default Settings"; |
d082ac49 |
263 | c2pstrcpy(psessionname, sessionname); |
264 | error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile); |
265 | if (error != noErr) goto out; |
ce283213 |
266 | return open_settings_r_fsp(&sessfile); |
267 | |
268 | out: |
269 | return NULL; |
270 | } |
271 | |
272 | void *open_settings_r_fsp(FSSpec *sessfile) |
273 | { |
274 | OSErr error; |
275 | int fd; |
276 | int *handle; |
277 | |
278 | fd = FSpOpenResFile(sessfile, fsRdPerm); |
d082ac49 |
279 | if (fd == 0) {error = ResError(); goto out;} |
280 | |
a7459895 |
281 | handle = smalloc(sizeof *handle); |
d082ac49 |
282 | *handle = fd; |
283 | return handle; |
284 | |
285 | out: |
286 | return NULL; |
287 | } |
288 | |
f5d2d791 |
289 | char *read_setting_s(void *handle, char const *key, char *buffer, int buflen) { |
d082ac49 |
290 | int fd; |
291 | Handle h; |
086efcde |
292 | size_t len; |
a7459895 |
293 | Str255 pkey; |
d082ac49 |
294 | |
295 | if (handle == NULL) goto out; |
296 | fd = *(int *)handle; |
297 | UseResFile(fd); |
298 | if (ResError() != noErr) goto out; |
a7459895 |
299 | c2pstrcpy(pkey, key); |
300 | h = Get1NamedResource(FOUR_CHAR_CODE('TEXT'), pkey); |
d082ac49 |
301 | if (h == NULL) goto out; |
302 | |
086efcde |
303 | len = GetHandleSize(h); |
304 | if (len + 1 > buflen) goto out; |
305 | memcpy(buffer, *h, len); |
306 | buffer[len] = '\0'; |
307 | |
d082ac49 |
308 | ReleaseResource(h); |
309 | if (ResError() != noErr) goto out; |
310 | return buffer; |
311 | |
312 | out: |
313 | return NULL; |
314 | } |
315 | |
f5d2d791 |
316 | int read_setting_i(void *handle, char const *key, int defvalue) { |
d082ac49 |
317 | int fd; |
318 | Handle h; |
d082ac49 |
319 | int value; |
a7459895 |
320 | Str255 pkey; |
d082ac49 |
321 | |
322 | if (handle == NULL) goto out; |
323 | fd = *(int *)handle; |
324 | UseResFile(fd); |
325 | if (ResError() != noErr) goto out; |
a7459895 |
326 | c2pstrcpy(pkey, key); |
327 | h = Get1NamedResource(FOUR_CHAR_CODE('Int '), pkey); |
d082ac49 |
328 | if (h == NULL) goto out; |
329 | value = *(int *)*h; |
330 | ReleaseResource(h); |
331 | if (ResError() != noErr) goto out; |
332 | return value; |
333 | |
334 | out: |
335 | return defvalue; |
336 | } |
337 | |
9a30e26b |
338 | int read_setting_fontspec(void *handle, const char *name, FontSpec *result) |
339 | { |
340 | char *settingname; |
341 | FontSpec ret; |
e3c5b245 |
342 | char tmp[256]; |
9a30e26b |
343 | |
e3c5b245 |
344 | if (!read_setting_s(handle, name, tmp, sizeof(tmp))) |
9a30e26b |
345 | return 0; |
e3c5b245 |
346 | c2pstrcpy(ret.name, tmp); |
347 | settingname = dupcat(name, "Face", NULL); |
348 | ret.face = read_setting_i(handle, settingname, 0); |
9a30e26b |
349 | sfree(settingname); |
9a30e26b |
350 | settingname = dupcat(name, "Height", NULL); |
e3c5b245 |
351 | ret.size = read_setting_i(handle, settingname, 0); |
9a30e26b |
352 | sfree(settingname); |
e3c5b245 |
353 | if (ret.size == 0) return 0; |
9a30e26b |
354 | *result = ret; |
355 | return 1; |
356 | } |
357 | |
358 | void write_setting_fontspec(void *handle, const char *name, FontSpec font) |
359 | { |
360 | char *settingname; |
e3c5b245 |
361 | char tmp[256]; |
9a30e26b |
362 | |
e3c5b245 |
363 | p2cstrcpy(tmp, font.name); |
364 | write_setting_s(handle, name, tmp); |
365 | settingname = dupcat(name, "Face", NULL); |
366 | write_setting_i(handle, settingname, font.face); |
9a30e26b |
367 | sfree(settingname); |
e3c5b245 |
368 | settingname = dupcat(name, "Size", NULL); |
369 | write_setting_i(handle, settingname, font.size); |
9a30e26b |
370 | sfree(settingname); |
371 | } |
372 | |
a7459895 |
373 | int read_setting_filename(void *handle, const char *key, Filename *result) |
9a30e26b |
374 | { |
02cf4001 |
375 | int fd; |
376 | AliasHandle h; |
377 | Boolean changed; |
378 | OSErr err; |
a7459895 |
379 | Str255 pkey; |
02cf4001 |
380 | |
381 | if (handle == NULL) goto out; |
382 | fd = *(int *)handle; |
383 | UseResFile(fd); |
384 | if (ResError() != noErr) goto out; |
a7459895 |
385 | c2pstrcpy(pkey, key); |
386 | h = (AliasHandle)Get1NamedResource(rAliasType, pkey); |
02cf4001 |
387 | if (h == NULL) goto out; |
388 | if ((*h)->userType == 'pTTY' && (*h)->aliasSize == sizeof(**h)) |
389 | memset(result, 0, sizeof(*result)); |
390 | else { |
391 | err = ResolveAlias(NULL, h, &result->fss, &changed); |
392 | if (err != noErr && err != fnfErr) goto out; |
393 | if ((*h)->userType == 'pTTY') { |
394 | long dirid; |
395 | StrFileName fname; |
396 | |
397 | /* Tail of record is pascal string contaning leafname */ |
398 | if (FSpGetDirID(&result->fss, &dirid, FALSE) != noErr) goto out; |
399 | memcpy(fname, (char *)*h + (*h)->aliasSize, |
400 | GetHandleSize((Handle)h) - (*h)->aliasSize); |
401 | err = FSMakeFSSpec(result->fss.vRefNum, dirid, fname, |
402 | &result->fss); |
403 | if (err != noErr && err != fnfErr) goto out; |
404 | } |
405 | } |
406 | ReleaseResource((Handle)h); |
407 | if (ResError() != noErr) goto out; |
408 | return 1; |
409 | |
410 | out: |
411 | return 0; |
9a30e26b |
412 | } |
413 | |
a7459895 |
414 | void write_setting_filename(void *handle, const char *key, Filename fn) |
9a30e26b |
415 | { |
02cf4001 |
416 | int fd = *(int *)handle; |
417 | AliasHandle h; |
418 | int id; |
419 | OSErr error; |
a7459895 |
420 | Str255 pkey; |
02cf4001 |
421 | |
422 | UseResFile(fd); |
423 | if (ResError() != noErr) |
424 | fatalbox("Failed to open saved session (%d)", ResError()); |
425 | |
426 | if (filename_is_null(fn)) { |
427 | /* Generate a special "null" alias */ |
428 | h = (AliasHandle)NewHandle(sizeof(**h)); |
429 | if (h == NULL) |
430 | fatalbox("Failed to create fake alias"); |
431 | (*h)->userType = 'pTTY'; |
432 | (*h)->aliasSize = sizeof(**h); |
433 | } else { |
434 | error = NewAlias(NULL, &fn.fss, &h); |
435 | if (error == fnfErr) { |
436 | /* |
437 | * NewAlias can't create an alias for a nonexistent file. |
438 | * Create an alias for the directory, and record the |
439 | * filename as well. |
440 | */ |
441 | FSSpec tmpfss; |
442 | |
443 | FSMakeFSSpec(fn.fss.vRefNum, fn.fss.parID, NULL, &tmpfss); |
444 | error = NewAlias(NULL, &tmpfss, &h); |
445 | if (error != noErr) |
446 | fatalbox("Failed to create alias"); |
447 | (*h)->userType = 'pTTY'; |
448 | SetHandleSize((Handle)h, (*h)->aliasSize + fn.fss.name[0] + 1); |
449 | if (MemError() != noErr) |
450 | fatalbox("Failed to create alias"); |
451 | memcpy((char *)*h + (*h)->aliasSize, fn.fss.name, |
452 | fn.fss.name[0] + 1); |
453 | } |
454 | if (error != noErr) |
455 | fatalbox("Failed to create alias"); |
456 | } |
457 | /* Put the data in a resource. */ |
458 | id = Unique1ID(rAliasType); |
459 | if (ResError() != noErr) |
a7459895 |
460 | fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); |
461 | c2pstrcpy(pkey, key); |
462 | AddResource((Handle)h, rAliasType, id, pkey); |
02cf4001 |
463 | if (ResError() != noErr) |
a7459895 |
464 | fatalbox("Failed to add resource %s (%d)", key, ResError()); |
9a30e26b |
465 | } |
466 | |
d082ac49 |
467 | void close_settings_r(void *handle) { |
468 | int fd; |
469 | |
470 | if (handle == NULL) return; |
471 | fd = *(int *)handle; |
472 | CloseResFile(fd); |
473 | if (ResError() != noErr) |
474 | fatalbox("Close of saved session failed (%d)", ResError()); |
a7459895 |
475 | sfree(handle); |
d082ac49 |
476 | } |
477 | |
f5d2d791 |
478 | void del_settings(char const *sessionname) { |
d082ac49 |
479 | OSErr error; |
480 | FSSpec sessfile; |
481 | short sessVRefNum; |
482 | long sessDirID; |
483 | Str255 psessionname; |
484 | |
485 | error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID); |
486 | |
487 | c2pstrcpy(psessionname, sessionname); |
488 | error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile); |
489 | if (error != noErr) goto out; |
490 | |
491 | error = FSpDelete(&sessfile); |
492 | return; |
493 | out: |
494 | fatalbox("Delete session failed (%d)", error); |
495 | } |
496 | |
497 | struct enum_settings_state { |
498 | short vRefNum; |
499 | long dirID; |
500 | int index; |
501 | }; |
502 | |
503 | void *enum_settings_start(void) { |
504 | OSErr error; |
505 | struct enum_settings_state *state; |
506 | |
507 | state = safemalloc(sizeof(*state)); |
508 | error = get_session_dir(kDontCreateFolder, &state->vRefNum, &state->dirID); |
509 | if (error != noErr) { |
510 | safefree(state); |
511 | return NULL; |
512 | } |
513 | state->index = 1; |
514 | return state; |
515 | } |
516 | |
517 | char *enum_settings_next(void *handle, char *buffer, int buflen) { |
518 | struct enum_settings_state *e = handle; |
519 | CInfoPBRec pb; |
520 | OSErr error = noErr; |
521 | Str255 name; |
522 | |
523 | if (e == NULL) return NULL; |
524 | do { |
525 | pb.hFileInfo.ioNamePtr = name; |
526 | pb.hFileInfo.ioVRefNum = e->vRefNum; |
527 | pb.hFileInfo.ioDirID = e->dirID; |
528 | pb.hFileInfo.ioFDirIndex = e->index++; |
529 | error = PBGetCatInfoSync(&pb); |
530 | if (error != noErr) return NULL; |
531 | } while (!((pb.hFileInfo.ioFlAttrib & ioDirMask) == 0 && |
532 | pb.hFileInfo.ioFlFndrInfo.fdCreator == PUTTY_CREATOR && |
533 | pb.hFileInfo.ioFlFndrInfo.fdType == SESS_TYPE && |
534 | name[0] < buflen)); |
535 | |
536 | p2cstrcpy(buffer, name); |
537 | return buffer; |
538 | } |
539 | |
540 | void enum_settings_finish(void *handle) { |
541 | |
542 | safefree(handle); |
543 | } |
544 | |
0c4b7799 |
545 | #define SEED_SIZE 512 |
546 | |
547 | void read_random_seed(noise_consumer_t consumer) |
548 | { |
549 | short puttyVRefNum; |
550 | long puttyDirID; |
551 | OSErr error; |
552 | char buf[SEED_SIZE]; |
553 | short refnum; |
554 | long count = SEED_SIZE; |
555 | |
556 | if (get_putty_dir(kDontCreateFolder, &puttyVRefNum, &puttyDirID) != noErr) |
557 | return; |
558 | if (HOpenDF(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed", fsRdPerm, |
559 | &refnum) != noErr) |
560 | return; |
561 | error = FSRead(refnum, &count, buf); |
562 | if (error != noErr && error != eofErr) |
563 | return; |
564 | (*consumer)(buf, count); |
565 | FSClose(refnum); |
566 | } |
567 | |
604379f1 |
568 | /* |
569 | * We don't bother with the usual FSpExchangeFiles dance here because |
570 | * it doesn't really matter if the old random seed gets lost. |
571 | */ |
0c4b7799 |
572 | void write_random_seed(void *data, int len) |
573 | { |
604379f1 |
574 | short puttyVRefNum; |
575 | long puttyDirID; |
0c4b7799 |
576 | OSErr error; |
604379f1 |
577 | FSSpec dstfile; |
0c4b7799 |
578 | short refnum; |
579 | long count = len; |
580 | |
581 | if (get_putty_dir(kCreateFolder, &puttyVRefNum, &puttyDirID) != noErr) |
582 | return; |
583 | |
584 | error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed", |
585 | &dstfile); |
1d4cb74b |
586 | if (error == fnfErr) { |
587 | /* Set up standard resources */ |
588 | FSpCreateResFile(&dstfile, INTERNAL_CREATOR, SEED_TYPE, smRoman); |
589 | refnum = FSpOpenResFile(&dstfile, fsWrPerm); |
590 | if (ResError() == noErr) { |
591 | copy_resource('STR ', -16397); |
592 | CloseResFile(refnum); |
593 | } |
594 | } else if (error != noErr) return; |
0c4b7799 |
595 | |
604379f1 |
596 | if (FSpOpenDF(&dstfile, fsWrPerm, &refnum) != noErr) return; |
604379f1 |
597 | FSWrite(refnum, &count, data); |
0c4b7799 |
598 | FSClose(refnum); |
1d4cb74b |
599 | |
604379f1 |
600 | return; |
0c4b7799 |
601 | } |
d082ac49 |
602 | |
603 | /* |
604 | * Emacs magic: |
605 | * Local Variables: |
606 | * c-file-style: "simon" |
607 | * End: |
608 | */ |