📄 homework.c
字号:
#include<stdio.h>#include<fcntl.h>#include<string.h>#include<stdlib.h>main(){ int flag=1;//the flag to distinguish between "quit" and other dictate char statement[10];//statement can only in 10 rooms char filename[20];//name can only in 20 rooms char filename1[20];//for copying char dictate[50];//The dictate user tell char *tokenPtr; int str; int h;//user's dictate,now separate into two parts int s; int d; int f;//initial array int k;//for getchar(),connect with content[] int fd;//file needs parameter int fd1;//for copying char content[100];//content,can sustain with reuse char fg[10];//work with reading and writing char use[100]; int i=0; int j; int ptr=0;//for transfering with system char commend[10];//use for "man" int fl; while(flag==1) { h=0;//the most important in initial,or it will cause two dictate wrong for(s=0;s<50;s++) dictate[s]=NULL; for(d=0;d<10;d++) statement[d]=NULL; for(f=0;f<20;f++) filename[f]=NULL;//initial array printf("Please tell the dictate:\n"); while((str=getchar())!=10) { (int)dictate[h]=str; h=h+1; } tokenPtr=strtok(dictate," "); strcpy(statement,tokenPtr); //to solute the one dictate or with filename question //when meet " vi" "read""write" //then the copy the filename to array//separate the dictate if(strcmp(statement,"quit")==0) { flag=0; }//the flag to quit the shell program if(strcmp(statement,"vi")==0) { tokenPtr=strtok(NULL," "); strcpy(filename,tokenPtr);//copy the filename fd= open(filename,O_CREAT,0666); close(fd); printf("The file is correctly created!!\n"); } if(strcmp(statement,"write")==0) { tokenPtr=strtok(NULL," "); strcpy(filename,tokenPtr);//copy the filename fd=open(filename,O_APPEND|O_WRONLY,0666);// if(fd>=0) { printf("Please give the context,press Esc to quit:\n"); while((k=getchar())!=27)//while if getchar isn't Esc { if(i==100)//the content beyond the mark { i=0; write(fd,content,100); }//clear i,write the content content[i++]=k;//put k into the array } content[i]='\0';//end write(fd,content,i); close(fd); getchar();//for soluting the question //this is the most important sentence printf("The content is correctly written!! \n"); } else printf("The file isn't exist!!\n"); } if(strcmp(statement,"read")==0) { fl=0; tokenPtr=strtok(NULL," "); strcpy(filename,tokenPtr);//copy the filename fd=open(filename,O_APPEND|O_RDONLY,0666); if(fd>=0) { while(fl==0) { for(k=0;k<=9;k++) fg[k]=NULL; read(fd,fg,10); for(k=0;k<=9;k++) { if(fg[k]==NULL) { fl=1; continue; } printf("%c",fg[k]); } } close(fd); } else printf("The file isn't exist!!\n"); } if(strcmp(statement,"copy")==0) { fl=0; tokenPtr=strtok(NULL," "); strcpy(filename,tokenPtr);//copy info from the file//for reading tokenPtr=strtok(NULL," ");//copy info to the file1//for writing strcpy(filename1,tokenPtr); fd=open(filename,O_APPEND|O_RDONLY,0666);//for reading fd1=open(filename1,O_APPEND|O_WRONLY,0666);//for writing if(fd>=0) { if(fd1>=0) { while(fl==0) { for(k=0;k<=99;k++) { content[k]=NULL; }//reuse the buffer read(fd,content,100);//read 100 rooms from the file for(k=0;k<=99;k++) { if(content[k]==NULL) { i=1; content[k]='\0'; } }//search the buffer, if(i==0)//define outside write(fd1,content,100); else { write(fd1,content,k); fl=1; printf("The action 'copy' has successful been done!\n"); continue; } } } } else printf("The file isn't exist!!\n"); close(fd); close(fd1); } if(strcmp(statement,"rm")==0) { tokenPtr=strtok(NULL," "); strcpy(filename,tokenPtr); printf("Are you sure to remove this file(Y/N):\n"); if(getchar()=='Y') { use[0]='r';use[1]='m';use[2]=' ';use[3]='\0'; strcat(use,filename); system(use); printf("The file is successly delete!!\n"); } getchar(); }//use system to finish function "rm" if(strcmp(statement,"ls")==0) { printf("The file in the path is: \n"); use[0]='l';use[1]='s';use[2]='\0'; system(use); }//use fork() to finish function "ls" if(strcmp(statement,"Ls")==0) { tokenPtr=strtok(NULL," "); strcpy(filename,tokenPtr); use[0]='l';use[1]='s';use[2]=' ';use[3]='\0'; strcat(use,filename); system(use); } if(strcmp(statement,"help")==0) { printf("The statements in the Model Shell are:\n"); printf("vi write read rm ls quit help man Ls copy \n"); }//introduce to My shell if(strcmp(statement,"man")==0) { tokenPtr=strtok(NULL," "); strcpy(commend,tokenPtr); if(strcmp(commend,"vi")==0) { printf("This commend will creat a new file!!\n"); printf("Format is like this:vi filename/path filename\n"); printf("That will clue the message!\n"); } if(strcmp(commend,"write")==0) { printf("This commend will write a file!!\n"); printf("Format is like this:write filename\n"); printf("Ask you to tell the content,pass Esc to esc!!\n"); } if(strcmp(commend,"read")==0) { printf("This commend will read a file for you\n"); printf("Format is like this:read filename\n"); printf("Then will slow the content!!\n"); } if(strcmp(commend,"rm")==0) { printf("This commend will delete a file!!\n"); printf("Format is like this:rm filename\n"); printf("That will delete the file!!\n"); } if(strcmp(commend,"ls")==0) { printf("This will tell you the file in this index!!\n"); } if(strcmp(commend,"quit")==0) { printf("This will quit My shell!!\n"); } if(strcmp(commend,"help")==0) { printf("This will display the dictate in MY shell!!\n"); } if(strcmp(commend,"Ls")==0) { printf("This commend will sustain display the path information!\n"); } if(strcmp(commend,"copy")==0) { printf("This will copy a content to another file!!"); } } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -