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

Annotation of src/usr.bin/join/join.c, Revision 1.26

1.26    ! deraadt     1: /* $OpenBSD: join.c,v 1.25 2015/07/21 04:42:59 jasper Exp $    */
1.3       deraadt     2:
1.1       deraadt     3: /*-
1.4       michaels    4:  * Copyright (c) 1991, 1993, 1994
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     6:  *
                      7:  * This code is derived from software contributed to Berkeley by
1.4       michaels    8:  * Steve Hayman of the Computer Science Department, Indiana University,
                      9:  * Michiro Hikida and David Goodenough.
1.1       deraadt    10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
1.15      millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
1.4       michaels   36: #include <err.h>
1.1       deraadt    37: #include <stdio.h>
                     38: #include <stdlib.h>
                     39: #include <string.h>
1.22      millert    40: #include <unistd.h>
1.1       deraadt    41:
1.24      deraadt    42: #define MAXIMUM(a, b)  (((a) > (b)) ? (a) : (b))
                     43:
1.1       deraadt    44: /*
                     45:  * There's a structure per input file which encapsulates the state of the
                     46:  * file.  We repeatedly read lines from each file until we've read in all
                     47:  * the consecutive lines from the file with a common join field.  Then we
                     48:  * compare the set of lines with an equivalent set from the other file.
                     49:  */
                     50: typedef struct {
1.7       michaels   51:        char *line;                     /* line */
1.4       michaels   52:        u_long linealloc;               /* line allocated count */
                     53:        char **fields;                  /* line field(s) */
                     54:        u_long fieldcnt;                /* line field(s) count */
1.7       michaels   55:        u_long fieldalloc;              /* line field(s) allocated count */
                     56:        u_long cfieldc;                 /* current field count */
1.4       michaels   57:        long     fpos;                  /* fpos of start of field */
1.1       deraadt    58: } LINE;
                     59:
                     60: typedef struct {
1.7       michaels   61:        FILE *fp;                       /* file descriptor */
1.4       michaels   62:        u_long joinf;                   /* join field (-1, -2, -j) */
1.7       michaels   63:        int unpair;                     /* output unpairable lines (-a) */
1.17      otto       64:        u_long number;                  /* 1 for file 1, 2 for file 2 */
1.7       michaels   65:        LINE *set;                      /* set of lines with same field */
1.4       michaels   66:        int pushbool;                   /* if pushback is set */
                     67:        u_long pushback;                /* line on the stack */
                     68:        u_long setcnt;                  /* set count */
                     69:        u_long setalloc;                /* set allocated count */
                     70:        u_long setusedc;                /* sets used  */
1.1       deraadt    71: } INPUT;
1.17      otto       72: INPUT input1 = { NULL, 0, 0, 1, NULL, 0, 0, 0, 0, 0 },
                     73:       input2 = { NULL, 0, 0, 2, NULL, 0, 0, 0, 0, 0 };
1.1       deraadt    74:
                     75: typedef struct {
1.4       michaels   76:        u_long  filenum;        /* file number */
1.1       deraadt    77:        u_long  fieldno;        /* field number */
                     78: } OLIST;
                     79: OLIST *olist;                  /* output field list */
                     80: u_long olistcnt;               /* output field list count */
                     81: u_long olistalloc;             /* output field allocated count */
                     82:
                     83: int joinout = 1;               /* show lines with matched join fields (-v) */
                     84: int needsep;                   /* need separator character */
                     85: int spans = 1;                 /* span multiple delimiters (-t) */
                     86: char *empty;                   /* empty field replacement string (-e) */
                     87: char *tabchar = " \t";         /* delimiter characters (-t) */
                     88:
1.14      millert    89: int  cmp(LINE *, u_long, LINE *, u_long);
                     90: void fieldarg(char *);
                     91: void joinlines(INPUT *, INPUT *);
                     92: void obsolete(char **);
                     93: void outfield(LINE *, u_long, int);
                     94: void outoneline(INPUT *, LINE *);
                     95: void outtwoline(INPUT *, LINE *, INPUT *, LINE *);
                     96: void slurp(INPUT *);
                     97: void slurpit(INPUT *);
                     98: void usage(void);
1.1       deraadt    99:
                    100: int
