Listing 2

/*
 * xr.c - a cross-reference generator
 */
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "xrt.h"

int getword(char *word, size_t lim)
   {
   int c;
   char *w = word;
   
   assert(lim > 2);
   while (isspace(c = fgetc(stdin)) && c != '\n')
      ;
   if (c != EOF)
      *w++ = c;
   if (!isalpha(c))
      {
      *w = '\0';
      return c;
      }
   for ( ; lim-- > 0; ++w)
      if (!isalnum(*w = fgetc(stdin)))
          {
          ungetc(*w, stdin);
          break;
          }
   *w = '\0';
   return *word;
   }

#define MAXWORD 100

int main(void)
   {
   char word[MAXWORD];
   unsigned lineno = 1;
   
   while (getword(word, MAXWORD) != EOF)
      if (isalpha(word[0]))
          xrt_add(word, lineno);
      else if (word[0] == '\n')
          ++lineno;
   xrt_print();
   return 0;
   }