/* tsc.c, BOONE, 09/11/92
   C version of ts
   Copyright (C) 1992, Trustees of Michigan State University */

/* Modifications:
   09/11/92 Boone      Initial coding
   End Modifications */

/* Version 1.6 */

#include <stdio.h>
#include <strings.h>

#define MAXSTR       256
#define INDEXFL      ".ts/.tsdata"

void chop(char *line)

{
	line[strlen(line)-1] = '\0';
}

char *stristr(char *one, char *two)

{
	int i;
	char lone[MAXSTR];
	char ltwo[MAXSTR];

	for (i = 0; i <= strlen(one); i++)
		lone[i] = toupper(one[i]);
	for (i = 0; i <= strlen(two); i++)
		ltwo[i] = toupper(two[i]);

	if (strstr(lone, ltwo) != NULL)
		return one;
	else
		return NULL;
}

int main (int argc, char *argv[])

{
	char line[MAXSTR+1];
	char *cp1, *cp2;
	FILE *fd;
	char searchwords[16][MAXSTR+1];
	short numwords;
	int i, j, k;
	char charwas;

	numwords = 0;
	for (i = 1; i < argc; i++)
	{
		cp2 = searchwords[numwords];
		for (cp1 = argv[i]; *cp1; cp1++)
		{
			if (*cp1 != ' ')
				*cp2++ = *cp1;
			else
			{
				*cp2 = '\0';
				numwords++;
				cp2 = searchwords[numwords];
			}
		}
		*cp2 = '\0';
		numwords++;
	}
	if ((fd = fopen(INDEXFL, "r")) == NULL)
	{
		fprintf(stderr, "Can't open file\n");
		exit(1);
	}
	fgets(line, MAXSTR, fd);
	chop(line);
	while (! feof(fd))
	{
		if ((cp1 = strchr(line, '\t')) != NULL)
		{
			charwas = *cp1;
			*cp1 = '\0';
		}
		for (i = 0; i < numwords; i++)
		{
			if ((cp2 = stristr(line, searchwords[i])) != NULL)
			{
				*cp1 = charwas;
				fputs(line, stdout);
				fputs("\r\n", stdout);
				break;
			}
		}
		fgets(line, MAXSTR, fd);
		chop(line);
	}
	fclose(fd);
	fputs(".\r\n", stdout);
}
