Sunday, July 15, 2007

a dos dummy ...... just made for fun

a dos dummy ...... just made for fun


/*commands cd - change directory
md - make directory
mf - make file
show - show file contents
dos - run dos commands
dir - show dirctory and file listing
*/
#include"stdio.h"
#include"conpsl"
#include"stdlib.h"
typedef struct
{char name[16];
char *info;
struct file *next;
}file;
typedef struct
{
char name[16];
struct folder *next;
struct folder *in;
struct folder *up;
struct file *fil;
}folder;
void print(char* c)
{
int i=0;
while(1)
{
if(c[i]==0)
break;
if(c[i]=='\r')
printf("\n");
else
printf("%c",c[i]);
i++;
}
}

char *content()
{
char *c,a;
int i=0;
c=(char*)malloc(sizeof(char)*3);
c[i]=0;
while(1)
{
a=getch();
if(a==26)
break;
else
{
if(c[i]!='\n'&&c[i]!='\r')
c[i]=a;
else
c[i]='\r';
if(c[i]!='\n'&&c[i]!='\r')
putch(c[i]);
else
printf("\n");
i++;
c=(char*)realloc(c,sizeof(c)*(i+2));
c[i]=0;
}
}
return c;
}


int main()
{char command[10],chek[16],*c;
int i=0,d=0,f=0;
folder *first,*p,*cur,*t;
file *fi;
print("puneet is cool \n");
first=(folder*)malloc(sizeof(folder));
first->name[0]=0;
strcat(first->name,"pslos");
first->next=0;
first->in=0;
first->fil=0;
first->up=0;
cur=first;
while(1)
{p=cur;
while(p->up!=0)
{
printf("%s",p->name);
p=(folder*)p->up;
printf(" <- ");
}
printf("%s",p->name);
printf(" :> ");
gets(command);


if(strcmp(command,"dir")==0)
{
printf("\n");
p=(folder*)cur->in;
d=0;
if(p!=0)
while(p!=0)
{
printf("(dir) %s\n",p->name);
p=(folder*)p->next;
d=d+1;
}

fi=(file*)cur->fil;
f=0;
if(fi!=0)
while(fi!=0)
{
printf(" %s\n",fi->name);
fi=(file*)fi->next;
f=f+1;
}
printf("\n");
printf("Total number of directories = %d \nand files = %d\n\n",d,f);
}


else if(strcmp(command,"show")==0)
{
printf("enter files's name : ");
gets(chek);
fi=(file*)cur->fil;
while(1)
{
if(fi->next==0)
break;
if(strcmp(chek,fi->name)==0)
break;
fi=(file*)fi->next;
}
if(strcmp(chek,fi->name)==0)
{print(fi->info);
print("\n");}
else
printf("File not found\n");
}



else if(strcmp(command,"md")==0)
{
if(cur->in==0)
{
(folder*)cur->in=(folder*)malloc(sizeof(folder));
p=(folder*)cur->in;
printf("enter folder's name : ");
gets(p->name);
p->next=0;
(folder*)p->up=cur;
p->fil=0;
p->in=0;
}
else
{
p=(folder*)cur->in;
while(p->next!=0)
{p=(folder*)p->next;}
(folder*)p->next=(folder*)malloc(sizeof(folder));
p=(folder*)p->next;
printf("enter folder's name : ");
gets(p->name);
p->next=0;
(folder*)p->up=(folder*)cur;
p->fil=0;
p->in=0;
}
}

else if(strcmp(command,"cd")==0)
{
printf("enter folder's name : ");
gets(chek);
p=(folder*)cur->in;
if(p!=NULL)
{
if(strcmp(chek,"..")==0)
{
if(cur->up!=0)
cur=(folder*)cur->up;
}
else
{
while(1)
{
if(p->next==0)
break;
if(strcmp(chek,p->name)==0)
break;
p=(folder*)p->next;
}
if(strcmp(chek,p->name)==0)
cur=p;
}
}
else
{
if(strcmp(chek,"..")==0)
{
if(cur->up!=0)
cur=(folder*)cur->up;
else
printf("no super folder to this folder exists !!!\n");
}
else if(p==NULL)
printf("no folder with this name exists in this directory !!!\n");
}
}


else if(strcmp(command,"mf")==0)
{
if(cur->fil==0)
{
(file*)cur->fil=(file*)malloc(sizeof(file));
fi=(file*)cur->fil;
printf("enter file's name : ");
gets(fi->name);
printf("enter contents of file :\n");
fi->info=content();
fi->next=0;
}
else
{
fi=(file*)cur->fil;
while(fi->next!=0)
{fi=(file*)fi->next;}
(file*)fi->next=(file*)malloc(sizeof(file));
(file*)fi=(file*)fi->next;
printf("enter file's name : ");
gets(fi->name);
printf("enter contents of file :\n");
fi->info=content();
fi->next=0;
}
}


else if(strcmp(command,"quit")==0)
{
printf("bye - bye ! see you next time");
getch();
exit(1);
}
else if(strcmp(command,"dos")==0)
{
printf("enter the dos command : ");
gets(command);
system(command);
}
else if(strcmp(command,"clear")==0)
{
system("cls");
}
else printf("command not found\n");
}
getch();
return 0;
}

No comments: