join.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* join F1 F2 on stuff */
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <widec.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <errno.h>
#include <unistd.h>
static void output(int, int);
static int input(int);
static void Usage();
static void init_buf();
static void get_buf(int, int);
static void get_ppi(int);
static void get_olist(int);
#define JOINFIELD 9999
#define F1 0
#define F2 1
else \
#define max(a, b) (a >= b ? a : b)
int CNFLD; /* current number of fields per line */
int CNOFLD; /* current number of output fields */
static FILE *f[2];
static int *olist; /* output these fields */
static int *olistf; /* from these files */
static int no = 0; /* number of entries in olist */
static char *null = "";
static int aflg = 0;
static int vflg = 0;
static int tflg = 0;
int
{
int i, j;
int cmpresult;
int opt, filenumber;
int nextargopt;
size_t t;
char tmpfile[20];
int fd;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
(void) init_buf(); /* allocate buffers */
/* check for non-standard "-j#" option, last 2 arguments are files */
break;
if (i == argc - 3)
Usage();
for (j = i; j < argc - 1; j++) {
}
argc -= 2;
i--;
if (i >= argc - 3)
Usage();
for (j = i; j < argc - 1; j++) {
}
argc -= 2;
i--;
}
}
/* check for non-standard "-o" option, last 2 arguments are files */
/* stop looking for -o if "--" */
break;
for (j = i; j < argc; j++) {
}
argc--;
} else {
}
if (i == argc - 2)
Usage();
/* Get larger table for outlists */
/* (CNOFLD is updated.) */
}
(char **)NULL, 10);
nextargopt = 2 + (int)
(char **)NULL, 10);
nextargopt = 2 + (int)
} else if (argv[i][0] == '0') {
nextargopt = 1;
} else
break;
for (j = i; j < argc; j++) {
}
argc--;
} else
Usage();
}
}
}
/* get other options */
switch (opt) {
case '1':
break;
case '2':
break;
case 'a':
case 'v':
Usage();
aflg |= filenumber;
if (opt == 'v')
vflg |= filenumber;
break;
case 'e':
break;
case 'j':
break;
case 't':
tflg = 1;
break;
}
}
/* check options */
"join: bad join field number\n"));
Usage();
}
/* 0 origin */
for (i = 0; i < no; i++)
olist[i]--;
j1--;
j2--;
Usage();
exit(1);
}
exit(1);
}
exit(1);
}
/* tmpfile is automatically removed on exit */
"join: cannot write to tmpfile\n"));
exit(1);
}
}
exit(1);
}
/* set bottom mark on file2 */
/* input a line from each file */
get1();
get2();
/* while lines in both files or -a|v and lines in ether file */
/* compare join fields */
/* joinfiled1 > joinfield2 or file1 exasted */
/* outputing unpaired ? */
if (aflg & 2)
/* mark bottom of file2 */
/* input line from file2 */
get2();
/* joinfiled1 < joinfield2 or file2 exasted */
(n2 == 0)) {
/* outputing unpaired ? */
if (aflg & 1)
/* input line from file1 */
get1();
/* line1 and line2 not empty and joinfield1 == joinfield2 */
} else {
/* for lines in file2 that match join field */
/* if not -v output line */
if (!vflg)
/* mark top of file2 */
/* input line from file2 */
get2();
}
/* back to bottom line in file2 */
/* input line form file1 and file2 */
get2();
get1();
for (;;) {
/* compare join fields */
/*
* line1 and line2 not empty and
* joinfield1 == joinfield2
*/
/* if not -v output line */
if (!vflg)
/* input line from file2 */
get2();
/* joinfiled1 < joinfield2 or file2 exasted */
n2 == 0) {
/* seek to bottom of file2 */
/* input line form file1 and file2 */
get2();
get1();
/* file1 exasted or joinfile1 > joinfield2 */
} else {
/* seek to top of file2 */
/* set bottom at top */
/* input line form file2 */
get2();
break;
}
}
}
}
return (0);
}
static int
input(int n) /* get input line and split into fields */
{
int i, c;
int nread;
errno = 0;
if (errno) {
perror("join");
exit(1);
}
return (0);
} else {
"join: invalid char following \""));
perror("");
exit(1);
} else if (errno) {
perror("join");
exit(1);
}
/* Check for long lines */
/* Increase buffer by LINE_MAX */
/* (CBUFSIZE[n] is updated.) */
/* Read rest of line */
errno = 0;
if (errno) {
perror("join");
exit(1);
}
break;
"join: invalid char following \""));
perror("");
exit(1);
} else if (errno) {
perror("join");
exit(1);
}
}
}
}
i = 0;
do {
i++;
/* Check for overflow of ppi[n] */
/* Reallocate larger buffers (CNFLD is updated.) */
/* Restore local pointer */
}
if (!tflg) /* strip multiples */
bp++; /* skip blanks */
}
c != sep2 && c != L'\0') {
bp++;
}
return (i);
}
static void
{
int i;
if (no <= 0) { /* default case */
if (on1)
else
for (i = 0; i < on1; i++)
if (i != j1) {
}
for (i = 0; i < on2; i++)
if (i != j2) {
}
(void) putwchar(L'\n');
} else {
for (i = 0; i < no; i++) {
if (on1)
else
} else
if (i < no - 1)
#if defined(__lint) /* lint doesn't grok "%wc" */
/* EMPTY */;
#else
#endif
else
(void) putwchar(L'\n');
}
}
}
static int
{
int rc;
rc = 0;
else
rc = -1;
rc = 1;
else
return (rc);
}
static void
Usage()
{
"usage: join [-a file_number | -v file_number] [-o list [-e string]]\n"
" [-t char] [-1 field] [-2 field] file1 file2\n\n"
" join [-a file_number] [-j field] [-j1 field] [-j2 field]\n"
" [-o list [-e string]] [-t char] file1 file2\n"));
exit(1);
}
/*
* Allocate memory for buffers.
*/
static void
init_buf()
{
}
static void
int nfld;
{
int i;
for (i = 0; i < 2; i++) {
if (ppi[i]) {
== NULL) {
gettext("realloc pointer table failed\n"));
exit(1);
}
} else {
gettext("calloc pointer table failed\n"));
exit(1);
}
}
}
}
static void
int i;
int size;
{
if (buf[i]) {
== NULL) {
gettext("realloc input buffer failed\n"));
exit(1);
}
} else {
== NULL) {
gettext("calloc input buffer failed\n"));
exit(1);
}
}
}
static void
int onfld;
{
if (olist) {
gettext("realloc olist failed\n"));
exit(1);
}
} else {
if ((olist = (int *)
== NULL) {
gettext("calloc olist failed\n"));
exit(1);
}
}
if (olistf) {
gettext("realloc olistf failed\n"));
exit(1);
}
} else {
if ((olistf = (int *)
== NULL) {
exit(1);
}
}
}