14 extern char *f__r_mode
[], *f__w_mode
[];
23 f__fatal (131, "I/O recursion");
24 if (a
->aunit
>= MXUNIT
|| a
->aunit
< 0)
25 err (a
->aerr
, 101, "endfile");
26 b
= &f__units
[a
->aunit
];
30 sprintf (nbuf
, "fort.%ld", (long) a
->aunit
);
31 if ((tf
= fopen (nbuf
, f__w_mode
[0])))
36 return (b
->useek
? t_runc (a
) : 0);
39 #ifndef HAVE_FTRUNCATE
41 copy (FILE * from
, register long len
, FILE * to
)
46 while (fread (buf
, len1
= len
> BUFSIZ
? BUFSIZ
: (int) len
, 1, from
))
48 if (!fwrite (buf
, len1
, 1, to
))
50 if ((len
-= len1
) <= 0)
55 #endif /* !defined(HAVE_FTRUNCATE) */
64 #ifndef HAVE_FTRUNCATE
66 #endif /* !defined(HAVE_FTRUNCATE) */
68 b
= &f__units
[a
->aunit
];
70 return (0); /*don't truncate direct files */
71 loc
= FTELL (bf
= b
->ufd
);
72 FSEEK (bf
, 0, SEEK_END
);
74 if (loc
>= len
|| b
->useek
== 0 || b
->ufnm
== NULL
)
76 #ifndef HAVE_FTRUNCATE
81 if (!(bf
= fopen (b
->ufnm
, f__w_mode
[b
->ufmt
])))
87 if (!(bf
= fopen (b
->ufnm
, f__r_mode
[0])) || !(tf
= tmpfile ()))
95 if (copy (bf
, loc
, tf
))
101 if (!(bf
= freopen (b
->ufnm
, f__w_mode
[0], bf
)))
103 FSEEK (tf
, 0, SEEK_SET
);
104 if (copy (tf
, loc
, bf
))
108 #ifdef NON_UNIX_STDIO
112 if (!(bf
= fopen (b
->ufnm
, f__w_mode
[3])))
114 FSEEK (bf
, 0, SEEK_END
);
122 #else /* !defined(HAVE_FTRUNCATE) */
124 rc
= ftruncate (fileno (b
->ufd
), loc
);
125 FSEEK (bf
, loc
, SEEK_SET
);
126 #endif /* !defined(HAVE_FTRUNCATE) */
128 err (a
->aerr
, 111, "endfile");