home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume12 / cake / part04 / Aux / needed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-14  |  1001 b   |  78 lines

  1. /*
  2. **    needed: an even simpler utility for text processing with cake
  3. */
  4.  
  5. #include    <stdio.h>
  6. #include    "std.h"
  7.  
  8. #define    MAXLINE    256
  9.  
  10. main(argc, argv)
  11. int    argc;
  12. char    **argv;
  13. {
  14.     extern    char    *match();
  15.     reg    FILE    *fp;
  16.     reg    char    *rest;
  17.     char        line[MAXLINE];
  18.  
  19.     if (argc != 2)
  20.     {
  21.         printf("Usage: needed file\n");
  22.         exit(127);
  23.     }
  24.  
  25.     if ((fp = fopen(argv[1], "r")) == NULL)
  26.     {
  27.         printf("needed: cannot open %s\n", argv[1]);
  28.         exit(127);
  29.     }
  30.  
  31.     while (getline(fp, line, MAXLINE) > 0)
  32.         if ((rest = match(line, "NEED")) != NULL)
  33.         {
  34.             printf("%s", rest);
  35.             exit(0);
  36.         }
  37.  
  38.     exit(0);
  39. }
  40.  
  41. int
  42. getline(fp, s, lim)
  43. reg    FILE    *fp;
  44. char        s[];
  45. reg    int    lim;
  46. {
  47.     reg    int    c, i;
  48.  
  49.     i = 0;
  50.     while (--lim > 0 && (c = getc(fp)) != EOF && c != '\n')
  51.         s[i++] = c;
  52.  
  53.     if (c == '\n')
  54.         s[i++] = c;
  55.  
  56.     s[i] = '\0';
  57.     return i;
  58. }
  59.  
  60. char *
  61. match(s, t)
  62. reg    char    *s;
  63. reg    char    *t;
  64. {
  65.     reg    int    i, j;
  66.  
  67.     for (; *s != '\0'; s++)
  68.     {
  69.         for (i = 0, j = 0; t[j] != '\0' && s[i] == t[j]; i++, j++)
  70.             ;
  71.  
  72.         if (t[j] == '\0')
  73.             return s+j;
  74.     }
  75.  
  76.     return NULL;
  77. }
  78.