0% found this document useful (0 votes)
1K views12 pages

Sys/types.h Sys/stat.h FCNTL.H Stdio.h Stdlib.h String.h

The document contains code for a shell program called MyShell that acts as a command line interpreter. It can execute normal shell commands by launching a child process. It also implements additional commands to count lines, words and characters in a file, search for patterns in a file, print certain lines of a file, and list files and directories with options. The code includes functions for tokenizing commands, executing commands by calling the relevant functions, and forking child processes to execute external commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views12 pages

Sys/types.h Sys/stat.h FCNTL.H Stdio.h Stdlib.h String.h

The document contains code for a shell program called MyShell that acts as a command line interpreter. It can execute normal shell commands by launching a child process. It also implements additional commands to count lines, words and characters in a file, search for patterns in a file, print certain lines of a file, and list files and directories with options. The code includes functions for tokenizing commands, executing commands by calling the relevant functions, and forking child processes to execute external commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Name : Mohammed Electricwala

Roll No : 23
Assignment : TOYSHELL

2. Write a program that behaves like a shell ( Command Interpreter). It has its own
prompt say “MyShell $”. Any normal shell command is executed from your shell by
starting a child process to execute the system program corresponding to the
command. It should additionally interpret the following command : a. Count C <
filename > : To print number of Characters in the file. b. Count W < filename > : To
print number of Words in the file. c. Count L < filename > : To print number of Lines in
the file. ( Hint : System Calls Used : fork ( ) , execlp ( ) , waitpid ( ), open ( ), close( ),
read ( ), write ( ) )

ANSWER:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void make_toks(char *s, char *tok[])


{
int i=0;
char *p;

p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}

tok[i]=NULL;
}

void count(char *fn, char op)


{
int fh,cc=0,wc=0,lc=0;
char c;

fh = open(fn,O_RDONLY);
if(fh==-1)
{
printf("File %s not found.\n",fn);
return;
}

while(read(fh,&c,1)>0)
{
if(c==' ') wc++;
else if(c=='\n')
{
wc++;
lc++;
}
cc++;
}

close(fh);

switch(op)
{
case 'c':
printf("No.of characters:%d\n",cc);
break;
case 'w':
printf("No.of words:%d\n",wc);
break;
case 'l':
printf("No.of lines:%d\n",lc);
break;
}
}

int main()
{
char buff[80],*args[10];
int pid;

while(1)
{
printf("myshell$");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='\0';
make_toks(buff,args);
if(strcmp(args[0],"count")==0)
count(args[2],args[1][0]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.\n");
}
}
}

return 0;
}

OUTPUT:
./main
myshell$ count c A.txt
No.of characters:53
myshell$ count w A.txt
No.of words:12
myshell$ count l A.txt
No.of lines: 3

SLOT 2
1) Write a program that behaves like a shell ( Command Interpreter). It has its own
prompt say “MyShell $ “. Any normal shell command is executed from your shell by
starting a child process to execute the system program corresponding to the
command. It should additionally interpret the following command :
a. Search F < pattern > < filename > : To search first occurance of pattern in the file.
b. Search C < pattern > < filename > : To count number of occurances of pattern in the
file.
c. Search A < pattern > < filename > : To Search number of occurances of pattern in
the file. ( Hint : System Calls Used : fork ( ) , execlp ( ) , waitpid ( ), open ( ), close( ),
read ( ), write ( ) )

ANSWER:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;

p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}

tok[i]=NULL;
}

void search(char *fn, char op, char *pattern)


{
int fh,count=0,i=0,j=0;
char buff[255],c,*p;

fh = open(fn,O_RDONLY);
if(fh==-1)
{
printf("File %s Not Found\n",fn);
return;
}

switch(op)
{
case 'f':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j=0;
i++;
if(strstr(buff,pattern))
{
printf("%d: %s",i,buff);
break;
}
}
}
break;
case 'c':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j=0;
p = buff;
while(p=strstr(p,pattern))
{
count++;
p++;
}
}
}
printf("Total No.of Occurrences = %d\n",count);
break;
case 'a':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j = 0;
i++;
if(strstr(buff,pattern))
printf("%d: %s",i,buff);
}
}
}//switch
close(fh);
}//search

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='\0';
make_toks(buff,args);
if(strcmp(args[0],"search")==0)
search(args[3],args[1][0],args[2]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.\n");
}
}
}

return 0;
}
A.txt
aaaabcdfg
dfrgh
e
hii
jerrr
kll
iii
qqq

