home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1989 / 04 / tricks / fgrep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-10  |  4.5 KB  |  162 lines

  1. /* ------------------------------------------------------ */
  2. /*                      FGREP.C                           */
  3. /* ------------------------------------------------------ */
  4.  
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <dir.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10.  
  11. #define VERSION "2.0"
  12. #define MAXLINE 120                   /* max. Zeilenlänge */
  13. #define TRUE  1
  14. #define FALSE 0
  15.  
  16. char upper   = FALSE;                /* Option i */
  17. char count   = FALSE;                /* Option c */
  18. char fnonly  = FALSE;                /* Option l */
  19. char linen   = FALSE;                /* Option n */
  20. char header  = TRUE;                 /* Option h */
  21. char nomatch = FALSE;                /* Option v */
  22.  
  23. void usage(void)
  24. {
  25.   cprintf("\nFGREP Version %s\n\n", VERSION);
  26.   cprintf("Syntax  : FGREP <Suchmuster> <Datei(en)> ");
  27.   cprintf("[<Optionen>]\n\n");
  28.   cprintf("Optionen: i Groß-/Kleinschreibung nicht ");
  29.   cprintf("beachten\n");
  30.   cprintf("          l nur Dateinamen ausgeben\n");
  31.   cprintf("          c Zeilen zählen\n");
  32.   cprintf("          h kein Dateinamen/Zeilennummer ");
  33.   cprintf("ausgeben\n");
  34.   cprintf("          n Zeilennummer ausgeben\n");
  35.   cprintf("          v alle Zeilen ausgeben, die nicht ");
  36.   cprintf("übereinstimmen\n\n");
  37.   exit(1);
  38. }
  39.  
  40. void delete(str,pos,n)
  41.                       /* n Zeichen aus str ab pos löschen */
  42. char *str;
  43. int pos,n;
  44. {
  45.   int i = pos + n;
  46.   while ((str[pos++] = str[i++]) != '\0');
  47. }
  48.  
  49. void del_spaces(line)
  50.         /* Führende Leerzeichen (Space und Tab) entfernen */
  51. char *line;
  52. {
  53.   while (line[0]==' ' || line[0]==0x09) delete(line, 0, 1);
  54. }
  55.  
  56. void getpath(path, line)
  57.               /* Pfad aus line (enthält Pfad + Dateiname) */
  58.               /* nach path kopieren                       */
  59. char *path, *line;
  60. {
  61.   char *i;
  62.   strcpy(path,line);
  63.   if ((i = strrchr(path,'\\')) != NULL) *(i+1) = '\0';
  64.   else *path = '\0';
  65.   strupr(path);
  66. }
  67.  
  68. FILE *open_file(name)
  69.                                           /* Datei öffnen */
  70. char *name;
  71. {
  72. FILE *fp;
  73.   if ((fp=fopen(name,"r")) == 0)
  74.   {
  75.     fprintf
  76.         (stderr,"Datei %s kann nicht geöffnet werden",name);
  77.     exit(1);
  78.   }
  79.   return fp;
  80. }
  81.  
  82. char strstrc(char *str1, char *str2)
  83.   /* Feststellen, ob str2 in str1 enthalten ist           */
  84.   /* In Abhängigkeit von upper Groß-/Kleinschreibung      */
  85.   /* beachten bzw. nicht beachten                         */
  86. {
  87.   if (upper)
  88.   {
  89.     int i, n = strlen(str2), len = strlen(str1) - n;
  90.     for (i = 0; i <= len; i++)
  91.       if (strnicmp(&str1[i], str2, n) == NULL) return TRUE;
  92.   }
  93.   return strstr(str1, str2) != NULL;
  94. }
  95.  
  96. void main(argc,argv)
  97. int argc;
  98. char *argv[];
  99. {
  100.   char line[MAXLINE], path[120], name[120], match;
  101.   int lineno, eod, cnt = 0;
  102.   struct ffblk dir;                          /* Directory */
  103.   FILE *fp;
  104.  
  105.   if (argc < 3) usage();       /* falsche Parameteranzahl */
  106.   if (argc == 4)               /* Optionen prüfen         */
  107.   {
  108.     argv[3] = strupr(argv[3]);
  109.     if (strchr(argv[3], 'I') != NULL) upper   = TRUE;
  110.     if (strchr(argv[3], 'N') != NULL) linen   = TRUE;
  111.     if (strchr(argv[3], 'C') != NULL) count   = TRUE;
  112.     if (strchr(argv[3], 'L') != NULL) fnonly  = TRUE;
  113.     if (strchr(argv[3], 'H') != NULL) header  = FALSE;
  114.     if (strchr(argv[3], 'V') != NULL) nomatch = TRUE;
  115.   }
  116.   getpath(path,argv[2]);
  117.                     /* ersten Eintrag im Directory suchen */
  118.   if ((eod = findfirst(argv[2], &dir, 0)) != 0)
  119.   {
  120.     fprintf(stderr, "Keine Dateien %s gefunden", argv[2]);
  121.     exit(1);
  122.   }
  123.                           /* Text in allen Dateien suchen */
  124.   while (!eod)
  125.   {
  126.     lineno=0;
  127.     strcpy(name, path);
  128.     strcat(name, dir.ff_name);
  129.     fp = open_file(name);
  130.     while (fgets(line, MAXLINE, fp) != NULL)
  131.     {
  132.       if (((match = strstrc(line, argv[1])) == TRUE
  133.             && !nomatch) || (!match && nomatch))
  134.       {
  135.         cnt++;
  136.         if (!count)
  137.         {
  138.           if (header)
  139.           {
  140.             printf("%-12s ", dir.ff_name);
  141.             if (fnonly)
  142.             {
  143.               putchar('\n');
  144.               break;
  145.             }
  146.             if (linen) printf("%04d", lineno);
  147.             putchar(':');
  148.           }
  149.           del_spaces(line);
  150.           printf("%s", line);
  151.         }
  152.       }
  153.       lineno++;
  154.     }
  155.     fclose(fp);
  156.     eod = findnext(&dir);
  157.   }
  158.   if (count) printf("%d Zeilen gefunden",cnt);
  159. }
  160. /* ------------------------------------------------------ */
  161. /*                  Ende von FGREP.C                      */
  162.