Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532383
  • 博文数量: 252
  • 博客积分: 6057
  • 博客等级: 准将
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-21 10:17
文章分类

全部博文(252)

文章存档

2013年(1)

2012年(1)

2011年(32)

2010年(212)

2009年(6)

分类:

2010-03-19 11:53:35

本代码由本人自己编写,虽然代码算法上有待改进,但也是本人原创。复制时请标明作者:杭师大--杨立春

上篇:

#include
#include
#include
#include
#include

#define LEN 300
#define PARAMETER 3

#define LENGTH 1000000
#define NUM 1000

typedef struct memory
{
char content[LENGTH/NUM+1]; /*LEN/NUM 表示每块数据的大小*/
/* realAddress;*/
}memory;
typedef struct fat
{
memory *memoryaddress;
fat *next;
/* realAddress;*/
}fat;
typedef struct File_Name /*文件名字*/
{
char FileName[50];   /*存储文件名*/
File_Name * File_Brother; /*指向相同路径下的文件地址*/
long File_Length;
char Create_Date[25];   /*创建日期*/
fat *File_address;
} File_Name;
typedef struct Folder_Name   /*文件夹名字*/
{
char FolderName[50];   /*存储文件夹名字*/
char Create_Date[25];
File_Name *FileChild;   /*存储该文件夹下的文件地址*/
Folder_Name *FolderChild; /*存贮该文件夹下的文件夹地址*/
Folder_Name *FolderBrother; /*存储当前文目录下的其他文件夹地址*/
} Folder_Name;

