static char *sccsid
= "@(#)join.c 4.1 (Berkeley) %G%";
/* join F1 F2 on stuff */
#define NFLD 20 /* max field per line */
#define comp() cmp(ppi[F1][j1],ppi[F2][j2])
char buf
[2][BUFSIZ
]; /*input lines */
char *ppi
[2][NFLD
]; /* pointers to fields in lines */
int j1
= 1; /* join of this field of file 1 */
int j2
= 1; /* join of this field of file 2 */
int olist
[2*NFLD
]; /* output these fields */
int olistf
[2*NFLD
]; /* from these files */
int no
; /* number of entries in olist */
int sep1
= ' '; /* default field separator */
while (argc
> 1 && argv
[1][0] == '-') {
sep1
= sep2
= argv
[1][2];
for (no
= 0; no
< 2*NFLD
; no
++) {
if (argv
[2][0] == '1' && argv
[2][1] == '.') {
olist
[no
] = atoi(&argv
[2][2]);
} else if (argv
[2][0] == '2' && argv
[2][1] == '.') {
olist
[no
] = atoi(&argv
[2][2]);
else if (argv
[1][2] == '2')
olist
[i
]--; /* 0 origin */
error("usage: join [-j1 x -j2 y] [-o list] file1 file2");
j2
--; /* everyone else believes in 0 origin */
else if ((f
[F1
] = fopen(argv
[1], "r")) == NULL
)
error("can't open %s", argv
[1]);
if ((f
[F2
] = fopen(argv
[2], "r")) == NULL
)
error("can't open %s", argv
[2]);
#define get1() n1=input(F1)
#define get2() n2=input(F2)
while(n1
>0 && n2
>0 || aflg
!=0 && n1
+n2
>0) {
if(n1
>0 && n2
>0 && comp()>0 || n1
==0) {
if(aflg
&2) output(0, n2
);
} else if(n1
>0 && n2
>0 && comp()<0 || n2
==0) {
if(aflg
&1) output(n1
, 0);
} else /*(n1>0 && n2>0 && comp()==0)*/ {
while(n2
>0 && comp()==0) {
if(n1
>0 && n2
>0 && comp()==0) {
} else if(n1
>0 && n2
>0 && comp()<0 || n2
==0) {
} else /*(n1>0 && n2>0 && comp()>0 || n1==0)*/{
input(n
) /* get input line and split into fields */
if (fgets(bp
, BUFSIZ
, f
[n
]) == NULL
)
if (sep1
== ' ') /* strip multiples */
while ((c
= *bp
) == sep1
|| c
== sep2
)
if (c
== '\n' || c
== '\0')
*pp
++ = bp
; /* record beginning */
while ((c
= *bp
) != sep1
&& c
!= '\n' && c
!= sep2
&& c
!= '\0')
*bp
++ = '\0'; /* mark end by overwriting blank */
/* fails badly if string doesn't have \n at end */
output(on1
, on2
) /* print items from olist */
if (no
<= 0) { /* default case */
printf("%s", on1
? ppi
[F1
][j1
]: ppi
[F2
][j2
]);
for (i
= 0; i
< on1
; i
++)
printf("%c%s", sep1
, ppi
[F1
][i
]);
for (i
= 0; i
< on2
; i
++)
printf("%c%s", sep1
, ppi
[F2
][i
]);
for (i
= 0; i
< no
; i
++) {
temp
= ppi
[olistf
[i
]][olist
[i
]];
if(olistf
[i
]==F1
&& on1
<=olist
[i
] ||
olistf
[i
]==F2
&& on2
<=olist
[i
] ||
error(s1
, s2
, s3
, s4
, s5
)
fprintf(stderr
, "join: ");
fprintf(stderr
, s1
, s2
, s3
, s4
, s5
);