Showing posts with label Simulation of unix commands. Show all posts
Showing posts with label Simulation of unix commands. Show all posts

Simulation of Unix Commands - wc


SIMULATION OF UNIX COMMANDS - Word Count (wc)

Programs
// 4. wc command
#include<stdio.h>
int wc,lc,cc;

main(int argc, char * argv[] )
{
FILE *fp;
int i;
char a[100];

if(argc!=2)
{
          printf(“Usage error”);
          return;
}

fp = fopen(argv[1],"r");
if(fp==NULL)
{
          printf("File opening error");
          return;
}

i=load(fp,a);
while(i==1)
{
          count(a);
          i=load(fp,a);
}

printf("\nWord Count : %d", wc);
printf("\nLine Count : %d", lc);
printf("\nCharacter Count : %d", cc);

close(fp);
}



load(fp1,g)
FILE *fp1;
char g[];
{
          int n;
         
          for(n=0;(g[n]=getc(fp1))!=EOF;n++)
                   if(g[n]=='\n')
                   {
                             g[n]='\0';
                             return 1;
                   }
          return 0;
}

count(g)
char g[];
{
          int n;
          for(n=0;g[n]!='\0';n++)
          {
                   lc++;
                   if(g[n]!='\t' && g[n]!=' ' )
                             cc++;
                   if( (g[n]=='\t' && g[n+1]!='\t' )||(g[n]==' ' && g[n+1]!=' ' ) )
                             wc++;
          }
}





Simulation of Unix Commands - cat


SIMULATION OF UNIX COMMANDS

Programs
// 2. cat command
#include<fcntl.h>                                     
#include<sys/stat.h>

#define BUFSIZE 1



int main(int argc, char **argv)

{

          int fd1;
          int n;
          char buf;
          fd1=open(argv[1],O_RDONLY);

  
while((n=read(fd1,&buf,1))>0)   
printf("%c",buf);
return (0);
}




Simulation of Unix Commands - ls



SIMULATION OF UNIX COMMANDS

Programs
// 1. ls command
#include<stdio.h>                                     
#include<fcntl.h>
#include<sys/stat.h>                       
#include<dirent.h>

int main(int argc,char *argv[])
{
DIR *dp;
struct dirent *sd;
dp=opendir(argv[1]);

while((sd=readdir(dp))!=NULL)
printf("%s\t",sd->d_name);

closedir(dp);
}



Simulation of Unix Commands - grep


SIMULATION OF UNIX COMMANDS

Programs
// 3. Grep command
#include <stdio.h>                 
#include <stdlib.h>

int substr( char *line, char *pat);
int main(int argc, char *argv[])
{
        FILE *fp;
        char line[128];

        if ( argc == 2 )
                fp = stdin;
        else if ( argc == 3 )
        {
                fp = fopen(argv[2], "r");
                if ( fp == NULL )
                        printf("file %s cannot be opened \n", argv[1]);
        }
        else
        {
                printf("Usage: %s <pattern> <file> \n", argv[0]);
                exit(1);
        }
        while( fgets(line, sizeof(line), fp) )
                if ( substr(line, argv[1]) )
                        printf("%s",line);
        fclose (fp);
        return 0;
}
int substr( char *line, char *pat)
{
        int i,j,k;
        for( i=0; line[i] != '\0'; i++)
                for( j=i,k=0; line[j]==pat[k] && pat[k] != '\0'; j++,k++);
                             if ( pat[k] == '\0')
                                      return 1;
        return 0;
}