[BACK]Return to msort.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sort

Annotation of src/usr.bin/sort/msort.c, Revision 1.6

1.6     ! millert     1: /*     $OpenBSD: msort.c,v 1.5 1997/06/30 04:24:40 millert Exp $       */
1.1       millert     2:
                      3: /*-
                      4:  * Copyright (c) 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Peter McIlroy.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)msort.c    8.1 (Berkeley) 6/6/93";
                     42: #else
1.6     ! millert    43: static char rcsid[] = "$OpenBSD: msort.c,v 1.5 1997/06/30 04:24:40 millert Exp $";
1.1       millert    44: #endif
                     45: #endif /* not lint */
                     46:
                     47: #include "sort.h"
                     48: #include "fsort.h"
                     49:
                     50: #include <stdlib.h>
                     51: #include <string.h>
                     52: #include <unistd.h>
                     53:
                     54: /* Subroutines using comparisons: merge sort and check order */
                     55: #define DELETE (1)
1.5       millert    56: #define LALIGN(n) ((n+(sizeof(long)-1)) & ~(sizeof(long)-1))
1.1       millert    57:
                     58: typedef struct mfile {
                     59:        u_char *end;
                     60:        short flno;
1.6     ! millert    61:        RECHEADER rec[1];
1.1       millert    62: } MFILE;
                     63: typedef struct tmfile {
                     64:        u_char *end;
                     65:        short flno;
1.6     ! millert    66:        TRECHEADER rec[1];
1.1       millert    67: } TMFILE;
                     68: u_char *wts, *wts1 = 0;
                     69: struct mfile *cfilebuf;
                     70:
1.6     ! millert    71: static int cmp __P((RECHEADER *, RECHEADER *));
1.1       millert    72: static int insert __P((struct mfile **, struct mfile **, int, int));
                     73:
                     74: void
                     75: fmerge(binno, files, nfiles, get, outfp, fput, ftbl)
                     76:        union f_handle files;
                     77:        int binno, nfiles;
                     78:        int (*get)();
                     79:        FILE *outfp;
                     80:        void (*fput)();
                     81:        struct field *ftbl;
                     82: {
                     83:        FILE *tout;
                     84:        int i, j, last;
1.6     ! millert    85:        void (*put)(RECHEADER *, FILE *);
1.1       millert    86:        extern int geteasy();
                     87:        struct tempfile *l_fstack;
                     88:
                     89:        wts = ftbl->weights;
                     90:        if (!UNIQUE && SINGL_FLD && ftbl->flags & F)
                     91:                wts1 = (ftbl->flags & R) ? Rascii : ascii;
                     92:        if (!cfilebuf)
                     93:                cfilebuf = malloc(MAXLLEN + sizeof(TMFILE));
                     94:
                     95:        i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE));
                     96:        if (!buffer || i > BUFSIZE) {
                     97:                buffer = buffer ? realloc(buffer, i) : malloc(i);
                     98:                if (!buffer)
                     99:                        err(2, NULL);
                    100:                if (!SINGL_FLD)
                    101:                        linebuf = malloc(MAXLLEN);
                    102:        }
                    103:
                    104:        if (binno >= 0)
                    105:                l_fstack = fstack + files.top;
                    106:        else
                    107:                l_fstack = fstack;
1.4       millert   108:
1.1       millert   109:        while (nfiles) {
                    110:                put = putrec;
                    111:                for (j = 0; j < nfiles; j += 16) {
                    112:                        if (nfiles <= 16) {
                    113:                                tout = outfp;
                    114:                                put = fput;
                    115:                        }
                    116:                        else
                    117:                                tout = ftmp();
                    118:                        last = min(16, nfiles - j);
                    119:                        if (binno < 0) {
                    120:                                for (i = 0; i < last; i++)
                    121:                                        if (!(l_fstack[i+MAXFCT-1-16].fp =
                    122:                                            fopen(files.names[j + i], "r")))
1.3       millert   123:                                                err(2, files.names[j+i]);
1.1       millert   124:                                merge(MAXFCT-1-16, last, get, tout, put, ftbl);
1.4       millert   125:                        } else {
1.1       millert   126:                                for (i = 0; i< last; i++)
                    127:                                        rewind(l_fstack[i+j].fp);
                    128:                                merge(files.top+j, last, get, tout, put, ftbl);
                    129:                        }
1.4       millert   130:                        if (nfiles > 16)
                    131:                                l_fstack[j/16].fp = tout;
1.1       millert   132:                }
                    133:                nfiles = (nfiles + 15) / 16;
                    134:                if (nfiles == 1)
                    135:                        nfiles = 0;
                    136:                if (binno < 0) {
                    137:                        binno = 0;
                    138:                        get = geteasy;
                    139:                        files.top = 0;
                    140:                }
                    141:        }
                    142: }
                    143:
                    144: void
                    145: merge(infl0, nfiles, get, outfp, put, ftbl)
                    146:        int infl0, nfiles;
                    147:        int (*get)();
