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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#include "sfdchdr.h"
/* Make a sequence of streams act like a single stream.
** This is for reading only.
**
** Written by Kiem-Phong Vo, kpv@research.att.com, 03/18/1998.
*/
#define UNSEEKABLE 1
typedef struct _file_s
{ Sfio_t* f; /* the stream */
Sfoff_t lower; /* its lowest end */
} File_t;
typedef struct _union_s
{
Sfdisc_t disc; /* discipline structure */
short type; /* type of streams */
short c; /* current stream */
short n; /* number of streams */
Sfoff_t here; /* current location */
File_t f[1]; /* array of streams */
} Union_t;
#if __STD_C
static ssize_t unwrite(Sfio_t* f, const Void_t* buf, size_t n, Sfdisc_t* disc)
#else
static ssize_t unwrite(f, buf, n, disc)
Sfio_t* f; /* stream involved */
Void_t* buf; /* buffer to read into */
size_t n; /* number of bytes to read */
Sfdisc_t* disc; /* discipline */
#endif
{
return -1;
}
#if __STD_C
static ssize_t unread(Sfio_t* f, Void_t* buf, size_t n, Sfdisc_t* disc)
#else
static ssize_t unread(f, buf, n, disc)
Sfio_t* f; /* stream involved */
Void_t* buf; /* buffer to read into */
size_t n; /* number of bytes to read */
Sfdisc_t* disc; /* discipline */
#endif
{
reg Union_t* un;
reg ssize_t r, m;
un = (Union_t*)disc;
m = n;
f = un->f[un->c].f;
while(1)
{ if((r = sfread(f,buf,m)) < 0 || (r == 0 && un->c == un->n-1) )
break;
m -= r;
un->here += r;
if(m == 0)
break;
buf = (char*)buf + r;
if(sfeof(f) && un->c < un->n-1)
f = un->f[un->c += 1].f;
}
return n-m;
}
#if __STD_C
static Sfoff_t unseek(Sfio_t* f, Sfoff_t addr, int type, Sfdisc_t* disc)
#else
static Sfoff_t unseek(f, addr, type, disc)
Sfio_t* f;
Sfoff_t addr;
int type;
Sfdisc_t* disc;
#endif
{
reg Union_t* un;
reg int i;
reg Sfoff_t extent, s;
un = (Union_t*)disc;
if(un->type&UNSEEKABLE)
return -1L;
if(type == 2)
{ extent = 0;
for(i = 0; i < un->n; ++i)
extent += (sfsize(un->f[i].f) - un->f[i].lower);
addr += extent;
}
else if(type == 1)
addr += un->here;
if(addr < 0)
return -1;
/* find the stream where the addr could be in */
extent = 0;
for(i = 0; i < un->n-1; ++i)
{ s = sfsize(un->f[i].f) - un->f[i].lower;
if(addr < extent + s)
break;
extent += s;
}
s = (addr-extent) + un->f[i].lower;
if(sfseek(un->f[i].f,s,0) != s)
return -1;
un->c = i;
un->here = addr;
for(i += 1; i < un->n; ++i)
sfseek(un->f[i].f,un->f[i].lower,0);
return addr;
}
/* on close, remove the discipline */
#if __STD_C
static int unexcept(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc)
#else
static int unexcept(f,type,data,disc)
Sfio_t* f;
int type;
Void_t* data;
Sfdisc_t* disc;
#endif
{
if(type == SF_FINAL || type == SF_DPOP)
free(disc);
return 0;
}
#if __STD_C
int sfdcunion(Sfio_t* f, Sfio_t** array, int n)
#else
int sfdcunion(f, array, n)
Sfio_t* f;
Sfio_t** array;
int n;
#endif
{
reg Union_t* un;
reg int i;
if(n <= 0)
return -1;
if(!(un = (Union_t*)malloc(sizeof(Union_t)+(n-1)*sizeof(File_t))) )
return -1;
memset(un, 0, sizeof(*un));
un->disc.readf = unread;
un->disc.writef = unwrite;
un->disc.seekf = unseek;
un->disc.exceptf = unexcept;
un->n = n;
for(i = 0; i < n; ++i)
{ un->f[i].f = array[i];
if(!(un->type&UNSEEKABLE))
{ un->f[i].lower = sfseek(array[i],(Sfoff_t)0,1);
if(un->f[i].lower < 0)
un->type |= UNSEEKABLE;
}
}
if(sfdisc(f,(Sfdisc_t*)un) != (Sfdisc_t*)un)
{ free(un);
return -1;
}
return 0;
}
|