1.16      deraadt   101: main(int argc, char *argv[])
1.1       deraadt   102: {
1.4       michaels  103:        INPUT *F1, *F2;
1.1       deraadt   104:        int aflag, ch, cval, vflag;
                    105:        char *end;
                    106:
1.26    ! deraadt   107:        if (tame("stdio rpath", NULL) == -1)
        !           108:                err(1, "tame");
        !           109:
1.1       deraadt   110:        F1 = &input1;
                    111:        F2 = &input2;
                    112:
                    113:        aflag = vflag = 0;
                    114:        obsolete(argv);
1.6       millert   115:        while ((ch = getopt(argc, argv, "\01a:e:j:1:2:o:t:v:")) != -1) {
1.1       deraadt   116:                switch (ch) {
1.4       michaels  117:                case '\01':             /* See comment in obsolete(). */
1.1       deraadt   118:                        aflag = 1;
                    119:                        F1->unpair = F2->unpair = 1;
                    120:                        break;
                    121:                case '1':
                    122:                        if ((F1->joinf = strtol(optarg, &end, 10)) < 1)
1.4       michaels  123:                                errx(1, "-1 option field number less than 1");
1.1       deraadt   124:                        if (*end)
1.4       michaels  125:                                errx(1, "illegal field number -- %s", optarg);
1.1       deraadt   126:                        --F1->joinf;
                    127:                        break;
                    128:                case '2':
                    129:                        if ((F2->joinf = strtol(optarg, &end, 10)) < 1)
1.4       michaels  130:                                errx(1, "-2 option field number less than 1");
1.1       deraadt   131:                        if (*end)
1.4       michaels  132:                                errx(1, "illegal field number -- %s", optarg);
1.1       deraadt   133:                        --F2->joinf;
                    134:                        break;
                    135:                case 'a':
                    136:                        aflag = 1;
                    137:                        switch(strtol(optarg, &end, 10)) {
                    138:                        case 1:
                    139:                                F1->unpair = 1;
                    140:                                break;
                    141:                        case 2:
                    142:                                F2->unpair = 1;
                    143:                                break;
                    144:                        default:
1.4       michaels  145:                                errx(1, "-a option file number not 1 or 2");
1.1       deraadt   146:                                break;
                    147:                        }
                    148:                        if (*end)
1.4       michaels  149:                                errx(1, "illegal file number -- %s", optarg);
1.1       deraadt   150:                        break;
                    151:                case 'e':
                    152:                        empty = optarg;
                    153:                        break;
                    154:                case 'j':
1.4       michaels  155:                        if ((F1->joinf = F2->joinf = strtol(optarg, &end, 10)) < 1)
                    156:                                errx(1, "-j option field number less than 1");
1.1       deraadt   157:                        if (*end)
1.4       michaels  158:                                errx(1, "illegal field number -- %s", optarg);
1.1       deraadt   159:                        --F1->joinf;
                    160:                        --F2->joinf;
                    161:                        break;
                    162:                case 'o':
                    163:                        fieldarg(optarg);
                    164:                        break;
                    165:                case 't':
                    166:                        spans = 0;
                    167:                        if (strlen(tabchar = optarg) != 1)
1.4       michaels  168:                                errx(1, "illegal tab character specification");
1.1       deraadt   169:                        break;
                    170:                case 'v':
                    171:                        vflag = 1;
                    172:                        joinout = 0;
1.4       michaels  173:                        switch (strtol(optarg, &end, 10)) {
1.1       deraadt   174:                        case 1:
                    175:                                F1->unpair = 1;
                    176:                                break;
                    177:                        case 2:
                    178:                                F2->unpair = 1;
                    179:                                break;
                    180:                        default:
1.4       michaels  181:                                errx(1, "-v option file number not 1 or 2");
1.1       deraadt   182:                                break;
                    183:                        }
                    184:                        if (*end)
1.4       michaels  185:                                errx(1, "illegal file number -- %s", optarg);
1.1       deraadt   186:                        break;
                    187:                case '?':
                    188:                default:
                    189:                        usage();
                    190:                }
                    191:        }
                    192:        argc -= optind;
                    193:        argv += optind;
                    194:
                    195:        if (aflag && vflag)
1.4       michaels  196:                errx(1, "the -a and -v options are mutually exclusive");
1.1       deraadt   197:
                    198:        if (argc != 2)
                    199:                usage();
                    200:
                    201:        /* Open the files; "-" means stdin. */
                    202:        if (!strcmp(*argv, "-"))
                    203:                F1->fp = stdin;
                    204:        else if ((F1->fp = fopen(*argv, "r")) == NULL)
1.4       michaels  205:                err(1, "%s", *argv);
1.1       deraadt   206:        ++argv;
                    207:        if (!strcmp(*argv, "-"))
                    208:                F2->fp = stdin;
                    209:        else if ((F2->fp = fopen(*argv, "r")) == NULL)
1.4       michaels  210:                err(1, "%s", *argv);
1.1       deraadt   211:        if (F1->fp == stdin && F2->fp == stdin)
1.4       michaels  212:                errx(1, "only one input file may be stdin");
1.26    ! deraadt   213:
        !           214:        if (tame("stdio", NULL) == -1)
        !           215:                err(1, "tame");
1.1       deraadt   216:
1.4       michaels  217:        F1->setusedc = 0;
                    218:        F2->setusedc = 0;
1.1       deraadt   219:        slurp(F1);
                    220:        slurp(F2);
1.5       michaels  221:        F1->set->cfieldc = 0;
                    222:        F2->set->cfieldc = 0;
1.4       michaels  223:
1.5       michaels  224:        /*
                    225:         * We try to let the files have the same field value, advancing
                    226:         * whoever falls behind and always advancing the file(s) we output
                    227:         * from.
                    228:        */
1.1       deraadt   229:        while (F1->setcnt && F2->setcnt) {
                    230:                cval = cmp(F1->set, F1->joinf, F2->set, F2->joinf);
                    231:                if (cval == 0) {
                    232:                        /* Oh joy, oh rapture, oh beauty divine! */
                    233:                        if (joinout)
                    234:                                joinlines(F1, F2);
1.5       michaels  235:                        slurp(F1);
1.1       deraadt   236:                        slurp(F2);
1.4       michaels  237:                }
                    238:                else {
1.7       michaels  239:                        if (F1->unpair
                    240:                        && (cval < 0 || F2->set->cfieldc == F2->setusedc -1)) {
1.1       deraadt   241:                                joinlines(F1, NULL);
1.4       michaels  242:                                slurp(F1);
                    243:                        }
1.25      jasper    244:                        else if (cval < 0)
1.5       michaels  245:                                /* File 1 takes the lead... */
1.4       michaels  246:                                slurp(F1);
1.7       michaels  247:                        if (F2->unpair
                    248:                        && (cval > 0 || F1->set->cfieldc == F1->setusedc -1)) {
1.1       deraadt   249:                                joinlines(F2, NULL);
1.4       michaels  250:                                slurp(F2);
                    251:                        }
1.5       michaels  252:                        else if (cval > 0)
                    253:                                /* File 2 takes the lead... */
1.4       michaels  254:                                slurp(F2);
1.1       deraadt   255:                }
                    256:        }
                    257:
                    258:        /*
                    259:         * Now that one of the files is used up, optionally output any
                    260:         * remaining lines from the other file.
                    261:         */
                    262:        if (F1->unpair)
                    263:                while (F1->setcnt) {
                    264:                        joinlines(F1, NULL);
                    265:                        slurp(F1);
                    266:                }
                    267:        if (F2->unpair)
                    268:                while (F2->setcnt) {
                    269:                        joinlines(F2, NULL);
                    270:                        slurp(F2);
                    271:                }
1.5       michaels  272:
1.4       michaels  273:        return 0;
1.1       deraadt   274: }
                    275:
