~ubuntu-branches/ubuntu/raring/texlive-bin/raring

« back to all changes in this revision

Viewing changes to utils/pmx/pmx-2.6.17/libf2c/open.c

  • Committer: Package Import Robot
  • Author(s): Norbert Preining
  • Date: 2012-05-30 11:02:05 UTC
  • mfrom: (4.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20120530110205-9gb0n01ahjs4g15y
Tags: 2012.20120530-1
* new upstream snapshot (svn 26726)
  exporting kpse_cnf_get (Closes: #675109)
* cnf.h is again installed, don't install it via libkpathsea-dev.install
* patch handling:
  . removed: 41_maketexmf, 12_fix_epstopdf_invocation
    both included upstream or not needed anymore
  . new: set-e-fmtutil part of set-e-in-various-scripts that still 
    applies
  . disabled: 57_texconfig_papersizes_for_upstream,
    58_texconfig_papersizes_use_ucf, superseeded by 55_texconfig_stuff
  . disabled: set-e-in-various-scripts: split into set-e-fmtutil
    and a disabled part for texconfig

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "f2c.h"
2
 
#include "fio.h"
3
 
#include "string.h"
4
 
#ifndef NON_POSIX_STDIO
5
 
#ifdef MSDOS
6
 
#include "io.h"
7
 
#else
8
 
#include "unistd.h"     /* for access */
9
 
#endif
10
 
#endif
11
 
 
12
 
#ifdef KR_headers
13
 
extern char *malloc();
14
 
#ifdef NON_ANSI_STDIO
15
 
extern char *mktemp();
16
 
#endif
17
 
extern integer f_clos();
18
 
#else
19
 
#undef abs
20
 
#undef min
21
 
#undef max
22
 
#include "stdlib.h"
23
 
#ifdef __cplusplus
24
 
extern "C" {
25
 
#endif
26
 
extern int f__canseek(FILE*);
27
 
extern integer f_clos(cllist*);
28
 
#endif
29
 
 
30
 
#ifdef NON_ANSI_RW_MODES
31
 
char *f__r_mode[2] = {"r", "r"};
32
 
char *f__w_mode[4] = {"w", "w", "r+w", "r+w"};
33
 
#elif defined(WIN32)
34
 
char *f__r_mode[2] = {"rb", "rb"};
35
 
char *f__w_mode[4] = {"wb", "wb", "rb+", "rb+"};
36
 
#else
37
 
char *f__r_mode[2] = {"rb", "r"};
38
 
char *f__w_mode[4] = {"wb", "w", "r+b", "r+"};
39
 
#endif
40
 
 
41
 
 static char f__buf0[400], *f__buf = f__buf0;
42
 
 int f__buflen = (int)sizeof(f__buf0);
43
 
 
44
 
 static void
45
 
#ifdef KR_headers
46
 
f__bufadj(n, c) int n, c;
47
 
#else
48
 
f__bufadj(int n, int c)
49
 
#endif
50
 
{
51
 
        unsigned int len;
52
 
        char *nbuf, *s, *t, *te;
53
 
 
54
 
        if (f__buf == f__buf0)
55
 
                f__buflen = 1024;
56
 
        while(f__buflen <= n)
57
 
                f__buflen <<= 1;
58
 
        len = (unsigned int)f__buflen;
59
 
        if (len != f__buflen || !(nbuf = (char*)malloc(len)))
60
 
                f__fatal(113, "malloc failure");
61
 
        s = nbuf;
62
 
        t = f__buf;
63
 
        te = t + c;
64
 
        while(t < te)
65
 
                *s++ = *t++;
66
 
        if (f__buf != f__buf0)
67
 
                free(f__buf);
68
 
        f__buf = nbuf;
69
 
        }
70
 
 
71
 
 int
72
 
#ifdef KR_headers
73
 
f__putbuf(c) int c;
74
 
#else
75
 
f__putbuf(int c)
76
 
#endif
77
 
{
78
 
        char *s, *se;
79
 
        int n;
80
 
 
81
 
        if (f__hiwater > f__recpos)
82
 
                f__recpos = f__hiwater;
83
 
        n = f__recpos + 1;
84
 
        if (n >= f__buflen)
85
 
                f__bufadj(n, f__recpos);
86
 
        s = f__buf;
87
 
        se = s + f__recpos;
88
 
        if (c)
89
 
                *se++ = c;
90
 
        *se = 0;
91
 
        for(;;) {
92
 
                fputs(s, f__cf);
93
 
                s += strlen(s);
94
 
                if (s >= se)
95
 
                        break;  /* normally happens the first time */
96
 
                putc(*s++, f__cf);
97
 
                }
98
 
        return 0;
99
 
        }
100
 
 
101
 
 void
102
 
#ifdef KR_headers
103
 
x_putc(c)
104
 
#else
105
 
x_putc(int c)
106
 
#endif
107
 
{
108
 
        if (f__recpos >= f__buflen)
109
 
                f__bufadj(f__recpos, f__buflen);
110
 
        f__buf[f__recpos++] = c;
111
 
        }
112
 
 
113
 
#define opnerr(f,m,s) {if(f) errno= m; else opn_err(m,s,a); return(m);}
114
 
 
115
 
 static void
116
 
#ifdef KR_headers
117
 
opn_err(m, s, a) int m; char *s; olist *a;
118
 
#else
119
 
opn_err(int m, char *s, olist *a)
120
 
#endif
121
 
{
122
 
        if (a->ofnm) {
123
 
                /* supply file name to error message */
124
 
                if (a->ofnmlen >= f__buflen)
125
 
                        f__bufadj((int)a->ofnmlen, 0);
126
 
                g_char(a->ofnm, a->ofnmlen, f__curunit->ufnm = f__buf);
127
 
                }
128
 
        f__fatal(m, s);
129
 
        }
130
 
 
131
 
#ifdef KR_headers
132
 
integer f_open(a) olist *a;
133
 
#else
134
 
integer f_open(olist *a)
135
 
#endif
136
 
{       unit *b;
137
 
        integer rv;
138
 
        char buf[256], *s;
139
 
        cllist x;
140
 
        int ufmt;
141
 
        FILE *tf;
142
 
#ifndef NON_UNIX_STDIO
143
 
        int n;
144
 
#endif
145
 
        f__external = 1;
146
 
        if(a->ounit>=MXUNIT || a->ounit<0)
147
 
                err(a->oerr,101,"open")
148
 
        if (!f__init)
149
 
                f_init();
150
 
        f__curunit = b = &f__units[a->ounit];
151
 
        if(b->ufd) {
152
 
                if(a->ofnm==0)
153
 
                {
154
 
                same:   if (a->oblnk)
155
 
                                b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
156
 
                        return(0);
157
 
                }
158
 
#ifdef NON_UNIX_STDIO
159
 
                if (b->ufnm
160
 
                 && strlen(b->ufnm) == a->ofnmlen
161
 
                 && !strncmp(b->ufnm, a->ofnm, (unsigned)a->ofnmlen))
162
 
                        goto same;
163
 
#else
164
 
                g_char(a->ofnm,a->ofnmlen,buf);
165
 
                if (f__inode(buf,&n) == b->uinode && n == b->udev)
166
 
                        goto same;
167
 
#endif
168
 
                x.cunit=a->ounit;
169
 
                x.csta=0;
170
 
                x.cerr=a->oerr;
171
 
                if ((rv = f_clos(&x)) != 0)
172
 
                        return rv;
173
 
                }
174
 
        b->url = (int)a->orl;
175
 
        b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
176
 
        if(a->ofm==0)
177
 
        {       if(b->url>0) b->ufmt=0;
178
 
                else b->ufmt=1;
179
 
        }
180
 
        else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
181
 
        else b->ufmt=0;
182
 
        ufmt = b->ufmt;
183
 
#ifdef url_Adjust
184
 
        if (b->url && !ufmt)
185
 
                url_Adjust(b->url);
186
 
#endif
187
 
        if (a->ofnm) {
188
 
                g_char(a->ofnm,a->ofnmlen,buf);
189
 
                if (!buf[0])
190
 
                        opnerr(a->oerr,107,"open")
191
 
                }
192
 
        else
193
 
                sprintf(buf, "fort.%ld", (long)a->ounit);
194
 
        b->uscrtch = 0;
195
 
        b->uend=0;
196
 
        b->uwrt = 0;
197
 
        b->ufd = 0;
198
 
        b->urw = 3;
199
 
        switch(a->osta ? *a->osta : 'u')
200
 
        {
201
 
        case 'o':
202
 
        case 'O':
203
 
#ifdef NON_POSIX_STDIO
204
 
                if (!(tf = FOPEN(buf,"r")))
205
 
                        opnerr(a->oerr,errno,"open")
206
 
                fclose(tf);
207
 
#else
208
 
                if (access(buf,0))
209
 
                        opnerr(a->oerr,errno,"open")
210
 
#endif
211
 
                break;
212
 
         case 's':
213
 
         case 'S':
214
 
                b->uscrtch=1;
215
 
#ifdef NON_ANSI_STDIO
216
 
                (void) strcpy(buf,"tmp.FXXXXXX");
217
 
                (void) mktemp(buf);
218
 
                goto replace;
219
 
#else
220
 
                if (!(b->ufd = tmpfile()))
221
 
                        opnerr(a->oerr,errno,"open")
222
 
                b->ufnm = 0;
223
 
#ifndef NON_UNIX_STDIO
224
 
                b->uinode = b->udev = -1;
225
 
#endif
226
 
                b->useek = 1;
227
 
                return 0;
228
 
#endif
229
 
 
230
 
        case 'n':
231
 
        case 'N':
232
 
#ifdef NON_POSIX_STDIO
233
 
                if ((tf = FOPEN(buf,"r")) || (tf = FOPEN(buf,"a"))) {
234
 
                        fclose(tf);
235
 
                        opnerr(a->oerr,128,"open")
236
 
                        }
237
 
#else
238
 
                if (!access(buf,0))
239
 
                        opnerr(a->oerr,128,"open")
240
 
#endif
241
 
                /* no break */
242
 
        case 'r':       /* Fortran 90 replace option */
243
 
        case 'R':
244
 
#ifdef NON_ANSI_STDIO
245
 
 replace:
246
 
#endif
247
 
                if (tf = FOPEN(buf,f__w_mode[0]))
248
 
                        fclose(tf);
249
 
        }
250
 
 
251
 
        b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
252
 
        if(b->ufnm==NULL) opnerr(a->oerr,113,"no space");
253
 
        (void) strcpy(b->ufnm,buf);
254
 
        if ((s = a->oacc) && b->url)
255
 
                ufmt = 0;
256
 
        if(!(tf = FOPEN(buf, f__w_mode[ufmt|2]))) {
257
 
                if (tf = FOPEN(buf, f__r_mode[ufmt]))
258
 
                        b->urw = 1;
259
 
                else if (tf = FOPEN(buf, f__w_mode[ufmt])) {
260
 
                        b->uwrt = 1;
261
 
                        b->urw = 2;
262
 
                        }
263
 
                else
264
 
                        err(a->oerr, errno, "open");
265
 
                }
266
 
        b->useek = f__canseek(b->ufd = tf);
267
 
#ifndef NON_UNIX_STDIO
268
 
        if((b->uinode = f__inode(buf,&b->udev)) == -1)
269
 
                opnerr(a->oerr,108,"open")
270
 
#endif
271
 
        if(b->useek)
272
 
                if (a->orl)
273
 
                        rewind(b->ufd);
274
 
                else if ((s = a->oacc) && (*s == 'a' || *s == 'A')
275
 
                        && FSEEK(b->ufd, 0L, SEEK_END))
276
 
                                opnerr(a->oerr,129,"open");
277
 
        return(0);
278
 
}
279
 
 
280
 
 int
281
 
#ifdef KR_headers
282
 
fk_open(seq,fmt,n) ftnint n;
283
 
#else
284
 
fk_open(int seq, int fmt, ftnint n)
285
 
#endif
286
 
{       char nbuf[10];
287
 
        olist a;
288
 
        (void) sprintf(nbuf,"fort.%ld",(long)n);
289
 
        a.oerr=1;
290
 
        a.ounit=n;
291
 
        a.ofnm=nbuf;
292
 
        a.ofnmlen=strlen(nbuf);
293
 
        a.osta=NULL;
294
 
        a.oacc= (char*)(seq==SEQ?"s":"d");
295
 
        a.ofm = (char*)(fmt==FMT?"f":"u");
296
 
        a.orl = seq==DIR?1:0;
297
 
        a.oblnk=NULL;
298
 
        return(f_open(&a));
299
 
}
300
 
#ifdef __cplusplus
301
 
}
302
 
#endif