为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

一个简单的SHELL(在UNIX系统下创建进程和管理进程)

2018-06-28 26页 doc 50KB 104阅读

用户头像

is_337177

暂无简介

举报
一个简单的SHELL(在UNIX系统下创建进程和管理进程)一个简单的SHELL(在UNIX系统下创建进程和管理进程) 设计一: 设计任务: 实现一个简单的shell(命令行解释器),类似于bash, csh等。本设计的主要目的在于学会如何在Unix系统下创建进程和管理进程。 要求实现的shell支持以下内部命令: 1. cd 更改当前的工作目录到另一个。如果未指定,输出当前工作目录。如果不存在,要求有适当的错误信息提示。改命令应能够改变PWD的环境变量。 2. environ 列出所有环境变量字符串的设置(类似于Unix系统下的env命令)。 3. echo ...
一个简单的SHELL(在UNIX系统下创建进程和管理进程)
一个简单的SHELL(在UNIX系统下创建进程和管理进程) 一: 设计任务: 实现一个简单的shell(命令行解释器),类似于bash, csh等。本设计的主要目的在于学会如何在Unix系统下创建进程和管理进程。 要求实现的shell支持以下内部命令: 1. cd <目录> 更改当前的工作目录到另一个<目录>。如果<目录>未指定,输出当前工作目录。如果<目录>不存在,要求有适当的错误信息提示。改命令应能够改变PWD的环境变量。 2. environ 列出所有环境变量字符串的设置(类似于Unix系统下的env命令)。 3. echo <内容> 显示echo后的内容且换行。 4. help 简短概要地输出你的shell的使用方法和基本功能。 5. jobs 输出shell当前的一系列子进程,要求提供子进程的命名和PID号。 6. quit, exit, bye 退出shell。 所有的内部命令应当优于在$PATH中同名的程序。 任何非内部命令必须请求shell创建一个新进程,且该子进程执行指定的程序。这个新进程必须继承shell的环境变量和指定的命令行参数。 要求实现的shell支持以下内部命令: 1. Batch Processing 如果shell启动带有一个文件名作为参数,打开该文件并执行文件里所有命令。待所有进程全部结束退出shell。 2. Debugging 提供-v选项,shell启动时打开此选项将在运行过程中输出若干调试信息。在该模式下,shell应该显示所有被创建了的进程的PID号,通报已结束的子进程和传递给子进程的参数等。 3. Prompt (命令行提示符) 解释器打印$PS2作为提示符。 4. Background Processing 如果命令以符合&终止,在后台并发执行该程序。Shell立即等待下一命令的输入,而不等待该程序的结束。 注:所有命令和参数由空格或tab符分隔。 实验代码 #include #include #include #include #include #include #include /*定义全局变量*/ char root_dir[3]; char pre_dir[255]; char *cmd_line[255]; char curuser[10]; struct userinf { char username[10]; char userpass[10]; }; /*函数申明*/ void init(); int login(); int getcmd(); void dir(); void cd(); void clear(); void newdir(); void deldir(); void del(); void copy(); void cut(); void account(); void help(); main() { init(); while(1)/* 消息循环 */ { switch(getcmd()) { case 0: help(); break; case 1: dir(); break; case 2: cd(); break; case 3: newdir(); break; case 4: deldir(); break; case 5: del(); break; case 6: copy(); break; case 7: cut(); break; case 8: account(); break; } } } void init()/* 程序初始化 */ { if(login()==0) { exit(0); } strcpy(pre_dir,"C:\\");/*设定当前目录*/ clear();/* 清屏 */ printf("S Shell-Above Windows XP [Ver 1.0]\n"); printf("(C) Copyright 2007 stars_625.\n\n"); getchar();/* 清空缓冲区 */ } int login()/* 程序登陆 */ { char name[10]; char pass[10]; int logintime=3; FILE *fp; struct userinf inf; while(logintime>0)/* 登陆错误超过三次自动退出 */ { printf("Login:"); scanf("%s",name); printf("Password:"); scanf("%s",pass); if((fp=fopen("inf.dll","r"))==NULL) { printf("Can't open inf.dll file!\n"); printf("Press any key to exit..."); getch(); exit(0); } while(fread(&inf,sizeof(inf),1,fp)==1 && strcmp(inf.username,name)!=0) { } fclose(fp); if(strcmp(inf.username,name)==0) { if(strcmp(inf.userpass,pass)==0) { strcpy(curuser,inf.username); clear(); return 1; } else { printf("Login error, Press any key to relogin!\n"); getch(); clear(); } } else { printf("The user is not exist, Press any key to relogin!\n"); getch(); clear(); } logintime--; } printf("Login error above three times, Press any key to exit!"); getch(); return 0; } int getcmd()/* 获得命令 */ { int i=0,j=0,k=0; char buf[255]; printf("%s>",pre_dir);/* 打印提示符 */ fgets(buf, 255, stdin); cmd_line[j] = calloc(255, sizeof(char)); while (buf[i] != '\n' && buf[i] != '\0')/* 命令分析 */ { if (buf[i] != ' ') { cmd_line[j][k] = buf[i]; ++k; } else { cmd_line[j + 1] = calloc(255, sizeof(char)); k = 0; ++j; } ++i; } cmd_line[j + 1]=0; if(strcmp(cmd_line[0],"exit")==0) { exit(0); } else if(strcmp(cmd_line[0],"/?")==0 || strcmp(cmd_line[1],"/?")==0) { return 0; } else if(strcmp(cmd_line[0],"dir")==0) { return 1; } else if(strcmp(cmd_line[0],"cd")==0) { return 2; } else if(strcmp(cmd_line[0],"newdir")==0) { return 3; } else if(strcmp(cmd_line[0],"deldir")==0) { return 4; } else if(strcmp(cmd_line[0],"del")==0) { return 5; } else if(strcmp(cmd_line[0],"copy")==0) { return 6; } else if(strcmp(cmd_line[0],"cut")==0) { return 7; } else if(strcmp(cmd_line[0],"account")==0) { return 8; } else if(cmd_line[0][1]==':') { strcpy(pre_dir,cmd_line[0]); strcat(pre_dir,"\\"); } else if(strcmp(cmd_line[0],"clear")==0) { clear(); } else { printf("The command is not supported!\n"); } } void dir()/* 列出文件及文件夹 */ { struct ffblk ff; char filepath[255]; strcpy(filepath,pre_dir); findfirst(strcat(filepath,"*.*"),&ff,FA_DIREC); if(ff.ff_attrib==16) { printf("\t"); } else { printf(" \t"); } printf("%s\n",ff.ff_name); while(findnext(&ff)==0) { if(ff.ff_attrib==16) { printf("\t"); } else { printf(" \t"); } printf("%s\n",ff.ff_name); } } void cd()/* 改变当前目录 */ { int i=0; struct ffblk ff; char filepath[255]; strcpy(filepath,pre_dir); if(strcmp(cmd_line[1],"..")==0)/* 返回上一层目录 */ { while(filepath[i]!='\0') { i++; } if(filepath[i-2]!=':') { i=i-2; while(filepath[i]!='\\' && i>=2) { i--; } filepath[i+1]='\0'; strcpy(pre_dir,filepath); } } else if(strcmp(cmd_line[1],"\\")==0)/*返回根目录*/ { while(filepath[i]!='\\') { i++; } filepath[i+1]='\0'; strcpy(pre_dir,filepath); } else { findfirst(strcat(filepath,"*.*"),&ff,FA_DIREC); while(strcmp(ff.ff_name,cmd_line[1])!=0) { if(findnext(&ff)!=0) { break; } } if(strcmp(ff.ff_name,cmd_line[1])==0) { strcat(pre_dir,cmd_line[1]); strcat(pre_dir,"\\"); } else { printf("Can't find the file!\n"); } } } void clear() { clrscr(); } void newdir() { char filepath[255]; strcpy(filepath,pre_dir); if(mkdir(strcat(filepath,cmd_line[1]))==0) { printf("Make dir '%s' successfully!\n",cmd_line[1]); } else { printf("Make dir error!\n"); } } void deldir() { char filepath[255]; strcpy(filepath,pre_dir); if(rmdir(strcat(filepath,cmd_line[1]))==0) { printf("Delete dir '%s' successfully!\n",cmd_line[1]); } else { printf("Delete dir error!\n"); } } void del() { char filepath[255]; strcpy(filepath,pre_dir); if(unlink(strcat(filepath,cmd_line[1]))==0) { printf("Delete %s successfully!\n",cmd_line[1]); } else { printf("Delete error!\n"); } } void copy() { char filepath[255]; char sourcepath[255]; char aimpath[255]; FILE *newfp; FILE *oldfp; char ch; strcpy(filepath,pre_dir); if(cmd_line[1][1]!=':') { strcpy(sourcepath,filepath); strcat(sourcepath,cmd_line[1]); } else { strcpy(sourcepath,cmd_line[1]); } if(cmd_line[2][1]!=':') { strcpy(aimpath,filepath); strcat(aimpath,cmd_line[2]); } else { strcpy(aimpath,cmd_line[2]); } if((oldfp=fopen(sourcepath,"r"))==NULL) { printf("Can't open old file!\n"); } if((newfp=fopen(aimpath,"w"))==NULL) { printf("Can't creat new file!\n"); } while((ch=fgetc(oldfp))!=EOF) { fputc(ch,newfp); } fclose(oldfp); fclose(newfp); printf("Copy from %s to %s successfully!\n",sourcepath,aimpath); } void cut() { char filepath[255]; char sourcepath[255]; char aimpath[255]; FILE *newfp; FILE *oldfp; char ch; strcpy(filepath,pre_dir); if(cmd_line[1][1]!=':') { strcpy(sourcepath,filepath); strcat(sourcepath,cmd_line[1]); } else { strcpy(sourcepath,cmd_line[1]); } if(cmd_line[2][1]!=':') { strcpy(aimpath,filepath); strcat(aimpath,cmd_line[2]); } else { strcpy(aimpath,cmd_line[2]); } if((oldfp=fopen(sourcepath,"r"))==NULL) { printf("Can't open old file!\n"); } if((newfp=fopen(aimpath,"w"))==NULL) { printf("Can't creat new file!\n"); } while((ch=fgetc(oldfp))!=EOF) { fputc(ch,newfp); } fclose(oldfp); fclose(newfp); if(unlink(sourcepath)==0) { printf("Cut from %s to %s successfully!\n",sourcepath,aimpath); } else { printf("Delete old file error!\n"); } } void account() { FILE *fp; struct userinf inf; if(strcmp(cmd_line[1],"/add")==0) { if((fp=fopen("inf.dll","r+"))==NULL) { printf("Can't open inf.dll file!\n"); printf("Press any key to exit..."); getch(); } while(fread(&inf,sizeof(inf),1,fp)==1 && strcmp(inf.username,cmd_line[2])!=0) { } if(strcmp(inf.username,cmd_line[2])==0) { printf("Create user error, the user is exist!\n"); } else { strcpy(inf.username,cmd_line[2]); strcpy(inf.userpass,cmd_line[3]); if(fwrite(&inf,sizeof(inf),1,fp)==1) { printf("Create user %s successfully!\n",inf.username); } else { printf("Create user error!\n"); } } fclose(fp); } else if(strcmp(cmd_line[1],"/edit")==0) { if((fp=fopen("inf.dll","r+"))==NULL) { printf("Can't open inf.dll file!\n"); printf("Press any key to exit..."); getch(); } while(fread(&inf,sizeof(inf),1,fp)==1 && strcmp(inf.username,cmd_line[2])!=0) { } if(strcmp(inf.username,cmd_line[2])!=0) { printf("Edit user error, the user is not exist!\n"); } else { strcpy(inf.username,cmd_line[2]); strcpy(inf.userpass,cmd_line[3]); fseek(fp,-20L,1); if(fwrite(&inf,sizeof(inf),1,fp)==1) { printf("Edit user %s successfully!\n",inf.username); } else { printf("Edit user error!\n"); } } fclose(fp); } else { printf("Please enter correct parameter,type /? for help!\n"); } } void help() { if(strcmp(cmd_line[0],"/?")==0) { printf("The list of commands.\n\n"); printf(" dir\t\tList the files and dirs.\n"); printf(" cd\t\tChange the dir.\n"); printf(" clear\t\tClear the screen.\n"); printf(" newdir\tMake a dir.\n"); printf(" deldir\tDelete a dir.\n"); printf(" del\t\tDelete a file.\n"); printf(" copy\t\tCopy a file from a place to another.\n"); printf(" cut\t\tCut a file from a place to another.\n"); printf(" account\tAdd edit or delete a account.\n\n"); printf("For more information add type /? after command.\n\n"); printf("Notice:All the command line is capitalization distinction!\n\n"); } else { if(strcmp(cmd_line[0],"dir")==0) { printf("List the files and dirs.\n\n"); printf("dir path\n\n"); printf(" path\t\tThe dir you want to list.\n"); printf(" \t\tif path is NULL then list the current dir.\n\n"); } else if(strcmp(cmd_line[0],"cd")==0) { printf("Change the dir.\n\n"); printf("cd < \\ | .. | path>\n\n"); printf(" \\\t\tReturn to the root dir.\n"); printf(" ..\t\tReturn to the parent dir.\n"); printf(" path\t\tThe dir you want change to.\n\n"); } else if(strcmp(cmd_line[0],"clear")==0) { printf("Clear the screen.\n\n"); printf("clear\n\n"); } else if(strcmp(cmd_line[0],"newdir")==0) { printf("Make a dir.\n\n"); printf("newdir path\n\n"); printf(" path\t\tThe dir path which you want to create.\n\n"); } else if(strcmp(cmd_line[0],"deldir")==0) { printf("Delete a dir.\n\n"); printf("deldir path\n\n"); printf(" path\t\tThe dir path which you want to delete.\n\n"); } else if(strcmp(cmd_line[0],"del")==0) { printf("Delete a file.\n\n"); printf("del filepath\n\n"); printf(" filepath\tThe filepath which you want to delete.\n\n"); } else if(strcmp(cmd_line[0],"copy")==0) { printf("Copy a file from a place to another.\n\n"); printf("copy source aim\n\n"); printf(" source\tThe source file path.\n"); printf(" aim\t\tThe aim file path.\n\n"); } else if(strcmp(cmd_line[0],"cut")==0) { printf("Cut a file from a place to another.\n\n"); printf("cut source aim\n\n"); printf(" source\tThe source file path.\n"); printf(" aim\t\tThe aim file path.\n\n"); } else if(strcmp(cmd_line[0],"account")==0) { printf("Add edit or delete a account.\n\n"); printf("account < /add | /edit | /del > username userpass\n\n"); printf(" username\tThe account name.\n"); printf(" userpass\tThe account password.\n"); printf(" /add\t\tAdd a account.\n"); printf(" /edit\t\tEdit a account.\n"); printf(" /del\t\tDelete a account.\n\n"); } else { printf("The command is not supported!\n"); } } }
/
本文档为【一个简单的SHELL(在UNIX系统下创建进程和管理进程)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索