Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenfind.cGo to the documentation of this file.00001 /* find.c */ 00002 00003 /* Copyright (C) 1994-2002, Jim Hall <jhall@freedos.org> */ 00004 00005 /* Adapted for ReactOS */ 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) 00053 { 00054 int i, length; 00055 long line_number = 0, total_lines = 0; 00056 char *c, temp_str[MAX_STR], this_line[MAX_STR]; 00057 00058 /* Convert to upper if needed */ 00059 if (ignore_case) 00060 { 00061 length = strlen (sz); 00062 for (i = 0; i < length; i++) 00063 sz[i] = toupper (sz[i]); 00064 } 00065 00066 /* Scan the file until EOF */ 00067 while (fgets (temp_str, MAX_STR, p) != NULL) 00068 { 00069 /* Remove the trailing newline */ 00070 length = strlen (temp_str); 00071 if (temp_str[length-1] == '\n') 00072 { 00073 temp_str[length-1] = '\0'; 00074 } 00075 00076 /* Increment number of lines */ 00077 line_number++; 00078 strcpy (this_line, temp_str); 00079 00080 /* Convert to upper if needed */ 00081 if (ignore_case) 00082 { 00083 for (i = 0; i < length; i++) 00084 { 00085 temp_str[i] = toupper (temp_str[i]); 00086 } 00087 } 00088 00089 /* Locate the substring */ 00090 00091 /* strstr() returns a pointer to the first occurrence in the 00092 string of the substring */ 00093 c = strstr (temp_str, sz); 00094 00095 if ( ((invert_search) ? (c == NULL) : (c != NULL)) ) 00096 { 00097 if (!count_lines) 00098 { 00099 if (number_output) 00100 printf ("%ld:", line_number); 00101 00102 /* Print the line of text */ 00103 puts (this_line); 00104 } 00105 00106 total_lines++; 00107 } /* long if */ 00108 } /* while fgets */ 00109 00110 if (count_lines) 00111 { 00112 /* Just show num. lines that contain the string */ 00113 printf ("%ld\n", total_lines); 00114 } 00115 00116 00117 /* RETURN: If the string was found at least once, returns 1. 00118 * If the string was not found at all, returns 0. 00119 */ 00120 return (total_lines > 0 ? 1 : 0); 00121 } 00122 00123 /* Show usage */ 00124 void 00125 usage (void) 00126 { 00127 TCHAR lpUsage[4096]; 00128 00129 LoadString( GetModuleHandle(NULL), IDS_USAGE, (LPTSTR)lpUsage, 4096); 00130 CharToOem(lpUsage, lpUsage); 00131 printf( lpUsage ); 00132 } 00133 00134 00135 /* Main program */ 00136 int 00137 main (int argc, char **argv) 00138 { 00139 char *opt, *needle = NULL; 00140 int ret = 0; 00141 TCHAR lpMessage[4096]; 00142 00143 int invert_search = 0; /* flag to invert the search */ 00144 int count_lines = 0; /* flag to whether/not count lines */ 00145 int number_output = 0; /* flag to print line numbers */ 00146 int ignore_case = 0; /* flag to be case insensitive */ 00147 00148 FILE *pfile; /* file pointer */ 00149 int hfind; /* search handle */ 00150 struct _finddata_t finddata; /* _findfirst, filenext block */ 00151 00152 /* Scan the command line */ 00153 while ((--argc) && (needle == NULL)) 00154 { 00155 if (*(opt = *++argv) == '/') 00156 { 00157 switch (opt[1]) 00158 { 00159 case 'c': 00160 case 'C': /* Count */ 00161 count_lines = 1; 00162 break; 00163 00164 case 'i': 00165 case 'I': /* Ignore */ 00166 ignore_case = 1; 00167 break; 00168 00169 case 'n': 00170 case 'N': /* Number */ 00171 number_output = 1; 00172 break; 00173 00174 case 'v': 00175 case 'V': /* Not with */ 00176 invert_search = 1; 00177 break; 00178 00179 default: 00180 usage (); 00181 exit (2); /* syntax error .. return error 2 */ 00182 break; 00183 } 00184 } 00185 else 00186 { 00187 /* Get the string */ 00188 if (needle == NULL) 00189 { 00190 /* Assign the string to find */ 00191 needle = *argv; 00192 } 00193 } 00194 } 00195 00196 /* Check for search string */ 00197 if (needle == NULL) 00198 { 00199 /* No string? */ 00200 usage (); 00201 exit (1); 00202 } 00203 00204 /* Scan the files for the string */ 00205 if (argc == 0) 00206 { 00207 ret = find_str (needle, stdin, invert_search, count_lines, 00208 number_output, ignore_case); 00209 } 00210 00211 while (--argc >= 0) 00212 { 00213 hfind = _findfirst (*++argv, &finddata); 00214 if (hfind < 0) 00215 { 00216 /* We were not able to find a file. Display a message and 00217 set the exit status. */ 00218 LoadString( GetModuleHandle(NULL), IDS_NO_SUCH_FILE, (LPTSTR)lpMessage, 4096); 00219 CharToOem(lpMessage, lpMessage); 00220 fprintf (stderr, lpMessage, *argv);// 00221 } 00222 else 00223 { 00224 /* repeat find next file to match the filemask */ 00225 do 00226 { 00227 /* We have found a file, so try to open it */ 00228 if ((pfile = fopen (finddata.name, "r")) != NULL) 00229 { 00230 printf ("---------------- %s\n", finddata.name); 00231 ret = find_str (needle, pfile, invert_search, count_lines, 00232 number_output, ignore_case); 00233 fclose (pfile); 00234 } 00235 else 00236 { 00237 LoadString(GetModuleHandle(NULL), IDS_CANNOT_OPEN, (LPTSTR)lpMessage, 4096); 00238 CharToOem(lpMessage, lpMessage); 00239 fprintf (stderr, lpMessage, 00240 finddata.name); 00241 } 00242 } 00243 while (_findnext(hfind, &finddata) > 0); 00244 } 00245 _findclose(hfind); 00246 } /* for each argv */ 00247 00248 /* RETURN: If the string was found at least once, returns 0. 00249 * If the string was not found at all, returns 1. 00250 * (Note that find_str.c returns the exact opposite values.) 00251 */ 00252 exit ( (ret ? 0 : 1) ); 00253 } 00254 00255 Generated on Thu Feb 9 04:39:00 2012 for ReactOS by
1.6.3
|