5 |
|
* Sorting routines for meta-files |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "platform.h" /* [_]snprintf() */ |
9 |
+ |
#include "rtprocess.h" /* getpid() */ |
10 |
+ |
#include "rterror.h" |
11 |
+ |
#include "meta.h" |
12 |
|
|
13 |
+ |
|
14 |
|
#define PBSIZE 1000 /* max size of sort block */ |
15 |
|
/* maxalloc must be >= this */ |
16 |
|
|
12 |
– |
|
17 |
|
#define NFILES 4 /* number of sort files */ |
18 |
|
|
19 |
|
|
20 |
+ |
static void treemerge(int height, int nt, int nf, PLIST *pl, |
21 |
+ |
int (*pcmp)(), FILE *ofp); |
22 |
+ |
static void sendsort( PLIST *pl, int (*pcmp)()); |
23 |
+ |
static void order(PRIMITIVE *pa[], int n, PLIST *pl); |
24 |
+ |
static char * tfname( int lvl, int num); |
25 |
|
|
17 |
– |
#include "meta.h" |
18 |
– |
|
19 |
– |
|
20 |
– |
|
21 |
– |
|
22 |
– |
|
23 |
– |
|
24 |
– |
|
26 |
|
/* |
27 |
|
* This sort routine does a combination quicksort and |
28 |
|
* n-ary mergesort |
29 |
|
*/ |
30 |
|
|
31 |
|
|
32 |
< |
sort(infp, pcmp) /* sort primitives according to pcmp */ |
33 |
< |
|
34 |
< |
FILE *infp; |
35 |
< |
int (*pcmp)(); /* compares pointers to pointers to primitives! */ |
36 |
< |
|
32 |
> |
void |
33 |
> |
sort( /* sort primitives according to pcmp */ |
34 |
> |
FILE *infp, |
35 |
> |
int (*pcmp)() /* compares pointers to pointers to primitives! */ |
36 |
> |
) |
37 |
|
{ |
38 |
|
PRIMITIVE *prims[PBSIZE]; /* pointers to primitives */ |
39 |
|
PLIST primlist; /* our primitives list */ |
80 |
|
* 0 or 1 global commands. |
81 |
|
*/ |
82 |
|
|
83 |
< |
mergesort(fi, nf, pl, pcmp, ofp) /* merge sorted files with list */ |
83 |
> |
void |
84 |
> |
pmergesort( /* merge sorted files with list */ |
85 |
|
|
86 |
< |
FILE *fi[]; /* array of input files */ |
87 |
< |
int nf; /* number of input files */ |
88 |
< |
PLIST *pl; /* sorted list */ |
89 |
< |
int (*pcmp)(); /* comparison function, takes primitive handles */ |
90 |
< |
FILE *ofp; /* output file */ |
91 |
< |
|
86 |
> |
FILE *fi[], /* array of input files */ |
87 |
> |
int nf, /* number of input files */ |
88 |
> |
PLIST *pl, /* sorted list */ |
89 |
> |
int (*pcmp)(), /* comparison function, takes primitive handles */ |
90 |
> |
FILE *ofp /* output file */ |
91 |
> |
) |
92 |
|
{ |
93 |
|
PRIMITIVE *plp; /* position in list */ |
94 |
|
PRIMITIVE *pp[NFILES]; /* input primitives */ |
95 |
< |
int minf; |
95 |
> |
int minf = 0; |
96 |
|
PRIMITIVE *minp; |
97 |
|
register int i; |
98 |
|
|
103 |
|
|
104 |
|
for (i = 0; i < nf; i++) { /* initialize input files */ |
105 |
|
if ((pp[i] = palloc()) == NULL) |
106 |
< |
error(SYSTEM, "memory exhausted in mergesort"); |
106 |
> |
error(SYSTEM, "memory exhausted in pmergesort"); |
107 |
|
readp(pp[i], fi[i]); |
108 |
|
} |
109 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
< |
static |
149 |
< |
treemerge(height, nt, nf, pl, pcmp, ofp) /* merge into one file */ |
148 |
> |
static void |
149 |
> |
treemerge( /* merge into one file */ |
150 |
|
|
151 |
< |
int height, nt, nf; |
152 |
< |
PLIST *pl; |
153 |
< |
int (*pcmp)(); |
154 |
< |
FILE *ofp; |
155 |
< |
|
151 |
> |
int height, int nt, int nf, |
152 |
> |
PLIST *pl, |
153 |
> |
int (*pcmp)(), |
154 |
> |
FILE *ofp |
155 |
> |
) |
156 |
|
{ |
155 |
– |
char *tfname(); |
157 |
|
FILE *fi[NFILES], *fp; |
158 |
|
int i; |
159 |
|
|
165 |
|
if ((fp = ofp) == NULL) |
166 |
|
fp = efopen(tfname(height + 1, nt), "w"); |
167 |
|
|
168 |
< |
mergesort(fi, nf, pl, pcmp, fp); |
168 |
> |
pmergesort(fi, nf, pl, pcmp, fp); |
169 |
|
|
170 |
|
for (i = 0; i < nf; i++) { |
171 |
|
fclose(fi[i]); |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
< |
static |
196 |
< |
sendsort(pl, pcmp) /* send a sorted list */ |
195 |
> |
static void |
196 |
> |
sendsort( /* send a sorted list */ |
197 |
|
|
198 |
< |
PLIST *pl; |
199 |
< |
int (*pcmp)(); |
200 |
< |
|
198 |
> |
PLIST *pl, |
199 |
> |
int (*pcmp)() |
200 |
> |
) |
201 |
|
{ |
202 |
|
static int nf = 0, |
203 |
|
intree = FALSE; |
203 |
– |
FILE *fp; |
204 |
|
|
205 |
|
if (isglob(pl->pbot->com)) { |
206 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
+ |
static void |
251 |
+ |
order( /* order the first n array primitives into list */ |
252 |
|
|
253 |
< |
static |
254 |
< |
order(pa, n, pl) /* order the first n array primitives into list */ |
255 |
< |
|
256 |
< |
PRIMITIVE *pa[]; |
255 |
< |
int n; |
256 |
< |
PLIST *pl; |
257 |
< |
|
253 |
> |
PRIMITIVE *pa[], |
254 |
> |
int n, |
255 |
> |
PLIST *pl |
256 |
> |
) |
257 |
|
{ |
258 |
|
register int i; |
259 |
|
|
270 |
|
|
271 |
|
|
272 |
|
static char * |
273 |
< |
tfname(lvl, num) /* create temporary file name */ |
273 |
> |
tfname( /* create temporary file name */ |
274 |
|
|
275 |
< |
int lvl, num; |
276 |
< |
|
275 |
> |
int lvl, int num |
276 |
> |
) |
277 |
|
{ |
278 |
< |
static char fnbuf[32]; |
278 |
> |
static char pathbuf[PATH_MAX]; |
279 |
> |
static char fnbuf[PATH_MAX]; |
280 |
> |
static size_t psiz; |
281 |
|
|
282 |
< |
sprintf(fnbuf, "%sS%d%c%d", TDIR, getpid(), lvl+'A', num); |
282 |
> |
if (pathbuf[0] == '\0') { /* first time */ |
283 |
> |
temp_directory(pathbuf, sizeof(pathbuf)); |
284 |
> |
psiz = strlen(pathbuf); |
285 |
> |
} |
286 |
> |
snprintf(fnbuf, sizeof(pathbuf)-psiz, |
287 |
> |
"%s/S%d%c%d", pathbuf, getpid(), lvl+'A', num); |
288 |
> |
/*sprintf(fnbuf, "%sS%d%c%d", TDIR, getpid(), lvl+'A', num);*/ |
289 |
|
|
290 |
|
return(fnbuf); |
291 |
|
} |