--- ray/src/meta/sort.c 2003/02/22 02:07:26 1.1 +++ ray/src/meta/sort.c 2003/06/16 14:54:54 1.5 @@ -1,38 +1,41 @@ #ifndef lint -static const char RCSid[] = "$Id: sort.c,v 1.1 2003/02/22 02:07:26 greg Exp $"; +static const char RCSid[] = "$Id: sort.c,v 1.5 2003/06/16 14:54:54 greg Exp $"; #endif /* * Sorting routines for meta-files */ +#include "paths.h" +#include "meta.h" +#ifdef _WIN32 + #include /* getpid() */ +#endif + + #define PBSIZE 1000 /* max size of sort block */ /* maxalloc must be >= this */ - #define NFILES 4 /* number of sort files */ +static void treemerge(int height, int nt, int nf, PLIST *pl, + int (*pcmp)(), FILE *ofp); +static void sendsort( PLIST *pl, int (*pcmp)()); +static void order(PRIMITIVE *pa[], int n, PLIST *pl); +static char * tfname( int lvl, int num); -#include "meta.h" - - - - - - - /* * This sort routine does a combination quicksort and * n-ary mergesort */ -sort(infp, pcmp) /* sort primitives according to pcmp */ - -FILE *infp; -int (*pcmp)(); /* compares pointers to pointers to primitives! */ - +void +sort( /* sort primitives according to pcmp */ +FILE *infp, +int (*pcmp)() /* compares pointers to pointers to primitives! */ +) { PRIMITIVE *prims[PBSIZE]; /* pointers to primitives */ PLIST primlist; /* our primitives list */ @@ -79,14 +82,15 @@ int (*pcmp)(); /* compares pointers to pointers to p * 0 or 1 global commands. */ -mergesort(fi, nf, pl, pcmp, ofp) /* merge sorted files with list */ +void +pmergesort( /* merge sorted files with list */ -FILE *fi[]; /* array of input files */ -int nf; /* number of input files */ -PLIST *pl; /* sorted list */ -int (*pcmp)(); /* comparison function, takes primitive handles */ -FILE *ofp; /* output file */ - +FILE *fi[], /* array of input files */ +int nf, /* number of input files */ +PLIST *pl, /* sorted list */ +int (*pcmp)(), /* comparison function, takes primitive handles */ +FILE *ofp /* output file */ +) { PRIMITIVE *plp; /* position in list */ PRIMITIVE *pp[NFILES]; /* input primitives */ @@ -101,7 +105,7 @@ FILE *ofp; /* output file */ for (i = 0; i < nf; i++) { /* initialize input files */ if ((pp[i] = palloc()) == NULL) - error(SYSTEM, "memory exhausted in mergesort"); + error(SYSTEM, "memory exhausted in pmergesort"); readp(pp[i], fi[i]); } @@ -143,16 +147,15 @@ FILE *ofp; /* output file */ -static -treemerge(height, nt, nf, pl, pcmp, ofp) /* merge into one file */ +static void +treemerge( /* merge into one file */ -int height, nt, nf; -PLIST *pl; -int (*pcmp)(); -FILE *ofp; - +int height, int nt, int nf, +PLIST *pl, +int (*pcmp)(), +FILE *ofp +) { - char *tfname(); FILE *fi[NFILES], *fp; int i; @@ -164,7 +167,7 @@ FILE *ofp; if ((fp = ofp) == NULL) fp = efopen(tfname(height + 1, nt), "w"); - mergesort(fi, nf, pl, pcmp, fp); + pmergesort(fi, nf, pl, pcmp, fp); for (i = 0; i < nf; i++) { fclose(fi[i]); @@ -191,12 +194,12 @@ FILE *ofp; -static -sendsort(pl, pcmp) /* send a sorted list */ +static void +sendsort( /* send a sorted list */ -PLIST *pl; -int (*pcmp)(); - +PLIST *pl, +int (*pcmp)() +) { static int nf = 0, intree = FALSE; @@ -247,14 +250,13 @@ int (*pcmp)(); +static void +order( /* order the first n array primitives into list */ -static -order(pa, n, pl) /* order the first n array primitives into list */ - -PRIMITIVE *pa[]; -int n; -PLIST *pl; - +PRIMITIVE *pa[], +int n, +PLIST *pl +) { register int i; @@ -271,14 +273,22 @@ PLIST *pl; static char * -tfname(lvl, num) /* create temporary file name */ +tfname( /* create temporary file name */ -int lvl, num; - +int lvl, int num +) { - static char fnbuf[32]; + static char pathbuf[PATH_MAX]; + static char fnbuf[PATH_MAX]; + static size_t psiz; - sprintf(fnbuf, "%sS%d%c%d", TDIR, getpid(), lvl+'A', num); + if (pathbuf[0] == '\0') { /* first time */ + temp_directory(pathbuf, sizeof(pathbuf)); + psiz = strlen(pathbuf); + } + snprintf(fnbuf, sizeof(pathbuf)-psiz, + "%s/S%d%c%d", pathbuf, getpid(), lvl+'A', num); + /*sprintf(fnbuf, "%sS%d%c%d", TDIR, getpid(), lvl+'A', num);*/ return(fnbuf); }