Advertisement
Guest User

newgrep

a guest
Oct 24th, 2010
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6.  
  7. char * searchBuffer( char *, char * , int * , int *, int);
  8.  
  9. //
  10. //Above - Header file
  11. //Below - Main Source
  12. //
  13.  
  14. #include "newgrep.h"
  15.  
  16. int main ( int argc, char * argv[] ){
  17.  
  18.   char  outFile [81],   // Output file
  19.         keyword [81],   // Word for string compare
  20.         filename [81],  // File argument in argv(a.k.a. file)
  21.         **lineOut,      // Output from token loop
  22.         *buffer,        // Line for searching
  23.         option;         // Option for switch statement
  24.  
  25.   int   i = 0, // Loop Counter
  26.         j = 0, // Argument String elements
  27.         k = 0, // Secondary Loop Counter
  28.         l = 0, // lineOut element counter for searchBuffer return
  29.         lines = 0,  // how many lines occur is counted in
  30.         occur = 0,  // how many times keyword occurs
  31.         boolI = 0,  // Boolean value for option I
  32.         boolO = 0;  // Boolean value for option O
  33.        
  34.   FILE * fp,  // File Pointer for input file (3rd arg)
  35.        * ofp; // File pointer for output file
  36.  
  37.   //Set both character arrays to zero for option check
  38.   filename[0] = 0;
  39.   keyword[0] = 0;
  40.  
  41.   // Echo number of arguments passed into main
  42.   if( argc < 3 ){
  43.     fprintf (stderr, "\nInsufficient arguments!\n\n" );
  44.     exit(1);
  45.   }else{
  46.     printf ( "\nThere were %d arguments passed to args.\n\n", argc);
  47.   }
  48.  
  49.   // Finds Option elements, loops skips first argument(program name)
  50.   for( i = 1 ; i < argc ; i++){  // Loop thru arguments, re-init i
  51.     if ( argv[i][0] == '-' ){    // Look for '-' as first character
  52.       j =  strlen(argv[i]);      // Set j for max loop cycles
  53.       for( k = 0 ; k < j ; k++){ // loop cycles element i's char array
  54.         option = argv[i][k];     // set current char to option
  55.         switch( option ){        // test for either 'i'|'I' or 'o'|'O'
  56.  
  57.           case 'i':
  58.           case 'I':
  59.             boolI = 1;
  60.             break;
  61.  
  62.           case 'o':
  63.           case 'O':
  64.             boolO = 1;
  65.             if( argv[i+1][0] != NULL ){
  66.               strcpy(outFile, argv[i+1]);
  67.             }else{
  68.               fprintf ( stderr, "Invalid File Argument <%s>!\n",
  69.               argv[i+1][0]);
  70.               exit(1);
  71.             }
  72.             break;
  73.  
  74.           default:
  75.             break;
  76.         }
  77.       }
  78.     }else if( keyword[0] == 0 ){
  79.       strcpy(keyword, argv[i]);
  80.     }else if( filename[0] == 0 ){
  81.       strcpy(filename, argv[i]);
  82.     }
  83.   }
  84.  
  85.   // #########################
  86.   // #### File Operations ####
  87.   // #########################
  88.  
  89.   // Check to see if file is valid
  90.   if ( (fp=fopen (filename, "r") ) == NULL){
  91.       fprintf ( stderr, "File IO: Unable to open %s!\n",
  92.       filename);
  93.       exit(1);
  94.     }
  95.   // Allocate memory to buffer then read in until newline
  96.   buffer = (char*) malloc(256);
  97.   while ( fgets(buffer, 255, fp) != NULL ){
  98.     if( buffer[strlen(buffer) - 1] == '\n' )
  99.       buffer[strlen(buffer) - 1] = '\0';  // Replace new line at end
  100.     lineOut[l] = searchBuffer( keyword, buffer, &lines, &occur, boolI);
  101. puts("searchBuffer WORKED!!!!");
  102. getchar();
  103. fflush(stdin);
  104.     l++;
  105.   }
  106. printf("well that wasn't so bad\n");
  107.  
  108. //
  109. //Output section removed
  110. //
  111.   return(0);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement