Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.74 KB | None | 0 0
  1. /* findstr.c */
  2.  
  3. /* Copyright (C) 1994-2002, Jim Hall <jhall@freedos.org> */
  4.  
  5. /* Adapted for ReactOS -Edited for Findstr.exe K'Williams */
  6.  
  7. /*
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License along
  19.    with this program; if not, write to the Free Software Foundation, Inc.,
  20.    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22.  
  23.  
  24. /* This program locates a string in a text file and prints those lines
  25.  * that contain the string.  Multiple files are clearly separated.
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <windows.h>
  33.  
  34. #include <io.h>
  35. #include <dos.h>
  36.  
  37. #include "resource.h"
  38.  
  39.  
  40. /* Symbol definition */
  41. #define MAX_STR 1024
  42.  
  43.  
  44. /* This function prints out all lines containing a substring.  There are some
  45.  * conditions that may be passed to the function.
  46.  *
  47.  * RETURN: If the string was found at least once, returns 1.
  48.  * If the string was not found at all, returns 0.
  49.  */
  50. int
  51. find_str (char *sz, FILE *p, int invert_search,
  52.           int count_lines, int number_output, int ignore_case, int at_start, int literal_search,
  53.           int at_end, int reg_express, int exact_match, int sub_dirs, int only_fname)
  54. {
  55.   int i, length;
  56.   long line_number = 0, total_lines = 0;
  57.   char *c, temp_str[MAX_STR], this_line[MAX_STR];
  58.  
  59.   /* Convert to upper if needed */
  60.   if (ignore_case)
  61.     {
  62.       length = strlen (sz);
  63.       for (i = 0; i < length; i++)
  64.     sz[i] = toupper (sz[i]);
  65.     }
  66.  
  67.   /* Scan the file until EOF */
  68.   while (fgets (temp_str, MAX_STR, p) != NULL)
  69.     {
  70.       /* Remove the trailing newline */
  71.       length = strlen (temp_str);
  72.       if (temp_str[length-1] == '\n')
  73.     {
  74.       temp_str[length-1] = '\0';
  75.     }
  76.  
  77.       /* Increment number of lines */
  78.       line_number++;
  79.       strcpy (this_line, temp_str);
  80.  
  81.       /* Convert to upper if needed */
  82.       if (ignore_case)
  83.     {
  84.       for (i = 0; i < length; i++)
  85.         {
  86.           temp_str[i] = toupper (temp_str[i]);
  87.         }
  88.     }
  89.  
  90.       /* Locate the substring */
  91.  
  92.       /* strstr() returns a pointer to the first occurrence in the
  93.        string of the substring */
  94.       c = strstr (temp_str, sz);
  95.  
  96.       if ( ((invert_search) ? (c == NULL) : (c != NULL)) )
  97.     {
  98.       if (!count_lines)
  99.         {
  100.           if (number_output)
  101.         printf ("%ld:", line_number);
  102.  
  103.           /* Print the line of text */
  104.           puts (this_line);
  105.         }
  106.  
  107.       total_lines++;
  108.     } /* long if */
  109.     } /* while fgets */
  110.  
  111.   if (count_lines)
  112.     {
  113.       /* Just show num. lines that contain the string */
  114.       printf ("%ld\n", total_lines);
  115.     }
  116.  
  117.  
  118.  /* RETURN: If the string was found at least once, returns 1.
  119.   * If the string was not found at all, returns 0.
  120.   */
  121.   return (total_lines > 0 ? 1 : 0);
  122. }
  123.  
  124. /* Show usage */
  125. void
  126. usage (void)
  127. {
  128.     TCHAR lpUsage[4096];
  129.  
  130.     LoadString( GetModuleHandle(NULL), IDS_USAGE, (LPTSTR)lpUsage, 4096);
  131.     CharToOem(lpUsage, lpUsage);
  132.     printf( lpUsage );
  133. }
  134.  
  135.  
  136. /* Main program */
  137. int
  138. main (int argc, char **argv)
  139. {
  140.   char *opt, *needle = NULL;
  141.   int ret = 0;
  142.   TCHAR lpMessage[4096];
  143.  
  144.   int invert_search = 0;        /* flag to invert the search */
  145.   int count_lines = 0;          /* flag to whether/not count lines */
  146.   int number_output = 0;        /* flag to print line numbers */
  147.   int ignore_case = 0;          /* flag to be case insensitive */
  148.   int at_start = 0;             /* flag to Match if at the beginning of a line. */
  149.   int at_end = 0;               /* flag to Match if at the beginning of a line. */
  150.   int reg_express = 0;         /* flag to use/not use regular expressions */
  151.   int exact_match = 0;          /* flag to be exact match */
  152.   int sub_dirs= 0;              /* this and all subdirectories */
  153.   int only_fname= 0;            /* print only the name of the file*/
  154.  
  155.   FILE *pfile;              /* file pointer */
  156.   int hfind;                /* search handle */
  157.   struct _finddata_t finddata;      /* _findfirst, filenext block */
  158.  
  159.   /* Scan the command line */
  160.   while ((--argc) && (needle == NULL))
  161.     {
  162.       if (*(opt = *++argv) == '/')
  163.         {
  164.           switch (opt[1])
  165.         {
  166.           case 'b':
  167.           case 'B':     /* Matches pattern if at the beginning of a line */
  168.             at_start = 1;
  169.             break;
  170.            
  171.           case '/c:':
  172.           case '/C:':       /* Literal? */
  173.             literal_search = 1;
  174.             break;
  175.  
  176.           case 'e':
  177.           case 'E':     /* matches pattern if at end of line */
  178.             at_end = 1;
  179.             break;
  180.            
  181.           case 'i':
  182.           case 'I':     /* Ignore */
  183.             ignore_case = 1;
  184.             break;
  185.  
  186.           case 'm':
  187.           case 'M':     /* only filename */
  188.             only_fname = 1;
  189.             break;         
  190.            
  191.           case 'n':
  192.           case 'N':     /* Number */
  193.             number_output = 1;
  194.             break;
  195.            
  196.           case 'r':
  197.           case 'R':     /* search strings as regular expressions */
  198.             reg_express = 1;
  199.             break; 
  200.  
  201.           case 's':
  202.           case 'S':     /* search files in child directory too*/
  203.             sub_dirs = 1;
  204.             break;             
  205.  
  206.           case 'v':
  207.           case 'V':     /* Not with */
  208.             invert_search = 1;
  209.             break;
  210.  
  211.           case 'x':
  212.           case 'X':     /* exact match */
  213.             exact_match = 1;
  214.             break;         
  215.            
  216.           default:
  217.             usage ();
  218.             exit (2);       /* syntax error .. return error 2 */
  219.             break;
  220.         }
  221.         }
  222.       else
  223.         {
  224.           /* Get the string */
  225.       if (needle == NULL)
  226.         {
  227.               /* Assign the string to find */
  228.               needle = *argv;
  229.         }
  230.     }
  231.     }
  232.  
  233.   /* Check for search string */
  234.   if (needle == NULL)
  235.     {
  236.       /* No string? */
  237.       usage ();
  238.       exit (1);
  239.     }
  240.  
  241.   /* Scan the files for the string */
  242.   if (argc == 0)
  243.     {
  244.       ret = find_str (needle, stdin, invert_search, count_lines,
  245.                       number_output, ignore_case, at_start, literal_search, at_end, reg_express, exact_match,
  246.                       sub_dirs, only_fname);
  247.     }
  248.  
  249.   while (--argc >= 0)
  250.     {
  251.       hfind = _findfirst (*++argv, &finddata);
  252.       if (hfind < 0)
  253.     {
  254.       /* We were not able to find a file. Display a message and
  255.          set the exit status. */
  256.       LoadString( GetModuleHandle(NULL), IDS_NO_SUCH_FILE, (LPTSTR)lpMessage, 4096);
  257.       CharToOem(lpMessage, lpMessage);
  258.       fprintf (stderr, lpMessage, *argv);//
  259.     }
  260.       else
  261.         {
  262.           /* repeat find next file to match the filemask */
  263.       do
  264.             {
  265.               /* We have found a file, so try to open it */
  266.           if ((pfile = fopen (finddata.name, "r")) != NULL)
  267.             {
  268.               printf ("---------------- %s\n", finddata.name);
  269.               ret = find_str (needle, pfile, invert_search, count_lines,
  270.                       number_output, ignore_case, at_start, literal_search, at_end, reg_express, exact_match,
  271.                       sub_dirs, only_fname);
  272.               fclose (pfile);
  273.             }
  274.           else
  275.             {
  276.               LoadString(GetModuleHandle(NULL), IDS_CANNOT_OPEN, (LPTSTR)lpMessage, 4096);
  277.               CharToOem(lpMessage, lpMessage);
  278.               fprintf (stderr, lpMessage,
  279.                    finddata.name);
  280.                 }
  281.         }
  282.           while (_findnext(hfind, &finddata) > 0);
  283.         }
  284.       _findclose(hfind);
  285.     } /* for each argv */
  286.  
  287.  /* RETURN: If the string was found at least once, returns 0.
  288.   * If the string was not found at all, returns 1.
  289.   * (Note that find_str.c returns the exact opposite values.)
  290.   */
  291.   exit ( (ret ? 0 : 1) );
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement