34773273 |
1 | /* $Id: macmisc.c,v 1.3 2003/03/29 23:07:55 ben Exp $ */ |
06c24bc0 |
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> |
3fa61577 |
31 | #include <MacWindows.h> |
32 | #include <Processes.h> |
06c24bc0 |
33 | #include <Quickdraw.h> |
34 | #include <TextUtils.h> |
35 | |
36 | #include <stdarg.h> |
37 | #include <stdio.h> |
38 | |
39 | #include "putty.h" |
3fa61577 |
40 | #include "mac.h" |
06c24bc0 |
41 | |
42 | #if TARGET_API_MAC_CARBON |
43 | /* |
44 | * This is used by (I think) CarbonStdCLib, but only exists in |
45 | * CarbonLib 1.1 and later. Muppets. Happily, it's documented to be |
46 | * a synonym for NULL. |
47 | */ |
48 | #include <CFBase.h> |
49 | const CFAllocatorRef kCFAllocatorDefault = NULL; |
50 | #else |
51 | QDGlobals qd; |
52 | #endif |
53 | |
3fa61577 |
54 | /* |
55 | * Like FrontWindow(), but return NULL if we aren't the front process |
56 | * (i.e. the front window isn't one of ours). |
57 | */ |
58 | WindowPtr mac_frontwindow(void) |
59 | { |
60 | ProcessSerialNumber frontpsn; |
61 | ProcessSerialNumber curpsn = { 0, kCurrentProcess }; |
62 | Boolean result; |
63 | |
64 | GetFrontProcess(&frontpsn); |
65 | if (SameProcess(&frontpsn, &curpsn, &result) == noErr && result) |
66 | return FrontWindow(); |
67 | return NULL; |
68 | } |
69 | |
06c24bc0 |
70 | void fatalbox(char *fmt, ...) { |
71 | va_list ap; |
72 | Str255 stuff; |
73 | |
74 | va_start(ap, fmt); |
75 | /* We'd like stuff to be a Pascal string */ |
76 | stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap); |
77 | va_end(ap); |
78 | ParamText(stuff, NULL, NULL, NULL); |
79 | StopAlert(128, NULL); |
80 | cleanup_exit(1); |
81 | } |
82 | |
83 | void modalfatalbox(char *fmt, ...) { |
84 | va_list ap; |
85 | Str255 stuff; |
86 | |
87 | va_start(ap, fmt); |
88 | /* We'd like stuff to be a Pascal string */ |
89 | stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap); |
90 | va_end(ap); |
91 | ParamText(stuff, NULL, NULL, NULL); |
92 | StopAlert(128, NULL); |
93 | cleanup_exit(1); |
94 | } |
95 | |
96 | Filename filename_from_str(const char *str) |
97 | { |
98 | Filename ret; |
99 | Str255 tmp; |
100 | |
101 | /* XXX This fails for filenames over 255 characters long. */ |
102 | c2pstrcpy(tmp, str); |
103 | FSMakeFSSpec(0, 0, tmp, &ret.fss); |
104 | return ret; |
105 | } |
106 | |
107 | /* |
108 | * Convert a filename to a string for display purposes. |
109 | * See pp 2-44--2-46 of IM:Files |
110 | * |
111 | * XXX static storage considered harmful |
112 | */ |
113 | const char *filename_to_str(const Filename *fn) |
114 | { |
115 | CInfoPBRec pb; |
116 | Str255 dirname; |
117 | OSErr err; |
118 | static char *path = NULL; |
119 | char *newpath; |
120 | |
121 | if (path != NULL) sfree(path); |
34773273 |
122 | path = snewn(fn->fss.name[0], char); |
06c24bc0 |
123 | p2cstrcpy(path, fn->fss.name); |
124 | pb.dirInfo.ioNamePtr = dirname; |
125 | pb.dirInfo.ioVRefNum = fn->fss.vRefNum; |
126 | pb.dirInfo.ioDrParID = fn->fss.parID; |
127 | pb.dirInfo.ioFDirIndex = -1; |
128 | do { |
129 | pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID; |
130 | err = PBGetCatInfoSync(&pb); |
131 | |
132 | /* XXX Assume not A/UX */ |
34773273 |
133 | newpath = snewn(strlen(path) + dirname[0] + 2, char); |
06c24bc0 |
134 | p2cstrcpy(newpath, dirname); |
135 | strcat(newpath, ":"); |
136 | strcat(newpath, path); |
137 | sfree(path); |
138 | path = newpath; |
139 | } while (pb.dirInfo.ioDrDirID != fsRtDirID); |
140 | return path; |
141 | } |
142 | |
143 | int filename_equal(Filename f1, Filename f2) |
144 | { |
145 | |
146 | return f1.fss.vRefNum == f2.fss.vRefNum && |
147 | f1.fss.parID == f2.fss.parID && |
148 | f1.fss.name[0] == f2.fss.name[0] && |
149 | memcmp(f1.fss.name + 1, f2.fss.name + 1, f1.fss.name[0]) == 0; |
150 | } |
151 | |
152 | int filename_is_null(Filename fn) |
153 | { |
154 | |
155 | return fn.fss.vRefNum == 0 && fn.fss.parID == 0 && fn.fss.name[0] == 0; |
156 | } |
157 | |
158 | FILE *f_open(Filename fn, char const *mode) |
159 | { |
160 | short savevol; |
161 | long savedir; |
162 | char tmp[256]; |
163 | FILE *ret; |
164 | |
165 | HGetVol(NULL, &savevol, &savedir); |
166 | if (HSetVol(NULL, fn.fss.vRefNum, fn.fss.parID) == noErr) { |
167 | p2cstrcpy(tmp, fn.fss.name); |
168 | ret = fopen(tmp, mode); |
169 | } else |
170 | ret = NULL; |
171 | HSetVol(NULL, savevol, savedir); |
172 | return ret; |
173 | } |
174 | |
175 | /* |
176 | * Local Variables: |
177 | * c-file-style: "simon" |
178 | * End: |
179 | */ |