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