1.5       michaels  276: /* wrapper around slurpit() to keep track of what field we are on */
1.16      deraadt   277: void slurp(INPUT *F)
1.4       michaels  278: {
                    279:        long fpos;
                    280:        u_long cfieldc;
                    281:
                    282:        if (F->set == NULL) {
                    283:                fpos = 0;
1.25      jasper    284:                cfieldc = 0;
1.4       michaels  285:        }
                    286:        else {
                    287:                fpos = F->set->fpos;
                    288:                cfieldc = F->set->cfieldc;
                    289:        }
                    290:        slurpit(F);
                    291:        if (F->set == NULL)
                    292:                return;
                    293:        else if (fpos != F->set->fpos)
                    294:                F->set->cfieldc = cfieldc+1;
1.25      jasper    295: }
1.4       michaels  296:
1.1       deraadt   297: void
1.16      deraadt   298: slurpit(INPUT *F)
1.1       deraadt   299: {
1.4       michaels  300:        LINE *lp, *lastlp, tmp;
1.1       deraadt   301:        size_t len;
1.17      otto      302:        u_long cnt;
1.1       deraadt   303:        char *bp, *fieldp;
1.4       michaels  304:        long fpos;
1.1       deraadt   305:        /*
                    306:         * Read all of the lines from an input file that have the same
                    307:         * join field.
                    308:         */
1.4       michaels  309:
1.1       deraadt   310:        F->setcnt = 0;
1.4       michaels  311:        for (lastlp = NULL; ; ++F->setcnt, lastlp = lp) {
1.1       deraadt   312:                /*
                    313:                 * If we're out of space to hold line structures, allocate
                    314:                 * more.  Initialize the structure so that we know that this
                    315:                 * is new space.
                    316:                 */
                    317:                if (F->setcnt == F->setalloc) {
1.17      otto      318:                        LINE *p;
                    319:                        u_long newsize = F->setalloc + 50;
1.1       deraadt   320:                        cnt = F->setalloc;
1.23      doug      321:                        if ((p = reallocarray(F->set, newsize, sizeof(LINE)))
                    322:                            == NULL)
1.4       michaels  323:                                err(1, NULL);
1.17      otto      324:                        F->set = p;
                    325:                        F->setalloc = newsize;
1.4       michaels  326:                        memset(F->set + cnt, 0, 50 * sizeof(LINE));
                    327:                        /* re-set lastlp in case it moved */
                    328:                        if (lastlp != NULL)
                    329:                                lastlp = &F->set[F->setcnt - 1];
1.1       deraadt   330:                }
                    331:                /*
                    332:                 * Get any pushed back line, else get the next line.  Allocate
                    333:                 * space as necessary.  If taking the line from the stack swap
1.4       michaels  334:                 * the two structures so that we don't lose space allocated to
                    335:                 * either structure.  This could be avoided by doing another
                    336:                 * level of indirection, but it's probably okay as is.
1.1       deraadt   337:                 */
                    338:                lp = &F->set[F->setcnt];
1.4       michaels  339:                if (F->pushbool) {
1.1       deraadt   340:                        tmp = F->set[F->setcnt];
                    341:                        F->set[F->setcnt] = F->set[F->pushback];
                    342:                        F->set[F->pushback] = tmp;
1.4       michaels  343:                        F->pushbool = 0;
1.1       deraadt   344:                        continue;
                    345:                }
                    346:                if ((bp = fgetln(F->fp, &len)) == NULL)
                    347:                        return;
1.4       michaels  348:                /*
1.7       michaels  349:                 * we depend on knowing on what field we are, one safe way is
                    350:                 * the file position.
1.4       michaels  351:                */
                    352:                fpos = ftell(F->fp) - len;
1.1       deraadt   353:                if (lp->linealloc <= len + 1) {
1.17      otto      354:                        char *p;
                    355:                        u_long newsize = lp->linealloc +
1.24      deraadt   356:                            MAXIMUM(100, len + 1 - lp->linealloc);
1.17      otto      357:                        if ((p = realloc(lp->line, newsize)) == NULL)
1.4       michaels  358:                                err(1, NULL);
1.17      otto      359:                        lp->line = p;
                    360:                        lp->linealloc = newsize;
1.4       michaels  361:                }
                    362:                F->setusedc++;
                    363:                memmove(lp->line, bp, len);
                    364:                lp->fpos = fpos;
                    365:                /* Replace trailing newline, if it exists. */
1.1       deraadt   366:                if (bp[len - 1] == '\n')
                    367:                        lp->line[len - 1] = '\0';
                    368:                else
                    369:                        lp->line[len] = '\0';
                    370:                bp = lp->line;
                    371:
                    372:                /* Split the line into fields, allocate space as necessary. */
                    373:                lp->fieldcnt = 0;
                    374:                while ((fieldp = strsep(&bp, tabchar)) != NULL) {
                    375:                        if (spans && *fieldp == '\0')
                    376:                                continue;
                    377:                        if (lp->fieldcnt == lp->fieldalloc) {
1.17      otto      378:                                char **p;
                    379:                                u_long newsize = lp->fieldalloc + 50;
1.23      doug      380:                                if ((p = reallocarray(lp->fields, newsize,
                    381:                                    sizeof(char *))) == NULL)
1.4       michaels  382:                                        err(1, NULL);
1.17      otto      383:                                lp->fields = p;
                    384:                                lp->fieldalloc = newsize;
1.1       deraadt   385:                        }
                    386:                        lp->fields[lp->fieldcnt++] = fieldp;
                    387:                }
                    388:
                    389:                /* See if the join field value has changed. */
                    390:                if (lastlp != NULL && cmp(lp, F->joinf, lastlp, F->joinf)) {
1.4       michaels  391:                        F->pushbool = 1;
1.1       deraadt   392:                        F->pushback = F->setcnt;
                    393:                        break;
                    394:                }
                    395:        }
                    396: }
                    397:
                    398: int
