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

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

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


Revision 1.5 - (show annotations) (download)
Tue Feb 26 17:05:00 2008 UTC (16 years, 1 month ago) by jmc
Branch: MAIN
CVS Tags: checkpoint64y, checkpoint64x, checkpoint64z, checkpoint64q, checkpoint64p, checkpoint64s, checkpoint64r, checkpoint64u, checkpoint64t, checkpoint64w, checkpoint64v, checkpoint64i, checkpoint64h, checkpoint64k, checkpoint64j, checkpoint64m, checkpoint64l, checkpoint64o, checkpoint64n, checkpoint64a, checkpoint64c, checkpoint64b, checkpoint64e, checkpoint64d, checkpoint64g, checkpoint64f, checkpoint63p, checkpoint63q, checkpoint63r, checkpoint63s, checkpoint63l, checkpoint63m, checkpoint63n, checkpoint63o, checkpoint63h, checkpoint63i, checkpoint63j, checkpoint63k, checkpoint63d, checkpoint63e, checkpoint63f, checkpoint63g, checkpoint63a, checkpoint63b, checkpoint63c, checkpoint64, checkpoint65, checkpoint60, checkpoint61, checkpoint62, checkpoint63, checkpoint66g, checkpoint66f, checkpoint66e, checkpoint66d, checkpoint66c, checkpoint66b, checkpoint66a, checkpoint66o, checkpoint66n, checkpoint66m, checkpoint66l, checkpoint66k, checkpoint66j, checkpoint66i, checkpoint66h, checkpoint65z, checkpoint65x, checkpoint65y, checkpoint65r, checkpoint65s, checkpoint65p, checkpoint65q, checkpoint65v, checkpoint65w, checkpoint65t, checkpoint65u, checkpoint65j, checkpoint65k, checkpoint65h, checkpoint65i, checkpoint65n, checkpoint65o, checkpoint65l, checkpoint65m, checkpoint65b, checkpoint65c, checkpoint65a, checkpoint65f, checkpoint65g, checkpoint65d, checkpoint65e, checkpoint59q, checkpoint59p, checkpoint59r, checkpoint59o, checkpoint62c, checkpoint62b, checkpoint62a, checkpoint62g, checkpoint62f, checkpoint62e, checkpoint62d, checkpoint62k, checkpoint62j, checkpoint62i, checkpoint62h, checkpoint62o, checkpoint62n, checkpoint62m, checkpoint62l, checkpoint62s, checkpoint62r, checkpoint62q, checkpoint62p, checkpoint62w, checkpoint62v, checkpoint62u, checkpoint62t, checkpoint62z, checkpoint62y, checkpoint62x, checkpoint61f, checkpoint61g, checkpoint61d, checkpoint61e, checkpoint61b, checkpoint61c, checkpoint61a, checkpoint61n, checkpoint61o, checkpoint61l, checkpoint61m, checkpoint61j, checkpoint61k, checkpoint61h, checkpoint61i, checkpoint61v, checkpoint61w, checkpoint61t, checkpoint61u, checkpoint61r, checkpoint61s, checkpoint61p, checkpoint61q, checkpoint61z, checkpoint61x, checkpoint61y, HEAD
Changes since 1.4: +22 -9 lines
File MIME type: text/plain
put again new version (1.6-4, see MITgcm_contrib/mpack_src) of mpack sources.
among others:
 - fix MD5 code on some 64 bit platforms.
 - fix lot of compilation warnings.

