Wednesday, January 20, 2010

Simulation of "ls" and "grep" commands

Simulation of ls command



#include <stdio.h>

#include <sys/types.h>

#include <dirent.h>

main(int age, char *argv[])

{

DIR *dir;

struct dirent *rddir;

printf(“/n Enter directory with outoput command”);

dir=opeindir(argv[1]);

while((rddir=readdir(dir))!=NULL)

{

printf(“%s\t”,rddir->d_name);

}

closedir(dir);

}


Simulation of GREP command

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp;

char c[100], pat[10];

int l,i,j=0,count=0,len,k,flag=0;

printf("\n Enter the pattern");

scanf("%s", pat);

len=strlen(pat);

fp=fopen("nn.txt","r");

while(!feof(fp))

{

fscanf(fp,"%s",c);

l=strlen(c);

count++;

for(i=0;i<l;i++)

{

if(c[i]==pat[j])

{

flag=0;

for(k=1;k<len;k++)

{

if(c[i|k]!=pat[k])

{

flag=1;

}

if(flag==0)

printf("\n The Pattern %s is present in word %d",pat,count");

}

}

}

}

No comments: