home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / f / find37.zip / find-3.7 / locate / bigram.c next >
C/C++ Source or Header  |  1992-07-14  |  2KB  |  88 lines

  1. /* bigram -- list bigrams for locate
  2.  
  3.    Usage: bigram < text > bigrams
  4.    
  5.    Use 'code' to encode a file using this output.
  6.  
  7.    Author: James A. Woods (jaw@riacs.edu)
  8.    Modified by David MacKenzie (djm@ai.mit.edu)
  9.    Public domain. */
  10.  
  11. #include <stdio.h>
  12. #if defined(USG) || defined(STDC_HEADERS)
  13. #include <string.h>
  14. #else
  15. #include <strings.h>
  16. #endif
  17. #ifdef STDC_HEADERS
  18. #include <stdlib.h>
  19. #endif
  20. #include <sys/types.h>
  21. #include "pathmax.h"
  22.  
  23. char *xmalloc ();
  24. int prefix_length ();
  25.  
  26. /* The name this program was run with.  */
  27. char *program_name;
  28.  
  29. char *path;
  30.  
  31. char *oldpath;
  32.  
  33. void
  34. main (argc, argv)
  35.      int argc;
  36.      char **argv;
  37. {
  38.   register int count, j;
  39.   unsigned line_length;
  40.   int path_max;
  41.  
  42.   program_name = argv[0];
  43.   path_max = PATH_MAX;
  44.   path = xmalloc (path_max + 2);
  45.   oldpath = xmalloc (path_max + 2);
  46.   path[path_max] = '\0';
  47.   strcpy (oldpath, " ");
  48.  
  49.   while (fgets (path, path_max, stdin) != NULL)
  50.     {
  51.       line_length = strlen (path);
  52.       if (line_length == 0)
  53.     fprintf (stderr, "%s: null line in input\n", argv[0]);
  54.       else if (path[line_length - 1] != '\n')
  55.     fprintf (stderr, "%s: long line in input; truncating to `%s'\n",
  56.          argv[0], path);
  57.       else
  58.     path[line_length - 1] = '\0'; /* Remove newline. */
  59.  
  60.       count = prefix_length (oldpath, path);
  61.       /* Output post-residue bigrams only. */
  62.       for (j = count; path[j] != '\0'; j += 2)
  63.     {
  64.       if (path[j + 1] == '\0')
  65.         break;
  66.       putchar (path[j]);
  67.       putchar (path[j + 1]);
  68.       putchar ('\n');
  69.     }
  70.       strcpy (oldpath, path);
  71.     }
  72.   exit (0);
  73. }
  74.  
  75. /* Return length of longest common prefix of strings S1 and S2. */
  76.  
  77. int
  78. prefix_length (s1, s2)
  79.      char *s1, *s2;
  80. {
  81.   register char *start;
  82.  
  83.   for (start = s1; *s1 == *s2; s1++, s2++)
  84.     if (*s1 == '\0')
  85.       break;
  86.   return s1 - start;
  87. }
  88.