int handle_error(int error_number, void* args) { switch (error_number) { case ERR_FOPEN: pe("Couldn't open file %s" ENDL, fargs(args)); exit(error_number); break; case ERR_MEM: pe("Couldn't allocate memory %s" ENDL, margs(args)); exit(error_number); break; case ERR_BWRITE: pe("Writing to the output file: %s" ENDL, fargs(args)); break; case ERR_BREAD: pe("Unable to read from the output file: %s" ENDL, fargs(args)); break; case ERR_PACKET_ADD: pe("An error occured when trying to reallocate memory for the " "hold %s" ENDL, margs(args)); exit(error_number); break; case ERR_PACKING: pe("An error occured while trying to pack the packet" ENDL); break; case ERR_CONNECTION: pe("An error occurred trying create the socket" ENDL); break; default: return 0; } return error_number; }
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 */ ) { PRIMITIVE *plp; /* position in list */ PRIMITIVE *pp[NFILES]; /* input primitives */ int minf = 0; PRIMITIVE *minp; register int i; if (pl == NULL) plp = NULL; /* initialize list */ else plp = pl->ptop; for (i = 0; i < nf; i++) { /* initialize input files */ if ((pp[i] = palloc()) == NULL) error(SYSTEM, "memory exhausted in pmergesort"); readp(pp[i], fi[i]); } for ( ; ; ) { if (plp != NULL && isprim(plp->com)) minp = plp; else minp = NULL; for (i = 0; i < nf; i++) if (isprim(pp[i]->com) && (minp == NULL || (*pcmp)(&pp[i], &minp) < 0)) minp = pp[minf=i]; if (minp == NULL) break; writep(minp, ofp); if (minp == plp) plp = plp->pnext; else { fargs(pp[minf]); readp(pp[minf], fi[minf]); } } if (plp != NULL && plp->com != PEOF) writep(plp, ofp); for (i = 0; i < nf; i++) { if (pp[i]->com != PEOF) writep(pp[i], ofp); pfree(pp[i]); } }