home *** CD-ROM | disk | FTP | other *** search
- /* ------------------------------------------------------ */
- /* FGREP.C */
- /* ------------------------------------------------------ */
-
- #include <stdio.h>
- #include <conio.h>
- #include <dir.h>
- #include <ctype.h>
- #include <string.h>
-
- #define VERSION "2.0"
- #define MAXLINE 120 /* max. Zeilenlänge */
- #define TRUE 1
- #define FALSE 0
-
- char upper = FALSE; /* Option i */
- char count = FALSE; /* Option c */
- char fnonly = FALSE; /* Option l */
- char linen = FALSE; /* Option n */
- char header = TRUE; /* Option h */
- char nomatch = FALSE; /* Option v */
-
- void usage(void)
- {
- cprintf("\nFGREP Version %s\n\n", VERSION);
- cprintf("Syntax : FGREP <Suchmuster> <Datei(en)> ");
- cprintf("[<Optionen>]\n\n");
- cprintf("Optionen: i Groß-/Kleinschreibung nicht ");
- cprintf("beachten\n");
- cprintf(" l nur Dateinamen ausgeben\n");
- cprintf(" c Zeilen zählen\n");
- cprintf(" h kein Dateinamen/Zeilennummer ");
- cprintf("ausgeben\n");
- cprintf(" n Zeilennummer ausgeben\n");
- cprintf(" v alle Zeilen ausgeben, die nicht ");
- cprintf("übereinstimmen\n\n");
- exit(1);
- }
-
- void delete(str,pos,n)
- /* n Zeichen aus str ab pos löschen */
- char *str;
- int pos,n;
- {
- int i = pos + n;
- while ((str[pos++] = str[i++]) != '\0');
- }
-
- void del_spaces(line)
- /* Führende Leerzeichen (Space und Tab) entfernen */
- char *line;
- {
- while (line[0]==' ' || line[0]==0x09) delete(line, 0, 1);
- }
-
- void getpath(path, line)
- /* Pfad aus line (enthält Pfad + Dateiname) */
- /* nach path kopieren */
- char *path, *line;
- {
- char *i;
- strcpy(path,line);
- if ((i = strrchr(path,'\\')) != NULL) *(i+1) = '\0';
- else *path = '\0';
- strupr(path);
- }
-
- FILE *open_file(name)
- /* Datei öffnen */
- char *name;
- {
- FILE *fp;
- if ((fp=fopen(name,"r")) == 0)
- {
- fprintf
- (stderr,"Datei %s kann nicht geöffnet werden",name);
- exit(1);
- }
- return fp;
- }
-
- char strstrc(char *str1, char *str2)
- /* Feststellen, ob str2 in str1 enthalten ist */
- /* In Abhängigkeit von upper Groß-/Kleinschreibung */
- /* beachten bzw. nicht beachten */
- {
- if (upper)
- {
- int i, n = strlen(str2), len = strlen(str1) - n;
- for (i = 0; i <= len; i++)
- if (strnicmp(&str1[i], str2, n) == NULL) return TRUE;
- }
- return strstr(str1, str2) != NULL;
- }
-
- void main(argc,argv)
- int argc;
- char *argv[];
- {
- char line[MAXLINE], path[120], name[120], match;
- int lineno, eod, cnt = 0;
- struct ffblk dir; /* Directory */
- FILE *fp;
-
- if (argc < 3) usage(); /* falsche Parameteranzahl */
- if (argc == 4) /* Optionen prüfen */
- {
- argv[3] = strupr(argv[3]);
- if (strchr(argv[3], 'I') != NULL) upper = TRUE;
- if (strchr(argv[3], 'N') != NULL) linen = TRUE;
- if (strchr(argv[3], 'C') != NULL) count = TRUE;
- if (strchr(argv[3], 'L') != NULL) fnonly = TRUE;
- if (strchr(argv[3], 'H') != NULL) header = FALSE;
- if (strchr(argv[3], 'V') != NULL) nomatch = TRUE;
- }
- getpath(path,argv[2]);
- /* ersten Eintrag im Directory suchen */
- if ((eod = findfirst(argv[2], &dir, 0)) != 0)
- {
- fprintf(stderr, "Keine Dateien %s gefunden", argv[2]);
- exit(1);
- }
- /* Text in allen Dateien suchen */
- while (!eod)
- {
- lineno=0;
- strcpy(name, path);
- strcat(name, dir.ff_name);
- fp = open_file(name);
- while (fgets(line, MAXLINE, fp) != NULL)
- {
- if (((match = strstrc(line, argv[1])) == TRUE
- && !nomatch) || (!match && nomatch))
- {
- cnt++;
- if (!count)
- {
- if (header)
- {
- printf("%-12s ", dir.ff_name);
- if (fnonly)
- {
- putchar('\n');
- break;
- }
- if (linen) printf("%04d", lineno);
- putchar(':');
- }
- del_spaces(line);
- printf("%s", line);
- }
- }
- lineno++;
- }
- fclose(fp);
- eod = findnext(&dir);
- }
- if (count) printf("%d Zeilen gefunden",cnt);
- }
- /* ------------------------------------------------------ */
- /* Ende von FGREP.C */