1.16      deraadt   399: cmp(LINE *lp1, u_long fieldno1, LINE *lp2, u_long fieldno2)
1.1       deraadt   400: {
1.4       michaels  401:        if (lp1->fieldcnt <= fieldno1)
1.1       deraadt   402:                return (-1);
1.4       michaels  403:        else if (lp2->fieldcnt <= fieldno2)
                    404:                return (1);
1.1       deraadt   405:        return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2]));
                    406: }
                    407:
                    408: void
1.16      deraadt   409: joinlines(INPUT *F1, INPUT *F2)
1.1       deraadt   410: {
1.17      otto      411:        u_long cnt1, cnt2;
1.1       deraadt   412:
                    413:        /*
                    414:         * Output the results of a join comparison.  The output may be from
                    415:         * either file 1 or file 2 (in which case the first argument is the
                    416:         * file from which to output) or from both.
                    417:         */
                    418:        if (F2 == NULL) {
                    419:                for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
                    420:                        outoneline(F1, &F1->set[cnt1]);
                    421:                return;
                    422:        }
                    423:        for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
                    424:                for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2)
                    425:                        outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]);
                    426: }
                    427:
                    428: void
1.16      deraadt   429: outoneline(INPUT *F, LINE *lp)
1.1       deraadt   430: {
1.17      otto      431:        u_long cnt;
1.1       deraadt   432:
                    433:        /*
                    434:         * Output a single line from one of the files, according to the
                    435:         * join rules.  This happens when we are writing unmatched single
                    436:         * lines.  Output empty fields in the right places.
                    437:         */
                    438:        if (olist)
                    439:                for (cnt = 0; cnt < olistcnt; ++cnt) {
1.4       michaels  440:                        if (olist[cnt].filenum == F->number)
                    441:                                outfield(lp, olist[cnt].fieldno, 0);
1.17      otto      442:                        else if (olist[cnt].filenum == 0)
                    443:                                outfield(lp, F->joinf, 0);
1.4       michaels  444:                        else
                    445:                                outfield(lp, 0, 1);
1.1       deraadt   446:                }
1.18      otto      447:        else {
                    448:                /*
                    449:                 * Output the join field, then the remaining fields from F
                    450:                 */
                    451:                outfield(lp, F->joinf, 0);
1.1       deraadt   452:                for (cnt = 0; cnt < lp->fieldcnt; ++cnt)
1.18      otto      453:                        if (F->joinf != cnt)
                    454:                                outfield(lp, cnt, 0);
                    455:        }
                    456:
1.4       michaels  457:        putchar('\n');
1.1       deraadt   458:        if (ferror(stdout))
1.4       michaels  459:                err(1, "stdout");
1.1       deraadt   460:        needsep = 0;
                    461: }
                    462:
                    463: void
