Annotation of src/usr.bin/join/join.c, Revision 1.22
1.22 ! millert 1: /* $OpenBSD: join.c,v 1.21 2009/10/27 23:59:39 deraadt 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 <sys/param.h>
37:
38: #include <err.h>
1.1 deraadt 39: #include <stdio.h>
40: #include <stdlib.h>
41: #include <string.h>
1.22 ! millert 42: #include <unistd.h>
1.1 deraadt 43:
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:
107: F1 = &input1;
108: F2 = &input2;
109:
110: aflag = vflag = 0;
111: obsolete(argv);
1.6 millert 112: while ((ch = getopt(argc, argv, "\01a:e:j:1:2:o:t:v:")) != -1) {
1.1 deraadt 113: switch (ch) {
1.4 michaels 114: case '\01': /* See comment in obsolete(). */
1.1 deraadt 115: aflag = 1;
116: F1->unpair = F2->unpair = 1;
117: break;
118: case '1':
119: if ((F1->joinf = strtol(optarg, &end, 10)) < 1)
1.4 michaels 120: errx(1, "-1 option field number less than 1");
1.1 deraadt 121: if (*end)
1.4 michaels 122: errx(1, "illegal field number -- %s", optarg);
1.1 deraadt 123: --F1->joinf;
124: break;
125: case '2':
126: if ((F2->joinf = strtol(optarg, &end, 10)) < 1)
1.4 michaels 127: errx(1, "-2 option field number less than 1");
1.1 deraadt 128: if (*end)
1.4 michaels 129: errx(1, "illegal field number -- %s", optarg);
1.1 deraadt 130: --F2->joinf;
131: break;
132: case 'a':
133: aflag = 1;
134: switch(strtol(optarg, &end, 10)) {
135: case 1:
136: F1->unpair = 1;
137: break;
138: case 2:
139: F2->unpair = 1;
140: break;
141: default:
1.4 michaels 142: errx(1, "-a option file number not 1 or 2");
1.1 deraadt 143: break;
144: }
145: if (*end)
1.4 michaels 146: errx(1, "illegal file number -- %s", optarg);
1.1 deraadt 147: break;
148: case 'e':
149: empty = optarg;
150: break;
151: case 'j':
1.4 michaels 152: if ((F1->joinf = F2->joinf = strtol(optarg, &end, 10)) < 1)
153: errx(1, "-j option field number less than 1");
1.1 deraadt 154: if (*end)
1.4 michaels 155: errx(1, "illegal field number -- %s", optarg);
1.1 deraadt 156: --F1->joinf;
157: --F2->joinf;
158: break;
159: case 'o':
160: fieldarg(optarg);
161: break;
162: case 't':
163: spans = 0;
164: if (strlen(tabchar = optarg) != 1)
1.4 michaels 165: errx(1, "illegal tab character specification");
1.1 deraadt 166: break;
167: case 'v':
168: vflag = 1;
169: joinout = 0;
1.4 michaels 170: switch (strtol(optarg, &end, 10)) {
1.1 deraadt 171: case 1:
172: F1->unpair = 1;
173: break;
174: case 2:
175: F2->unpair = 1;
176: break;
177: default:
1.4 michaels 178: errx(1, "-v option file number not 1 or 2");
1.1 deraadt 179: break;
180: }
181: if (*end)
1.4 michaels 182: errx(1, "illegal file number -- %s", optarg);
1.1 deraadt 183: break;
184: case '?':
185: default:
186: usage();
187: }
188: }
189: argc -= optind;
190: argv += optind;
191:
192: if (aflag && vflag)
1.4 michaels 193: errx(1, "the -a and -v options are mutually exclusive");
1.1 deraadt 194:
195: if (argc != 2)
196: usage();
197:
198: /* Open the files; "-" means stdin. */
199: if (!strcmp(*argv, "-"))
200: F1->fp = stdin;
201: else if ((F1->fp = fopen(*argv, "r")) == NULL)
1.4 michaels 202: err(1, "%s", *argv);
1.1 deraadt 203: ++argv;
204: if (!strcmp(*argv, "-"))
205: F2->fp = stdin;
206: else if ((F2->fp = fopen(*argv, "r")) == NULL)
1.4 michaels 207: err(1, "%s", *argv);
1.1 deraadt 208: if (F1->fp == stdin && F2->fp == stdin)
1.4 michaels 209: errx(1, "only one input file may be stdin");
1.1 deraadt 210:
1.4 michaels 211: F1->setusedc = 0;
212: F2->setusedc = 0;
1.1 deraadt 213: slurp(F1);
214: slurp(F2);
1.5 michaels 215: F1->set->cfieldc = 0;
216: F2->set->cfieldc = 0;
1.4 michaels 217:
1.5 michaels 218: /*
219: * We try to let the files have the same field value, advancing
220: * whoever falls behind and always advancing the file(s) we output
221: * from.
222: */
1.1 deraadt 223: while (F1->setcnt && F2->setcnt) {
224: cval = cmp(F1->set, F1->joinf, F2->set, F2->joinf);
225: if (cval == 0) {
226: /* Oh joy, oh rapture, oh beauty divine! */
227: if (joinout)
228: joinlines(F1, F2);
1.5 michaels 229: slurp(F1);
1.1 deraadt 230: slurp(F2);
1.4 michaels 231: }
232: else {
1.7 michaels 233: if (F1->unpair
234: && (cval < 0 || F2->set->cfieldc == F2->setusedc -1)) {
1.1 deraadt 235: joinlines(F1, NULL);
1.4 michaels 236: slurp(F1);
237: }
238: else if (cval < 0)
1.5 michaels 239: /* File 1 takes the lead... */
1.4 michaels 240: slurp(F1);
1.7 michaels 241: if (F2->unpair
242: && (cval > 0 || F1->set->cfieldc == F1->setusedc -1)) {
1.1 deraadt 243: joinlines(F2, NULL);
1.4 michaels 244: slurp(F2);
245: }
1.5 michaels 246: else if (cval > 0)
247: /* File 2 takes the lead... */
1.4 michaels 248: slurp(F2);
1.1 deraadt 249: }
250: }
251:
252: /*
253: * Now that one of the files is used up, optionally output any
254: * remaining lines from the other file.
255: */
256: if (F1->unpair)
257: while (F1->setcnt) {
258: joinlines(F1, NULL);
259: slurp(F1);
260: }
261: if (F2->unpair)
262: while (F2->setcnt) {
263: joinlines(F2, NULL);
264: slurp(F2);
265: }
1.5 michaels 266:
1.4 michaels 267: return 0;
1.1 deraadt 268: }
269:
1.5 michaels 270: /* wrapper around slurpit() to keep track of what field we are on */
1.16 deraadt 271: void slurp(INPUT *F)
1.4 michaels 272: {
273: long fpos;
274: u_long cfieldc;
275:
276: if (F->set == NULL) {
277: fpos = 0;
278: cfieldc = 0;
279: }
280: else {
281: fpos = F->set->fpos;
282: cfieldc = F->set->cfieldc;
283: }
284: slurpit(F);
285: if (F->set == NULL)
286: return;
287: else if (fpos != F->set->fpos)
288: F->set->cfieldc = cfieldc+1;
289: }
290:
1.1 deraadt 291: void
1.16 deraadt 292: slurpit(INPUT *F)
1.1 deraadt 293: {
1.4 michaels 294: LINE *lp, *lastlp, tmp;
1.1 deraadt 295: size_t len;
1.17 otto 296: u_long cnt;
1.1 deraadt 297: char *bp, *fieldp;
1.4 michaels 298: long fpos;
1.1 deraadt 299: /*
300: * Read all of the lines from an input file that have the same
301: * join field.
302: */
1.4 michaels 303:
1.1 deraadt 304: F->setcnt = 0;
1.4 michaels 305: for (lastlp = NULL; ; ++F->setcnt, lastlp = lp) {
1.1 deraadt 306: /*
307: * If we're out of space to hold line structures, allocate
308: * more. Initialize the structure so that we know that this
309: * is new space.
310: */
311: if (F->setcnt == F->setalloc) {
1.17 otto 312: LINE *p;
313: u_long newsize = F->setalloc + 50;
1.1 deraadt 314: cnt = F->setalloc;
1.17 otto 315: if ((p = realloc(F->set,
316: newsize * sizeof(LINE))) == NULL)
1.4 michaels 317: err(1, NULL);
1.17 otto 318: F->set = p;
319: F->setalloc = newsize;
1.4 michaels 320: memset(F->set + cnt, 0, 50 * sizeof(LINE));
321: /* re-set lastlp in case it moved */
322: if (lastlp != NULL)
323: lastlp = &F->set[F->setcnt - 1];
1.1 deraadt 324: }
325: /*
326: * Get any pushed back line, else get the next line. Allocate
327: * space as necessary. If taking the line from the stack swap
1.4 michaels 328: * the two structures so that we don't lose space allocated to
329: * either structure. This could be avoided by doing another
330: * level of indirection, but it's probably okay as is.
1.1 deraadt 331: */
332: lp = &F->set[F->setcnt];
1.4 michaels 333: if (F->pushbool) {
1.1 deraadt 334: tmp = F->set[F->setcnt];
335: F->set[F->setcnt] = F->set[F->pushback];
336: F->set[F->pushback] = tmp;
1.4 michaels 337: F->pushbool = 0;
1.1 deraadt 338: continue;
339: }
340: if ((bp = fgetln(F->fp, &len)) == NULL)
341: return;
1.4 michaels 342: /*
1.7 michaels 343: * we depend on knowing on what field we are, one safe way is
344: * the file position.
1.4 michaels 345: */
346: fpos = ftell(F->fp) - len;
1.1 deraadt 347: if (lp->linealloc <= len + 1) {
1.17 otto 348: char *p;
349: u_long newsize = lp->linealloc +
350: MAX(100, len + 1 - lp->linealloc);
351: if ((p = realloc(lp->line, newsize)) == NULL)
1.4 michaels 352: err(1, NULL);
1.17 otto 353: lp->line = p;
354: lp->linealloc = newsize;
1.4 michaels 355: }
356: F->setusedc++;
357: memmove(lp->line, bp, len);
358: lp->fpos = fpos;
359: /* Replace trailing newline, if it exists. */
1.1 deraadt 360: if (bp[len - 1] == '\n')
361: lp->line[len - 1] = '\0';
362: else
363: lp->line[len] = '\0';
364: bp = lp->line;
365:
366: /* Split the line into fields, allocate space as necessary. */
367: lp->fieldcnt = 0;
368: while ((fieldp = strsep(&bp, tabchar)) != NULL) {
369: if (spans && *fieldp == '\0')
370: continue;
371: if (lp->fieldcnt == lp->fieldalloc) {
1.17 otto 372: char **p;
373: u_long newsize = lp->fieldalloc + 50;
374: if ((p = realloc(lp->fields,
375: newsize * sizeof(char *))) == NULL)
1.4 michaels 376: err(1, NULL);
1.17 otto 377: lp->fields = p;
378: lp->fieldalloc = newsize;
1.1 deraadt 379: }
380: lp->fields[lp->fieldcnt++] = fieldp;
381: }
382:
383: /* See if the join field value has changed. */
384: if (lastlp != NULL && cmp(lp, F->joinf, lastlp, F->joinf)) {
1.4 michaels 385: F->pushbool = 1;
1.1 deraadt 386: F->pushback = F->setcnt;
387: break;
388: }
389: }
390: }
391:
392: int
1.16 deraadt 393: cmp(LINE *lp1, u_long fieldno1, LINE *lp2, u_long fieldno2)
1.1 deraadt 394: {
1.4 michaels 395: if (lp1->fieldcnt <= fieldno1)
1.1 deraadt 396: return (-1);
1.4 michaels 397: else if (lp2->fieldcnt <= fieldno2)
398: return (1);
1.1 deraadt 399: return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2]));
400: }
401:
402: void
1.16 deraadt 403: joinlines(INPUT *F1, INPUT *F2)
1.1 deraadt 404: {
1.17 otto 405: u_long cnt1, cnt2;
1.1 deraadt 406:
407: /*
408: * Output the results of a join comparison. The output may be from
409: * either file 1 or file 2 (in which case the first argument is the
410: * file from which to output) or from both.
411: */
412: if (F2 == NULL) {
413: for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
414: outoneline(F1, &F1->set[cnt1]);
415: return;
416: }
417: for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
418: for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2)
419: outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]);
420: }
421:
422: void
1.16 deraadt 423: outoneline(INPUT *F, LINE *lp)
1.1 deraadt 424: {
1.17 otto 425: u_long cnt;
1.1 deraadt 426:
427: /*
428: * Output a single line from one of the files, according to the
429: * join rules. This happens when we are writing unmatched single
430: * lines. Output empty fields in the right places.
431: */
432: if (olist)
433: for (cnt = 0; cnt < olistcnt; ++cnt) {
1.4 michaels 434: if (olist[cnt].filenum == F->number)
435: outfield(lp, olist[cnt].fieldno, 0);
1.17 otto 436: else if (olist[cnt].filenum == 0)
437: outfield(lp, F->joinf, 0);
1.4 michaels 438: else
439: outfield(lp, 0, 1);
1.1 deraadt 440: }
1.18 otto 441: else {
442: /*
443: * Output the join field, then the remaining fields from F
444: */
445: outfield(lp, F->joinf, 0);
1.1 deraadt 446: for (cnt = 0; cnt < lp->fieldcnt; ++cnt)
1.18 otto 447: if (F->joinf != cnt)
448: outfield(lp, cnt, 0);
449: }
450:
1.4 michaels 451: putchar('\n');
1.1 deraadt 452: if (ferror(stdout))
1.4 michaels 453: err(1, "stdout");
1.1 deraadt 454: needsep = 0;
455: }
456:
457: void
1.16 deraadt 458: outtwoline(INPUT *F1, LINE *lp1, INPUT *F2, LINE *lp2)
1.1 deraadt 459: {
1.17 otto 460: u_long cnt;
1.1 deraadt 461:
462: /* Output a pair of lines according to the join list (if any). */
1.11 deraadt 463: if (olist) {
1.1 deraadt 464: for (cnt = 0; cnt < olistcnt; ++cnt)
1.17 otto 465: if (olist[cnt].filenum == 0) {
466: if (lp1->fieldcnt >= F1->joinf)
467: outfield(lp1, F1->joinf, 0);
468: else
469: outfield(lp2, F2->joinf, 0);
470: } else if (olist[cnt].filenum == 1)
1.4 michaels 471: outfield(lp1, olist[cnt].fieldno, 0);
472: else /* if (olist[cnt].filenum == 2) */
473: outfield(lp2, olist[cnt].fieldno, 0);
1.11 deraadt 474: } else {
1.1 deraadt 475: /*
476: * Output the join field, then the remaining fields from F1
477: * and F2.
478: */
1.4 michaels 479: outfield(lp1, F1->joinf, 0);
1.1 deraadt 480: for (cnt = 0; cnt < lp1->fieldcnt; ++cnt)
481: if (F1->joinf != cnt)
1.4 michaels 482: outfield(lp1, cnt, 0);
1.1 deraadt 483: for (cnt = 0; cnt < lp2->fieldcnt; ++cnt)
484: if (F2->joinf != cnt)
1.4 michaels 485: outfield(lp2, cnt, 0);
1.1 deraadt 486: }
1.4 michaels 487: putchar('\n');
1.1 deraadt 488: if (ferror(stdout))
1.4 michaels 489: err(1, "stdout");
1.1 deraadt 490: needsep = 0;
491: }
492:
493: void
1.16 deraadt 494: outfield(LINE *lp, u_long fieldno, int out_empty)
1.1 deraadt 495: {
496: if (needsep++)
1.4 michaels 497: putchar((int)*tabchar);
1.11 deraadt 498: if (!ferror(stdout)) {
1.12 aaron 499: if (lp->fieldcnt <= fieldno || out_empty) {
1.1 deraadt 500: if (empty != NULL)
1.4 michaels 501: fputs(empty, stdout);
1.1 deraadt 502: } else {
503: if (*lp->fields[fieldno] == '\0')
504: return;
1.4 michaels 505: fputs(lp->fields[fieldno], stdout);
1.1 deraadt 506: }
1.11 deraadt 507: }
1.1 deraadt 508: if (ferror(stdout))
1.4 michaels 509: err(1, "stdout");
1.1 deraadt 510: }
511:
512: /*
513: * Convert an output list argument "2.1, 1.3, 2.4" into an array of output
514: * fields.
515: */
516: void
1.16 deraadt 517: fieldarg(char *option)
1.1 deraadt 518: {
1.17 otto 519: u_long fieldno, filenum;
1.1 deraadt 520: char *end, *token;
521:
1.4 michaels 522: while ((token = strsep(&option, ", \t")) != NULL) {
1.1 deraadt 523: if (*token == '\0')
524: continue;
1.17 otto 525: if (token[0] == '0')
526: filenum = fieldno = 0;
527: else if ((token[0] == '1' || token[0] == '2') &&
528: token[1] == '.') {
529: filenum = token[0] - '0';
530: fieldno = strtol(token + 2, &end, 10);
531: if (*end)
532: errx(1, "malformed -o option field");
533: if (fieldno == 0)
534: errx(1, "field numbers are 1 based");
535: --fieldno;
536: } else
1.4 michaels 537: errx(1, "malformed -o option field");
1.1 deraadt 538: if (olistcnt == olistalloc) {
1.17 otto 539: OLIST *p;
540: u_long newsize = olistalloc + 50;
541: if ((p = realloc(olist,
542: newsize * sizeof(OLIST))) == NULL)
1.4 michaels 543: err(1, NULL);
1.17 otto 544: olist = p;
545: olistalloc = newsize;
1.1 deraadt 546: }
1.17 otto 547: olist[olistcnt].filenum = filenum;
548: olist[olistcnt].fieldno = fieldno;
1.1 deraadt 549: ++olistcnt;
550: }
551: }
552:
553: void
1.16 deraadt 554: obsolete(char **argv)
1.1 deraadt 555: {
1.17 otto 556: size_t len;
1.1 deraadt 557: char **p, *ap, *t;
558:
1.4 michaels 559: while ((ap = *++argv) != NULL) {
1.1 deraadt 560: /* Return if "--". */
1.9 deraadt 561: if (ap[0] == '-' && ap[1] == '-')
1.1 deraadt 562: return;
1.10 deraadt 563: /* skip if not an option */
564: if (ap[0] != '-')
565: continue;
1.1 deraadt 566: switch (ap[1]) {
567: case 'a':
568: /*
569: * The original join allowed "-a", which meant the
570: * same as -a1 plus -a2. POSIX 1003.2, Draft 11.2
571: * only specifies this as "-a 1" and "a -2", so we
572: * have to use another option flag, one that is
573: * unlikely to ever be used or accidentally entered
574: * on the command line. (Well, we could reallocate
575: * the argv array, but that hardly seems worthwhile.)
576: */
1.17 otto 577: if (ap[2] == '\0' && (argv[1] == NULL ||
578: (strcmp(argv[1], "1") != 0 &&
579: strcmp(argv[1], "2") != 0))) {
1.1 deraadt 580: ap[1] = '\01';
1.17 otto 581: warnx("-a option used without an argument; "
582: "reverting to historical behavior");
583: }
1.1 deraadt 584: break;
585: case 'j':
586: /*
587: * The original join allowed "-j[12] arg" and "-j arg".
588: * Convert the former to "-[12] arg". Don't convert
589: * the latter since getopt(3) can handle it.
590: */
591: switch(ap[2]) {
592: case '1':
593: case '2':
594: if (ap[3] != '\0')
595: goto jbad;
1.19 millert 596: ap[1] = ap[2];
1.1 deraadt 597: ap[2] = '\0';
598: break;
599: case '\0':
600: break;
601: default:
1.19 millert 602: jbad: warnx("unknown option -- %s", ap + 1);
1.1 deraadt 603: usage();
604: }
605: break;
606: case 'o':
607: /*
1.4 michaels 608: * The original join allowed "-o arg arg".
609: * Convert to "-o arg -o arg".
1.1 deraadt 610: */
1.19 millert 611: if (ap[2] != '\0' || argv[1] == NULL)
1.1 deraadt 612: break;
1.4 michaels 613: for (p = argv + 2; *p != NULL; ++p) {
1.17 otto 614: if (p[0][0] == '0' || ((p[0][0] != '1' &&
615: p[0][0] != '2') || p[0][1] != '.'))
1.1 deraadt 616: break;
617: len = strlen(*p);
618: if (len - 2 != strspn(*p + 2, "0123456789"))
619: break;
620: if ((t = malloc(len + 3)) == NULL)
1.4 michaels 621: err(1, NULL);
1.1 deraadt 622: t[0] = '-';
623: t[1] = 'o';
1.4 michaels 624: memmove(t + 2, *p, len + 1);
1.1 deraadt 625: *p = t;
626: }
627: argv = p - 1;
628: break;
629: }
630: }
631: }
632:
633: void
1.16 deraadt 634: usage(void)
1.1 deraadt 635: {
1.19 millert 636: int len;
637: extern char *__progname;
638:
639: len = strlen(__progname) + sizeof("usage: ");
640: (void)fprintf(stderr, "usage: %s [-1 field] [-2 field] "
641: "[-a file_number | -v file_number] [-e string]\n"
1.20 jmc 642: "%*s[-o list] [-t char] file1 file2\n",
1.19 millert 643: __progname, len, "");
1.1 deraadt 644: exit(1);
645: }