Fix `puttygen-unix-perms': f_open(), PuTTY's wrapper on fopen, now
[u/mdw/putty] / mac / macmisc.c
1 /* $Id$ */
2 /*
3 * Copyright (c) 1999, 2003 Ben Harris
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include <MacTypes.h>
29 #include <Dialogs.h>
30 #include <Files.h>
31 #include <MacWindows.h>
32 #include <Processes.h>
33 #include <Quickdraw.h>
34 #include <TextUtils.h>
35
36 #include <stdarg.h>
37 #include <stdio.h>
38
39 #include "putty.h"
40 #include "mac.h"
41 #include "ssh.h"
42
43 #if TARGET_API_MAC_CARBON
44 /*
45 * This is used by (I think) CarbonStdCLib, but only exists in
46 * CarbonLib 1.1 and later. Muppets. Happily, it's documented to be
47 * a synonym for NULL.
48 */
49 #include <CFBase.h>
50 const CFAllocatorRef kCFAllocatorDefault = NULL;
51 #else
52 QDGlobals qd;
53 #endif
54
55 /*
56 * Like FrontWindow(), but return NULL if we aren't the front process
57 * (i.e. the front window isn't one of ours).
58 */
59 WindowPtr mac_frontwindow(void)
60 {
61 ProcessSerialNumber frontpsn;
62 ProcessSerialNumber curpsn = { 0, kCurrentProcess };
63 Boolean result;
64
65 GetFrontProcess(&frontpsn);
66 if (SameProcess(&frontpsn, &curpsn, &result) == noErr && result)
67 return FrontWindow();
68 return NULL;
69 }
70
71 void fatalbox(char *fmt, ...) {
72 va_list ap;
73 Str255 stuff;
74
75 va_start(ap, fmt);
76 /* We'd like stuff to be a Pascal string */
77 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
78 va_end(ap);
79 ParamText(stuff, NULL, NULL, NULL);
80 StopAlert(128, NULL);
81 cleanup_exit(1);
82 }
83
84 void modalfatalbox(char *fmt, ...) {
85 va_list ap;
86 Str255 stuff;
87
88 va_start(ap, fmt);
89 /* We'd like stuff to be a Pascal string */
90 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
91 va_end(ap);
92 ParamText(stuff, NULL, NULL, NULL);
93 StopAlert(128, NULL);
94 cleanup_exit(1);
95 }
96
97 Filename filename_from_str(const char *str)
98 {
99 Filename ret;
100 Str255 tmp;
101
102 /* XXX This fails for filenames over 255 characters long. */
103 c2pstrcpy(tmp, str);
104 FSMakeFSSpec(0, 0, tmp, &ret.fss);
105 return ret;
106 }
107
108 /*
109 * Convert a filename to a string for display purposes.
110 * See pp 2-44--2-46 of IM:Files
111 *
112 * XXX static storage considered harmful
113 */
114 const char *filename_to_str(const Filename *fn)
115 {
116 CInfoPBRec pb;
117 Str255 dirname;
118 OSErr err;
119 static char *path = NULL;
120 char *newpath;
121
122 if (path != NULL) sfree(path);
123 path = snewn(fn->fss.name[0], char);
124 p2cstrcpy(path, fn->fss.name);
125 pb.dirInfo.ioNamePtr = dirname;
126 pb.dirInfo.ioVRefNum = fn->fss.vRefNum;
127 pb.dirInfo.ioDrParID = fn->fss.parID;
128 pb.dirInfo.ioFDirIndex = -1;
129 do {
130 pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
131 err = PBGetCatInfoSync(&pb);
132
133 /* XXX Assume not A/UX */
134 newpath = snewn(strlen(path) + dirname[0] + 2, char);
135 p2cstrcpy(newpath, dirname);
136 strcat(newpath, ":");
137 strcat(newpath, path);
138 sfree(path);
139 path = newpath;
140 } while (pb.dirInfo.ioDrDirID != fsRtDirID);
141 return path;
142 }
143
144 int filename_equal(Filename f1, Filename f2)
145 {
146
147 return f1.fss.vRefNum == f2.fss.vRefNum &&
148 f1.fss.parID == f2.fss.parID &&
149 f1.fss.name[0] == f2.fss.name[0] &&
150 memcmp(f1.fss.name + 1, f2.fss.name + 1, f1.fss.name[0]) == 0;
151 }
152
153 int filename_is_null(Filename fn)
154 {
155
156 return fn.fss.vRefNum == 0 && fn.fss.parID == 0 && fn.fss.name[0] == 0;
157 }
158
159 FILE *f_open(Filename fn, char const *mode, int is_private)
160 {
161 short savevol;
162 long savedir;
163 char tmp[256];
164 FILE *ret;
165
166 HGetVol(NULL, &savevol, &savedir);
167 if (HSetVol(NULL, fn.fss.vRefNum, fn.fss.parID) == noErr) {
168 p2cstrcpy(tmp, fn.fss.name);
169 ret = fopen(tmp, mode);
170 } else
171 ret = NULL;
172 HSetVol(NULL, savevol, savedir);
173 return ret;
174 }
175
176 struct tm ltime(void)
177 {
178 struct tm tm;
179 DateTimeRec d;
180 GetTime(&d);
181
182 tm.tm_sec=d.second;
183 tm.tm_min=d.minute;
184 tm.tm_hour=d.hour;
185 tm.tm_mday=d.day;
186 tm.tm_mon=d.month-1;
187 tm.tm_year=d.year-1900;
188 tm.tm_wday=d.dayOfWeek;
189 tm.tm_yday=1; /* GetTime doesn't tell us */
190 tm.tm_isdst=0; /* Have to do DST ourselves */
191
192 /* XXX find out DST adjustment and add it */
193
194 return tm;
195 }
196
197 const char platform_x11_best_transport[] = "localhost";
198
199 char *platform_get_x_display(void) {
200 return NULL;
201 }
202
203 /*
204 * Local Variables:
205 * c-file-style: "simon"
206 * End:
207 */