As 3
As 3
h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<dirent.h>
int i=0,cnt=0,n,in;
char ch;
in=open(filename,O_RDONLY);
if(in==-1){
printf("Error : File Not Found");
return;
}
else{
if(strcmp(a,"a")==0){
while(read(in,&ch,1)){
printf("%c",ch);
}
close(in);
return;
}
else{
n=atoi(a);
if(n>0){
while(read(in,&ch,1)){
if(ch=='\n'){
// On i th line....
i++;
}
if(i==n){
break;
}
printf("%c",ch);
}
close(in);
return;
}else if(n<0){
while(read(in,&ch,1)){
if(ch=='\n'){
cnt++;
}
}
// now ch have total number of lines .. we have to print ahead of
(cnt-n)
lseek(in,0,SEEK_SET);
while(read(in,&ch,1)){
if(ch=='\n'){
i++;
}
if(i==(cnt+n)){
break;
}
}
while(read(in,&ch,1)){
printf("%c",ch);
}
close(in);
return;
}
else{
printf("\nInvalid Arguments ..");
}
}
}
}
do{
printf("\nMyShell $");
gets(cmd);
switch(ntoken){
case 1:
if(strcasecmp(t[0],"exit")==0){
printf("\nExiting the program ..!!");
exit(0);
}
else{
pid=fork();
if(pid==0){
e=execlp(t[0],t[0],(char*)0);
if(e<0){
printf("Error Occured");
}
}else{
wait(NULL);
}
}
break;
case 2 :
pid=fork();
if(pid==0){
e=execlp(t[0],t[0],(char*)0);
if(e<0){
printf("Error Occured");
}
}else{
wait(NULL);
}
break;
case 3:
if(strcasecmp(t[0],"typeline")==0){
typeline(t[1],t[2]);
continue;
}
pid=fork();
if(pid==0){
e=execlp(t[0],t[0],(char*)0);
if(e<0){
printf("Error Occured");
}
}else{
wait(NULL);
}
break;
case 4 :
pid=fork();
if(pid==0){
e=execlp(t[0],t[0],(char*)0);
if(e<0){
printf("Error Occured");
}
}else{
wait(NULL);
}
break;
}
}while(1);
}