Assignment 1 : Toy Shell
toyshell.h
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
FILE *fp;
void list(char lmode,char *path)
{
struct dirent *entry;
DIR *dirp;
int tcount=0,fcount=0,dcount=0;
if(lmode!='n' && lmode!='i')
printf("\nInvalid mode.\n");
dirp=opendir(path);
if(dirp==NULL)
printf("\nDirectory is not present ");
else
{
if(lmode=='n')
{
while(entry=readdir(dirp))
{
tcount++;
if(entry->d_type==DT_DIR)
dcount++;
if(entry->d_type==DT_REG)
fcount++;
printf("%s\n",entry->d_name);
}
printf("\n Total Count =%d, File count =%d,Directory count =%d",tcount,fcount,dcount);
}
else if(lmode=='i')
{
printf("\nFile no \t File Name\n");
while(entry=readdir(dirp))
{
printf("\n %d /t %s",entry->d_ino,entry->d_name);
}
}
}
}
void count(char cc,char fn[20])
{
char ch;
int c=0,w=0,l=0;
int n;
fp=fopen(fn,"r");
while(!feof(fp))
{
ch=fgetc(fp);
c++;
if(ch==' '||ch=='\t')
w++;
if(ch=='\n')
{ w++;l++;}
}
switch(cc)
{
case 'c':printf("\nno of characters %d",c);break;
case 'w':printf("\nno of words %d",w);break;
case 'l':printf("\nno of lines %d",l);break;
}
fclose(fp);
}
void typeline(int nn,char fn[20])
{
char line[80];
char *s;
int l=0,i=0;
fp=fopen(fn,"r");
if(fp==NULL)
printf("File opening error.\n ");
else
{
if(nn>0)
{
while(!feof(fp))
{
if(fgets(line,80,fp))
{
if(l<nn)
puts(line);l++;
}
}
}
else if(nn<0)
{
while(fgets(line,80,fp))
{
l++;
}
printf("\n Line count=%d\n",l);
fseek(fp,0,SEEK_SET);
while(fgets(line,80,fp))
{
if(i<(l+nn))
{
;
i++;
}
else
puts(line);
}
}
else
{
while(fgets(line,80,fp))
{
puts(line);
}
}
}
fclose(fp);
}
toyshell.c
#include "toyshell.h"
void search( char op,char *fn,char *pattern)
{
int ch=0,i=0;
char line[80];
fp=fopen(fn,"r");
if(fp==NULL)
printf("\nFile opening error\n");
else
{
switch(op)
{
case 'f'://first occu
while(fgets(line,80,fp))
{
if(strstr(line,pattern)!=NULL)
{
i++;
if(i==1)
printf("\n%s",line);
break;
}
}
break;
case 'a'://all occu
while(fgets(line,80,fp))
{
if(strstr(line,pattern))
{
printf("%s",line);
}
}
break;
case 'c'://total occu
while(fgets(line,80,fp))
{
if(strstr(line,pattern))
ch++;
}
printf("\nTotal Occurence =%d",ch);
break;
}
}
fclose(fp);
}
char fname[20];
char cmd[20];
void main()
{
char *token[5],s1[20],s2[10],s3[20],s[20],s4[20];
int n,x,a;
char *t1,*t2,*t3,*t4;system("clear");
//token=(char**)malloc(sizeof(char *)*5);
while(1)
{
n=0;
printf("\n myshell $]");
gets(cmd);
n=sscanf(cmd,"%s%s%s%s",s1,s2,s3,s4);
t1=(char *)malloc(strlen(s1)*sizeof(char));
t2=(char *)malloc(strlen(s2)*sizeof(char));
t3=(char *)malloc(strlen(s3)*sizeof(char));
t1=s1;
t2=s2;
t3=s3;
token[0]=t1;token[1]=t2;token[2]=t3;
//printf("hhh %s %s %s n %d",token[0],token[1],token[2],n);
if(strcmp("count",s1)==0)
n=1;
else if(strcmp("typeline",s1)==0)
n=2;
else if(strcmp("list",s1)==0)
n=3;
else n=4;
switch(n)
{
case 1:
count(s2[0],s3);
break;
case 2:
a=atoi(t2);
typeline(a,s3);
break;
case 3:
list(s2[0],s3);
break;
case 4:
search(s2[0],s3,s4);
break;
default:
strcpy(s,"/bin/"); strcat(s,s1);
printf("\nnbnb %s",s);
x=fork();
if(x==0)
{
execv(s,token);
}
else
wait(NULL);
break;
}
}
}
0 Comments
If anyone has Doubts or suggestions please let me know