Code:
Script started on Mon Oct 25 18:18:43 2010
%cat newgrep.h
/*********************************************************************
Name: Mark Clark
Assignment: Lab 4
Last Revision: 10/7/10
Desc: This program searches a file for occurences of a string. This
program is somewhat similar to the Unix grep utility, thus is called
newgrep.
*********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
char * searchBuffer( char *, char * , int * , int *, int);
%cat searchBuffer.c
/*
This function searches a string for a matching keyword passed
into it.
reads in: str, *str, *int, *int, int, int
*/
#include "newgrep.h"
char * searchBuffer( char *keyword, char * buffer, int * lines,
int * occur, int boolI){
char *lineOut, // Line containing keyword
*token = NULL, // Character ptr for strtok
lineOutNull[1] = {'\0'}; // Empty array for passing back
int i = 0, // Loop variables independent of main
boolLine = 0; // Boolean to bypass multiple "occur" counts
// Revision: Mem Alloc Unneccesary
lineOut = (char *) calloc( strlen(buffer), sizeof(buffer) );
if(lineOut == NULL){
fprintf(stderr, "MEMORY ALLOCATION FAILURE!\n");
exit(1);
}
// Copy buffer to lineOut before it is modified by strtok
strcpy(lineOut, buffer);
for( i = 0; i < strlen(lineOut); i++){ // uppercase lineOut
if( lineOut[i] != '\0')
lineOut[i]=(char)toupper(lineOut[i]);
}
// Upper-case buffer for -o option
if( boolI == 1 ){
for( i = 0; i < strlen(buffer); i++){
if( buffer[i] != '\0')
buffer[i]=(char)toupper(buffer[i]);
}
for( i = 0; i < strlen(keyword); i++)
if( keyword[i] != '\0')
keyword[i]=(char)toupper(keyword[i]);
}
i = 0; // re-initialize i
token = strtok(buffer, " ,.-\n"); // priming token
while(token != NULL){
if( strncmp(token, keyword, strlen(keyword)) == 0){
*occur = *occur + 1; // Count matches
boolLine = 1;
}
token = strtok(NULL, " ,.-\n"); // continue tokenizing
}
if( boolLine == 1 ){
*lines = *lines + 1;
}else{
return(lineOutNull);
}
return(lineOut);
}
%cat main.c
/**********************
Depends on newgrep.h
**********************/
#include "newgrep.h"
int main ( int argc, char * argv[] ){
char outFile [81], // Output file
keyword [81], // Word for string compare
filename [81], // File argument in argv(a.k.a. file)
**lineOut, // Output from token loop
*buffer, // Line for searching
option; // Option for switch statement
int i = 0, // Loop Counter
j = 0, // Argument String elements
k = 0, // Secondary Loop Counter
l = 0, // lineOut element counter for searchBuffer return
lines = 0, // how many lines occur is counted in
occur = 0, // how many times keyword occurs
boolI = 0, // Boolean value for option I
boolO = 0; // Boolean value for option O
FILE * fp, // File Pointer for input file (3rd arg)
* ofp; // File pointer for output file
//Set both character arrays to zero for option check
filename[0] = 0;
keyword[0] = 0;
// Echo number of arguments passed into main
if( argc < 3 ){
fprintf (stderr, "\nInsufficient arguments!\n\n" );
exit(1);
}else{
printf ( "\nThere were %d arguments passed to args.\n\n", argc);
}
// Finds Option elements, loops skips first argument(program name)
for( i = 1 ; i < argc ; i++){ // Loop thru arguments, re-init i
if ( argv[i][0] == '-' ){ // Look for '-' as first character
j = strlen(argv[i]); // Set j for max loop cycles
for( k = 0 ; k < j ; k++){ // loop cycles element i's char array
option = argv[i][k]; // set current char to option
switch( option ){ // test for either 'i'|'I' or 'o'|'O'
case 'i':
case 'I':
boolI = 1;
break;
case 'o':
case 'O':
boolO = 1;
if( argv[i+1][0] != NULL ){
strcpy(outFile, argv[i+1]);
}else{
fprintf ( stderr, "Invalid File Argument <%s>!\n",
argv[i+1][0]);
exit(1);
}
break;
default:
break;
}
}
}else if( keyword[0] == 0 ){
strcpy(keyword, argv[i]);
}else if( filename[0] == 0 ){
strcpy(filename, argv[i]);
}
}
// #########################
// #### File Operations ####
// #########################
// Check to see if file is valid
if ( (fp=fopen (filename, "r") ) == NULL){
fprintf ( stderr, "File IO: Unable to open \"%s\"!\n",
filename);
exit(1);
}
// Allocate memory to buffer then read in until newline
buffer = (char*) malloc(256);
while ( fgets(buffer, 255, fp) != NULL ){
if( buffer[strlen(buffer) - 1] == '\n' )
buffer[strlen(buffer) - 1] = '\0'; // Replace new line at end
//lineOut is a 2-D array main, searchBuffer's lineOut is 1-D
lineOut[l] = searchBuffer( keyword, buffer, &lines, &occur, boolI);
l++; // move to next element of main's 2-D "lineOut"
}
// ################
// #### Output ####
// ################
if(boolO == 1){ // Check if -o Option is true
ofp = fopen(outFile, "w"); // Open file passed in from kernel
if (ofp != NULL){
fprintf(ofp, "\n"); // Initialize file with newline
for(i=0; i < l ; i++){
if(lineOut[i][0] != '\0') // Check if lineOut contains anything
fprintf(ofp, "Keyword found in line[%d]: %s\n", i+1,
lineOut[i] );
}
fprintf(ofp, "\n\"%s\" occurs %d times in %d lines.\n",
keyword, occur, lines);
fclose (ofp);
printf("Successful Write to %s\n", outFile);
}
// Prints to stdout
}else{
for(i=0; i < l; i++){
if(lineOut[i][0] != '\0') // Check if lineOut contains anything
printf("Keyword found in line[%d]: %s\n", i+1, lineOut[i] );
}
printf("\n\"%s\" occurs %d times in %d lines.\n",
keyword, occur, lines);
}
return(0);
}
%newgrep is file.dat
There were 3 arguments passed to args.
Keyword found in line[1]: THIS IS A FILE
"is" occurs 1 times in 1 lines.
%exit
exit
script done on Mon Oct 25 18:19:20 2010