/*
          Copyright (c) 1996, SAS Institute Inc.
             Unpublished - All Rights Reserved
                   Language: C
         Prints out each unique word and its count.
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "wrdcnt2c.h" /* Wordcount2 include file    */

wordlist *head;

   /*------------------MAIN------------------------------*/
   /* Open input file and call readin                    */
   /*                                                    */
void main()
{
FILE *input;         /* input file */

if ((input=fopen("ddn:DATAIN","r")) != NULL)
                     /* open input file */
   readin(input);
else {
   fprintf(stderr,"Error: cannot open input file\n");
   exit(1);
   }
exit(0);
}

   /*------------------READIN----------------------------*/
   /* Read in words from input file.                     */
   /* Words are truncated if > MAXLEN.                   */
   /* Call insertw - inserts words in wordlist           */
   /* Call printlst - print final wordlist               */
   /*                                                    */
void readin(FILE *f)

{
int c,wordlen=0,inword=FALSE;
char wordstrg(|MAXLEN+1|);

head=(wordlist *)malloc(sizeof(wordlist));
head->next=NULL;
   /* allocate a block of memory of size wordlist       */
   /* return a pointer to the block                     */
   /* assign the pointer to head (head of the wordlist) */
   /* set head->next to NULL (no items in the list)     */
   /* read in characters from input file                */

while((c=fgetc(f)) != EOF) {
      /* alphabetic character test */
   if (isalpha(c)) {
      inword=TRUE;
      if (wordlen <MAXLEN) {
          wordstrg(|wordlen|)=tolower(c);
             /* convert characters to lowercase */
          wordlen++;
          }
      }
      /* if inword, insert in list */
   else if (inword == TRUE) {
      wordstrg(|wordlen|)='\0';
      wordlen=0;
      insertw(wordstrg);
      inword=FALSE;
      }
   }
   /* print wordlist */
printlst(head->next);
}