Download
vkoskiv
/MacNTP
/prefs.c
(View History)
vkoskiv Add copyright headers | Latest amendment: 12 on 2023-09-07 |
1 | /* |
2 | * Copyright (c) 2023 Valtteri Koskivuori <vkoskiv@gmail.com> |
3 | * |
4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above |
6 | * copyright notice and this permission notice appear in all copies. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | */ |
16 | |
17 | /* |
18 | Prefs storage for MacNTP. |
19 | The prefs are just two STR resources with the following IDs: |
20 | 128 - NTP server URL as a Pascal string |
21 | 129 - UTC offset as a Pascal String |
22 | */ |
23 | |
24 | #include <OSUtils.h> |
25 | #include "prefs.h" |
26 | |
27 | // Yoink from dnr.c |
28 | void GetSystemFolder(short *vRefNumP, long *dirIDP); |
29 | |
30 | short SearchForVKOS(short vRefNum, long dirID); |
31 | short OpenMacNTP_RF(void); |
32 | |
33 | short SearchForVKOS(short vRefNum, long dirID) { |
34 | HParamBlockRec fi; |
35 | Str255 filename; |
36 | short refnum; |
37 | |
38 | fi.fileParam.ioCompletion = nil; |
39 | fi.fileParam.ioNamePtr = filename; |
40 | fi.fileParam.ioVRefNum = vRefNum; |
41 | fi.fileParam.ioDirID = dirID; |
42 | fi.fileParam.ioFDirIndex = 1; |
43 | |
44 | while (PBHGetFInfo(&fi, false) == noErr) { |
45 | /* scan system folder for driver resource files of specific type & creator */ |
46 | //TODO: Maybe also check for xNIT? Is it a problem if this file is in use? |
47 | if ((fi.fileParam.ioFlFndrInfo.fdType == 'INIT' || |
48 | fi.fileParam.ioFlFndrInfo.fdType == 'xNIT') && |
49 | fi.fileParam.ioFlFndrInfo.fdCreator == 'VKOS') { |
50 | /* found the MacNTP file? */ |
51 | refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm); |
52 | return refnum; |
53 | } |
54 | |
55 | /* check next file in system folder */ |
56 | fi.fileParam.ioFDirIndex++; |
57 | fi.fileParam.ioDirID = dirID; /* PBHGetFInfo() clobbers ioDirID */ |
58 | } |
59 | return -1; |
60 | } |
61 | |
62 | short OpenMacNTP_RF(void) { |
63 | short refnum; |
64 | short vRefNum; |
65 | long dirID; |
66 | |
67 | vRefNum = 0; |
68 | dirID = 0; |
69 | |
70 | GetSystemFolder(&vRefNum, &dirID); |
71 | |
72 | // VKOS is the creator ID for MacNTP. |
73 | // It used to be that Apple wanted developers to officially register |
74 | // these creator IDs before using them in production, but I doubt they |
75 | // accept registrations these days! |
76 | refnum = SearchForVKOS(vRefNum, dirID); |
77 | if (refnum = -1) { |
78 | return -1; |
79 | } |
80 | |
81 | return refnum; |
82 | } |
83 | |
84 | /* It might be that these will only be useful for the cdev, I'm having |
85 | a hard time trying to get the INIT to look in the system folder. |
86 | I think it makes sense that only the cdev cares about these to update |
87 | the rsrc file on disk, and we just load it while we load everything else |
88 | in the init, and keep those in the system heap. It will only ever |
89 | query the time on boot anyway. |
90 | */ |
91 | |
92 | // This should hopefully maybe maybe return a C string of the URL maybe |
93 | // Written at 1AM on a wednesday though, so buyer beware. |
94 | short get_url_param(Str255 url) { |
95 | StringHandle url_handle; |
96 | short refnum; |
97 | refnum = OpenMacNTP_RF(); |
98 | if (refnum == -1) { |
99 | return -1; |
100 | } |
101 | |
102 | url_handle = GetString(128); |
103 | HLock(url_handle); |
104 | if (url_handle == NULL) { |
105 | return -1; |
106 | } |
107 | BlockMove(url_handle, &url, 1 + *(char *)(url_handle)); //sus |
108 | HUnlock(url_handle); |
109 | DisposHandle(url_handle); // Needed?? |
110 | CloseResFile(refnum); |
111 | |
112 | PtoCstr(url); |
113 | return 0; |
114 | } |
115 | |
116 | short set_url_param(Str255 url) { |
117 | //TODO |
118 | return -1; |
119 | } |
120 | |
121 | short get_utc_param(Str255 url) { |
122 | //TODO |
123 | return -1; |
124 | } |
125 | |
126 | short set_utc_param(Str255 url) { |
127 | //TODO |
128 | return -1; |
129 | } |