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