OUTPUT :
myshell$ search f a A.txt
1: search : aaaabcdfg
myshell$ search c a A.txt
Total No.of Occurrences = 5
myshell$ search a a A.txt
1: aaaabcdfg
myshell$ search a q A.txt
8: qqq
2) Write a program that behaves like a shell ( Command Interpreter). It has its own
prompt say “MyShell $ “. Any normal shell command is executed from your shell by
starting a child process to execute the system program corresponding to the
command. It should additionally interpret the following command :
a. Typeline +n < filename > : To print first ‘n’ lines of file file.
b. Typeline –n < filename > : To print last ‘n’ lines of the file.
c. Typeline a < filename > : To print all lines of the file. ( Hint : System Calls Used :
fork ( ) , execlp ( ) , waitpid ( ), open ( ), close( ), read ( ), write ( ), lseek ( ) )

Answer:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void typeline(char *fn, char *op)
{
int fh,i,j,n;
char c;
fh = open(fn,O_RDONLY);
if(fh==-1)
{
printf("File %s not found.\n",fn);
return;
}
if(strcmp(op,"a")==0)
{
while(read(fh,&c,1)>0)
printf("%c",c);
close(fh);
return;
}
n = atoi(op);
if(n>0)
{
i=0;
while(read(fh,&c,1)>0)
{
printf("%c",c);
if(c=='\n') i++;
if(i==n) break;
}
}
if(n<0)
{
i=0;
while(read(fh,&c,1)>0)
{
if(c=='\n') i++;
}
lseek(fh,0,SEEK_SET);
j=0;
while(read(fh,&c,1)>0)
{
if(c=='\n') j++;
if(j==i+n) break;
}
while(read(fh,&c,1)>0)
{
printf("%c",c);
}
}
close(fh);
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='\0';
make_toks(buff,args);
if(strcmp(args[0],"typeline")==0)
typeline(args[2],args[1]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.\n");
}
}
}
return 0;
}
OUTPUT:
myshell$typeline +3 A.txt
hey
what are u doing
how are you
myshell$typeline -2 A.txt
rtyiksl
123thsum
myshell$typeline a A.txt
hey
what are u doing
how are you
thank you
wwwwwa
bcdefgh
wetrhyd
rtyiksl
123thsum
myshell$

Write a program that behaves like a shell ( Command Interpreter). It has its own
prompt say “MyShell $ “. Any normal shell command is executed from your shell by
starting a child process to execute the system program corresponding to the
command. It should additionally interpret the following command :
a. list +n dirname : To display filenames in a given directory
b. list –n dirname : To count the number of entries in a given directory
C. list i dirname : to display filenames and their inode number for the files in a given
directory.
Answer:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/stat.h>
#include <string.h>
#include<fcntl.h>

void list(char option[],char dname[])


{
DIR *dir;
struct dirent *entry;
struct stat info;
int cnt=0;
dir=opendir(dname);
if(dir==NULL)
{
printf("\nDirectory %s not found!!\n",dname);
return;
}
if(strcmp(option,"F")==0)
{
while(entry==readdir(dir)!=NULL)
{
stat(entry->d_name,&info);
if(info.st_mode & S_IFREG)
printf("%s\n",entry->d_name);
}
}
else if(strcmp(option,"N")==0)
{
cnt=0;
while((entry==readdir(dir))!=NULL)
{
cnt++;
}
printf("\nTotal no of entries in directory '%s'=%d",dname,cnt);
}
else if(strcmp(option,"I")==0)
{
while((entry==readdir(dir))!=NULL)
{
stat(entry->d_name,&info);
if(info.st_mode & S_IFREG)
{
printf("File name =%s\t",entry->d_name);
printf("Inode=%lu\n",info.st_ino);

}
}
}
else
{
printf("Invalid option\n");
}
closedir(dir);
}
int main()
{
char cmd[80],tok1[10],tok2[10],tok3[10],tok4[10];
int n;
while(1)
{
printf("\nMy Shell $]");
fgets(cmd,80,stdin);
n=sscanf(cmd,"%s%s%s%s",tok1,tok2,tok3,tok4);
switch(n)
{
case 1:
if(fork()==0)
{
execlp(tok1,tok1,NULL);
}
wait(0);
break;
case 2:
if(fork()==0)
{
execlp(tok1,tok1,tok2,NULL);
}
wait(0);
break;
case 3:
if(strcmp(tok1,"list")==0)
{
count(tok2,tok3);
}
else
{
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,NULL);
}
wait(0);
}
break;
case 4:
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,tok4,NULL);
}
wait(0);
break;
}
}
}

OUTPUT:
myshell$ list n toyshell

2 Dir(s) 2 File(s)

myshell$ list n toyshell

2 Dir(s) 2 File(s)

myshell$ list i toyshell

a.txt 2889465

b.txt 2889468

You might also like