1.16      deraadt   464: outtwoline(INPUT *F1, LINE *lp1, INPUT *F2, LINE *lp2)
1.1       deraadt   465: {
1.17      otto      466:        u_long cnt;
1.1       deraadt   467:
                    468:        /* Output a pair of lines according to the join list (if any). */
1.11      deraadt   469:        if (olist) {
1.1       deraadt   470:                for (cnt = 0; cnt < olistcnt; ++cnt)
1.17      otto      471:                        if (olist[cnt].filenum == 0) {
                    472:                                if (lp1->fieldcnt >= F1->joinf)
                    473:                                        outfield(lp1, F1->joinf, 0);
                    474:                                else
                    475:                                        outfield(lp2, F2->joinf, 0);
                    476:                        } else if (olist[cnt].filenum == 1)
1.4       michaels  477:                                outfield(lp1, olist[cnt].fieldno, 0);
                    478:                        else /* if (olist[cnt].filenum == 2) */
                    479:                                outfield(lp2, olist[cnt].fieldno, 0);
1.11      deraadt   480:        } else {
1.1       deraadt   481:                /*
                    482:                 * Output the join field, then the remaining fields from F1
                    483:                 * and F2.
                    484:                 */
1.4       michaels  485:                outfield(lp1, F1->joinf, 0);
1.1       deraadt   486:                for (cnt = 0; cnt < lp1->fieldcnt; ++cnt)
                    487:                        if (F1->joinf != cnt)
1.4       michaels  488:                                outfield(lp1, cnt, 0);
1.1       deraadt   489:                for (cnt = 0; cnt < lp2->fieldcnt; ++cnt)
                    490:                        if (F2->joinf != cnt)
1.4       michaels  491:                                outfield(lp2, cnt, 0);
1.1       deraadt   492:        }
1.4       michaels  493:        putchar('\n');
1.1       deraadt   494:        if (ferror(stdout))
1.4       michaels  495:                err(1, "stdout");
1.1       deraadt   496:        needsep = 0;
                    497: }
                    498:
                    499: void
