Annotation of src/usr.bin/join/join.c, Revision 1.29
1.29 ! otto 1: /* $OpenBSD: join.c,v 1.28 2018/07/18 17:20:54 millert 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 */
1.1 deraadt 56: } LINE;
57:
58: typedef struct {
1.7 michaels 59: FILE *fp; /* file descriptor */
1.4 michaels 60: u_long joinf; /* join field (-1, -2, -j) */
1.7 michaels 61: int unpair; /* output unpairable lines (-a) */
1.17 otto 62: u_long number; /* 1 for file 1, 2 for file 2 */
1.7 michaels 63: LINE *set; /* set of lines with same field */
1.4 michaels 64: int pushbool; /* if pushback is set */
65: u_long pushback; /* line on the stack */
66: u_long setcnt; /* set count */
67: u_long setalloc; /* set allocated count */
1.1 deraadt 68: } INPUT;
1.29 ! otto 69: INPUT input1 = { NULL, 0, 0, 1, NULL, 0, 0, 0, 0 },
! 70: input2 = { NULL, 0, 0, 2, NULL, 0, 0, 0, 0 };
1.1 deraadt 71:
72: typedef struct {
1.4 michaels 73: u_long filenum; /* file number */
1.1 deraadt 74: u_long fieldno; /* field number */
75: } OLIST;
76: OLIST *olist; /* output field list */
77: u_long olistcnt; /* output field list count */
78: u_long olistalloc; /* output field allocated count */
79:
80: int joinout = 1; /* show lines with matched join fields (-v) */
81: int needsep; /* need separator character */
82: int spans = 1; /* span multiple delimiters (-t) */
83: char *empty; /* empty field replacement string (-e) */
84: char *tabchar = " \t"; /* delimiter characters (-t) */
85:
1.14 millert 86: int cmp(LINE *, u_long, LINE *, u_long);
87: void fieldarg(char *);
88: void joinlines(INPUT *, INPUT *);
89: void obsolete(char **);
90: void outfield(LINE *, u_long, int);
91: void outoneline(INPUT *, LINE *);
92: void outtwoline(INPUT *, LINE *, INPUT *, LINE *);
93: void slurp(INPUT *);
94: void slurpit(INPUT *);
95: void usage(void);
1.1 deraadt 96:
97: int
1.16 deraadt 98: main(int argc, char *argv[])
1.1 deraadt 99: {
1.4 michaels 100: INPUT *F1, *F2;
1.1 deraadt 101: int aflag, ch, cval, vflag;
102: char *end;
103:
1.27 deraadt 104: if (pledge("stdio rpath", NULL) == -1)
105: err(1, "pledge");
1.26 deraadt 106:
1.1 deraadt 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.26 deraadt 210:
1.27 deraadt 211: if (pledge("stdio", NULL) == -1)
212: err(1, "pledge");
1.1 deraadt 213:
214: slurp(F1);
215: slurp(F2);
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.29 ! otto 230: } else if (cval < 0) {
! 231: /* File 1 takes the lead... */
! 232: if (F1->unpair)
1.1 deraadt 233: joinlines(F1, NULL);
1.29 ! otto 234: slurp(F1);
! 235: } else {
! 236: /* File 2 takes the lead... */
! 237: if (F2->unpair)
1.1 deraadt 238: joinlines(F2, NULL);
1.29 ! otto 239: slurp(F2);
1.1 deraadt 240: }
241: }
242:
243: /*
244: * Now that one of the files is used up, optionally output any
245: * remaining lines from the other file.
246: */
247: if (F1->unpair)
248: while (F1->setcnt) {
249: joinlines(F1, NULL);
250: slurp(F1);
251: }
252: if (F2->unpair)
253: while (F2->setcnt) {
254: joinlines(F2, NULL);
255: slurp(F2);
256: }
1.5 michaels 257:
1.4 michaels 258: return 0;
1.1 deraadt 259: }
260:
261: void
1.29 ! otto 262: slurp(INPUT *F)
1.1 deraadt 263: {
1.4 michaels 264: LINE *lp, *lastlp, tmp;
1.28 millert 265: ssize_t len;
266: size_t linesize;
1.17 otto 267: u_long cnt;
1.28 millert 268: char *bp, *fieldp, *line;
1.29 ! otto 269:
1.1 deraadt 270: /*
271: * Read all of the lines from an input file that have the same
272: * join field.
273: */
1.4 michaels 274:
1.1 deraadt 275: F->setcnt = 0;
1.28 millert 276: line = NULL;
277: linesize = 0;
1.29 ! otto 278: for (lastlp = NULL; ; ++F->setcnt) {
1.1 deraadt 279: /*
280: * If we're out of space to hold line structures, allocate
281: * more. Initialize the structure so that we know that this
282: * is new space.
283: */
284: if (F->setcnt == F->setalloc) {
1.17 otto 285: LINE *p;
286: u_long newsize = F->setalloc + 50;
1.1 deraadt 287: cnt = F->setalloc;
1.23 doug 288: if ((p = reallocarray(F->set, newsize, sizeof(LINE)))
289: == NULL)
1.4 michaels 290: err(1, NULL);
1.17 otto 291: F->set = p;
292: F->setalloc = newsize;
1.4 michaels 293: memset(F->set + cnt, 0, 50 * sizeof(LINE));
294: /* re-set lastlp in case it moved */
295: if (lastlp != NULL)
296: lastlp = &F->set[F->setcnt - 1];
1.1 deraadt 297: }
298: /*
299: * Get any pushed back line, else get the next line. Allocate
300: * space as necessary. If taking the line from the stack swap
1.4 michaels 301: * the two structures so that we don't lose space allocated to
302: * either structure. This could be avoided by doing another
303: * level of indirection, but it's probably okay as is.
1.1 deraadt 304: */
305: lp = &F->set[F->setcnt];
1.29 ! otto 306: if (F->setcnt)
! 307: lastlp = &F->set[F->setcnt - 1];
1.4 michaels 308: if (F->pushbool) {
1.1 deraadt 309: tmp = F->set[F->setcnt];
310: F->set[F->setcnt] = F->set[F->pushback];
311: F->set[F->pushback] = tmp;
1.4 michaels 312: F->pushbool = 0;
1.1 deraadt 313: continue;
314: }
1.28 millert 315: if ((len = getline(&line, &linesize, F->fp)) == -1)
316: break;
317:
318: /* Remove trailing newline, if it exists, and copy line. */
319: if (line[len - 1] == '\n')
320: len--;
1.1 deraadt 321: if (lp->linealloc <= len + 1) {
1.17 otto 322: char *p;
323: u_long newsize = lp->linealloc +
1.24 deraadt 324: MAXIMUM(100, len + 1 - lp->linealloc);
1.17 otto 325: if ((p = realloc(lp->line, newsize)) == NULL)
1.4 michaels 326: err(1, NULL);
1.17 otto 327: lp->line = p;
328: lp->linealloc = newsize;
1.4 michaels 329: }
1.28 millert 330: memcpy(lp->line, line, len);
331: lp->line[len] = '\0';
1.1 deraadt 332:
333: /* Split the line into fields, allocate space as necessary. */
334: lp->fieldcnt = 0;
1.28 millert 335: bp = lp->line;
1.1 deraadt 336: while ((fieldp = strsep(&bp, tabchar)) != NULL) {
337: if (spans && *fieldp == '\0')
338: continue;
339: if (lp->fieldcnt == lp->fieldalloc) {
1.17 otto 340: char **p;
341: u_long newsize = lp->fieldalloc + 50;
1.23 doug 342: if ((p = reallocarray(lp->fields, newsize,
343: sizeof(char *))) == NULL)
1.4 michaels 344: err(1, NULL);
1.17 otto 345: lp->fields = p;
346: lp->fieldalloc = newsize;
1.1 deraadt 347: }
348: lp->fields[lp->fieldcnt++] = fieldp;
349: }
350:
351: /* See if the join field value has changed. */
352: if (lastlp != NULL && cmp(lp, F->joinf, lastlp, F->joinf)) {
1.4 michaels 353: F->pushbool = 1;
1.1 deraadt 354: F->pushback = F->setcnt;
355: break;
356: }
357: }
1.28 millert 358: free(line);
1.1 deraadt 359: }
360:
361: int
1.16 deraadt 362: cmp(LINE *lp1, u_long fieldno1, LINE *lp2, u_long fieldno2)
1.1 deraadt 363: {
1.4 michaels 364: if (lp1->fieldcnt <= fieldno1)
1.1 deraadt 365: return (-1);
1.4 michaels 366: else if (lp2->fieldcnt <= fieldno2)
367: return (1);
1.1 deraadt 368: return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2]));
369: }
370:
371: void
1.16 deraadt 372: joinlines(INPUT *F1, INPUT *F2)
1.1 deraadt 373: {
1.17 otto 374: u_long cnt1, cnt2;
1.1 deraadt 375:
376: /*
377: * Output the results of a join comparison. The output may be from
378: * either file 1 or file 2 (in which case the first argument is the
379: * file from which to output) or from both.
380: */
381: if (F2 == NULL) {
382: for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
383: outoneline(F1, &F1->set[cnt1]);
384: return;
385: }
386: for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
387: for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2)
388: outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]);
389: }
390:
391: void
1.16 deraadt 392: outoneline(INPUT *F, LINE *lp)
1.1 deraadt 393: {
1.17 otto 394: u_long cnt;
1.1 deraadt 395:
396: /*
397: * Output a single line from one of the files, according to the
398: * join rules. This happens when we are writing unmatched single
399: * lines. Output empty fields in the right places.
400: */
401: if (olist)
402: for (cnt = 0; cnt < olistcnt; ++cnt) {
1.4 michaels 403: if (olist[cnt].filenum == F->number)
404: outfield(lp, olist[cnt].fieldno, 0);
1.17 otto 405: else if (olist[cnt].filenum == 0)
406: outfield(lp, F->joinf, 0);
1.4 michaels 407: else
408: outfield(lp, 0, 1);
1.1 deraadt 409: }
1.18 otto 410: else {
411: /*
412: * Output the join field, then the remaining fields from F
413: */
414: outfield(lp, F->joinf, 0);
1.1 deraadt 415: for (cnt = 0; cnt < lp->fieldcnt; ++cnt)
1.18 otto 416: if (F->joinf != cnt)
417: outfield(lp, cnt, 0);
418: }
419:
1.4 michaels 420: putchar('\n');
1.1 deraadt 421: if (ferror(stdout))
1.4 michaels 422: err(1, "stdout");
1.1 deraadt 423: needsep = 0;
424: }
425:
426: void
1.16 deraadt 427: outtwoline(INPUT *F1, LINE *lp1, INPUT *F2, LINE *lp2)
1.1 deraadt 428: {
1.17 otto 429: u_long cnt;
1.1 deraadt 430:
431: /* Output a pair of lines according to the join list (if any). */
1.11 deraadt 432: if (olist) {
1.1 deraadt 433: for (cnt = 0; cnt < olistcnt; ++cnt)
1.17 otto 434: if (olist[cnt].filenum == 0) {
435: if (lp1->fieldcnt >= F1->joinf)
436: outfield(lp1, F1->joinf, 0);
437: else
438: outfield(lp2, F2->joinf, 0);
439: } else if (olist[cnt].filenum == 1)
1.4 michaels 440: outfield(lp1, olist[cnt].fieldno, 0);
441: else /* if (olist[cnt].filenum == 2) */
442: outfield(lp2, olist[cnt].fieldno, 0);
1.11 deraadt 443: } else {
1.1 deraadt 444: /*
445: * Output the join field, then the remaining fields from F1
446: * and F2.
447: */
1.4 michaels 448: outfield(lp1, F1->joinf, 0);
1.1 deraadt 449: for (cnt = 0; cnt < lp1->fieldcnt; ++cnt)
450: if (F1->joinf != cnt)
1.4 michaels 451: outfield(lp1, cnt, 0);
1.1 deraadt 452: for (cnt = 0; cnt < lp2->fieldcnt; ++cnt)
453: if (F2->joinf != cnt)
1.4 michaels 454: outfield(lp2, cnt, 0);
1.1 deraadt 455: }
1.4 michaels 456: putchar('\n');
1.1 deraadt 457: if (ferror(stdout))
1.4 michaels 458: err(1, "stdout");
1.1 deraadt 459: needsep = 0;
460: }
461:
462: void
1.16 deraadt 463: outfield(LINE *lp, u_long fieldno, int out_empty)
1.1 deraadt 464: {
465: if (needsep++)
1.4 michaels 466: putchar((int)*tabchar);
1.11 deraadt 467: if (!ferror(stdout)) {
1.12 aaron 468: if (lp->fieldcnt <= fieldno || out_empty) {
1.1 deraadt 469: if (empty != NULL)
1.4 michaels 470: fputs(empty, stdout);
1.1 deraadt 471: } else {
472: if (*lp->fields[fieldno] == '\0')
473: return;
1.4 michaels 474: fputs(lp->fields[fieldno], stdout);
1.1 deraadt 475: }
1.11 deraadt 476: }
1.1 deraadt 477: if (ferror(stdout))
1.4 michaels 478: err(1, "stdout");
1.1 deraadt 479: }
480:
481: /*
482: * Convert an output list argument "2.1, 1.3, 2.4" into an array of output
483: * fields.
484: */
485: void
1.16 deraadt 486: fieldarg(char *option)
1.1 deraadt 487: {
1.17 otto 488: u_long fieldno, filenum;
1.1 deraadt 489: char *end, *token;
490:
1.4 michaels 491: while ((token = strsep(&option, ", \t")) != NULL) {
1.1 deraadt 492: if (*token == '\0')
493: continue;
1.17 otto 494: if (token[0] == '0')
495: filenum = fieldno = 0;
496: else if ((token[0] == '1' || token[0] == '2') &&
497: token[1] == '.') {
498: filenum = token[0] - '0';
499: fieldno = strtol(token + 2, &end, 10);
500: if (*end)
501: errx(1, "malformed -o option field");
502: if (fieldno == 0)
503: errx(1, "field numbers are 1 based");
504: --fieldno;
505: } else
1.4 michaels 506: errx(1, "malformed -o option field");
1.1 deraadt 507: if (olistcnt == olistalloc) {
1.17 otto 508: OLIST *p;
509: u_long newsize = olistalloc + 50;
1.23 doug 510: if ((p = reallocarray(olist, newsize, sizeof(OLIST)))
511: == NULL)
1.4 michaels 512: err(1, NULL);
1.17 otto 513: olist = p;
514: olistalloc = newsize;
1.1 deraadt 515: }
1.17 otto 516: olist[olistcnt].filenum = filenum;
517: olist[olistcnt].fieldno = fieldno;
1.1 deraadt 518: ++olistcnt;
519: }
520: }
521:
522: void
1.16 deraadt 523: obsolete(char **argv)
1.1 deraadt 524: {
1.17 otto 525: size_t len;
1.1 deraadt 526: char **p, *ap, *t;
527:
1.4 michaels 528: while ((ap = *++argv) != NULL) {
1.1 deraadt 529: /* Return if "--". */
1.9 deraadt 530: if (ap[0] == '-' && ap[1] == '-')
1.1 deraadt 531: return;
1.10 deraadt 532: /* skip if not an option */
533: if (ap[0] != '-')
534: continue;
1.1 deraadt 535: switch (ap[1]) {
536: case 'a':
1.25 jasper 537: /*
1.1 deraadt 538: * The original join allowed "-a", which meant the
539: * same as -a1 plus -a2. POSIX 1003.2, Draft 11.2
540: * only specifies this as "-a 1" and "a -2", so we
541: * have to use another option flag, one that is
542: * unlikely to ever be used or accidentally entered
543: * on the command line. (Well, we could reallocate
544: * the argv array, but that hardly seems worthwhile.)
545: */
1.17 otto 546: if (ap[2] == '\0' && (argv[1] == NULL ||
547: (strcmp(argv[1], "1") != 0 &&
548: strcmp(argv[1], "2") != 0))) {
1.1 deraadt 549: ap[1] = '\01';
1.17 otto 550: warnx("-a option used without an argument; "
551: "reverting to historical behavior");
552: }
1.1 deraadt 553: break;
554: case 'j':
555: /*
556: * The original join allowed "-j[12] arg" and "-j arg".
557: * Convert the former to "-[12] arg". Don't convert
558: * the latter since getopt(3) can handle it.
559: */
560: switch(ap[2]) {
561: case '1':
562: case '2':
563: if (ap[3] != '\0')
564: goto jbad;
1.19 millert 565: ap[1] = ap[2];
1.1 deraadt 566: ap[2] = '\0';
567: break;
568: case '\0':
569: break;
570: default:
1.19 millert 571: jbad: warnx("unknown option -- %s", ap + 1);
1.1 deraadt 572: usage();
573: }
574: break;
575: case 'o':
576: /*
1.4 michaels 577: * The original join allowed "-o arg arg".
578: * Convert to "-o arg -o arg".
1.1 deraadt 579: */
1.19 millert 580: if (ap[2] != '\0' || argv[1] == NULL)
1.1 deraadt 581: break;
1.4 michaels 582: for (p = argv + 2; *p != NULL; ++p) {
1.17 otto 583: if (p[0][0] == '0' || ((p[0][0] != '1' &&
584: p[0][0] != '2') || p[0][1] != '.'))
1.1 deraadt 585: break;
586: len = strlen(*p);
587: if (len - 2 != strspn(*p + 2, "0123456789"))
588: break;
589: if ((t = malloc(len + 3)) == NULL)
1.4 michaels 590: err(1, NULL);
1.1 deraadt 591: t[0] = '-';
592: t[1] = 'o';
1.4 michaels 593: memmove(t + 2, *p, len + 1);
1.1 deraadt 594: *p = t;
595: }
596: argv = p - 1;
597: break;
598: }
599: }
600: }
601:
602: void
1.16 deraadt 603: usage(void)
1.1 deraadt 604: {
1.19 millert 605: int len;
606: extern char *__progname;
607:
608: len = strlen(__progname) + sizeof("usage: ");
609: (void)fprintf(stderr, "usage: %s [-1 field] [-2 field] "
610: "[-a file_number | -v file_number] [-e string]\n"
1.20 jmc 611: "%*s[-o list] [-t char] file1 file2\n",
1.19 millert 612: __progname, len, "");
1.1 deraadt 613: exit(1);
614: }