home *** CD-ROM | disk | FTP | other *** search
- /*********************
- *
- * gfind.c - Find strings in filenames which match wildcard.
- *
- * Purpose: This program expands the wildcard input to all files and
- * outputs them to the standard output.
- *
- * gfind [string] [file] <options> to find string in file
- * where string can include ? wildcard character
- * file can include ? or * wildcard characters
- *
- * options: -n output line number found
- * -c search case sensitive
- * -f output file name second (for sort)
- * -F output file name first (before string)
- * -s output string name
- * -x suppress output of text line found in
- *
- * default is: -n
- *
- * Blackstar C Function Library
- * (c) Copyright 1985 Sterling Castle Software
- *
- *******/
-
- #include <stdio.h>
- #include <string.h>
- #include <process.h>
- #include "blackstr.h"
-
- char *fptr[200], fbuff[8192], temp[MAXLINE];
- char *(*search)(); /* pointer to match function */
-
- /********
- *
- * fstrfile(fnam,strptr,opt) - find strings in file
- *
- **/
-
- void fstrfile(char *fnam, char *strptr, int opt){
- char buff[139];
- char *bptr;
- int ln,i,lf;
- FILE *fp;
- extern char *(*search)();
-
- lf = i = 0;
- ln = 1;
- strupr(fnam);
- printf("\n ---- %s \n",fnam);
- if ((fp=fopen(fnam,"r")) != 0)
- while (fgets(buff,138,fp)) {
- if ((bptr = (*search)(buff, strptr)) != 0) {
- lf = FALSE;
- if (opt&0x80)
- printf("%s/ ",fnam);
- if (opt&0x02)
- printf("%s/ ",strncpy(temp,bptr,strlen(strptr)));
- if (opt&0x01)
- printf("%s/ ",fnam); /* for str first */
- if (opt&0x04)
- printf("%4d/ ",ln);
- if (opt&0x08) {
- printf("%s",buff);
- lf = TRUE;
- }
- if (!lf)
- printf("\n");
- }
- ++ln;
- i=0;
- }
- else
- printf("\n ERROR file not found -- %s",fnam);
- fclose(fp);
- }
-
-
- /********
- *
- * get_opts(nopt,optr)
- *
- **/
-
- int get_opts(int nopt, char *optr[]){
- int i,opt;
- char *ptr;
- extern char *(*search)();
- extern char *st_instr(),*st_instri();
-
- opt = 0x0c; /* default to print line number and text line */
- search = st_instri; /* ignore case is default */
- if(nopt<=3)
- return(12); /* text and line number */
- for(i=3; i<nopt; ++i) {
- ptr = optr[i];
- switch (ptr[1]) {
- case 'c':
- search = st_instr;
- break;
- case 'l':
- case 'n':
- opt |= 4; /* line number */
- break;
- case 'f':
- opt |= 1; /* file name */
- break;
- case 's':
- opt |= 2; /* string name */
- break;
- case 'x':
- opt &= 0xf7; /* no text line */
- break;
- case 'F':
- opt |= 0x80; /* file name first */
- break;
- }
- }
- return(opt);
- }
-
-
- /********
- *
- * main() - This program expands the wildcard input to all files and
- * outputs them to the standard output.
- **/
-
- void main(int argc, char *argv[]){
- int i, fcnt, opt;
- char str[80], *ptr;
-
- if (argc<3) {
- printf("Usage: gfind <string> <file> [options]\n\n");
- printf("\t<string> may include ? wildcard.\n");
- printf("\t <file> may include ? or * wildcards.\n\n");
- printf("\tOptions:\n");
- printf("\t-n output line number found\n");
- printf("\t-c search case sensitive\n");
- printf("\t-f output file name second (for sort)\n");
- printf("\t-F output file name first\n");
- printf("\t-s output string name\n");
- printf("\t-x suppress output of text line found in\n\n");
- printf("\tdefault:\n");
- printf("\t-n\n");
- exit(-1);
- }
-
- strcpy(str, argv[1]);
- ptr = argv[2]; /* point to file name */
- if (st_wild(ptr)){
- fcnt = fl_files(fbuff, ptr, TRUE); /* get wildcard names */
- st_ptrs(fbuff, fptr, fcnt); /* pointers to fptr */
- }
-
- else {
- fptr[0] = fbuff; /* put file name in fbuff */
- strcpy (fptr[0], argv[2]);
- fptr[1] = 0;
- fcnt = 1;
- }
-
- /* Get options */
- i = 0;
- opt = get_opts(argc, argv);
-
- /* Now search files */
- while(fptr[i]) {
- fstrfile(fptr[i], str, opt); /* all parms, string first */
- ++i;
- }
-
- if(!fcnt) {
- printf("No files found. ");
- exit(-1);
- }
- }
-