1.16      deraadt   500: outfield(LINE *lp, u_long fieldno, int out_empty)
1.1       deraadt   501: {
                    502:        if (needsep++)
1.4       michaels  503:                putchar((int)*tabchar);
1.11      deraadt   504:        if (!ferror(stdout)) {
1.12      aaron     505:                if (lp->fieldcnt <= fieldno || out_empty) {
1.1       deraadt   506:                        if (empty != NULL)
1.4       michaels  507:                                fputs(empty, stdout);
1.1       deraadt   508:                } else {
                    509:                        if (*lp->fields[fieldno] == '\0')
                    510:                                return;
1.4       michaels  511:                        fputs(lp->fields[fieldno], stdout);
1.1       deraadt   512:                }
1.11      deraadt   513:        }
1.1       deraadt   514:        if (ferror(stdout))
1.4       michaels  515:                err(1, "stdout");
1.1       deraadt   516: }
                    517:
                    518: /*
                    519:  * Convert an output list argument "2.1, 1.3, 2.4" into an array of output
                    520:  * fields.
                    521:  */
                    522: void
1.16      deraadt   523: fieldarg(char *option)
1.1       deraadt   524: {
1.17      otto      525:        u_long fieldno, filenum;
1.1       deraadt   526:        char *end, *token;
                    527:
1.4       michaels  528:        while ((token = strsep(&option, ", \t")) != NULL) {
1.1       deraadt   529:                if (*token == '\0')
                    530:                        continue;
1.17      otto      531:                if (token[0] == '0')
                    532:                        filenum = fieldno = 0;
                    533:                else if ((token[0] == '1' || token[0] == '2') &&
                    534:                    token[1] == '.') {
                    535:                        filenum = token[0] - '0';
                    536:                        fieldno = strtol(token + 2, &end, 10);
                    537:                        if (*end)
                    538:                                errx(1, "malformed -o option field");
                    539:                        if (fieldno == 0)
                    540:                                errx(1, "field numbers are 1 based");
                    541:                        --fieldno;
                    542:                } else
1.4       michaels  543:                        errx(1, "malformed -o option field");
1.1       deraadt   544:                if (olistcnt == olistalloc) {
1.17      otto      545:                        OLIST *p;
                    546:                        u_long newsize = olistalloc + 50;
1.23      doug      547:                        if ((p = reallocarray(olist, newsize, sizeof(OLIST)))
                    548:                            == NULL)
1.4       michaels  549:                                err(1, NULL);
1.17      otto      550:                        olist = p;
                    551:                        olistalloc = newsize;
1.1       deraadt   552:                }
1.17      otto      553:                olist[olistcnt].filenum = filenum;
                    554:                olist[olistcnt].fieldno = fieldno;
1.1       deraadt   555:                ++olistcnt;
                    556:        }
                    557: }
                    558:
                    559: void
