2001-05-16 12:26:52 +02:00
|
|
|
#include "config.h"
|
1998-02-01 02:37:08 +01:00
|
|
|
#include "f2c.h"
|
|
|
|
#include "fio.h"
|
|
|
|
|
2001-05-16 12:26:52 +02:00
|
|
|
#include <sys/types.h>
|
2001-02-26 21:23:41 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1998-02-01 02:37:08 +01:00
|
|
|
#undef abs
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
extern char *f__r_mode[], *f__w_mode[];
|
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
integer
|
|
|
|
f_end (alist * a)
|
1998-02-01 02:37:08 +01:00
|
|
|
{
|
2002-06-01 14:38:32 +02:00
|
|
|
unit *b;
|
|
|
|
FILE *tf;
|
1998-05-19 12:52:03 +02:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
if (f__init & 2)
|
|
|
|
f__fatal (131, "I/O recursion");
|
|
|
|
if (a->aunit >= MXUNIT || a->aunit < 0)
|
|
|
|
err (a->aerr, 101, "endfile");
|
|
|
|
b = &f__units[a->aunit];
|
|
|
|
if (b->ufd == NULL)
|
|
|
|
{
|
|
|
|
char nbuf[10];
|
|
|
|
sprintf (nbuf, "fort.%ld", (long) a->aunit);
|
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 (nbuf, f__w_mode[0])))
|
2002-06-01 14:38:32 +02:00
|
|
|
fclose (tf);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
b->uend = 1;
|
|
|
|
return (b->useek ? t_runc (a) : 0);
|
1998-02-01 02:37:08 +01:00
|
|
|
}
|
|
|
|
|
2001-02-26 21:23:41 +01:00
|
|
|
#ifndef HAVE_FTRUNCATE
|
2002-06-01 14:38:32 +02:00
|
|
|
static int
|
|
|
|
copy (FILE * from, register long len, FILE * to)
|
1998-02-01 02:37:08 +01:00
|
|
|
{
|
2002-06-01 14:38:32 +02:00
|
|
|
int len1;
|
|
|
|
char buf[BUFSIZ];
|
1998-02-01 02:37:08 +01:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
while (fread (buf, len1 = len > BUFSIZ ? BUFSIZ : (int) len, 1, from))
|
|
|
|
{
|
|
|
|
if (!fwrite (buf, len1, 1, to))
|
|
|
|
return 1;
|
|
|
|
if ((len -= len1) <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-26 21:23:41 +01:00
|
|
|
#endif /* !defined(HAVE_FTRUNCATE) */
|
1998-02-01 02:37:08 +01:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
int
|
|
|
|
t_runc (alist * a)
|
1998-02-01 02:37:08 +01:00
|
|
|
{
|
2002-06-01 14:38:32 +02:00
|
|
|
off_t loc, len;
|
|
|
|
unit *b;
|
|
|
|
int rc;
|
|
|
|
FILE *bf;
|
2001-02-26 21:23:41 +01:00
|
|
|
#ifndef HAVE_FTRUNCATE
|
2002-06-01 14:38:32 +02:00
|
|
|
FILE *tf;
|
2001-02-26 21:23:41 +01:00
|
|
|
#endif /* !defined(HAVE_FTRUNCATE) */
|
1998-02-01 02:37:08 +01:00
|
|
|
|
2002-06-01 14:38:32 +02:00
|
|
|
b = &f__units[a->aunit];
|
|
|
|
if (b->url)
|
|
|
|
return (0); /*don't truncate direct files */
|
|
|
|
loc = FTELL (bf = b->ufd);
|
|
|
|
FSEEK (bf, 0, SEEK_END);
|
|
|
|
len = FTELL (bf);
|
|
|
|
if (loc >= len || b->useek == 0 || b->ufnm == NULL)
|
|
|
|
return (0);
|
2001-02-26 21:23:41 +01:00
|
|
|
#ifndef HAVE_FTRUNCATE
|
2002-06-01 14:38:32 +02:00
|
|
|
rc = 0;
|
|
|
|
fclose (b->ufd);
|
|
|
|
if (!loc)
|
|
|
|
{
|
|
|
|
if (!(bf = fopen (b->ufnm, f__w_mode[b->ufmt])))
|
|
|
|
rc = 1;
|
|
|
|
if (b->uwrt)
|
|
|
|
b->uwrt = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!(bf = fopen (b->ufnm, f__r_mode[0])) || !(tf = tmpfile ()))
|
|
|
|
{
|
1999-03-17 09:21:44 +01:00
|
|
|
#ifdef NON_UNIX_STDIO
|
2002-06-01 14:38:32 +02:00
|
|
|
bad:
|
1999-03-17 09:21:44 +01:00
|
|
|
#endif
|
2002-06-01 14:38:32 +02:00
|
|
|
rc = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (copy (bf, loc, tf))
|
|
|
|
{
|
|
|
|
bad1:
|
|
|
|
rc = 1;
|
|
|
|
goto done1;
|
|
|
|
}
|
|
|
|
if (!(bf = freopen (b->ufnm, f__w_mode[0], bf)))
|
|
|
|
goto bad1;
|
|
|
|
FSEEK (tf, 0, SEEK_SET);
|
|
|
|
if (copy (tf, loc, bf))
|
|
|
|
goto bad1;
|
|
|
|
b->uwrt = 1;
|
|
|
|
b->urw = 2;
|
1998-05-19 12:52:03 +02:00
|
|
|
#ifdef NON_UNIX_STDIO
|
2002-06-01 14:38:32 +02:00
|
|
|
if (b->ufmt)
|
|
|
|
{
|
|
|
|
fclose (bf);
|
|
|
|
if (!(bf = fopen (b->ufnm, f__w_mode[3])))
|
|
|
|
goto bad;
|
|
|
|
FSEEK (bf, 0, SEEK_END);
|
|
|
|
b->urw = 3;
|
|
|
|
}
|
1998-05-19 12:52:03 +02:00
|
|
|
#endif
|
1998-02-01 02:37:08 +01:00
|
|
|
done1:
|
2002-06-01 14:38:32 +02:00
|
|
|
fclose (tf);
|
1998-02-01 02:37:08 +01:00
|
|
|
done:
|
2002-06-01 14:38:32 +02:00
|
|
|
f__cf = b->ufd = bf;
|
|
|
|
#else /* !defined(HAVE_FTRUNCATE) */
|
|
|
|
fflush (b->ufd);
|
|
|
|
rc = ftruncate (fileno (b->ufd), loc);
|
|
|
|
FSEEK (bf, loc, SEEK_SET);
|
2001-02-26 21:23:41 +01:00
|
|
|
#endif /* !defined(HAVE_FTRUNCATE) */
|
2002-06-01 14:38:32 +02:00
|
|
|
if (rc)
|
|
|
|
err (a->aerr, 111, "endfile");
|
|
|
|
return 0;
|
|
|
|
}
|