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.3 (Berkeley) 04/16/94"; 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 267 /* 268 * Get any pushed back line, else get the next line. Allocate 269 * space as necessary. If taking the line from the stack swap 270 * the two structures so that we don't lose space allocated to 271 * either structure. This could be avoided by doing another 272 * level of indirection, but it's probably okay as is. 273 * but it's probably okay as is. 274 */ 275 lp = &F->set[F->setcnt]; 276 if (F->pushbool) { 277 tmp = F->set[F->setcnt]; 278 F->set[F->setcnt] = F->set[F->pushback]; 279 F->set[F->pushback] = tmp; 280 F->pushbool = 0; 281 continue; 282 } 283 if ((bp = fgetln(F->fp, &len)) == NULL) 284 return; 285 if (lp->linealloc <= len + 1) { 286 lp->linealloc += MAX(100, len + 1); 287 if ((lp->line = 288 realloc(lp->line, lp->linealloc)) == NULL) 289 err(1, NULL); 290 } 291 memmove(lp->line, bp, len); 292 293 /* Replace trailing newline, if it exists. */ 294 if (bp[len - 1] == '\n') 295 lp->line[len - 1] = '\0'; 296 else 297 lp->line[len] = '\0'; 298 bp = lp->line; 299 300 /* Split the line into fields, allocate space as necessary. */ 301 lp->fieldcnt = 0; 302 while ((fieldp = strsep(&bp, tabchar)) != NULL) { 303 if (spans && *fieldp == '\0') 304 continue; 305 if (lp->fieldcnt == lp->fieldalloc) { 306 lp->fieldalloc += 50; 307 if ((lp->fields = realloc(lp->fields, 308 lp->fieldalloc * sizeof(char *))) == NULL) 309 err(1, NULL); 310 } 311 lp->fields[lp->fieldcnt++] = fieldp; 312 } 313 314 /* See if the join field value has changed. */ 315 if (lastlp != NULL && cmp(lp, F->joinf, lastlp, F->joinf)) { 316 F->pushbool = 1; 317 F->pushback = F->setcnt; 318 break; 319 } 320 } 321 } 322 323 int 324 cmp(lp1, fieldno1, lp2, fieldno2) 325 LINE *lp1, *lp2; 326 u_long fieldno1, fieldno2; 327 { 328 if (lp1->fieldcnt < fieldno1) 329 return (lp2->fieldcnt < fieldno2 ? 0 : 1); 330 if (lp2->fieldcnt < fieldno2) 331 return (-1); 332 return (strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2])); 333 } 334 335 void 336 joinlines(F1, F2) 337 INPUT *F1, *F2; 338 { 339 int cnt1, cnt2; 340 341 /* 342 * Output the results of a join comparison. The output may be from 343 * either file 1 or file 2 (in which case the first argument is the 344 * file from which to output) or from both. 345 */ 346 if (F2 == NULL) { 347 for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1) 348 outoneline(F1, &F1->set[cnt1]); 349 return; 350 } 351 for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1) 352 for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2) 353 outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]); 354 } 355 356 void 357 outoneline(F, lp) 358 INPUT *F; 359 LINE *lp; 360 { 361 int cnt; 362 363 /* 364 * Output a single line from one of the files, according to the 365 * join rules. This happens when we are writing unmatched single 366 * lines. Output empty fields in the right places. 367 */ 368 if (olist) 369 for (cnt = 0; cnt < olistcnt; ++cnt) { 370 if (olist[cnt].filenum == F->number) 371 outfield(lp, olist[cnt].fieldno, 0); 372 else 373 outfield(lp, 0, 1); 374 } 375 else 376 for (cnt = 0; cnt < lp->fieldcnt; ++cnt) 377 outfield(lp, cnt, 0); 378 (void)printf("\n"); 379 if (ferror(stdout)) 380 err(1, "stdout"); 381 needsep = 0; 382 } 383 384 void 385 outtwoline(F1, lp1, F2, lp2) 386 INPUT *F1, *F2; 387 LINE *lp1, *lp2; 388 { 389 int cnt; 390 391 /* Output a pair of lines according to the join list (if any). */ 392 if (olist) 393 for (cnt = 0; cnt < olistcnt; ++cnt) 394 if (olist[cnt].filenum == 1) 395 outfield(lp1, olist[cnt].fieldno, 0); 396 else /* if (olist[cnt].filenum == 2) */ 397 outfield(lp2, olist[cnt].fieldno, 0); 398 else { 399 /* 400 * Output the join field, then the remaining fields from F1 401 * and F2. 402 */ 403 outfield(lp1, F1->joinf, 0); 404 for (cnt = 0; cnt < lp1->fieldcnt; ++cnt) 405 if (F1->joinf != cnt) 406 outfield(lp1, cnt, 0); 407 for (cnt = 0; cnt < lp2->fieldcnt; ++cnt) 408 if (F2->joinf != cnt) 409 outfield(lp2, cnt, 0); 410 } 411 (void)printf("\n"); 412 if (ferror(stdout)) 413 err(1, "stdout"); 414 needsep = 0; 415 } 416 417 void 418 outfield(lp, fieldno, out_empty) 419 LINE *lp; 420 u_long fieldno; 421 int out_empty; 422 { 423 if (needsep++) 424 (void)printf("%c", *tabchar); 425 if (!ferror(stdout)) 426 if (lp->fieldcnt < fieldno || out_empty) { 427 if (empty != NULL) 428 (void)printf("%s", empty); 429 } else { 430 if (*lp->fields[fieldno] == '\0') 431 return; 432 (void)printf("%s", lp->fields[fieldno]); 433 } 434 if (ferror(stdout)) 435 err(1, "stdout"); 436 } 437 438 /* 439 * Convert an output list argument "2.1, 1.3, 2.4" into an array of output 440 * fields. 441 */ 442 void 443 fieldarg(option) 444 char *option; 445 { 446 u_long fieldno; 447 char *end, *token; 448 449 while ((token = strsep(&option, " \t")) != NULL) { 450 if (*token == '\0') 451 continue; 452 if (token[0] != '1' && token[0] != '2' || token[1] != '.') 453 errx(1, "malformed -o option field"); 454 fieldno = strtol(token + 2, &end, 10); 455 if (*end) 456 errx(1, "malformed -o option field"); 457 if (fieldno == 0) 458 errx(1, "field numbers are 1 based"); 459 if (olistcnt == olistalloc) { 460 olistalloc += 50; 461 if ((olist = realloc(olist, 462 olistalloc * sizeof(OLIST))) == NULL) 463 err(1, NULL); 464 } 465 olist[olistcnt].filenum = token[0] - '0'; 466 olist[olistcnt].fieldno = fieldno - 1; 467 ++olistcnt; 468 } 469 } 470 471 void 472 obsolete(argv) 473 char **argv; 474 { 475 int len; 476 char **p, *ap, *t; 477 478 while ((ap = *++argv) != NULL) { 479 /* Return if "--". */ 480 if (ap[0] == '-' && ap[1] == '-') 481 return; 482 switch (ap[1]) { 483 case 'a': 484 /* 485 * The original join allowed "-a", which meant the 486 * same as -a1 plus -a2. POSIX 1003.2, Draft 11.2 487 * only specifies this as "-a 1" and "a -2", so we 488 * have to use another option flag, one that is 489 * unlikely to ever be used or accidentally entered 490 * on the command line. (Well, we could reallocate 491 * the argv array, but that hardly seems worthwhile.) 492 */ 493 if (ap[2] == '\0') 494 ap[1] = '\01'; 495 break; 496 case 'j': 497 /* 498 * The original join allowed "-j[12] arg" and "-j arg". 499 * Convert the former to "-[12] arg". Don't convert 500 * the latter since getopt(3) can handle it. 501 */ 502 switch(ap[2]) { 503 case '1': 504 if (ap[3] != '\0') 505 goto jbad; 506 ap[1] = '1'; 507 ap[2] = '\0'; 508 break; 509 case '2': 510 if (ap[3] != '\0') 511 goto jbad; 512 ap[1] = '2'; 513 ap[2] = '\0'; 514 break; 515 case '\0': 516 break; 517 default: 518 jbad: errx(1, "illegal option -- %s", ap); 519 usage(); 520 } 521 break; 522 case 'o': 523 /* 524 * The original join allowed "-o arg arg". 525 * Convert to "-o arg -o arg". 526 */ 527 if (ap[2] != '\0') 528 break; 529 for (p = argv + 2; *p; ++p) { 530 if (p[0][0] != '1' && 531 p[0][0] != '2' || p[0][1] != '.') 532 break; 533 len = strlen(*p); 534 if (len - 2 != strspn(*p + 2, "0123456789")) 535 break; 536 if ((t = malloc(len + 3)) == NULL) 537 err(1, NULL); 538 t[0] = '-'; 539 t[1] = 'o'; 540 memmove(t + 2, *p, len + 1); 541 *p = t; 542 } 543 argv = p - 1; 544 break; 545 } 546 } 547 } 548 549 void 550 usage() 551 { 552 (void)fprintf(stderr, "%s%s\n", 553 "usage: join [-a fileno | -v fileno ] [-e string] [-1 field] ", 554 "[-2 field]\n [-o list] [-t char] file1 file2"); 555 exit(1); 556 } 557