fat *Initial(Folder_Name **Root_Folder);
/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
void main_II(fat *head_fat,Folder_Name *Root_Folder);
void optimize(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER]);
int pressAnyKey(char A[PARAMETER][LEN/PARAMETER]);
int dealWith(char A[PARAMETER][LEN/PARAMETER],int num,char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void PrintHelp();
void printFolder(char cmd[LEN/PARAMETER], char parameter[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void clear_screen();
void copyFile(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void outPut(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void REname(char oldname[LEN/PARAMETER],char newname[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void cdFloder(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void ERROR();
/*FILE *copyFile_Exception_check(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],char openFileName[LEN/PARAMETER*2]);*/
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
void cdFloder_virtual(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void printFolder_virtual(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void REname_virtual(char oldname[LEN/PARAMETER],char newname[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void outPut_virtual(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
void copyFile_virtual(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder);
int existChar(char A[LEN/PARAMETER],char ch);
void CatchFileNameFromTheRoute(char *route,char *FileName);

int main()
{
fat *head_fat; /*head_fat记录着fat表数组的起始地址*/
Folder_Name *Root_Folder;
head_fat = Initial(&Root_Folder);
main_II(head_fat,Root_Folder);

return 0;
}
fat *Initial(Folder_Name **Root_Folder)
{
Folder_Name *p;
fat *head;
int len=LENGTH/NUM;
int i;
time_t   lt;   /*define   a   longint   time   varible*/  

    lt=time(NULL);/*system   time   and   date*/
*Root_Folder = (Folder_Name *)malloc(sizeof(Folder_Name));
p=(*Root_Folder)->FolderChild=(Folder_Name *)malloc(sizeof(Folder_Name));
strcpy(p->FolderName,"folder1");
strcpy(p->Create_Date,ctime(<));
p->Create_Date[24]='\0';
p->FileChild=NULL;
p->FolderChild=NULL;
p->FolderBrother=NULL;    //初始化子文件夹
strcpy((*Root_Folder)->FolderName,"root:"); //初始化根目录
strcpy((*Root_Folder)->Create_Date,ctime(<));
(*Root_Folder)->Create_Date[24]='\0';
(*Root_Folder)->FileChild=NULL;
(*Root_Folder)->FolderBrother=NULL;     /*初始化virtual disk 的根目录,使之为空*/

head = (fat *)malloc(sizeof(struct fat)*len); /*fat表为一个带有一个next指针的数组。指针用于指向文件的下一块区域*/
for(i=0;i {
   head[i].memoryaddress = (memory *)malloc(sizeof(struct memory));
   head[i].memoryaddress->content[0]='\0';
   head[i].next=NULL;
}
return head;
}
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
void main_II(fat *head_fat,Folder_Name *Root_Folder)  
{
    char cmd_parameter[PARAMETER][LEN/PARAMETER];
    char CurrentFolder[LEN/PARAMETER]="c:\\";
    int result=0,answer=0,i;

    printf("Operation System experiment -- by yanglc [version 2.2.2.2]\n");
    printf("enter 'exit' to end the program or enter 'help' to get help...\n");
printf("When you see this screen,they is a virtual disk created named 'u:\\>'\n");
printf("为了实验的方便,在程序运行之初U盘符根目录下已经初始化一个文件夹,名字为:folder1\n\n");

    while(answer!=-1)/*exit*/
    {
        if(answer>=0)
        {
            if(answer!=0)
    {
     optimize(cmd_parameter,CurrentFolder);           
     result=dealWith(cmd_parameter,answer,CurrentFolder,head_fat,Root_Folder);      /*程序第二次运行到此语句时开始运行*/
    }
    printf("%s>",CurrentFolder);
        }
        for(i=0;i             cmd_parameter[i][0]='\0';      /*initial*/
        answer=pressAnyKey(cmd_parameter);
        if(-2==answer)
        {
            printf("\nInput String's length had oversteped the cmd string's length!\n");
            printf("press any key to continue_.");
        }
        if(strcmp(cmd_parameter[0],"exit")==0)
            break;
    }
    printf("press any key to continue...");
}
void opreate(char *A,char *CurrentFolder)
{
char str[LEN/PARAMETER];

if(A[0]&&A[1]!=':' && A[0]!='.')   //转换为绝对路径
{
   strcpy(str,A);
   strcat(strcpy(A,CurrentFolder),str);   //具有放回上一级目录的功能
}
if(A[0]&&A[strlen(A)-1]!='\\'&&!existChar(A,'.')) //给路径填上反斜杠
   strcat(A,"\\");
if(A[0]&&A[1]==':'&&A[2]!='\\')     //检查路径中的一些低级错误或是不规范格式并改正
{
   strcpy(str,&A[2]);
   strcat(strcpy(&A[2],");
}
}
void optimize(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER])
{
opreate(A[1],CurrentFolder);
opreate(A[2],CurrentFolder);
}
int dealWith(char A[PARAMETER][LEN/PARAMETER],int num,char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder) /* head_fat,Root_Folder */
{/*CurrentFolder主要用于扩展功能,当系统添加了cd切换目录命令后CurrentFolder将记录当前目录*/
if(!strcmp(A[0],"help"))
{
   PrintHelp(); return 1;
}
if(!strcmp(A[0],"dir"))
{
   printFolder(A[0],A[1],CurrentFolder,head_fat,Root_Folder); return 1;
}
if(!strcmp(A[0],"copy"))
{
    if(num>2)
     copyFile(A,CurrentFolder,head_fat,Root_Folder);
    else
     ERROR();
    return 1;
}  
if(!strcmp(A[0],"type"))
{
   if(num>1)
    outPut(A[1],CurrentFolder,head_fat,Root_Folder);
   else
    ERROR();
   return 1;
}
if(!strcmp(A[0],"rename"))
{
   if(num>2)
    REname(A[1],A[2],CurrentFolder,head_fat,Root_Folder);
   else
    ERROR();
   return 1;
}
if(!strcmp(A[0],"clc"))
{
   clear_screen(); return 1;
}
if(!strcmp(A[0],"cd"))
{
   if(num>1)
    cdFloder(A[1],CurrentFolder,head_fat,Root_Folder);
   else
    ERROR();
   return 1;
}
else
   ERROR();
    return 1;
}
int inPut(char cmd[LEN])
{
char *p=cmd,ch;
int len=0;

    ch=getchar() ;
    while(ch != 27 && ch != 10 && len <=LEN)
    {
        *p=ch;           /*record all the input*/
        ++p;
        ++len;
        ch=getchar() ;
    }
    if(len>LEN)
        return -2;/*cmd's length overstep the string length, print error!*/
    if(27==ch)
        return -1; /*custom want to exit!*/
    p=cmd;         /*inital*/
return len;
}
int pressAnyKey(char A[PARAMETER][LEN/PARAMETER])         /*字符串处理*/
{
    char cmd[LEN];
    int len;/*record the len of input cmd*/
    char *p=cmd;
    int i,j=0;

len=inPut(cmd);
    for(i=0;len>=0&&i=0为了在最后一个命令上加上一个'\0'*/
    {
        if (len>0&&*p!=' '&&j         {
            A[i][j]=*p;
            ++j;
            ++p;
            --len;
        }
        else
        {
            if(j==LEN/PARAMETER)
                return -2;
            A[i][j]='\0';
            if(0==j)     /*此时输入了连续的空格或是命令之前出现了空格*/
            {
                ++p;
                --len;
                continue;
            }
            else
            {
                ++i;      /*next cmd or PARAMETER*/
                j=0;
                ++p;
                --len;
            }
        }   
    }
    return i; /* i stand for the parameter's number */
}
void PrintHelp()    /*打印帮助*/
{
    printf("\n\ndir      print the information of the files which in currently directory.\n");
    printf("copy     copy filea to fileb\n");
    printf("type     \n");
    printf("rename   perfect the file or folder name\n");
    printf("clc      clear the screen.\n");
    printf("cd       change the current directory.\n\n");
}
void printFolder(char cmd[LEN/PARAMETER], char parameter[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)        /*dir命令函数*/
{
if( CurrentFolder[0]=='u'&&(parameter[0]=='\0' || parameter[1]!=':') || (parameter[0]!='\0'&¶meter[0]=='u'&¶meter[1]==':'))
{/*当前路径指向了u     同时     参数为空或参数中的无驱动器名         或者       参数中指明了是u盘符       */
   printFolder_virtual(parameter,CurrentFolder,head_fat,Root_Folder);
   return;
}
if(parameter[0]=='\0')
   system(strcat(strcat(cmd," "),CurrentFolder));
    else
   system(strcat(strcat(cmd," "),parameter));
}
FILE *copyFile_Exception_check(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],char openFileName[LEN/PARAMETER*2])
{
FILE *fp1;
char *p=A[2],*q;

    if(!existChar(A[2],'.'))
        CatchFileNameFromTheRoute(A[1], &A[2][strlen(A[2])]);
    if((fp1=fopen(A[1],"r"))==NULL)
    {
        printf("Can't open the file: %s.\n",openFileName);
        return NULL;
    }
    strcpy(openFileName,A[2]);
return fp1;
}
void copyFile(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)                /*copy命令函数*/
{
    FILE *fp1,*fp2;
    char openFileName[LEN/PARAMETER*2]="";
    char temp;
   
if(CurrentFolder[0]=='u'&&(A[1][1]!=':'||A[2][1]!=':')||(A[1][0]=='u'&&A[1][1]==':')||(A[2][0]=='u'&&A[2][1]==':'))
{
   copyFile_virtual(A,CurrentFolder,head_fat,Root_Folder);
   return;
}
    fp1=copyFile_Exception_check(A,CurrentFolder,openFileName);
if(fp1==NULL)
   return;
    if((fp2=fopen(openFileName,"w"))==NULL)
    {
        printf("Can't open the file: %s.\n",openFileName);
        return ;
    }
    temp=fgetc(fp1);
    while(!feof(fp1))
    {
        fputc(temp,fp2);
        temp=fgetc(fp1);
    }
    fclose(fp1);
    fclose(fp2);
    printf("one file copied\n");
}
void outPut(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)          /*type命令函数*/
{
    char *p=A,temp;
    FILE *fp1;
    char fileName[LEN/PARAMETER*2];

if(CurrentFolder[0]=='u'&&A[1]!=':'||(A[0]=='u'&&A[1]==':'))
{
   outPut_virtual(A,CurrentFolder,head_fat,Root_Folder);
   return;
}
    if((fp1=fopen(A,"r"))==NULL)
    {
        printf("Can't open the file: %s.\n",A);
        return ;
    }
    temp=fgetc(fp1);
    while(!feof(fp1))
    {
        printf("%c",temp);
        temp=fgetc(fp1);
    }
    printf("\n");
}
void REname(char oldname[LEN/PARAMETER],char newname[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)      /*rename重命名函数*/
{ /*             原来的名字                    新名字                      当前路径   */

    char *p_old_name=oldname,*p_new_name=newname;
   
if(CurrentFolder[0]=='u'&&(oldname[1]!=':'||newname[1]!=':')||(oldname[0]=='u'&&oldname[1]==':')||(newname[0]=='u'&&newname[1]==':'))
{
   REname_virtual(oldname, newname, CurrentFolder,head_fat,Root_Folder);
   return;
}
    /* Rename the file */
    if (rename(oldname, newname) == 0)
        printf("Renamed %s to %s.\n", oldname, newname);
    else
        perror("rename");
    return ;
}
void clear_screen()
{
/*    clrscr();     */
    system("cls");
/*    system("cd");*/
}
void cdFloder_version2(char *A,char CurrentFolder[LEN/PARAMETER])/*判断一个特例"../"返回父路径*/
{
char smallStr[4],*p=A,*q=CurrentFolder;

smallStr[3]='\0';
while(!strcmp(strncpy(smallStr,p,3),"../"))
{
   if(strlen(CurrentFolder)>3)
    CurrentFolder[strlen(CurrentFolder)-1]='\0';
   q=CurrentFolder;
   while(existChar(q,'\\'))
    q +=existChar(q,'\\');
   if(strlen(CurrentFolder)>3)
    *q = '\0';
   p +=3;
}
strcat(CurrentFolder,p);
}
void cdFloder(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)        /*cd 命令的实现*/
{
    char smallStr[4],*p=A+3;

smallStr[3]='\0';
if( strcmp(strncpy(smallStr,A,3),"../") && ((A[0]<'A' || A[0]>'z'||(A[0]<'a' && A[0]>'Z')) || (0) ) ) /*(0)处应该该为判断该盘符是否存在,不存在返回1*/
    {
        ERROR();     /*非字母盘符*/
        return ;
    }
    if(CurrentFolder[0]=='u'&&A[1]!=':'||(A[0]=='u'&&A[1]==':'))
{
   cdFloder_virtual(A,CurrentFolder,head_fat,Root_Folder);
   return;
}
if(!strcmp(smallStr,"../"))
{
   cdFloder_version2(A,CurrentFolder);
   return;
}
    strcpy(CurrentFolder,A);  
}
void ERROR()
{
    printf("Bad cmd!\n");
}

阅读(335) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~