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