/[MITgcm]/MITgcm/tools/mpack-1.6/macos.c
ViewVC logotype

Contents of /MITgcm/tools/mpack-1.6/macos.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Tue Aug 26 20:45:25 2003 UTC (20 years, 7 months ago) by edhill
Branch: MAIN
CVS Tags: checkpoint64y, checkpoint64x, checkpoint58l_post, checkpoint64z, checkpoint51k_post, checkpoint57t_post, checkpoint64q, checkpoint64p, checkpoint64s, checkpoint64r, checkpoint64u, checkpoint64t, checkpoint64w, checkpoint64v, checkpoint64i, checkpoint64h, checkpoint64k, checkpoint64j, checkpoint64m, checkpoint64l, checkpoint64o, checkpoint64n, checkpoint64a, checkpoint57o_post, checkpoint64c, checkpoint64b, checkpoint64e, checkpoint64d, checkpoint64g, checkpoint64f, checkpoint52l_pre, checkpoint52e_pre, hrcube4, checkpoint58e_post, mitgcm_mapl_00, checkpoint52n_post, checkpoint52j_post, checkpoint53d_post, checkpoint58u_post, checkpoint58w_post, checkpoint54a_pre, checkpoint51o_pre, checkpoint57m_post, checkpoint55c_post, checkpoint54e_post, checkpoint52e_post, checkpoint57s_post, checkpoint51n_pre, checkpoint54a_post, checkpoint63p, checkpoint63q, checkpoint63r, checkpoint63s, checkpoint63l, checkpoint63m, checkpoint63n, checkpoint63o, checkpoint63h, checkpoint63i, checkpoint63j, checkpoint63k, checkpoint63d, checkpoint63e, checkpoint63f, checkpoint63g, checkpoint53c_post, checkpoint63a, checkpoint63b, checkpoint63c, checkpoint57k_post, checkpoint55d_pre, checkpoint57d_post, checkpoint51l_post, checkpoint57g_post, checkpoint51q_post, checkpoint64, checkpoint65, checkpoint60, checkpoint61, checkpoint62, checkpoint63, checkpoint57b_post, checkpoint57c_pre, checkpoint51j_post, checkpoint58r_post, checkpoint55j_post, checkpoint56b_post, checkpoint57i_post, checkpoint57y_post, hrcube_1, checkpoint57e_post, checkpoint66g, checkpoint66f, checkpoint66e, checkpoint66d, checkpoint66c, checkpoint66b, checkpoint66a, checkpoint66o, checkpoint66n, checkpoint66m, checkpoint66l, checkpoint66k, checkpoint66j, checkpoint66i, checkpoint66h, branch-netcdf, checkpoint52d_pre, checkpoint52l_post, checkpoint55h_post, checkpoint58n_post, checkpoint53b_post, checkpoint58x_post, checkpoint52k_post, checkpoint52b_pre, checkpoint57g_pre, checkpoint54b_post, checkpoint53b_pre, checkpoint55b_post, checkpoint58t_post, checkpoint58h_post, checkpoint65z, checkpoint65x, checkpoint65y, checkpoint54d_post, checkpoint65r, checkpoint65s, checkpoint65p, checkpoint65q, checkpoint65v, checkpoint65w, checkpoint65t, checkpoint65u, checkpoint65j, checkpoint65k, checkpoint65h, checkpoint65i, checkpoint65n, checkpoint65o, checkpoint65l, checkpoint65m, checkpoint65b, checkpoint65c, checkpoint65a, checkpoint65f, checkpoint65g, checkpoint65d, checkpoint65e, checkpoint56c_post, checkpoint52m_post, checkpoint57y_pre, checkpoint55, checkpoint53a_post, checkpoint57f_pre, checkpoint57a_post, checkpoint54, checkpoint58q_post, checkpoint54f_post, checkpoint57v_post, checkpoint59q, checkpoint59p, checkpoint55g_post, checkpoint59r, checkpoint51o_post, checkpoint51p_post, checkpoint58j_post, checkpoint52a_pre, checkpoint59e, checkpoint59d, checkpoint59g, checkpoint59f, checkpoint59a, checkpoint55f_post, checkpoint59c, checkpoint59b, checkpoint59m, checkpoint59l, checkpoint59o, checkpoint59n, checkpoint59i, checkpoint59h, checkpoint59k, checkpoint59j, checkpoint57r_post, checkpoint59, checkpoint58, checkpoint57a_pre, checkpoint55i_post, checkpoint57, checkpoint56, checkpoint51i_post, checkpoint53, checkpoint52, checkpoint51f_post, checkpoint52d_post, eckpoint57e_pre, checkpoint51r_post, checkpoint52a_post, checkpoint57h_done, checkpoint58f_post, checkpoint52b_post, checkpoint53g_post, checkpoint52f_post, branchpoint-genmake2, checkpoint57x_post, checkpoint57n_post, checkpoint52c_post, checkpoint58d_post, checkpoint58c_post, checkpoint57w_post, checkpoint57p_post, checkpint57u_post, checkpoint57f_post, checkpoint58a_post, checkpoint51h_pre, checkpoint51l_pre, checkpoint58i_post, checkpoint57q_post, checkpoint51g_post, checkpoint58g_post, ecco_c52_e35, hrcube5, checkpoint58o_post, checkpoint57z_post, checkpoint62c, checkpoint62b, checkpoint62a, checkpoint62g, checkpoint62f, checkpoint62e, checkpoint62d, checkpoint62k, checkpoint62j, checkpoint62i, checkpoint62h, checkpoint62o, checkpoint62n, checkpoint62m, checkpoint62l, checkpoint62s, checkpoint62r, checkpoint62q, checkpoint62p, checkpoint62w, checkpoint62v, checkpoint62u, checkpoint62t, checkpoint57c_post, checkpoint62z, checkpoint62y, checkpoint62x, checkpoint58y_post, checkpoint55e_post, checkpoint58k_post, checkpoint52i_post, checkpoint52j_pre, checkpoint58v_post, checkpoint53f_post, checkpoint55a_post, checkpoint51t_post, checkpoint53d_pre, checkpoint54c_post, checkpoint58s_post, checkpoint61f, checkpoint61g, checkpoint61d, checkpoint61e, checkpoint61b, checkpoint61c, checkpoint58p_post, checkpoint61a, checkpoint61n, checkpoint61o, checkpoint61l, checkpoint61m, checkpoint61j, checkpoint61k, checkpoint61h, checkpoint61i, checkpoint61v, checkpoint61w, checkpoint61t, checkpoint61u, checkpoint61r, checkpoint61s, checkpoint61p, checkpoint61q, checkpoint51n_post, checkpoint57j_post, checkpoint61z, checkpoint61x, checkpoint61y, checkpoint58b_post, checkpoint57h_pre, checkpoint51i_pre, checkpoint58m_post, checkpoint57l_post, checkpoint52i_pre, checkpoint51u_post, checkpoint52h_pre, checkpoint52f_pre, checkpoint57h_post, hrcube_2, hrcube_3, checkpoint56a_post, checkpoint51m_post, checkpoint51s_post, checkpoint55d_post, HEAD
Branch point for: branch-nonh, branch-genmake2, tg2-branch, checkpoint51n_branch, netcdf-sm0
File MIME type: text/plain
Initial check-in of the CMU "mpack" utility.  This allows us to (portably)
send MITgcm output as MIME-encoded email messages and will be used by the
"testreport" script.  The CMU license is basically an "AS-IS" statement.

