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