0% found this document useful (0 votes)
9 views3 pages

As 3

Hshbsbhsjnwnsn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

As 3

Hshbsbhsjnwnsn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

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>

void typeline(char *a,char *filename ){

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 ..");
}
}
}
}

int main(int argc, char argv[]){


char cmd[20],*token,t[10][20];
int pid,ntoken,e;

do{

printf("\nMyShell $");
gets(cmd);

token = strtok(cmd," ");


ntoken=0;
strcpy(t[ntoken],token);
while(token){
ntoken++;
token = strtok(NULL," ");
if(token != NULL){
strcpy(t[ntoken],token);
}
}

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);
}

You might also like