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