1 /* macos.c -- operating system dependant mpack code for the Macintosh
2 */
3 /* (C) Copyright 1993-1995 by Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear in
10 * supporting documentation, and that the name of Carnegie Mellon University
11 * not be used in advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission. Carnegie
13 * Mellon University makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is" without
15 * express or implied warranty.
16 *
17 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
18 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
20 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 * SOFTWARE.
25 */
26
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <errno.h>
31 #include "common.h"
32 #include "macnapp.h"
33 #include "macmpack.h"
34
35 extern char *malloc(), *realloc();
36
37 char copy_buf[COPY_BUFSIZE];
38
39 char *xmalloc(unsigned size)
40 {
41 char *ret;
42
43 if (ret = malloc((unsigned) size))
44 return ret;
45
46 yell("Not enough memory available.");
47 maccleanup();
48 exit(1);
49 }
50
51 char *xrealloc (char *ptr, unsigned size)
52 {
53 char *ret;
54
55 /* xrealloc (NULL, size) behaves like xmalloc (size), as in ANSI C */
56 if (ret = !ptr ? malloc ((unsigned) size) : realloc (ptr, (unsigned) size))
57 return ret;
58
59 yell("Not enough memory available");
60 maccleanup();
61 exit(1);
62 }
63
64 char *strsave(char *str)
65 {
66 char *p = xmalloc(strlen(str)+1);
67 strcpy(p, str);
68 return p;
69 }
70
71 /* save output filename buffer */
72 static char *output_fname = NULL;
73 static short applefile_flag = 0;
74 static FSSpec fspec;
75
76 /* rename or copy a file to a new location
77 */
78 static void rename_or_copy(srcfname, dstfname, dstvol, dirid)
79 char *srcfname;
80 PCstr *dstfname;
81 short dstvol;
82 long dirid;
83 {
84 PCstr tstr[257];
85 short srefnum, drefnum;
86 long count;
87 WDPBRec wdpb;
88 HParamBlockRec hpb;
89 CMovePBRec cpb;
90
91 wdpb.ioNamePtr = P(tstr);
92 if (PBHGetVol(&wdpb, FALSE) != noErr) return;
93 CtoPCstrcpy(tstr, srcfname);
94 if (HOpen(0, 0, P(tstr), fsRdPerm, &srefnum) != noErr) return;
95 if (GetEOF(srefnum, &count) == noErr && !count) {
96 FSClose(srefnum);
97 return;
98 }
99 if (wdpb.ioWDVRefNum == dstvol) {
100 /* files on same drive -- do a rename/move */
101 FSClose(srefnum);
102 hpb.fileParam.ioNamePtr = P(tstr);
103 hpb.fileParam.ioVRefNum = 0;
104 hpb.ioParam.ioMisc = (Ptr) P(dstfname);
105 hpb.fileParam.ioDirID = 0;
106 if (PBHRenameSync(&hpb) == noErr && wdpb.ioWDDirID != dirid) {
107 cpb.ioNamePtr = P(dstfname);
108 cpb.ioVRefNum = 0;
109 cpb.ioNewName = NULL;
110 cpb.ioNewDirID = dirid;
111 cpb.ioDirID = 0;
112 PBCatMoveSync(&cpb);
113 }
114 } else {
115 /* files on different drive -- do a copy */
116 if (HCreate(dstvol, dirid, P(dstfname), _fcreator, _ftype) != noErr) {
117 FSClose(srefnum);
118 return;
119 }
120 if (HOpen(dstvol, dirid, P(dstfname), fsWrPerm, &drefnum) != noErr) {
121 FSClose(srefnum);
122 HDelete(dstvol, dirid, P(dstfname));
123 return;
124 }
125 count = sizeof (copy_buf);
126 while (FSRead(srefnum, &count, (Ptr) copy_buf) != noErr && count > 0) {
127 FSWrite(drefnum, &count, (Ptr) copy_buf);
128 count = sizeof (copy_buf);
129 }
130 FSClose(srefnum);
131 FSClose(drefnum);
132 }
133 }
134
135 /* Generate a message-id */
136 char *os_genid()
137 {
138 char *result;
139 long tick;
140 unsigned long time;
141
142 tick = TickCount();
143 result = malloc(64);
144 GetDateTime(&time);
145 HLock((Handle) mpack_prefs);
146 sprintf(result, "%lu.%lu@%s", tick, time, (*mpack_prefs)->internet_host);
147 HUnlock((Handle) mpack_prefs);
148
149 /* make sure tick count is bumped before we return... */
150 while (tick == TickCount());
151
152 return (result);
153 }
154
155 /* Create and return directory for a message-id
156 */
157 char *os_idtodir(id)
158 char *id;
159 {
160 static PCstr buf[257];
161 PCstr idbuf[257];
162 char *fname;
163 short uqid;
164 OSErr err;
165 long dirID;
166 Handle h;
167 ResType type = 'IDna';
168
169 /* prepare filename */
170 PtoPCstrcpy(buf, (char *) P(pfolder->prefs));
171 fname = C(buf) + PCstrlen(buf);
172
173 /* is there a mapping for the id? */
174 CtoPCstrcpy(idbuf, id);
175 h = GetNamedResource(type, P(idbuf));
176
177 /* no mapping -- create one */
178 if (!h) {
179 while ((uqid = UniqueID(type)) < 128);
180 h = NewHandle(sizeof (short));
181 if (h) (**(short **)h) = uqid;
182 AddResource(h, type, uqid, P(idbuf));
183 if ((err = ResError()) != noErr) {
184 return (NULL);
185 }
186 } else {
187 uqid = ** (short **) h;
188 }
189
190 /* set directory name & create it */
191 sprintf(fname, "%d:", uqid);
192 SetPlen(buf);
193 err = DirCreate(pfolder->fspec.vRefNum, 0, P(buf), &dirID);
194 if (err != noErr && err != dupFNErr) {
195 RmveResource(h);
196 DisposHandle(h);
197 h = NULL;
198 }
199
200 return (h ? C(buf) : NULL);
201 }
202
203 /*
204 * We are done with the directory returned by os_idtodir()
205 * Remove it
206 */
207 os_donewithdir(dir)
208 char *dir;
209 {
210 PCstr buf[257];
211 short uqid;
212 char *fname;
213 Handle h;
214
215 CtoPCstrcpy(buf, dir);
216 HDelete(0, 0, P(buf));
217 fname = strrchr(C(buf), ':');
218 while (fname > C(buf) && *--fname != ':');
219 if (fname > C(buf)) {
220 uqid = atoi(fname + 1);
221 h = GetResource('IDna', uqid);
222 if (h) {
223 RmveResource(h);
224 DisposHandle(h);
225 }
226 }
227 }
228
229 /* rename the description file
230 */
231 void renameDescFile(char *fname, short vRefNum, long dirid)
232 {
233 PCstr tstr[65];
234 char *p;
235
236 MapTypeCreator("text/plain", 0);
237
238 /* save description file */
239 CtoPCstrcpy(tstr, fname);
240 if (p = strrchr(C(tstr), '.')) *p = '\0';
241 strcat(C(tstr), ".desc");
242 SetPlen(tstr);
243 rename_or_copy(TEMPFILENAME, tstr, vRefNum, dirid);
244 (void) remove(TEMPFILENAME);
245 }
246
247 FILE *os_createnewfile(fname)
248 char *fname;
249 {
250 return fopen(fname, "w");
251 }
252
253 /*
254 * Create a new file, with suggested filename "fname".
255 * "fname" may have come from an insecure source, so clean it up first.
256 * It may also be null.
257 * "contentType" is passed in for use by systems that have typed filesystems.
258 * "flags" contains a bit pattern describing attributes of the new file.
259 */
260 FILE *os_newtypedfile(fname, contentType, flags, contentParams)
261 char *fname;
262 char *contentType;
263 int flags;
264 params *contentParams;
265 {
266 char *p;
267 int applefile;
268 FILE *outfile = 0, *tmpf;
269 StandardFileReply reply;
270 PCstr tstr[257];
271
272 if (!fname) fname = "";
273
274 /* Translate ':' to underscore */
275 for (p=fname; *p; p++) {
276 if (*p == ':' || !isprint(*p)) *p = '_';
277 }
278
279 /* chop filename to length */
280 if (strlen(fname) > 31) {
281 fname += strlen(fname) - 31;
282 }
283
284 /* remove leading periods, to protect from ".SONY" attacks */
285 while (*fname == '.') ++fname;
286
287 /* get filename from user */
288 applefile = !strcmp(contentType, "application/applefile");
289 if (!applefile || !(flags & FILE_INAPPLEDOUBLE)) {
290 sprintf(C(tstr), "Found file: %s (%s)",
291 fname[0] ? fname : "Untitled", contentType);
292 SetPlen(tstr);
293 stattext(P(tstr), 1);
294 }
295 if (!applefile && (!applefile_flag || !(flags & FILE_INAPPLEDOUBLE))) {
296 SetCursor(&arrow);
297 CtoPstr(fname);
298 NAputFile("\pSave decoded file as:",
299 fname[0] ? (unsigned char *) fname : "\pUntitled", &reply);
300 PtoCstr((unsigned char *) fname);
301 SetCursor(&watch);
302 statrefresh();
303 if (!reply.sfGood) {
304 didchat = -1;
305 return (NULL);
306 }
307 }
308
309 /* set the type */
310 MapTypeCreator(contentType, 0);
311
312 /* save file */
313 tmpf = tmpfile();
314 if (applefile) {
315 outfile = tmpf;
316 tmpf = NULL;
317 } else if ((flags & FILE_INAPPLEDOUBLE) && applefile_flag) {
318 outfile = Macopen(tmpf, fspec.name, fspec.vRefNum, fspec.parID,
319 flags & FILE_BINARY, 0, fsWrPerm);
320 } else {
321 HCreate(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, _fcreator, _ftype);
322 outfile = Macopen(tmpf, reply.sfFile.name, reply.sfFile.vRefNum, reply.sfFile.parID,
323 flags & FILE_BINARY, 0, fsWrPerm);
324 }
325 applefile_flag = applefile;
326 if (tmpf) fclose(tmpf);
327 PtoCstr(reply.sfFile.name);
328 fname = (char *) reply.sfFile.name;
329 if (!outfile) {
330 sprintf(C(tstr), "Couldn't open file %s", fname);
331 warn(C(tstr));
332 return (0);
333 }
334
335 if (output_fname) free(output_fname);
336 output_fname = strsave(fname);
337
338 renameDescFile(fname, reply.sfFile.vRefNum, reply.sfFile.parID);
339
340 return outfile;
341 }
342
343 /*
344 * Close a file opened by os_newTypedFile()
345 */
346 os_closetypedfile(outfile)
347 FILE *outfile;
348 {
349 char buf[128];
350
351 if (applefile_flag) {
352 rewind(outfile);
353 if (decode_applefile(outfile, &fspec) < 0) {
354 sprintf(buf, "Failed to decode file %s", output_fname);
355 if (didchat >= 0) warn(buf);
356 applefile_flag = 0;
357 }
358 }
359
360 /* close file */
361 fclose(outfile);
362 }
363
364 /*
365 * Warn user that the MD5 digest of the last file created by os_newtypedfile()
366 * did not match that supplied in the Content-MD5: header.
367 */
368 os_warnMD5mismatch()
369 {
370 char *warning;
371
372 warning = xmalloc(strlen(output_fname) + 100);
373 sprintf(warning, "%s was corrupted in transit",
374 output_fname);
375 warn(warning);
376 free(warning);
377 }
378
379 /* bring up an error dialog for a file error
380 */
381 void os_perror(char *str)
382 {
383 extern int errno;
384 char *err = strerror(errno), *scan;
385 char msg[256];
386 short maxflen;
387
388 maxflen = 255 - (strlen(err) + 2);
389 if (strlen(str) > maxflen) {
390 str += strlen(str) - maxflen;
391 for (scan = str; *scan && *scan++ != ':';);
392 if (*scan) str = scan;
393 }
394 sprintf(msg, "%s: %s", str, err);
395 yell(msg);
396 }

  ViewVC Help
Powered by ViewVC 1.1.22