程序效果:
只实现了login 、cd 、ls 、cat 四个命令。而且只能在 Windows 下运行。
代码:
//main.cpp 1 #include <iostream> #include "shell.h" using namespace std; int com_to_int(int com_num,char * command,char** coms){ ; } int main() { ; char command[MAX_LENGTH_OF_COMMAND]; char *coms[COM_NUM]={"login","ls","cd","cat","exit"}; int (*func_ptrs[COM_NUM])(char *)={login,ls,cd,cat,shellexit}; while(true){ printf("%s%s%s%s%s","[",USER_NAME," ",GLOBLE_PATH,"]"); gets(command); ){ continue; } //printf("command: %s\n",command); //printf("command: %s\n",command); int i; ;i<COM_NUM;i++){ ){ func_ptrs[i](command); break; } } if(i==COM_NUM){ printf("command not found.\n"); } } cd("C:/0"); ls(GLOBLE_PATH); ; }
//shell.h 1 #ifndef SHELL_H_INCLUDED #define SHELL_H_INCLUDED #include<io.h> #include<string.h> #include<stdio.h> #include<stdlib.h> #define MAX_LENGTH_OF_PATH 256 #define MAX_LENGTH_OF_USERNAME 50 #define MAX_LENGTH_OF_COMMAND 50 #define MAX_LENGTH_OF_LINE 256 ','\0'}; char USER_NAME[MAX_LENGTH_OF_USERNAME]= {'g','u','e','s','t','\0'}; int login(char * command) { printf("user name:"); char username[MAX_LENGTH_OF_USERNAME]; gets(username); FILE * pwd; pwd = fopen ("pwd","r"); if (pwd!=NULL) { char line[MAX_LENGTH_OF_LINE]; while (!feof(pwd)) { if(fgets(line,MAX_LENGTH_OF_LINE,pwd)!=NULL) { char * name=strtok(line," "); //printf("%s\n",name); ) { char password[MAX_LENGTH_OF_USERNAME]; printf("password:"); gets(password); char * pasw=strtok(NULL," \n\r"); //printf("file password:%s\n",pasw); ) { strcpy(USER_NAME,username); } else { printf("wrong password.\n"); } fclose (pwd); ; } } } printf("user not exist.\n"); fclose (pwd); } else { printf("login error.pwd file not exist or not able to open.\n"); } ; } int shellexit(char * command) { exit(); ; } int cat_file(char * file_path) { FILE* f=fopen(file_path,"r"); if(f==NULL) { printf("file does not exist.\n"); ; } char line[MAX_LENGTH_OF_LINE]; while(!feof(f)) { fgets(line,MAX_LENGTH_OF_LINE,f); ;i<strlen(line);i++){ if(line[i]=='\0')break; printf("%c",line[i]); } } printf("\n"); ; } int cat(char * command) { char * path=strtok(command," "); path=strtok(NULL," "); //printf("path:%s",path); cat_file(path); ; } int cd_path(char *path) { struct _finddata_t f; int p; //printf("1"); /* FILE * pFile; pFile = fopen (path,"r"); if (pFile!=NULL) { printf("2"); }printf("3");*/ ) { //printf("%d",p); //printf("2"); //printf(" %d ",f.attrib); if(f.attrib&_A_SUBDIR) { //printf("4"); strcpy(GLOBLE_PATH,path); ; } else { printf("destination path is not a folder.\n"); } } else { printf("destination not exist.\n"); } ; } int cd(char * command) { char * path=strtok(command," "); path=strtok(NULL," "); //printf("path:%s",path); cd_path(path); ; } int ls_path(char * path) { struct _finddata_t f; int p; char name[MAX_LENGTH_OF_PATH]; strcpy(name,path); strcat(name,"/*"); //printf("1"); ) { //read(p,filename,) //printf("2"); printf(f.name); printf("\n"); ) { printf(f.name); printf("\n"); } } else { } ; } int ls(char * command) { char * path=strtok(command," "); path=strtok(NULL," "); //printf("path:%s",path); ) { ls_path(GLOBLE_PATH); } else { ls_path(path); } ; } #endif // SHELL_H_INCLUDED
外部资源文件:
pwd(login 命令使用):
ma xue wei
程序写于大三下学期,2016年3月。
2016.4.12更新博客。