Sunday, March 11, 2012

Implementing GREP command using System calls





Ex. No.: 4(a)(ii)
Implementing GREP command using System calls

AIM:
            To write a C program to implement the UNIX command grep.        
Syntax
grep [options] PATTERN [FILE...]

 
 



Description:
            Grep searches the named input FILEs (or standard input if no files are named) for lines containing a match to the given PATTERN.  By default, grep prints the matching lines.

Algorithm
  1. Start of Algorithm
  2. Get the command as the command line argument and check for the syntax of the command.
  3. Check for the existence of the file if not exists gives the error message.
  4. Open the file if exists.
  5. Read the file contents line by line and check for the pattern matching if match occurs print the line.
  6. End of algorithm.

Program:
#include <string.h>
#include <fcntl.h>   //Defines flags for open and related library functions
#include <sys/uio.h>
#include <unistd.h>
main(int argc, char *argv[])
{
     int des, fhandle, i,k, arg_ind=2;
     int linecount;
     int lineflag;
     char buf,line[160];
     if (argc < 4)  // To check the syntax
    {
       printf( "\n Error... Correct Syntax is : grep  \"pattern\" \"filename\"\n");
        exit(0);
     }
     lineflag=0;
     des = open(argv[3],O_RDONLY);     //Open the file specified by the command
     linecount=0;
       do          // to read upto the end of file and copy to the array line[].
       {
          i=0;
        do       // to read a line
          {
              fhandle=read(des,&buf,1);
              line[i++]=buf;
           } while(( fhandle!= 0) && (buf != '\n'));

           line[i]='\0';
            linecount++;
            i=0;

         while(line[i] != '\0')        // to check character matching
          {
            if(line[i] == argv[1][0])
             {
                //continue checking till the end of pattern
               k=1;
               i++;
               while((k < strlen(argv[1])) && (line[i] == argv[1][k]))
               {
                    i++; k++;
               }
               if(k == strlen(argv[1]))
              
                      printf("%s",line);
                       }
           i++;
         }
      }while( fhandle!= 0);
close(des);
      arg_ind++;
      printf( "\n");
    }

Output:
$ cat input
Operating System Services are
Process management
Main-memory Management
File Management
I/O System Management
Secondary Storage Management

$ ./a.out grep Management input

Process management
Main-memory Management
File Management
I/O System Management
Secondary Storage Management
RESULT :

            Thus the program to simulate the UNIX command grep was written and executed successfully.

6 comments:

  1. bal 6ire6is...this program doesn't work...

    ReplyDelete
  2. Correct Program -

    #include
    #include //Defines flags for open and related library functions
    #include
    #include
    #include
    #include
    int main(int argc, char *argv[])
    {
    int des, fhandle, i,k, arg_ind=2; int j;
    int linecount;
    int lineflag;
    char buf,line[160];
    if (argc < 4) // To check the syntax
    {
    printf( "\n Error... Correct Syntax is : grep \"pattern\" \"filename\"\n");
    exit(0);
    }

    lineflag=0;
    des = open(argv[3],O_RDONLY); //Open the file specified by the command
    linecount=0;
    do // to read upto the end of file and copy to the array line[].
    {
    i=0;
    do // to read a line
    {
    fhandle=read(des,&buf,1);
    line[i++]=buf;
    } while(( fhandle!= 0) && (buf != '\n'));



    line[i]='\0';
    linecount++;
    i=0;

    while(line[i] != '\0') // to check character matching
    {
    if(line[i] == argv[2][0])
    {
    k=1;
    i++;
    while((k < strlen(argv[2])) && (line[i] == argv[2][k]))
    {
    i++; k++;
    }
    if(k == strlen(argv[2]))

    printf("%s",line);
    }
    i++;
    }

    }while( fhandle!= 0);
    close(des);
    arg_ind++;
    printf( "\n");
    }

    ReplyDelete
  3. Header files for the above program are -
    string.h
    fcntl.h
    unistd.h
    stdio.h
    stdlib.h
    sys/uio.h

    ReplyDelete