2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Steve Hayman of the Computer Science Department, Indiana University,
7 * Michiro Hikida and David Goodenough.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char copyright[] =
40 "@(#) Copyright (c) 1991, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n";
46 static char sccsid[] = "@(#)join.c 8.6 (Berkeley) 5/4/95";
48 static const char rcsid[] =
52 #include <sys/param.h>
63 * There's a structure per input file which encapsulates the state of the
64 * file. We repeatedly read lines from each file until we've read in all
65 * the consecutive lines from the file with a common join field. Then we
66 * compare the set of lines with an equivalent set from the other file.
69 char *line; /* line */
70 u_long linealloc; /* line allocated count */
71 char **fields; /* line field(s) */
72 u_long fieldcnt; /* line field(s) count */
73 u_long fieldalloc; /* line field(s) allocated count */
77 FILE *fp; /* file descriptor */
78 u_long joinf; /* join field (-1, -2, -j) */
79 int unpair; /* output unpairable lines (-a) */
80 int number; /* 1 for file 1, 2 for file 2 */
82 LINE *set; /* set of lines with same field */
83 int pushbool; /* if pushback is set */
84 u_long pushback; /* line on the stack */
85 u_long setcnt; /* set count */
86 u_long setalloc; /* set allocated count */
88 INPUT input1 = { NULL, 0, 0, 1, NULL, 0, 0, 0, },
89 input2 = { NULL, 0, 0, 2, NULL, 0, 0, 0, };
92 u_long filenum; /* file number */
93 u_long fieldno; /* field number */
95 OLIST *olist; /* output field list */
96 u_long olistcnt; /* output field list count */
97 u_long olistalloc; /* output field allocated count */
99 int joinout = 1; /* show lines with matched join fields (-v) */
100 int needsep; /* need separator character */
101 int spans = 1; /* span multiple delimiters (-t) */
102 char *empty; /* empty field replacement string (-e) */
103 char *tabchar = " \t"; /* delimiter characters (-t) */
105 int cmp __P((LINE *, u_long, LINE *, u_long));
106 void fieldarg __P((char *));
107 void joinlines __P((INPUT *, INPUT *));
108 void obsolete __P((char **));
109 void outfield __P((LINE *, u_long, int));
110 void outoneline __P((INPUT *, LINE *));
111 void outtwoline __P((INPUT *, LINE *, INPUT *, LINE *));
112 void slurp __P((INPUT *));
113 void usage __P((void));
121 int aflag, ch, cval, vflag;
129 while ((ch = getopt(argc, argv, "\01a:e:j:1:2:o:t:v:")) != -1) {
131 case '\01': /* See comment in obsolete(). */
133 F1->unpair = F2->unpair = 1;
136 if ((F1->joinf = strtol(optarg, &end, 10)) < 1)
137 errx(1, "-1 option field number less than 1");
139 errx(1, "illegal field number -- %s", optarg);
143 if ((F2->joinf = strtol(optarg, &end, 10)) < 1)
144 errx(1, "-2 option field number less than 1");
146 errx(1, "illegal field number -- %s", optarg);
151 switch(strtol(optarg, &end, 10)) {
159 errx(1, "-a option file number not 1 or 2");
163 errx(1, "illegal file number -- %s", optarg);
169 if ((F1->joinf = F2->joinf =
170 strtol(optarg, &end, 10)) < 1)
171 errx(1, "-j option field number less than 1");
173 errx(1, "illegal field number -- %s", optarg);
182 if (strlen(tabchar = optarg) != 1)
183 errx(1, "illegal tab character specification");
188 switch (strtol(optarg, &end, 10)) {
196 errx(1, "-v option file number not 1 or 2");
200 errx(1, "illegal file number -- %s", optarg);
211 errx(1, "the -a and -v options are mutually exclusive");
216 /* Open the files; "-" means stdin. */
217 if (!strcmp(*argv, "-"))
219 else if ((F1->fp = fopen(*argv, "r")) == NULL)
222 if (!strcmp(*argv, "-"))
224 else if ((F2->fp = fopen(*argv, "r")) == NULL)
226 if (F1->fp == stdin && F2->fp == stdin)
227 errx(1, "only one input file may be stdin");
231 while (F1->setcnt && F2->setcnt) {
232 cval = cmp(F1->set, F1->joinf, F2->set, F2->joinf);
234 /* Oh joy, oh rapture, oh beauty divine! */
239 } else if (cval < 0) {
240 /* File 1 takes the lead... */
245 /* File 2 takes the lead... */
253 * Now that one of the files is used up, optionally output any
254 * remaining lines from the other file.
273 LINE *lp, *lastlp, tmp;
279 * Read all of the lines from an input file that have the same
283 for (lastlp = NULL;; ++F->setcnt) {
285 * If we're out of space to hold line structures, allocate
286 * more. Initialize the structure so that we know that this
289 if (F->setcnt == F->setalloc) {
292 if ((F->set = realloc(F->set,
293 F->setalloc * sizeof(LINE))) == NULL)
295 memset(F->set + cnt, 0, 50 * sizeof(LINE));
297 /* re-set lastlp in case it moved */
299 lastlp = &F->set[F->setcnt - 1];
303 * Get any pushed back line, else get the next line. Allocate
304 * space as necessary. If taking the line from the stack swap
305 * the two structures so that we don't lose space allocated to
306 * either structure. This could be avoided by doing another
307 * level of indirection, but it's probably okay as is.
309 lp = &F->set[F->setcnt];
311 lastlp = &F->set[F->setcnt - 1];
313 tmp = F->set[F->setcnt];
314 F->set[F->setcnt] = F->set[F->pushback];
315 F->set[F->pushback] = tmp;
319 if ((bp = fgetln(F->fp, &len)) == NULL)
321 if (lp->linealloc <= len + 1) {
322 lp->linealloc += MAX(100, len + 1 - lp->linealloc);
324 realloc(lp->line, lp->linealloc)) == NULL)
327 memmove(lp->line, bp, len);
329 /* Replace trailing newline, if it exists. */
330 if (bp[len - 1] == '\n')
331 lp->line[len - 1] = '\0';
333 lp->line[len] = '\0';
336 /* Split the line into fields, allocate space as necessary. */
338 while ((fieldp = strsep(&bp, tabchar)) != NULL) {
339 if (spans && *fieldp == '\0')
341 if (lp->fieldcnt == lp->fieldalloc) {
342 lp->fieldalloc += 50;
343 if ((lp->fields = realloc(lp->fields,
344 lp->fieldalloc * sizeof(char *))) == NULL)
347 lp->fields[lp->fieldcnt++] = fieldp;
350 /* See if the join field value has changed. */
351 if (lastlp != NULL && cmp(lp, F->joinf, lastlp, F->joinf)) {
353 F->pushback = F->setcnt;
360 cmp(lp1, fieldno1, lp2, fieldno2)
362 u_long fieldno1, fieldno2;
364 if (lp1->fieldcnt <= fieldno1)
365 return (lp2->fieldcnt <= fieldno2 ? 0 : 1);
366 if (lp2->fieldcnt <= fieldno2)
368 return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2]));
378 * Output the results of a join comparison. The output may be from
379 * either file 1 or file 2 (in which case the first argument is the
380 * file from which to output) or from both.
383 for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
384 outoneline(F1, &F1->set[cnt1]);
387 for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
388 for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2)
389 outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]);
400 * Output a single line from one of the files, according to the
401 * join rules. This happens when we are writing unmatched single
402 * lines. Output empty fields in the right places.
405 for (cnt = 0; cnt < olistcnt; ++cnt) {
406 if (olist[cnt].filenum == F->number)
407 outfield(lp, olist[cnt].fieldno, 0);
412 for (cnt = 0; cnt < lp->fieldcnt; ++cnt)
413 outfield(lp, cnt, 0);
421 outtwoline(F1, lp1, F2, lp2)
427 /* Output a pair of lines according to the join list (if any). */
429 for (cnt = 0; cnt < olistcnt; ++cnt)
430 if (olist[cnt].filenum == 1)
431 outfield(lp1, olist[cnt].fieldno, 0);
432 else /* if (olist[cnt].filenum == 2) */
433 outfield(lp2, olist[cnt].fieldno, 0);
436 * Output the join field, then the remaining fields from F1
439 outfield(lp1, F1->joinf, 0);
440 for (cnt = 0; cnt < lp1->fieldcnt; ++cnt)
441 if (F1->joinf != cnt)
442 outfield(lp1, cnt, 0);
443 for (cnt = 0; cnt < lp2->fieldcnt; ++cnt)
444 if (F2->joinf != cnt)
445 outfield(lp2, cnt, 0);
454 outfield(lp, fieldno, out_empty)
460 (void)printf("%c", *tabchar);
461 if (!ferror(stdout)) {
462 if (lp->fieldcnt <= fieldno || out_empty) {
464 (void)printf("%s", empty);
466 if (*lp->fields[fieldno] == '\0')
468 (void)printf("%s", lp->fields[fieldno]);
476 * Convert an output list argument "2.1, 1.3, 2.4" into an array of output
486 while ((token = strsep(&option, ", \t")) != NULL) {
489 if (token[0] != '1' && token[0] != '2' || token[1] != '.')
490 errx(1, "malformed -o option field");
491 fieldno = strtol(token + 2, &end, 10);
493 errx(1, "malformed -o option field");
495 errx(1, "field numbers are 1 based");
496 if (olistcnt == olistalloc) {
498 if ((olist = realloc(olist,
499 olistalloc * sizeof(OLIST))) == NULL)
502 olist[olistcnt].filenum = token[0] - '0';
503 olist[olistcnt].fieldno = fieldno - 1;
515 while ((ap = *++argv) != NULL) {
516 /* Return if "--". */
517 if (ap[0] == '-' && ap[1] == '-')
519 /* skip if not an option */
525 * The original join allowed "-a", which meant the
526 * same as -a1 plus -a2. POSIX 1003.2, Draft 11.2
527 * only specifies this as "-a 1" and "a -2", so we
528 * have to use another option flag, one that is
529 * unlikely to ever be used or accidentally entered
530 * on the command line. (Well, we could reallocate
531 * the argv array, but that hardly seems worthwhile.)
538 * The original join allowed "-j[12] arg" and "-j arg".
539 * Convert the former to "-[12] arg". Don't convert
540 * the latter since getopt(3) can handle it.
558 jbad: errx(1, "illegal option -- %s", ap);
564 * The original join allowed "-o arg arg".
565 * Convert to "-o arg -o arg".
569 for (p = argv + 2; *p; ++p) {
570 if (p[0][0] != '1' &&
571 p[0][0] != '2' || p[0][1] != '.')
574 if (len - 2 != strspn(*p + 2, "0123456789"))
576 if ((t = malloc(len + 3)) == NULL)
580 memmove(t + 2, *p, len + 1);
592 (void)fprintf(stderr, "%s %s\n%s\n",
593 "usage: join [-a fileno | -v fileno ] [-e string] [-1 field]",
595 " [-o list] [-t char] file1 file2");