Giả Lập DOS
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
#include <dir.h>
#include <dos.h>
char CacLenh [] [10] = {"MD", "CD", "RD", "DEL", "REN", "TYPE"};
char Curdir[256];
int Tachchuoi (char *S, char M[][50])
{
int dem=0;
char *p=strtok(S," ");
while(p)
{
strcpy(M[dem++], p);
p=strtok (Null, " ");
}
return dem;
}
void XuatMangLenh(char M[][50], int n)
{
for(int i=0;i<n;i++)
printf("\n %s", M[i]);
}
int TimVT_X(char A[][10], int n,char *X)
{
for(int i=0;i<n;i++)
if (strcmp (A[i], X)==0)
return i;
return -1;
}
void TaoThuMuc(char *DIRNAME)
{
int stat;
stat = mkdir(DIRNAME);
if (!stat)
printf("Directory created\n");
else
{
printf("Unable to create directory\n");
exit(1);
}
}
void ChuyenDoiThuMuc(char *DIRNAME)
{
if (chdir("\\")) // chuyen ve thu muc goc
{
perror ("chdir()");
exit(1);
}
if (chdir (DIRNAME))
{
perror("\chdir");
exit(1);
}
}
void XoaThuMuc (char *DIRNAME)
{ int stat;
stat = rmdir(DIRNAME);
if (!stat)
printf("\nDirectory deleted\n");
else
{
perror("\nUnable to delete directory\n");
exit(1);
}
}
void XoaTapTin(char *file)
{
if (remove(file) == 0)
printf("removed %s.\n",file);
else
perror("\nLoi");
}
void DoiTenTapTin(char oldname[], char newname[])
{
if (rename(olaname, newname) == 0)
printf("Renamed, %s to %s.\n", oldname, newname);
else
perror("rename");
}
int DocFileVB(char *FileName, char *S)
{
FILE *f=fopen(FileName, "rt");
if(f==NULL)
return 0;
char S1[256];
S[0] =0;
while(1)
{
char *p=fgets(S1,256,f);
if(P==NULL)
break;
strcat(S,S1);
}
fclose(f);
return 1;
}
void XemNoiDungTapTin(char *FileName)
{
char S[256];
if(DocFileVB(FileName, S) ==0)
printf("\n Loi");
else
printf("\n&s\n",S);
}
void GiaLapDos(char M[][50])
{
int vt=TimVT_X(CacLenh,6,M[0]);
char DIRNAME[256];
switch(vt)
{
case 0:
strcpy(DIRNAME,Curdir);
strcat(DIRNAME,M[1]);
TaoThuMuc(DIRNAME);
break;
case 1:
strcpy(DIRNAME,M[1]);
ChuyenDoiThuMuc(DIRNAME);
break;
case 2:
strcpy(DIRNAME,M[1]);
strcat(DIRNAME,M[1]);
XoaThuMuc(DIRNAME);
break;
case 3:
ctrcpy(DIRNAME,M[1]);
XoaTapTin(DIRNAME);
break;
case 4:
DoiTenTapTin(M[1], M[2]);
break;
case 5:
XemNoiDungTapTin(M[1])
break;
default:
printf("\n Chua co lenh nay");
}
}
char *Current_directory(char *path)
{
strcpy(path,"D:\\");
path[0]='A' + getdisk();
getcurdir(0,path+3);
return (path);
}
void main()
{
clrscr();
char MangLenh[6][50];
char ChuoiLenh[256];
while(1)
{
Curdir[0] =0;
Current_directory(Curdir);
fflush(stdin);// rua bo dem ban phim
printf("%s>",Curdir);
gets(ChuoiLenh);
strupr(ChuoiLenh);
int dem=TachChuoi(ChuoiLenh,MangLenh);
if(strcmp(MangLenh[0], "EXIT")==0) break;
if (dem>0)
GiaLapDOS(MangLenh);
}
}
END
#include <conio.h>
#include <string.h>
#include <process.h>
#include <dir.h>
#include <dos.h>
char CacLenh [] [10] = {"MD", "CD", "RD", "DEL", "REN", "TYPE"};
char Curdir[256];
int Tachchuoi (char *S, char M[][50])
{
int dem=0;
char *p=strtok(S," ");
while(p)
{
strcpy(M[dem++], p);
p=strtok (Null, " ");
}
return dem;
}
void XuatMangLenh(char M[][50], int n)
{
for(int i=0;i<n;i++)
printf("\n %s", M[i]);
}
int TimVT_X(char A[][10], int n,char *X)
{
for(int i=0;i<n;i++)
if (strcmp (A[i], X)==0)
return i;
return -1;
}
void TaoThuMuc(char *DIRNAME)
{
int stat;
stat = mkdir(DIRNAME);
if (!stat)
printf("Directory created\n");
else
{
printf("Unable to create directory\n");
exit(1);
}
}
void ChuyenDoiThuMuc(char *DIRNAME)
{
if (chdir("\\")) // chuyen ve thu muc goc
{
perror ("chdir()");
exit(1);
}
if (chdir (DIRNAME))
{
perror("\chdir");
exit(1);
}
}
void XoaThuMuc (char *DIRNAME)
{ int stat;
stat = rmdir(DIRNAME);
if (!stat)
printf("\nDirectory deleted\n");
else
{
perror("\nUnable to delete directory\n");
exit(1);
}
}
void XoaTapTin(char *file)
{
if (remove(file) == 0)
printf("removed %s.\n",file);
else
perror("\nLoi");
}
void DoiTenTapTin(char oldname[], char newname[])
{
if (rename(olaname, newname) == 0)
printf("Renamed, %s to %s.\n", oldname, newname);
else
perror("rename");
}
int DocFileVB(char *FileName, char *S)
{
FILE *f=fopen(FileName, "rt");
if(f==NULL)
return 0;
char S1[256];
S[0] =0;
while(1)
{
char *p=fgets(S1,256,f);
if(P==NULL)
break;
strcat(S,S1);
}
fclose(f);
return 1;
}
void XemNoiDungTapTin(char *FileName)
{
char S[256];
if(DocFileVB(FileName, S) ==0)
printf("\n Loi");
else
printf("\n&s\n",S);
}
void GiaLapDos(char M[][50])
{
int vt=TimVT_X(CacLenh,6,M[0]);
char DIRNAME[256];
switch(vt)
{
case 0:
strcpy(DIRNAME,Curdir);
strcat(DIRNAME,M[1]);
TaoThuMuc(DIRNAME);
break;
case 1:
strcpy(DIRNAME,M[1]);
ChuyenDoiThuMuc(DIRNAME);
break;
case 2:
strcpy(DIRNAME,M[1]);
strcat(DIRNAME,M[1]);
XoaThuMuc(DIRNAME);
break;
case 3:
ctrcpy(DIRNAME,M[1]);
XoaTapTin(DIRNAME);
break;
case 4:
DoiTenTapTin(M[1], M[2]);
break;
case 5:
XemNoiDungTapTin(M[1])
break;
default:
printf("\n Chua co lenh nay");
}
}
char *Current_directory(char *path)
{
strcpy(path,"D:\\");
path[0]='A' + getdisk();
getcurdir(0,path+3);
return (path);
}
void main()
{
clrscr();
char MangLenh[6][50];
char ChuoiLenh[256];
while(1)
{
Curdir[0] =0;
Current_directory(Curdir);
fflush(stdin);// rua bo dem ban phim
printf("%s>",Curdir);
gets(ChuoiLenh);
strupr(ChuoiLenh);
int dem=TachChuoi(ChuoiLenh,MangLenh);
if(strcmp(MangLenh[0], "EXIT")==0) break;
if (dem>0)
GiaLapDOS(MangLenh);
}
}
END
Nhận xét
Đăng nhận xét