summaryrefslogtreecommitdiffstats
path: root/contrib/libf2c/libI77/endfile.c
blob: 513f2107ef79dd5d373f12163674323eb489e30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "config.h"
#include "f2c.h"
#include "fio.h"

#include <sys/types.h>
#include <unistd.h>

#undef abs
#undef min
#undef max
#include <stdlib.h>
#include <string.h>

extern char *f__r_mode[], *f__w_mode[];

integer
f_end (alist * a)
{
  unit *b;
  FILE *tf;

  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);
      if ((tf = fopen (nbuf, f__w_mode[0])))
	fclose (tf);
      return (0);
    }
  b->uend = 1;
  return (b->useek ? t_runc (a) : 0);
}

#ifndef HAVE_FTRUNCATE
static int
copy (FILE * from, register long len, FILE * to)
{
  int len1;
  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;
}
#endif /* !defined(HAVE_FTRUNCATE) */

int
t_runc (alist * a)
{
  off_t loc, len;
  unit *b;
  int rc;
  FILE *bf;
#ifndef HAVE_FTRUNCATE
  FILE *tf;
#endif /* !defined(HAVE_FTRUNCATE) */

  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);
#ifndef HAVE_FTRUNCATE
  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 ()))
    {
#ifdef NON_UNIX_STDIO
    bad:
#endif
      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;
#ifdef NON_UNIX_STDIO
  if (b->ufmt)
    {
      fclose (bf);
      if (!(bf = fopen (b->ufnm, f__w_mode[3])))
	goto bad;
      FSEEK (bf, 0, SEEK_END);
      b->urw = 3;
    }
#endif
done1:
  fclose (tf);
done:
  f__cf = b->ufd = bf;
#else /* !defined(HAVE_FTRUNCATE) */
  fflush (b->ufd);
  rc = ftruncate (fileno (b->ufd), loc);
  FSEEK (bf, loc, SEEK_SET);
#endif /* !defined(HAVE_FTRUNCATE) */
  if (rc)
    err (a->aerr, 111, "endfile");
  return 0;
}
OpenPOWER on IntegriCloud