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