2001-05-16 20:59:29 +02:00
|
|
|
#include "config.h"
|
1998-02-01 02:37:08 +01:00
|
|
|
#include "f2c.h"
|
|
|
|
#include "fio.h"
|
|
|
|
#include <string.h>
|
1998-05-19 12:52:03 +02:00
|
|
|
#ifndef NON_POSIX_STDIO
|
|
|
|
#ifdef MSDOS
|
|
|
|
#include "io.h"
|
|
|
|
#else
|
2002-06-01 14:38:32 +02:00
|
|
|
#include "unistd.h" /* for access */
|
1998-05-19 12:52:03 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
1998-02-01 02:37:08 +01:00
|
|
|
|
|
|
|
#undef abs
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
#include <stdlib.h>
|
2002-06-01 14:38:32 +02:00
|
|
|
extern int f__canseek (FILE *);
|
|
|
|
extern integer f_clos (cllist *);
|
1998-02-01 02:37:08 +01:00
|
|
|
|
|
|
|
#ifdef NON_ANSI_RW_MODES
|
2002-06-01 14:38:32 +02:00
|
|
|
char *f__r_mode[2] = { "r", "r" };
|
|
|
|
char *f__w_mode[4] = { "w", "w", "r+w", "r+w" };
|
1998-02-01 02:37:08 +01:00
|
|
|
#else
|
2002-06-01 14:38:32 +02:00
|
|
|
char *f__r_mode[2] = { "rb", "r" };
|
|
|
|
char *f__w_mode[4] = { "wb", "w", "r+b", "r+" };
|
1998-02-01 02:37:08 +01:00
|
|
|
#endif
|
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
static char f__buf0[400], *f__buf = f__buf0;
|
|
|
|
int f__buflen = (int) sizeof (f__buf0);
|
1998-05-19 12:52:03 +02:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
static void
|
|
|
|
f__bufadj (int n, int c)
|
1998-02-01 02:37:08 +01:00
|
|
|
{
|
2002-06-01 14:38:32 +02:00
|
|
|
unsigned int len;
|
|
|
|
char *nbuf, *s, *t, *te;
|
1998-02-01 02:37:08 +01:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
if (f__buf == f__buf0)
|
|
|
|
f__buflen = 1024;
|
|
|
|
while (f__buflen <= n)
|
|
|
|
f__buflen <<= 1;
|
|
|
|
len = (unsigned int) f__buflen;
|
|
|
|
if (len != f__buflen || !(nbuf = (char *) malloc (len)))
|
|
|
|
f__fatal (113, "malloc failure");
|
|
|
|
s = nbuf;
|
|
|
|
t = f__buf;
|
|
|
|
te = t + c;
|
|
|
|
while (t < te)
|
|
|
|
*s++ = *t++;
|
|
|
|
if (f__buf != f__buf0)
|
|
|
|
free (f__buf);
|
|
|
|
f__buf = nbuf;
|
|
|
|
}
|
1998-05-19 12:52:03 +02:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
int
|
|
|
|
f__putbuf (int c)
|
1998-05-19 12:52:03 +02:00
|
|
|
{
|
2002-06-01 14:38:32 +02:00
|
|
|
char *s, *se;
|
|
|
|
int n;
|
1998-02-01 02:37:08 +01:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
if (f__hiwater > f__recpos)
|
|
|
|
f__recpos = f__hiwater;
|
|
|
|
n = f__recpos + 1;
|
|
|
|
if (n >= f__buflen)
|
|
|
|
f__bufadj (n, f__recpos);
|
|
|
|
s = f__buf;
|
|
|
|
se = s + f__recpos;
|
|
|
|
if (c)
|
|
|
|
*se++ = c;
|
|
|
|
*se = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
fputs (s, f__cf);
|
|
|
|
s += strlen (s);
|
|
|
|
if (s >= se)
|
|
|
|
break; /* normally happens the first time */
|
|
|
|
putc (*s++, f__cf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1998-05-19 12:52:03 +02:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
void
|
|
|
|
x_putc (int c)
|
1998-05-19 12:52:03 +02:00
|
|
|
{
|
2002-06-01 14:38:32 +02:00
|
|
|
if (f__recpos >= f__buflen)
|
|
|
|
f__bufadj (f__recpos, f__buflen);
|
|
|
|
f__buf[f__recpos++] = c;
|
|
|
|
}
|
1998-05-19 12:52:03 +02:00
|
|
|
|
|
|
|
#define opnerr(f,m,s) \
|
|
|
|
do {if(f) {f__init &= ~2; errno= m;} else opn_err(m,s,a); return(m);} while(0)
|
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
static void
|
|
|
|
opn_err (int m, char *s, olist * a)
|
1998-05-19 12:52:03 +02:00
|
|
|
{
|
2002-06-01 14:38:32 +02:00
|
|
|
if (a->ofnm)
|
|
|
|
{
|
|
|
|
/* supply file name to error message */
|
|
|
|
if (a->ofnmlen >= f__buflen)
|
|
|
|
f__bufadj ((int) a->ofnmlen, 0);
|
|
|
|
g_char (a->ofnm, a->ofnmlen, f__curunit->ufnm = f__buf);
|
|
|
|
}
|
|
|
|
f__fatal (m, s);
|
|
|
|
}
|
1998-05-19 12:52:03 +02:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
integer
|
|
|
|
f_open (olist * a)
|
|
|
|
{
|
|
|
|
unit *b;
|
|
|
|
integer rv;
|
|
|
|
char buf[256], *s, *env;
|
|
|
|
cllist x;
|
|
|
|
int ufmt;
|
|
|
|
FILE *tf;
|
|
|
|
int fd, len;
|
1998-05-19 12:52:03 +02:00
|
|
|
#ifndef NON_UNIX_STDIO
|
2002-06-01 14:38:32 +02:00
|
|
|
int n;
|
1998-02-01 02:37:08 +01:00
|
|
|
#endif
|
2002-06-01 14:38:32 +02:00
|
|
|
if (f__init != 1)
|
|
|
|
f_init ();
|
|
|
|
f__external = 1;
|
|
|
|
if (a->ounit >= MXUNIT || a->ounit < 0)
|
|
|
|
err (a->oerr, 101, "open");
|
|
|
|
f__curunit = b = &f__units[a->ounit];
|
|
|
|
if (b->ufd)
|
|
|
|
{
|
|
|
|
if (a->ofnm == 0)
|
|
|
|
{
|
|
|
|
same:if (a->oblnk)
|
|
|
|
b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
|
|
|
|
return (0);
|
|
|
|
}
|
1998-02-01 02:37:08 +01:00
|
|
|
#ifdef NON_UNIX_STDIO
|
2002-06-01 14:38:32 +02:00
|
|
|
if (b->ufnm
|
|
|
|
&& strlen (b->ufnm) == a->ofnmlen
|
|
|
|
&& !strncmp (b->ufnm, a->ofnm, (unsigned) a->ofnmlen))
|
|
|
|
goto same;
|
1998-02-01 02:37:08 +01:00
|
|
|
#else
|
2002-06-01 14:38:32 +02:00
|
|
|
g_char (a->ofnm, a->ofnmlen, buf);
|
|
|
|
if (f__inode (buf, &n) == b->uinode && n == b->udev)
|
|
|
|
goto same;
|
1998-02-01 02:37:08 +01:00
|
|
|
#endif
|
2002-06-01 14:38:32 +02:00
|
|
|
x.cunit = a->ounit;
|
|
|
|
x.csta = 0;
|
|
|
|
x.cerr = a->oerr;
|
|
|
|
if ((rv = f_clos (&x)) != 0)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
b->url = (int) a->orl;
|
|
|
|
b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
|
|
|
|
if (a->ofm == 0)
|
2002-07-10 23:17:29 +02:00
|
|
|
b->ufmt = 1;
|
2002-06-01 14:38:32 +02:00
|
|
|
else if (*a->ofm == 'f' || *a->ofm == 'F')
|
|
|
|
b->ufmt = 1;
|
|
|
|
else
|
|
|
|
b->ufmt = 0;
|
|
|
|
ufmt = b->ufmt;
|
1998-02-01 02:37:08 +01:00
|
|
|
#ifdef url_Adjust
|
2002-06-01 14:38:32 +02:00
|
|
|
if (b->url && !ufmt)
|
|
|
|
url_Adjust (b->url);
|
1998-02-01 02:37:08 +01:00
|
|
|
#endif
|
2002-06-01 14:38:32 +02:00
|
|
|
if (a->ofnm)
|
|
|
|
{
|
|
|
|
g_char (a->ofnm, a->ofnmlen, buf);
|
|
|
|
if (!buf[0])
|
|
|
|
opnerr (a->oerr, 107, "open");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sprintf (buf, "fort.%ld", (long) a->ounit);
|
|
|
|
b->uscrtch = 0;
|
|
|
|
b->uend = 0;
|
|
|
|
b->uwrt = 0;
|
|
|
|
b->ufd = 0;
|
|
|
|
b->urw = 3;
|
|
|
|
switch (a->osta ? *a->osta : 'u')
|
|
|
|
{
|
|
|
|
case 'o':
|
|
|
|
case 'O':
|
1998-05-19 12:52:03 +02:00
|
|
|
#ifdef NON_POSIX_STDIO
|
2002-06-01 14:38:32 +02:00
|
|
|
if (!(tf = fopen (buf, "r")))
|
|
|
|
opnerr (a->oerr, errno, "open");
|
|
|
|
fclose (tf);
|
1998-02-01 02:37:08 +01:00
|
|
|
#else
|
2002-06-01 14:38:32 +02:00
|
|
|
if (access (buf, 0))
|
|
|
|
opnerr (a->oerr, errno, "open");
|
1998-02-01 02:37:08 +01:00
|
|
|
#endif
|
2002-06-01 14:38:32 +02:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
b->uscrtch = 1;
|
|
|
|
#ifdef HAVE_MKSTEMP /* Allow use of TMPDIR preferentially. */
|
|
|
|
env = getenv ("TMPDIR");
|
|
|
|
if (!env)
|
|
|
|
env = getenv ("TEMP");
|
|
|
|
if (!env)
|
|
|
|
env = "/tmp";
|
|
|
|
len = strlen (env);
|
2002-06-02 14:54:28 +02:00
|
|
|
if (len > 256 - (int) sizeof ("/tmp.FXXXXXX"))
|
2002-06-01 14:38:32 +02:00
|
|
|
err (a->oerr, 132, "open");
|
|
|
|
strcpy (buf, env);
|
|
|
|
strcat (buf, "/tmp.FXXXXXX");
|
|
|
|
fd = mkstemp (buf);
|
|
|
|
if (fd == -1 || close (fd))
|
|
|
|
err (a->oerr, 132, "open");
|
2000-11-15 19:26:42 +01:00
|
|
|
#else /* ! defined (HAVE_MKSTEMP) */
|
1998-03-22 11:43:37 +01:00
|
|
|
#ifdef HAVE_TEMPNAM /* Allow use of TMPDIR preferentially. */
|
2002-06-01 14:38:32 +02:00
|
|
|
s = tempnam (0, buf);
|
|
|
|
if (strlen (s) >= sizeof (buf))
|
|
|
|
err (a->oerr, 132, "open");
|
|
|
|
(void) strcpy (buf, s);
|
|
|
|
free (s);
|
1998-03-22 11:43:37 +01:00
|
|
|
#else /* ! defined (HAVE_TEMPNAM) */
|
2002-01-15 18:27:05 +01:00
|
|
|
#ifdef HAVE_TMPNAM
|
2002-06-01 14:38:32 +02:00
|
|
|
tmpnam (buf);
|
1998-02-01 02:37:08 +01:00
|
|
|
#else
|
2002-06-01 14:38:32 +02:00
|
|
|
(void) strcpy (buf, "tmp.FXXXXXX");
|
|
|
|
(void) mktemp (buf);
|
1998-02-01 02:37:08 +01:00
|
|
|
#endif
|
1998-03-22 11:43:37 +01:00
|
|
|
#endif /* ! defined (HAVE_TEMPNAM) */
|
2000-11-15 19:26:42 +01:00
|
|
|
#endif /* ! defined (HAVE_MKSTEMP) */
|
2002-06-01 14:38:32 +02:00
|
|
|
goto replace;
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
1998-05-19 12:52:03 +02:00
|
|
|
#ifdef NON_POSIX_STDIO
|
2002-06-01 14:38:32 +02:00
|
|
|
if ((tf = fopen (buf, "r")) || (tf = fopen (buf, "a")))
|
|
|
|
{
|
|
|
|
fclose (tf);
|
|
|
|
opnerr (a->oerr, 128, "open");
|
|
|
|
}
|
1998-02-01 02:37:08 +01:00
|
|
|
#else
|
2002-06-01 14:38:32 +02:00
|
|
|
if (!access (buf, 0))
|
|
|
|
opnerr (a->oerr, 128, "open");
|
1998-02-01 02:37:08 +01:00
|
|
|
#endif
|
2002-06-01 14:38:32 +02:00
|
|
|
/* no break */
|
|
|
|
case 'r': /* Fortran 90 replace option */
|
|
|
|
case 'R':
|
|
|
|
replace:
|
dfe.c (s_rdfe, s_wdfe): Wrap parentheses around assignment used as truth value.
* libI77/dfe.c (s_rdfe, s_wdfe): Wrap parentheses around
assignment used as truth value.
* libI77/due.c (s_rdue, s_wdue): Likewise.
* libI77/endfile.c (f_end): Likewise.
* libI77/iio.c (s_rsfi, s_wsfi): Likewise.
* libI77/lread.c (ERR, l_C, nmL_getc, s_rsle): Likewise.
* libI77/lwrite.c (l_g, l_put): Likewise.
* libI77/open.c (f_open): Likewise.
* libI77/rdfmt.c (rd_Z): Likewise.
* libI77/rsfe.c (s_rsfe): Likewise.
* libI77/rsne.c (hash, mk_hashtab, nl_init, getname, getdimen,
x_rsne, s_rsne): Likewise.
* libI77/sue.c (s_rsue, s_wsue): Likewise.
* libI77/wref.c (wrt_E, wrt_F): Likewise.
* libI77/wsfe.c (s_wsfe): Likewise.
* libI77/wsle.c (s_wsle): Likewise.
* libI77/wsne.c (s_wsne): Likewise.
From-SVN: r54172
2002-06-02 16:34:31 +02:00
|
|
|
if ((tf = fopen (buf, f__w_mode[0])))
|
2002-06-01 14:38:32 +02:00
|
|
|
fclose (tf);
|
|
|
|
}
|
1998-02-01 02:37:08 +01:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
b->ufnm = (char *) malloc ((unsigned int) (strlen (buf) + 1));
|
|
|
|
if (b->ufnm == NULL)
|
|
|
|
opnerr (a->oerr, 113, "no space");
|
|
|
|
(void) strcpy (b->ufnm, buf);
|
|
|
|
if ((s = a->oacc) && b->url)
|
|
|
|
ufmt = 0;
|
|
|
|
if (!(tf = fopen (buf, f__w_mode[ufmt | 2])))
|
|
|
|
{
|
dfe.c (s_rdfe, s_wdfe): Wrap parentheses around assignment used as truth value.
* libI77/dfe.c (s_rdfe, s_wdfe): Wrap parentheses around
assignment used as truth value.
* libI77/due.c (s_rdue, s_wdue): Likewise.
* libI77/endfile.c (f_end): Likewise.
* libI77/iio.c (s_rsfi, s_wsfi): Likewise.
* libI77/lread.c (ERR, l_C, nmL_getc, s_rsle): Likewise.
* libI77/lwrite.c (l_g, l_put): Likewise.
* libI77/open.c (f_open): Likewise.
* libI77/rdfmt.c (rd_Z): Likewise.
* libI77/rsfe.c (s_rsfe): Likewise.
* libI77/rsne.c (hash, mk_hashtab, nl_init, getname, getdimen,
x_rsne, s_rsne): Likewise.
* libI77/sue.c (s_rsue, s_wsue): Likewise.
* libI77/wref.c (wrt_E, wrt_F): Likewise.
* libI77/wsfe.c (s_wsfe): Likewise.
* libI77/wsle.c (s_wsle): Likewise.
* libI77/wsne.c (s_wsne): Likewise.
From-SVN: r54172
2002-06-02 16:34:31 +02:00
|
|
|
if ((tf = fopen (buf, f__r_mode[ufmt])))
|
2002-06-01 14:38:32 +02:00
|
|
|
b->urw = 1;
|
dfe.c (s_rdfe, s_wdfe): Wrap parentheses around assignment used as truth value.
* libI77/dfe.c (s_rdfe, s_wdfe): Wrap parentheses around
assignment used as truth value.
* libI77/due.c (s_rdue, s_wdue): Likewise.
* libI77/endfile.c (f_end): Likewise.
* libI77/iio.c (s_rsfi, s_wsfi): Likewise.
* libI77/lread.c (ERR, l_C, nmL_getc, s_rsle): Likewise.
* libI77/lwrite.c (l_g, l_put): Likewise.
* libI77/open.c (f_open): Likewise.
* libI77/rdfmt.c (rd_Z): Likewise.
* libI77/rsfe.c (s_rsfe): Likewise.
* libI77/rsne.c (hash, mk_hashtab, nl_init, getname, getdimen,
x_rsne, s_rsne): Likewise.
* libI77/sue.c (s_rsue, s_wsue): Likewise.
* libI77/wref.c (wrt_E, wrt_F): Likewise.
* libI77/wsfe.c (s_wsfe): Likewise.
* libI77/wsle.c (s_wsle): Likewise.
* libI77/wsne.c (s_wsne): Likewise.
From-SVN: r54172
2002-06-02 16:34:31 +02:00
|
|
|
else if ((tf = fopen (buf, f__w_mode[ufmt])))
|
2002-06-01 14:38:32 +02:00
|
|
|
{
|
|
|
|
b->uwrt = 1;
|
|
|
|
b->urw = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
err (a->oerr, errno, "open");
|
|
|
|
}
|
|
|
|
b->useek = f__canseek (b->ufd = tf);
|
1998-02-01 02:37:08 +01:00
|
|
|
#ifndef NON_UNIX_STDIO
|
2002-06-01 14:38:32 +02:00
|
|
|
if ((b->uinode = f__inode (buf, &b->udev)) == -1)
|
|
|
|
opnerr (a->oerr, 108, "open");
|
1998-02-01 02:37:08 +01:00
|
|
|
#endif
|
2002-06-01 14:38:32 +02:00
|
|
|
if (b->useek)
|
2002-06-02 14:57:28 +02:00
|
|
|
{
|
|
|
|
if (a->orl)
|
|
|
|
FSEEK (b->ufd, 0, SEEK_SET);
|
|
|
|
else if ((s = a->oacc) && (*s == 'a' || *s == 'A')
|
|
|
|
&& FSEEK (b->ufd, 0, SEEK_END))
|
|
|
|
opnerr (a->oerr, 129, "open");
|
|
|
|
}
|
2002-06-01 14:38:32 +02:00
|
|
|
return (0);
|
1998-02-01 02:37:08 +01:00
|
|
|
}
|
|
|
|
|
main.c (main): Avoid implicit int.
* libF77/main.c (main): Avoid implicit int.
* libI77/dfe.c (y_rsk, y_getc, c_dfe): Likewise.
* libI77/due.c (c_due): Likewise.
* libI77/err.c (f__canseek, f__nowreading, f__nowwriting):
Likewise.
* libI77/fmt.c (op_gen, ne_d, e_d, pars_f, type_f, en_fio):
Likewise.
* libI77/iio.c (z_getc, z_rnew, c_si, z_wnew): Likewise.
* libI77/lread.c (t_getc, c_le, l_read): Likewise.
* libI77/lwrite.c (l_write): Likewise.
* libI77/open.c (fk_open): Likewise.
* libI77/rdfmt.c (rd_ed, rd_ned): Likewise.
* libI77/rsfe.c (xrd_SL, x_getc, x_endp, x_rev): Likewise.
* libI77/rsne.c (t_getc, x_rsne): Likewise.
* libI77/sfe.c (c_sfe): Likewise.
* libI77/sue.c (c_sue): Likewise.
* libI77/uio.c (do_us): Likewise.
* libI77/wref.c (wrt_E, wrt_F): Likewise.
* libI77/wrtfmt.c (wrt_L, w_ed, w_ned): Likewise.
From-SVN: r54169
2002-06-02 15:01:12 +02:00
|
|
|
int
|
2002-06-01 14:38:32 +02:00
|
|
|
fk_open (int seq, int fmt, ftnint n)
|
|
|
|
{
|
|
|
|
char nbuf[10];
|
|
|
|
olist a;
|
|
|
|
int rtn;
|
|
|
|
int save_init;
|
|
|
|
|
|
|
|
(void) sprintf (nbuf, "fort.%ld", (long) n);
|
|
|
|
a.oerr = 1;
|
|
|
|
a.ounit = n;
|
|
|
|
a.ofnm = nbuf;
|
|
|
|
a.ofnmlen = strlen (nbuf);
|
|
|
|
a.osta = NULL;
|
|
|
|
a.oacc = seq == SEQ ? "s" : "d";
|
|
|
|
a.ofm = fmt == FMT ? "f" : "u";
|
|
|
|
a.orl = seq == DIR ? 1 : 0;
|
|
|
|
a.oblnk = NULL;
|
|
|
|
save_init = f__init;
|
|
|
|
f__init &= ~2;
|
|
|
|
rtn = f_open (&a);
|
|
|
|
f__init = save_init | 1;
|
|
|
|
return rtn;
|
1998-02-01 02:37:08 +01:00
|
|
|
}
|