码迷,mamicode.com
首页 > 编程语言 > 详细

Shell(C++实现,CodeBlocks+GCC编译)

时间:2016-03-14 00:11:32      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:

//main.cpp
1
#include <iostream> 2 #include "shell.h" 3 using namespace std; 4 int com_to_int(int com_num,char * command,char** coms){ 5 6 return -1; 7 } 8 int main() 9 { 10 const int COM_NUM=5; 11 char command[MAX_LENGTH_OF_COMMAND]; 12 char *coms[COM_NUM]={"login","ls","cd","cat","exit"}; 13 int (*func_ptrs[COM_NUM])(char *)={login,ls,cd,cat,shellexit}; 14 while(true){ 15 printf("%s%s%s%s%s","[",USER_NAME," ",GLOBLE_PATH,"]"); 16 gets(command); 17 if(strcmp(command,"")==0){ 18 continue; 19 } 20 //printf("command: %s\n",command); 21 //printf("command: %s\n",command); 22 int i; 23 for(i=0;i<COM_NUM;i++){ 24 if(strncmp(coms[i],command,strlen(coms[i]))==0){ 25 func_ptrs[i](command); 26 break; 27 } 28 } 29 if(i==COM_NUM){ 30 printf("command not found.\n"); 31 } 32 } 33 cd("C:/0"); 34 ls(GLOBLE_PATH); 35 return 0; 36 }

 

 

//shell.h
1
#ifndef SHELL_H_INCLUDED 2 #define SHELL_H_INCLUDED 3 #include<io.h> 4 #include<string.h> 5 #include<stdio.h> 6 #include<stdlib.h> 7 8 #define MAX_LENGTH_OF_PATH 256 9 #define MAX_LENGTH_OF_USERNAME 50 10 #define MAX_LENGTH_OF_COMMAND 50 11 #define MAX_LENGTH_OF_LINE 256 12 char GLOBLE_PATH[MAX_LENGTH_OF_PATH]= {C,:,/,0,\0}; 13 char USER_NAME[MAX_LENGTH_OF_USERNAME]= {g,u,e,s,t,\0}; 14 int login(char * command) 15 { 16 printf("user name:"); 17 char username[MAX_LENGTH_OF_USERNAME]; 18 gets(username); 19 FILE * pwd; 20 pwd = fopen ("pwd","r"); 21 22 if (pwd!=NULL) 23 { 24 char line[MAX_LENGTH_OF_LINE]; 25 while (!feof(pwd)) 26 { 27 if(fgets(line,MAX_LENGTH_OF_LINE,pwd)!=NULL) 28 { 29 char * name=strtok(line," "); 30 //printf("%s\n",name); 31 if(strcmp(name,username)==0) 32 { 33 char password[MAX_LENGTH_OF_USERNAME]; 34 printf("password:"); 35 gets(password); 36 char * pasw=strtok(NULL," \n\r"); 37 //printf("file password:%s\n",pasw); 38 if(strcmp(pasw,password)==0) 39 { 40 strcpy(USER_NAME,username); 41 } 42 else 43 { 44 printf("wrong password.\n"); 45 } 46 fclose (pwd); 47 return 0; 48 } 49 } 50 } 51 printf("user not exist.\n"); 52 fclose (pwd); 53 } 54 else 55 { 56 printf("login error.pwd file not exist or not able to open.\n"); 57 } 58 return 0; 59 } 60 int shellexit(char * command) 61 { 62 exit(0); 63 return 0; 64 } 65 int cat_file(char * file_path) 66 { 67 FILE* f=fopen(file_path,"r"); 68 if(f==NULL) 69 { 70 printf("file does not exist.\n"); 71 return -1; 72 } 73 char line[MAX_LENGTH_OF_LINE]; 74 while(!feof(f)) 75 { 76 fgets(line,MAX_LENGTH_OF_LINE,f); 77 for(int i=0;i<strlen(line);i++){ 78 if(line[i]==\0)break; 79 printf("%c",line[i]); 80 } 81 82 } 83 printf("\n"); 84 return 0; 85 } 86 int cat(char * command) 87 { 88 char * path=strtok(command," "); 89 path=strtok(NULL," "); 90 //printf("path:%s",path); 91 cat_file(path); 92 return 0; 93 } 94 95 int cd_path(char *path) 96 { 97 struct _finddata_t f; 98 int p; 99 //printf("1"); 100 /* FILE * pFile; 101 pFile = fopen (path,"r"); 102 if (pFile!=NULL) 103 { printf("2"); 104 }printf("3");*/ 105 if((p=_findfirst(path, &f))!=-1) 106 { 107 //printf("%d",p); 108 //printf("2"); 109 //printf(" %d ",f.attrib); 110 if(f.attrib&_A_SUBDIR) 111 { 112 //printf("4"); 113 strcpy(GLOBLE_PATH,path); 114 return 0; 115 } 116 else 117 { 118 printf("destination path is not a folder.\n"); 119 } 120 } 121 else 122 { 123 printf("destination not exist.\n"); 124 } 125 return -1; 126 } 127 128 int cd(char * command) 129 { 130 char * path=strtok(command," "); 131 path=strtok(NULL," "); 132 //printf("path:%s",path); 133 cd_path(path); 134 return 0; 135 } 136 137 int ls_path(char * path) 138 { 139 struct _finddata_t f; 140 int p; 141 char name[MAX_LENGTH_OF_PATH]; 142 strcpy(name,path); 143 strcat(name,"/*"); 144 //printf("1"); 145 if((p=_findfirst(name, &f))!=-1) 146 { 147 //read(p,filename,) 148 //printf("2"); 149 printf(f.name); 150 printf("\n"); 151 while(_findnext(p, &f)==0) 152 { 153 printf(f.name); 154 printf("\n"); 155 } 156 } 157 else 158 { 159 } 160 return 0; 161 } 162 int ls(char * command) 163 { 164 char * path=strtok(command," "); 165 path=strtok(NULL," "); 166 //printf("path:%s",path); 167 if(path==NULL||strcmp(path,"")==0) 168 { 169 ls_path(GLOBLE_PATH); 170 } 171 else 172 { 173 ls_path(path); 174 } 175 return 0; 176 } 177 #endif // SHELL_H_INCLUDED

 

 

pwd文件内容:

ma 123456
xue 123456
wei 123456

 

 

技术分享

Shell(C++实现,CodeBlocks+GCC编译)

标签:

原文地址:http://www.cnblogs.com/maxuewei2/p/5273372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!