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

  1. /*
  2. **    need: a simple 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.     reg    FILE    *fp;
  15.     char        line[MAXLINE];
  16.  
  17.     if (argc != 2 && argc != 3)
  18.     {
  19.         printf("Usage: need program [file]\n");
  20.         exit(127);
  21.     }
  22.  
  23.     if (argc == 2)
  24.         fp = stdin;
  25.     else
  26.     {
  27.         if ((fp = fopen(argv[2], "r")) == NULL)
  28.         {
  29.             printf("need: cannot open %s\n", argv[2]);
  30.             exit(127);
  31.         }
  32.     }
  33.  
  34.     while (getline(fp, line, MAXLINE) > 0)
  35.         if (match(line, "NEED"))
  36.             exit(match(line, argv[1])? 0: 1);
  37.     
  38.     exit(127);
  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. int
  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.         if (t[j] == '\0')
  72.             return TRUE;
  73.     }
  74.  
  75.     return FALSE;
  76. }
  77.