1 /* (C) Copyright 1993,1994 by Carnegie Mellon University
2 * All Rights Reserved.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of Carnegie
9 * Mellon University not be used in advertising or publicity
10 * pertaining to distribution of the software without specific,
11 * written prior permission. Carnegie Mellon University makes no
12 * representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
14 * warranty.
15 *
16 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
17 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
19 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
21 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
22 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
33 #include <time.h>
34 #include <netdb.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include "xmalloc.h"
38 #include "common.h"
39 #include "part.h"
40
41 extern void warn(char *s);
42
43 #ifndef MAXHOSTNAMELEN
44 #define MAXHOSTNAMELEN 64
45 #endif
46
47 #ifndef errno
48 extern int errno;
49 #endif
50
51 int overwrite_files = 0;
52 int didchat;
53
54 /* The name of the file we're writing */
55 static char *output_fname = 0;
56
57 /* Characters that shouldn't be in filenames */
58 #define BADCHARS "!$&*()|\'\";<>[]{}?/`\\ \t"
59
60 /* Generate a message-id */
61 char *os_genid(void)
62 {
63 static int pid = 0;
64 static time_t curtime;
65 static char hostname[MAXHOSTNAMELEN+1];
66 char *result;
67 struct hostent *hp;
68
69
70 if (pid == 0) {
71 pid = getpid();
72 time(&curtime);
73 gethostname(hostname, sizeof(hostname));
74
75 /* If we don't have a FQDN, try canonicalizing with gethostbyname */
76 if (!strchr(hostname, '.')) {
77 hp = gethostbyname(hostname);
78 if (hp) {
79 strcpy(hostname, hp->h_name);
80 }
81 }
82 }
83
84 result = malloc(25+strlen(hostname));
85 sprintf(result, "%d.%lu@%s", pid, (unsigned long) curtime++, hostname);
86 return result;
87 }
88
89 /* Create and return directory for a message-id */
90 char *os_idtodir(char *id)
91 {
92 static char buf[4096];
93 char *p;
94
95 if (getenv("TMPDIR")) {
96 strcpy(buf, getenv("TMPDIR"));
97 }
98 else {
99 strcpy(buf, "/var/tmp");
100 }
101 strcat(buf, "/m-prts-");
102 p = getenv("USER");
103 if (!p) p = getenv("LOGNAME");
104 if (!p) p = "x";
105 strcat(buf, p);
106
107 (void)mkdir(buf, 0700);
108
109 p = buf + strlen(buf);
110 *p++ = '/';
111 while (*id && p < buf+sizeof(buf)-10 ) {
112 if (isprint(*id) && !strchr(BADCHARS, *id)) *p++ = *id;
113 id++;
114 }
115 *p = '\0';
116 if (mkdir(buf, 0700) == -1 && errno != EEXIST) {
117 perror(buf);
118 return 0;
119 }
120 *p++ = '/';
121 *p = '\0';
122 return buf;
123 }
124
125 /*
126 * We are done with the directory returned by os_idtodir()
127 * Remove it
128 */
129 void os_donewithdir(char *dir)
130 {
131 char *p;
132
133 /* Remove trailing slash */
134 p = dir + strlen(dir) - 1;
135 *p = '\0';
136
137 rmdir(dir);
138 }
139
140 FILE *os_createnewfile(char *fname)
141 {
142 int fd;
143 FILE *ret;
144
145 #ifdef O_EXCL
146 struct stat statbuf;
147
148 if ((stat(fname, &statbuf) == 0) && (S_ISCHR(statbuf.st_mode))) {
149 fd=open(fname, O_RDWR);
150 }
151 else {
152 fd=open(fname, O_RDWR|O_CREAT|O_EXCL, 0644);
153 }
154 #else
155 fd=open(fname, O_RDWR|O_CREAT|O_TRUNC, 0644);
156 #endif
157
158 if (fd == -1)
159 return NULL;
160
161 ret=fdopen(fd, "w");
162 return ret;
163 }
164
165
166 /*
167 * Create a new file, with suggested filename "fname".
168 * "fname" may have come from an insecure source, so clean it up first.
169 * It may also be null.
170 * "contentType" is passed in for use by systems that have typed filesystems.
171 * "flags" contains a bit pattern describing attributes of the new file.
172 */
173 FILE *os_newtypedfile(char *fname, char *contentType, int flags, params contentParams)
174 {
175 char *p;
176 static int filesuffix=0;
177 char buf[128], *descfname=0;
178 FILE *outfile = 0;
179
180 if (!fname) fname = "";
181
182 /* If absolute path name, chop to tail */
183 if (*fname == '/') {
184 p = strrchr(fname, '/');
185 fname = p+1;
186 }
187
188 /* Get rid of leading ~ or ~/ */
189 while (*fname == '~' || *fname == '/') fname++;
190
191 while (!strncmp(fname, "../", 3)) fname += 3;
192
193 /* Clean out bad characters, create directories along path */
194 for (p=fname; *p; p++) {
195 if (*p == '/') {
196 if (!strncmp(p, "/../", 4)) {
197 p[1] = p[2] = 'X';
198 }
199 *p = '\0';
200 (void) mkdir(fname, 0777);
201 *p = '/';
202 }
203 else if (!isprint(*p) || strchr(BADCHARS, *p)) *p = 'X';
204 }
205
206 if (!fname[0]) {
207 do {
208 if (outfile) fclose(outfile);
209 sprintf(buf, "part%d", ++filesuffix);
210 } while ((outfile = fopen(buf, "r")));
211 fname = buf;
212 }
213 else if (!overwrite_files && (outfile = fopen(fname, "r"))) {
214 do {
215 fclose(outfile);
216 sprintf(buf, "%s.%d", fname, ++filesuffix);
217
218 } while ((outfile = fopen(buf, "r")));
219 fname = buf;
220 }
221
222 if (overwrite_files)
223 outfile = fopen(fname, "w");
224 else
225 outfile = os_createnewfile(fname);
226
227 if (!outfile) {
228 perror(fname);
229 }
230
231 if (output_fname) free(output_fname);
232 output_fname = strsave(fname);
233
234 if (strlen(fname) > sizeof(buf)-6) {
235 descfname = xmalloc(strlen(fname)+6);
236 }
237 else {
238 descfname = buf;
239 }
240 strcpy(descfname, fname);
241
242 p = strchr(descfname, '/');
243 if (!p) p = descfname;
244 if ((p = strrchr(p, '.'))) *p = '\0';
245
246 strcat(descfname, ".desc");
247 (void) rename(TEMPFILENAME, descfname);
248 if (descfname != buf) free(descfname);
249
250 fprintf(stdout, "%s (%s)\n", output_fname, contentType);
251 didchat = 1;
252
253 return outfile;
254 }
255
256 /*
257 * Close a file opened by os_newTypedFile()
258 */
259 void os_closetypedfile(FILE *outfile)
260 {
261 fclose(outfile);
262 }
263
264 /*
265 * (Don't) Handle a BinHex'ed file
266 */
267 int
268 os_binhex(struct part *inpart, int part, int nparts)
269 {
270 return 1;
271 }
272
273 /*
274 * Warn user that the MD5 digest of the last file created by os_newtypedfile()
275 * did not match that supplied in the Content-MD5: header.
276 */
277 void os_warnMD5mismatch(void)
278 {
279 char *warning;
280
281 warning = xmalloc(strlen(output_fname) + 100);
282 sprintf(warning, "%s was corrupted in transit",
283 output_fname);
284 warn(warning);
285 free(warning);
286 }
287
288 /*
289 * Report an error (in errno) concerning a filename
290 */
291 void os_perror(char *file)
292 {
293 perror(file);
294 }

  ViewVC Help
Powered by ViewVC 1.1.22