ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

findstr.c
Go to the documentation of this file.
00001 /* findstr.c */
00002 
00003 /* Copyright (C) 1994-2002, Jim Hall <jhall@freedos.org> */
00004 
00005 /* Adapted for ReactOS -Edited for Findstr.exe K'Williams */
00006 
00007 /*
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License along
00019    with this program; if not, write to the Free Software Foundation, Inc.,
00020    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021 */
00022 
00023 
00024 /* This program locates a string in a text file and prints those lines
00025  * that contain the string.  Multiple files are clearly separated.
00026  */
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <ctype.h>
00032 #include <windows.h>
00033 
00034 #include <io.h>
00035 #include <dos.h>
00036 
00037 #include "resource.h"
00038 
00039 
00040 /* Symbol definition */
00041 #define MAX_STR 1024
00042 
00043 
00044 /* This function prints out all lines containing a substring.  There are some
00045  * conditions that may be passed to the function.
00046  *
00047  * RETURN: If the string was found at least once, returns 1.
00048  * If the string was not found at all, returns 0.
00049  */
00050 int
00051 find_str (char *sz, FILE *p, int invert_search,
00052           int count_lines, int number_output, int ignore_case, int at_start, int literal_search,
00053           int at_end, int reg_express, int exact_match, int sub_dirs, int only_fname)
00054 {
00055   int i, length;
00056   long line_number = 0, total_lines = 0;
00057   char *c, temp_str[MAX_STR], this_line[MAX_STR];
00058 
00059   /* Convert to upper if needed */
00060   if (ignore_case)
00061     {
00062       length = strlen (sz);
00063       for (i = 0; i < length; i++)
00064     sz[i] = toupper (sz[i]);
00065     }
00066 
00067   /* Scan the file until EOF */
00068   while (fgets (temp_str, MAX_STR, p) != NULL)
00069     {
00070       /* Remove the trailing newline */
00071       length = strlen (temp_str);
00072       if (temp_str[length-1] == '\n')
00073     {
00074       temp_str[length-1] = '\0';
00075     }
00076 
00077       /* Increment number of lines */
00078       line_number++;
00079       strcpy (this_line, temp_str);
00080 
00081       /* Convert to upper if needed */
00082       if (ignore_case)
00083     {
00084       for (i = 0; i < length; i++)
00085         {
00086           temp_str[i] = toupper (temp_str[i]);
00087         }
00088     }
00089 
00090       /* Locate the substring */
00091 
00092       /* strstr() returns a pointer to the first occurrence in the
00093        string of the substring */
00094       c = strstr (temp_str, sz);
00095 
00096       if ( ((invert_search) ? (c == NULL) : (c != NULL)) )
00097     {
00098       if (!count_lines)
00099         {
00100           if (number_output)
00101         printf ("%ld:", line_number);
00102 
00103           /* Print the line of text */
00104           puts (this_line);
00105         }
00106 
00107       total_lines++;
00108     } /* long if */
00109     } /* while fgets */
00110 
00111   if (count_lines)
00112     {
00113       /* Just show num. lines that contain the string */
00114       printf ("%ld\n", total_lines);
00115     }
00116 
00117 
00118  /* RETURN: If the string was found at least once, returns 1.
00119   * If the string was not found at all, returns 0.
00120   */
00121   return (total_lines > 0 ? 1 : 0);
00122 }
00123 
00124 /* Show usage */
00125 void
00126 usage (void)
00127 {
00128     TCHAR lpUsage[4096];
00129 
00130     LoadString( GetModuleHandle(NULL), IDS_USAGE, (LPTSTR)lpUsage, 4096);
00131     CharToOem(lpUsage, lpUsage);
00132     printf( lpUsage );
00133 }
00134 
00135 
00136 /* Main program */
00137 int
00138 main (int argc, char **argv)
00139 {
00140   char *opt, *needle = NULL;
00141   int ret = 0;
00142   TCHAR lpMessage[4096];
00143 
00144   int invert_search = 0;        /* flag to invert the search */
00145   int count_lines = 0;          /* flag to whether/not count lines */
00146   int number_output = 0;        /* flag to print line numbers */
00147   int ignore_case = 0;          /* flag to be case insensitive */
00148   int at_start = 0;             /* flag to Match if at the beginning of a line. */
00149   int at_end = 0;               /* flag to Match if at the beginning of a line. */
00150   int reg_express = 0;         /* flag to use/not use regular expressions */
00151   int exact_match = 0;          /* flag to be exact match */
00152   int sub_dirs= 0;              /* this and all subdirectories */
00153   int only_fname= 0;            /* print only the name of the file*/
00154   int literal_search=0;
00155 
00156   FILE *pfile;              /* file pointer */
00157   int hfind;                /* search handle */
00158   struct _finddata_t finddata;      /* _findfirst, filenext block */
00159 
00160   /* Scan the command line */
00161   while ((--argc) && (needle == NULL))
00162     {
00163       if (*(opt = *++argv) == '/')
00164         {
00165           switch (opt[1])
00166         {
00167           case 'b':
00168           case 'B':     /* Matches pattern if at the beginning of a line */
00169             at_start = 1;
00170             break;
00171             
00172           //case 'c':
00173           //case 'C':       /* Literal? */
00174           //  literal_search = 1;
00175           //  break;
00176 
00177           case 'e':
00178           case 'E':     /* matches pattern if at end of line */
00179             at_end = 1;
00180             break;
00181             
00182           case 'i':
00183           case 'I':     /* Ignore */
00184             ignore_case = 1;
00185             break;
00186 
00187           case 'm':
00188           case 'M':     /* only filename */
00189             only_fname = 1;
00190             break;          
00191             
00192           case 'n':
00193           case 'N':     /* Number */
00194             number_output = 1;
00195             break;
00196             
00197           case 'r':
00198           case 'R':     /* search strings as regular expressions */
00199             reg_express = 1;
00200             break;  
00201 
00202           case 's':
00203           case 'S':     /* search files in child directory too*/
00204             sub_dirs = 1;
00205             break;              
00206 
00207           case 'v':
00208           case 'V':     /* Not with */
00209             invert_search = 1;
00210             break;
00211 
00212           case 'x':
00213           case 'X':     /* exact match */
00214             exact_match = 1;
00215             break;          
00216             
00217           default:
00218             usage ();
00219             exit (2);       /* syntax error .. return error 2 */
00220             break;
00221         }
00222         }
00223       else
00224         {
00225           /* Get the string */
00226       if (needle == NULL)
00227         {
00228               /* Assign the string to find */
00229               needle = *argv;
00230         }
00231     }
00232     }
00233 
00234   /* Check for search string */
00235   if (needle == NULL)
00236     {
00237       /* No string? */
00238       usage ();
00239       exit (1);
00240     }
00241 
00242   /* Scan the files for the string */
00243   if (argc == 0)
00244     {
00245       ret = find_str (needle, stdin, invert_search, count_lines,
00246                       number_output, ignore_case, at_start, literal_search, at_end, reg_express, exact_match,
00247                       sub_dirs, only_fname);
00248     }
00249 
00250   while (--argc >= 0)
00251     {
00252       hfind = _findfirst (*++argv, &finddata);
00253       if (hfind < 0)
00254     {
00255       /* We were not able to find a file. Display a message and
00256          set the exit status. */
00257       LoadString( GetModuleHandle(NULL), IDS_NO_SUCH_FILE, (LPTSTR)lpMessage, 4096);
00258       CharToOem(lpMessage, lpMessage);
00259       fprintf (stderr, lpMessage, *argv);//
00260     }
00261       else
00262         {
00263           /* repeat find next file to match the filemask */
00264       do
00265             {
00266               /* We have found a file, so try to open it */
00267           if ((pfile = fopen (finddata.name, "r")) != NULL)
00268             {
00269               printf ("---------------- %s\n", finddata.name);
00270               ret = find_str (needle, pfile, invert_search, count_lines,
00271                       number_output, ignore_case, at_start, literal_search, at_end, reg_express, exact_match,
00272                       sub_dirs, only_fname);
00273               fclose (pfile);
00274             }
00275           else
00276             {
00277               LoadString(GetModuleHandle(NULL), IDS_CANNOT_OPEN, (LPTSTR)lpMessage, 4096);
00278               CharToOem(lpMessage, lpMessage);
00279               fprintf (stderr, lpMessage,
00280                    finddata.name);
00281                 }
00282         }
00283           while (_findnext(hfind, &finddata) > 0);
00284         }
00285       _findclose(hfind);
00286     } /* for each argv */
00287 
00288  /* RETURN: If the string was found at least once, returns 0.
00289   * If the string was not found at all, returns 1.
00290   * (Note that find_str.c returns the exact opposite values.)
00291   */
00292   exit ( (ret ? 0 : 1) );
00293 }
00294 
00295 

Generated on Sat May 26 2012 04:15:37 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.