1.6     ! millert   148:        void (*put)(RECHEADER *, FILE *);
1.1       millert   149:        FILE *outfp;
                    150:        struct field *ftbl;
                    151: {
                    152:        int c, i, j;
                    153:        union f_handle dummy = {0};
                    154:        struct mfile *flist[16], *cfile;
1.2       millert   155:
1.1       millert   156:        for (i = j = 0; i < nfiles; i++) {
                    157:                cfile = (MFILE *) (buffer +
                    158:                    i * LALIGN(MAXLLEN + sizeof(TMFILE)));
                    159:                cfile->flno = j + infl0;
                    160:                cfile->end = cfile->rec->data + MAXLLEN;
                    161:                for (c = 1; c == 1;) {
                    162:                        if (EOF == (c = get(j+infl0, dummy, nfiles,
                    163:                           cfile->rec, cfile->end, ftbl))) {
1.4       millert   164:                                i--;
                    165:                                nfiles--;
1.1       millert   166:                                break;
                    167:                        }
                    168:                        if (i)
                    169:                                c = insert(flist, &cfile, i, !DELETE);
                    170:                        else
                    171:                                flist[0] = cfile;
                    172:                }
                    173:                j++;
                    174:        }
1.2       millert   175:        if (nfiles > 0) {
                    176:                cfile = cfilebuf;
                    177:                cfile->flno = flist[0]->flno;
                    178:                cfile->end = cfile->rec->data + MAXLLEN;
                    179:                while (nfiles) {
                    180:                        for (c = 1; c == 1;) {
                    181:                                if (EOF == (c = get(cfile->flno, dummy, nfiles,
                    182:                                   cfile->rec, cfile->end, ftbl))) {
                    183:                                        put(flist[0]->rec, outfp);
                    184:                                        memmove(flist, flist + 1,
                    185:                                            sizeof(MFILE *) * (--nfiles));
                    186:                                        cfile->flno = flist[0]->flno;
                    187:                                        break;
                    188:                                }
                    189:                                if (!(c = insert(flist, &cfile, nfiles, DELETE)))
                    190:                                        put(cfile->rec, outfp);
1.1       millert   191:                        }
1.2       millert   192:                }
1.1       millert   193:        }
                    194: }
                    195:
                    196: /*
                    197:  * if delete: inserts *rec in flist, deletes flist[0], and leaves it in *rec;
                    198:  * otherwise just inserts *rec in flist.
1.4       millert   199:  */
1.1       millert   200: static int
                    201: insert(flist, rec, ttop, delete)
                    202:        struct mfile **flist, **rec;
                    203:        int delete, ttop;                       /* delete = 0 or 1 */
                    204: {
                    205:        register struct mfile *tmprec;
                    206:        register int top, mid, bot = 0, cmpv = 1;
                    207:        tmprec = *rec;
                    208:        top = ttop;
                    209:        for (mid = top/2; bot +1 != top; mid = (bot+top)/2) {
                    210:                cmpv = cmp(tmprec->rec, flist[mid]->rec);
                    211:                if (cmpv < 0)
                    212:                        top = mid;
                    213:                else if (cmpv > 0)
                    214:                        bot = mid;
                    215:                else {
                    216:                        if (!UNIQUE)
                    217:                                bot = mid - 1;
                    218:                        break;
                    219:                }
                    220:        }
                    221:        if (delete) {
                    222:                if (UNIQUE) {
                    223:                        if (!bot && cmpv)
                    224:                                cmpv = cmp(tmprec->rec, flist[0]->rec);
                    225:                        if (!cmpv)
1.4       millert   226:                                return (1);
1.1       millert   227:                }
                    228:                tmprec = flist[0];
                    229:                if (bot)
                    230:                        memmove(flist, flist+1, bot * sizeof(MFILE **));
                    231:                flist[bot] = *rec;
                    232:                *rec = tmprec;
                    233:                (*rec)->flno = (*flist)->flno;
                    234:                return (0);
                    235:        }
                    236:        else {
                    237:                if (!bot && !(UNIQUE && !cmpv)) {
                    238:                        cmpv = cmp(tmprec->rec, flist[0]->rec);
                    239:                        if (cmpv < 0)
                    240:                                bot = -1;
                    241:                }
                    242:                if (UNIQUE && !cmpv)
                    243:                        return (1);
                    244:                bot++;
                    245:                memmove(flist + bot+1, flist + bot,
                    246:                    (ttop - bot) * sizeof(MFILE **));
                    247:                flist[bot] = *rec;
                    248:                return (0);
                    249:        }
                    250: }
                    251:
                    252: /*
                    253:  * check order on one file
                    254:  */
                    255: void
                    256: order(infile, get, ftbl)
                    257:        union f_handle infile;
                    258:        int (*get)();
                    259:        struct field *ftbl;
                    260: {
                    261:        u_char *end;
                    262:        int c;
1.6     ! millert   263:        RECHEADER *crec, *prec, *trec;
1.1       millert   264:
                    265:        if (!SINGL_FLD)
                    266:                linebuf = malloc(MAXLLEN);
                    267:        buffer = malloc(2 * (MAXLLEN + sizeof(TRECHEADER)));
                    268:        end = buffer + 2 * (MAXLLEN + sizeof(TRECHEADER));
                    269:        crec = (RECHEADER *) buffer;
                    270:        prec = (RECHEADER *) (buffer + MAXLLEN + sizeof(TRECHEADER));
                    271:        wts = ftbl->weights;
                    272:        if (SINGL_FLD && ftbl->flags & F)
                    273:                wts1 = ftbl->flags & R ? Rascii : ascii;
                    274:        else
                    275:                wts1 = 0;
1.4       millert   276:        if (get(-1, infile, 1, prec, end, ftbl) == 0)
                    277:                while (0 == get(-1, infile, 1, crec, end, ftbl)) {
                    278:                        if (0 < (c = cmp(prec, crec))) {
                    279:                                crec->data[crec->length-1] = 0;
                    280:                                errx(1, "found disorder: %s",
                    281:                                    crec->data+crec->offset);
                    282:                        }
                    283:                        if (UNIQUE && !c) {
                    284:                                crec->data[crec->length-1] = 0;
                    285:                                errx(1, "found non-uniqueness: %s",
                    286:                                    crec->data+crec->offset);
                    287:                        }
                    288:                        trec = prec;
                    289:                        prec = crec;
                    290:                        crec = trec;
1.1       millert   291:                }
                    292:        exit(0);
                    293: }
                    294:
                    295: static int
                    296: cmp(rec1, rec2)
1.6     ! millert   297:        RECHEADER *rec1, *rec2;
1.1       millert   298: {
                    299:        register r;
                    300:        register u_char *pos1, *pos2, *end;
                    301:        register u_char *cwts;
                    302:        for (cwts = wts; cwts; cwts = (cwts == wts1 ? 0 : wts1)) {
                    303:                pos1 = rec1->data;
                    304:                pos2 = rec2->data;
                    305:                if (!SINGL_FLD && UNIQUE)
                    306:                        end = pos1 + min(rec1->offset, rec2->offset);
                    307:                else
                    308:                        end = pos1 + min(rec1->length, rec2->length);
                    309:                for (; pos1 < end; ) {
                    310:                        if ((r = cwts[*pos1++] - cwts[*pos2++]))
                    311:                                return (r);
                    312:                }
                    313:        }
                    314:        return (0);
                    315: }