1.16      deraadt   560: obsolete(char **argv)
1.1       deraadt   561: {
1.17      otto      562:        size_t len;
1.1       deraadt   563:        char **p, *ap, *t;
                    564:
1.4       michaels  565:        while ((ap = *++argv) != NULL) {
1.1       deraadt   566:                /* Return if "--". */
1.9       deraadt   567:                if (ap[0] == '-' && ap[1] == '-')
1.1       deraadt   568:                        return;
1.10      deraadt   569:                /* skip if not an option */
                    570:                if (ap[0] != '-')
                    571:                        continue;
1.1       deraadt   572:                switch (ap[1]) {
                    573:                case 'a':
1.25      jasper    574:                        /*
1.1       deraadt   575:                         * The original join allowed "-a", which meant the
                    576:                         * same as -a1 plus -a2.  POSIX 1003.2, Draft 11.2
                    577:                         * only specifies this as "-a 1" and "a -2", so we
                    578:                         * have to use another option flag, one that is
                    579:                         * unlikely to ever be used or accidentally entered
                    580:                         * on the command line.  (Well, we could reallocate
                    581:                         * the argv array, but that hardly seems worthwhile.)
                    582:                         */
1.17      otto      583:                        if (ap[2] == '\0' && (argv[1] == NULL ||
                    584:                            (strcmp(argv[1], "1") != 0 &&
                    585:                            strcmp(argv[1], "2") != 0))) {
1.1       deraadt   586:                                ap[1] = '\01';
1.17      otto      587:                                warnx("-a option used without an argument; "
                    588:                                    "reverting to historical behavior");
                    589:                        }
1.1       deraadt   590:                        break;
                    591:                case 'j':
                    592:                        /*
                    593:                         * The original join allowed "-j[12] arg" and "-j arg".
                    594:                         * Convert the former to "-[12] arg".  Don't convert
                    595:                         * the latter since getopt(3) can handle it.
                    596:                         */
                    597:                        switch(ap[2]) {
                    598:                        case '1':
                    599:                        case '2':
                    600:                                if (ap[3] != '\0')
                    601:                                        goto jbad;
1.19      millert   602:                                ap[1] = ap[2];
1.1       deraadt   603:                                ap[2] = '\0';
                    604:                                break;
                    605:                        case '\0':
                    606:                                break;
                    607:                        default:
1.19      millert   608: jbad:                          warnx("unknown option -- %s", ap + 1);
1.1       deraadt   609:                                usage();
                    610:                        }
                    611:                        break;
                    612:                case 'o':
                    613:                        /*
1.4       michaels  614:                         * The original join allowed "-o arg arg".
                    615:                         * Convert to "-o arg -o arg".
1.1       deraadt   616:                         */
1.19      millert   617:                        if (ap[2] != '\0' || argv[1] == NULL)
1.1       deraadt   618:                                break;
1.4       michaels  619:                        for (p = argv + 2; *p != NULL; ++p) {
1.17      otto      620:                                if (p[0][0] == '0' || ((p[0][0] != '1' &&
                    621:                                    p[0][0] != '2') || p[0][1] != '.'))
1.1       deraadt   622:                                        break;
                    623:                                len = strlen(*p);
                    624:                                if (len - 2 != strspn(*p + 2, "0123456789"))
                    625:                                        break;
                    626:                                if ((t = malloc(len + 3)) == NULL)
1.4       michaels  627:                                        err(1, NULL);
1.1       deraadt   628:                                t[0] = '-';
                    629:                                t[1] = 'o';
1.4       michaels  630:                                memmove(t + 2, *p, len + 1);
1.1       deraadt   631:                                *p = t;
                    632:                        }
                    633:                        argv = p - 1;
                    634:                        break;
                    635:                }
                    636:        }
                    637: }
                    638:
                    639: void
1.16      deraadt   640: usage(void)
1.1       deraadt   641: {
1.19      millert   642:        int len;
                    643:        extern char *__progname;
                    644:
                    645:        len = strlen(__progname) + sizeof("usage: ");
                    646:        (void)fprintf(stderr, "usage: %s [-1 field] [-2 field] "
                    647:            "[-a file_number | -v file_number] [-e string]\n"
1.20      jmc       648:            "%*s[-o list] [-t char] file1 file2\n",
1.19      millert   649:            __progname, len, "");
1.1       deraadt   650:        exit(1);
                    651: }