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

Annotation of src/usr.bin/sort/append.c, Revision 1.9

1.9     ! deraadt     1: /*     $OpenBSD: append.c,v 1.8 2003/06/03 02:56:16 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.
1.8       millert    18:  * 3. Neither the name of the University nor the names of its contributors
1.1       millert    19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: #ifndef lint
                     36: #if 0
                     37: static char sccsid[] = "@(#)append.c   8.1 (Berkeley) 6/6/93";
                     38: #else
1.9     ! deraadt    39: static char rcsid[] = "$OpenBSD: append.c,v 1.8 2003/06/03 02:56:16 millert Exp $";
1.1       millert    40: #endif
                     41: #endif /* not lint */
                     42:
                     43: #include "sort.h"
                     44:
                     45: #include <stdlib.h>
                     46: #include <string.h>
                     47:
                     48: #define OUTPUT {                                                       \
                     49:        if ((n = cpos - ppos) > 1) {                                    \
                     50:                for (; ppos < cpos; ++ppos)                             \
                     51:                        *ppos -= odepth;                                \
                     52:                ppos -= n;                                              \
                     53:                radixsort((const u_char **)ppos, n, wts1, REC_D);       \
                     54:                for (; ppos < cpos; ppos++) {                           \
                     55:                        prec = (RECHEADER *) (*ppos - sizeof(TRECHEADER));\
                     56:                        put(prec, fp);                                  \
                     57:                }                                                       \
1.2       millert    58:        } else                                                          \
                     59:                put(prec, fp);                                          \
1.1       millert    60: }
                     61:
                     62: /*
                     63:  * copy sorted lines to output; check for uniqueness
                     64:  */
                     65: void
1.9     ! deraadt    66: append(u_char **keylist, int nelem, int depth, FILE *fp,
        !            67:     void (*put)(RECHEADER *, FILE *), struct field *ftbl)
1.1       millert    68: {
1.6       ericj      69:        u_char *wts, *wts1;
                     70:        int n, odepth;
                     71:        u_char **cpos, **ppos, **lastkey;
                     72:        u_char *cend, *pend, *start;
                     73:        RECHEADER *crec, *prec;
1.1       millert    74:
1.7       millert    75:        if (*keylist == NULL)
1.1       millert    76:                return;
                     77:        wts1 = wts = ftbl[0].weights;
                     78:        if ((!UNIQUE) && SINGL_FLD) {
                     79:                if (ftbl[0].flags & F && ftbl[0].flags & R)
                     80:                        wts1 = Rascii;
                     81:                else if (ftbl[0].flags & F)
                     82:                        wts1 = ascii;
                     83:                odepth = depth;
                     84:        }
                     85:        lastkey = keylist + nelem;
                     86:        depth += sizeof(TRECHEADER);
                     87:        if (SINGL_FLD && (UNIQUE || wts1 != wts)) {
                     88:                ppos = keylist;
                     89:                prec = (RECHEADER *) (*ppos - depth);
                     90:                if (UNIQUE)
                     91:                        put(prec, fp);
1.6       ericj      92:                for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
1.1       millert    93:                        crec = (RECHEADER *) (*cpos - depth);
                     94:                        if (crec->length  == prec->length) {
1.6       ericj      95:                                /*
                     96:                                 * Set pend and cend so that trailing NUL and
                     97:                                 * record separator is ignored.
                     98:                                 */
                     99:                                pend = (u_char *)&prec->data + prec->length - 2;
                    100:                                cend = (u_char *)&crec->data + crec->length - 2;
1.1       millert   101:                                for (start = *cpos; cend >= start; cend--) {
                    102:                                        if (wts[*cend] != wts[*pend])
                    103:                                                break;
                    104:                                        pend--;
                    105:                                }
                    106:                                if (pend + 1 != *ppos) {
1.2       millert   107:                                        if (!UNIQUE)
                    108:                                                OUTPUT
                    109:                                        else
1.1       millert   110:                                                put(crec, fp);
                    111:                                        ppos = cpos;
                    112:                                        prec = crec;
                    113:                                }
                    114:                        } else {
1.2       millert   115:                                if (!UNIQUE)
                    116:                                        OUTPUT
                    117:                                else
1.1       millert   118:                                        put(crec, fp);
                    119:                                ppos = cpos;
                    120:                                prec = crec;
                    121:                        }
                    122:                }
1.2       millert   123:                if (!UNIQUE)
1.4       mickey    124:                        OUTPUT
1.1       millert   125:        } else if (UNIQUE) {
                    126:                ppos = keylist;
                    127:                prec = (RECHEADER *) (*ppos - depth);
                    128:                put(prec, fp);
1.6       ericj     129:                for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
1.1       millert   130:                        crec = (RECHEADER *) (*cpos - depth);
                    131:                        if (crec->offset == prec->offset) {
1.6       ericj     132:                                /*
                    133:                                 * Set pend and cend so that trailing NUL and
                    134:                                 * record separator is ignored.
                    135:                                 */
                    136:                                pend = (u_char *)&prec->data + prec->offset - 2;
                    137:                                cend = (u_char *)&crec->data + crec->offset - 2;
1.1       millert   138:                                for (start = *cpos; cend >= start; cend--) {
                    139:                                        if (wts[*cend] != wts[*pend])
                    140:                                                break;
                    141:                                        pend--;
                    142:                                }
                    143:                                if (pend + 1 != *ppos) {
                    144:                                        ppos = cpos;
                    145:                                        prec = crec;
                    146:                                        put(prec, fp);
                    147:                                }
                    148:                        } else {
                    149:                                ppos = cpos;
                    150:                                prec = crec;
                    151:                                put(prec, fp);
                    152:                        }
                    153:                }
                    154:        } else for (cpos = keylist; cpos < lastkey; cpos++) {
                    155:                crec = (RECHEADER *) (*cpos - depth);
                    156:                put(crec, fp);
                    157:        }
                    158: }
                    159:
                    160: /*
                    161:  * output the already sorted eol bin.
                    162:  */
                    163: void
1.9     ! deraadt   164: rd_append(int binno, union f_handle infl0, int nfiles, FILE *outfp,
        !           165:     u_char *buffer, u_char *bufend)
1.1       millert   166: {
1.3       millert   167:        RECHEADER *rec;
1.2       millert   168:
1.1       millert   169:        rec = (RECHEADER *) buffer;
                    170:        if (!getnext(binno, infl0, nfiles, (RECHEADER *) buffer, bufend, 0)) {
                    171:                putline(rec, outfp);
                    172:                while (getnext(binno, infl0, nfiles, (RECHEADER *) buffer,
                    173:                        bufend, 0) == 0) {
                    174:                        if (!UNIQUE)
                    175:                                putline(rec, outfp);
                    176:                }
                    177:        }
                    178: }
                    179:
                    180: /*
                    181:  * append plain text--used after sorting the biggest bin.
                    182:  */
                    183: void
1.9     ! deraadt   184: concat(FILE *a, FILE *b)
1.1       millert   185: {
                    186:         int nread;
                    187:         char buffer[4096];
                    188:
                    189:        rewind(b);
                    190:         while ((nread = fread(buffer, 1, 4096, b)) > 0)
                    191:                 EWRITE(buffer, 1, nread, a);
                    192: }