Debian Codegraph @

utils/pmx/pmx-src/libf2c/open.c

raw
1 #ifdef _WIN32
2
3 /* On Windows, replace tmpfile() by _cairo_win_tmpfile() defined as follows,
4 * and presumably more secure than mktemp().
5 */
6
7 /* _cairo_win_tmpfile (void) - replace tmpfile() in Windows
8 * extracted from cairo-misc.c in cairo - a vector graphics library
9 * The functiion name is changed from
10 * _cairo_win32_tmpfile (void) to
11 * _cairo_win_tmpfile (void)
12 *
13 *
14 * Copyright 2002 University of Southern California
15 * Copyright 2005 Red Hat, Inc.
16 * Copyright 2007 Adrian Johnson
17 *
18 * This library is free software; you can redistribute it and/or
19 * modify it either under the terms of the GNU Lesser General Public
20 * License version 2.1 as published by the Free Software Foundation
21 * (the "LGPL") or, at your option, under the terms of the Mozilla
22 * Public License Version 1.1 (the "MPL"). If you do not alter this
23 * notice, a recipient may use your version of this file under either
24 * the MPL or the LGPL.
25 *
26 * You should have received a copy of the LGPL along with this library
27 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
29 * You should have received a copy of the MPL along with this library
30 * in the file COPYING-MPL-1.1
31 *
32 * The contents of this file are subject to the Mozilla Public License
33 * Version 1.1 (the "License"); you may not use this file except in
34 * compliance with the License. You may obtain a copy of the License at
35 * http://www.mozilla.org/MPL/
36 *
37 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
38 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
39 * the specific language governing rights and limitations.
40 *
41 * The Original Code is the cairo graphics library.
42 *
43 * The Initial Developer of the Original Code is University of Southern
44 * California.
45 *
46 * Contributor(s):
47 * Carl D. Worth <[email protected]>
48 * Adrian Johnson <[email protected]>
49 */
50
51 #include <stdio.h>
52 #define WIN32_LEAN_AND_MEAN
53 /* We require Windows 2000 features such as ETO_PDY */
54 #if !defined(WINVER) || (WINVER < 0x0500)
55 # define WINVER 0x0500
56 #endif
57 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
58 # define _WIN32_WINNT 0x0500
59 #endif
60
61 #include <windows.h>
62 #include <io.h>
63
64 /* tmpfile() replacement for Windows.
65 *
66 * On Windows tmpfile() creates the file in the root directory. This
67 * may fail due to unsufficient privileges. However, this isn't a
68 * problem on Windows CE so we don't use it there.
69 */
70 FILE *
71 _cairo_win_tmpfile (void)
72 {
73 DWORD path_len;
74 WCHAR path_name[MAX_PATH + 1];
75 WCHAR file_name[MAX_PATH + 1];
76 HANDLE handle;
77 int fd;
78 FILE *fp;
79
80 path_len = GetTempPathW (MAX_PATH, path_name);
81 if (path_len <= 0 || path_len >= MAX_PATH)
82 return NULL;
83
84 if (GetTempFileNameW (path_name, L"ps_", 0, file_name) == 0)
85 return NULL;
86
87 handle = CreateFileW (file_name,
88 GENERIC_READ | GENERIC_WRITE,
89 0,
90 NULL,
91 CREATE_ALWAYS,
92 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
93 NULL);
94 if (handle == INVALID_HANDLE_VALUE) {
95 DeleteFileW (file_name);
96 return NULL;
97 }
98
99 fd = _open_osfhandle((intptr_t) handle, 0);
100 if (fd < 0) {
101 CloseHandle (handle);
102 return NULL;
103 }
104
105 fp = _fdopen(fd, "w+b");
106 if (fp == NULL) {
107 _close(fd);
108 return NULL;
109 }
110
111 return fp;
112 }
113
114 #define tmpfile _cairo_win_tmpfile
115 #endif /* _WIN32 */
116
117 #include "f2c.h"
118 #include "fio.h"
119 #include "string.h"
120 #ifndef NON_POSIX_STDIO
121 #ifdef MSDOS
122 #include "io.h"
123 #else
124 #include "unistd.h" /* for access */
125 #endif
126 #endif
127
128 #ifdef KR_headers
129 extern char *malloc();
130 #ifdef NON_ANSI_STDIO
131 extern char *mktemp();
132 #endif
133 extern integer f_clos();
134 #else
135 #undef abs
136 #undef min
137 #undef max
138 #include "stdlib.h"
139 #ifdef __cplusplus
140 extern "C" {
141 #endif
142 extern int f__canseek(FILE*);
143 extern integer f_clos(cllist*);
144 #endif
145
146 #ifdef NON_ANSI_RW_MODES
147 char *f__r_mode[2] = {"r", "r"};
148 char *f__w_mode[4] = {"w", "w", "r+w", "r+w"};
149 #elif defined(WIN32)
150 char *f__r_mode[2] = {"rb", "rb"};
151 char *f__w_mode[4] = {"wb", "wb", "rb+", "rb+"};
152 #else
153 char *f__r_mode[2] = {"rb", "r"};
154 char *f__w_mode[4] = {"wb", "w", "r+b", "r+"};
155 #endif
156
157 static char f__buf0[400], *f__buf = f__buf0;
158 int f__buflen = (int)sizeof(f__buf0);
159
160 static void
161 #ifdef KR_headers
162 f__bufadj(n, c) int n, c;
163 #else
164 f__bufadj(int n, int c)
165 #endif
166 {
167 unsigned int len;
168 char *nbuf, *s, *t, *te;
169
170 if (f__buf == f__buf0)
171 f__buflen = 1024;
172 while(f__buflen <= n)
173 f__buflen <<= 1;
174 len = (unsigned int)f__buflen;
175 if (len != f__buflen || !(nbuf = (char*)malloc(len)))
176 f__fatal(113, "malloc failure");
177 s = nbuf;
178 t = f__buf;
179 te = t + c;
180 while(t < te)
181 *s++ = *t++;
182 if (f__buf != f__buf0)
183 free(f__buf);
184 f__buf = nbuf;
185 }
186
187 int
188 #ifdef KR_headers
189 f__putbuf(c) int c;
190 #else
191 f__putbuf(int c)
192 #endif
193 {
194 char *s, *se;
195 int n;
196
197 if (f__hiwater > f__recpos)
198 f__recpos = f__hiwater;
199 n = f__recpos + 1;
200 if (n >= f__buflen)
201 f__bufadj(n, f__recpos);
202 s = f__buf;
203 se = s + f__recpos;
204 if (c)
205 *se++ = c;
206 *se = 0;
207 for(;;) {
208 fputs(s, f__cf);
209 s += strlen(s);
210 if (s >= se)
211 break; /* normally happens the first time */
212 putc(*s++, f__cf);
213 }
214 return 0;
215 }
216
217 void
218 #ifdef KR_headers
219 x_putc(c)
220 #else
221 x_putc(int c)
222 #endif
223 {
224 if (f__recpos >= f__buflen)
225 f__bufadj(f__recpos, f__buflen);
226 f__buf[f__recpos++] = c;
227 }
228
229 #define opnerr(f,m,s) {if(f) errno= m; else opn_err(m,s,a); return(m);}
230
231 static void
232 #ifdef KR_headers
233 opn_err(m, s, a) int m; char *s; olist *a;
234 #else
235 opn_err(int m, char *s, olist *a)
236 #endif
237 {
238 if (a->ofnm) {
239 /* supply file name to error message */
240 if (a->ofnmlen >= f__buflen)
241 f__bufadj((int)a->ofnmlen, 0);
242 g_char(a->ofnm, a->ofnmlen, f__curunit->ufnm = f__buf);
243 }
244 f__fatal(m, s);
245 }
246
247 #ifdef KR_headers
248 integer f_open(a) olist *a;
249 #else
250 integer f_open(olist *a)
251 #endif
252 { unit *b;
253 integer rv;
254 char buf[256], *s;
255 cllist x;
256 int ufmt;
257 FILE *tf;
258 #ifndef NON_UNIX_STDIO
259 int n;
260 #endif
261 f__external = 1;
262 if(a->ounit>=MXUNIT || a->ounit<0)
263 err(a->oerr,101,"open")
264 if (!f__init)
265 f_init();
266 f__curunit = b = &f__units[a->ounit];
267 if(b->ufd) {
268 if(a->ofnm==0)
269 {
270 same: if (a->oblnk)
271 b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
272 return(0);
273 }
274 #ifdef NON_UNIX_STDIO
275 if (b->ufnm
276 && strlen(b->ufnm) == a->ofnmlen
277 && !strncmp(b->ufnm, a->ofnm, (unsigned)a->ofnmlen))
278 goto same;
279 #else
280 g_char(a->ofnm,a->ofnmlen,buf);
281 if (f__inode(buf,&n) == b->uinode && n == b->udev)
282 goto same;
283 #endif
284 x.cunit=a->ounit;
285 x.csta=0;
286 x.cerr=a->oerr;
287 if ((rv = f_clos(&x)) != 0)
288 return rv;
289 }
290 b->url = (int)a->orl;
291 b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
292 if(a->ofm==0)
293 { if(b->url>0) b->ufmt=0;
294 else b->ufmt=1;
295 }
296 else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
297 else b->ufmt=0;
298 ufmt = b->ufmt;
299 #ifdef url_Adjust
300 if (b->url && !ufmt)
301 url_Adjust(b->url);
302 #endif
303 if (a->ofnm) {
304 g_char(a->ofnm,a->ofnmlen,buf);
305 if (!buf[0])
306 opnerr(a->oerr,107,"open")
307 }
308 else
309 sprintf(buf, "fort.%ld", (long)a->ounit);
310 b->uscrtch = 0;
311 b->uend=0;
312 b->uwrt = 0;
313 b->ufd = 0;
314 b->urw = 3;
315 switch(a->osta ? *a->osta : 'u')
316 {
317 case 'o':
318 case 'O':
319 #ifdef NON_POSIX_STDIO
320 if (!(tf = FOPEN(buf,"r")))
321 opnerr(a->oerr,errno,"open")
322 fclose(tf);
323 #else
324 if (access(buf,0))
325 opnerr(a->oerr,errno,"open")
326 #endif
327 break;
328 case 's':
329 case 'S':
330 b->uscrtch=1;
331 #ifdef NON_ANSI_STDIO
332 (void) strcpy(buf,"tmp.FXXXXXX");
333 (void) mktemp(buf);
334 goto replace;
335 #else
336 if (!(b->ufd = tmpfile()))
337 opnerr(a->oerr,errno,"open")
338 b->ufnm = 0;
339 #ifndef NON_UNIX_STDIO
340 b->uinode = b->udev = -1;
341 #endif
342 b->useek = 1;
343 return 0;
344 #endif
345
346 case 'n':
347 case 'N':
348 #ifdef NON_POSIX_STDIO
349 if ((tf = FOPEN(buf,"r")) || (tf = FOPEN(buf,"a"))) {
350 fclose(tf);
351 opnerr(a->oerr,128,"open")
352 }
353 #else
354 if (!access(buf,0))
355 opnerr(a->oerr,128,"open")
356 #endif
357 /* no break */
358 case 'r': /* Fortran 90 replace option */
359 case 'R':
360 #ifdef NON_ANSI_STDIO
361 replace:
362 #endif
363 if (tf = FOPEN(buf,f__w_mode[0]))
364 fclose(tf);
365 }
366
367 b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
368 if(b->ufnm==NULL) opnerr(a->oerr,113,"no space");
369 (void) strcpy(b->ufnm,buf);
370 if ((s = a->oacc) && b->url)
371 ufmt = 0;
372 if(!(tf = FOPEN(buf, f__w_mode[ufmt|2]))) {
373 if (tf = FOPEN(buf, f__r_mode[ufmt]))
374 b->urw = 1;
375 else if (tf = FOPEN(buf, f__w_mode[ufmt])) {
376 b->uwrt = 1;
377 b->urw = 2;
378 }
379 else
380 err(a->oerr, errno, "open");
381 }
382 b->useek = f__canseek(b->ufd = tf);
383 #ifndef NON_UNIX_STDIO
384 if((b->uinode = f__inode(buf,&b->udev)) == -1)
385 opnerr(a->oerr,108,"open")
386 #endif
387 if(b->useek)
388 if (a->orl)
389 rewind(b->ufd);
390 else if ((s = a->oacc) && (*s == 'a' || *s == 'A')
391 && FSEEK(b->ufd, 0L, SEEK_END))
392 opnerr(a->oerr,129,"open");
393 return(0);
394 }
395
396 int
397 #ifdef KR_headers
398 fk_open(seq,fmt,n) ftnint n;
399 #else
400 fk_open(int seq, int fmt, ftnint n)
401 #endif
402 { char nbuf[10];
403 olist a;
404 (void) sprintf(nbuf,"fort.%ld",(long)n);
405 a.oerr=1;
406 a.ounit=n;
407 a.ofnm=nbuf;
408 a.ofnmlen=strlen(nbuf);
409 a.osta=NULL;
410 a.oacc= (char*)(seq==SEQ?"s":"d");
411 a.ofm = (char*)(fmt==FMT?"f":"u");
412 a.orl = seq==DIR?1:0;
413 a.oblnk=NULL;
414 return(f_open(&a));
415 }
416 #ifdef __cplusplus
417 }
418 #endif