一个简单的SHELL在UNIX系统下创建进程和管理进程_第1页
一个简单的SHELL在UNIX系统下创建进程和管理进程_第2页
一个简单的SHELL在UNIX系统下创建进程和管理进程_第3页
一个简单的SHELL在UNIX系统下创建进程和管理进程_第4页
一个简单的SHELL在UNIX系统下创建进程和管理进程_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、设计一:设计任务:实现一个简单的shell(命令行解释器),类似于bash, csh等。本设计的主要目的在于学会如何在Unix系统下创建进程和管理进程。要求实现的shell支持以下内部命令:1. cd <目录>更改当前的工作目录到另一个<目录>。如果<目录>未指定,输出当前工作目录。如果<目录>不存在,要求有适当的错误信息提示。改命令应能够改变PWD的环境变量。2. environ 列出所有环境变量字符串的设置(类似于Unix系统下的env命令)。3. echo <内容>显示echo后的内容且换行。4. help 简短概要地输出你的s

2、hell的使用方法和基本功能。5. jobs输出shell当前的一系列子进程,要求提供子进程的命名和PID号。6. quit, exit, bye退出shell。所有的内部命令应当优于在$PATH中同名的程序。任何非内部命令必须请求shell创建一个新进程,且该子进程执行指定的程序。这个新进程必须继承shell的环境变量和指定的命令行参数。要求实现的shell支持以下内部命令:1. Batch Processing 如果shell启动带有一个文件名作为参数,打开该文件并执行文件里所有命令。待所有进程全部结束退出shell。2. Debugging 提供-v选项,shell启动时打开此选项将在运

3、行过程中输出若干调试信息。在该模式下,shell应该显示所有被创建了的进程的PID号,通报已结束的子进程和传递给子进程的参数等。3. Prompt (命令行提示符) 解释器打印$PS2作为提示符。4. Background Processing 如果命令以符合&终止,在后台并发执行该程序。Shell立即等待下一命令的输入,而不等待该程序的结束。注:所有命令和参数由空格或tab符分隔。实验代码#include <string.h>#include <stdlib.h>#include <conio.h>#include <stdio.h>#

4、include <dir.h>#include <dos.h>#include <time.h>/*定义全局变量*/char root_dir3;char pre_dir255;char *cmd_line255;char curuser10;struct userinf char username10; char userpass10;/*函数申明*/void init();int login();int getcmd();void dir();void cd();void clear();void newdir();void deldir();void

5、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: acc

6、ount(); break; void init()/* 程序初始化 */ if(login()=0) exit(0); strcpy(pre_dir,"C:");/*设定当前目录*/ clear();/* 清屏 */ printf("S Shell-Above Windows XP Ver 1.0n"); printf("(C) Copyright 2007 stars_625.nn"); getchar();/* 清空缓冲区 */int login()/* 程序登陆 */ char name10; char pass10; int

7、 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"

8、); 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 erro

9、r, 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

10、buf255; printf("%s>",pre_dir);/* 打印提示符 */ fgets(buf, 255, stdin); cmd_linej = calloc(255, sizeof(char); while (bufi != 'n' && bufi != '0')/* 命令分析 */ if (bufi != ' ') cmd_linejk = bufi; +k; else cmd_linej + 1 = calloc(255, sizeof(char); k = 0; +j; +i; cmd_

11、linej + 1=0; if(strcmp(cmd_line0,"exit")=0) exit(0); else if(strcmp(cmd_line0,"/?")=0 | strcmp(cmd_line1,"/?")=0) return 0; else if(strcmp(cmd_line0,"dir")=0) return 1; else if(strcmp(cmd_line0,"cd")=0) return 2; else if(strcmp(cmd_line0,"newdir

12、")=0) return 3; else if(strcmp(cmd_line0,"deldir")=0) return 4; else if(strcmp(cmd_line0,"del")=0) return 5; else if(strcmp(cmd_line0,"copy")=0) return 6; else if(strcmp(cmd_line0,"cut")=0) return 7; else if(strcmp(cmd_line0,"account")=0) return

13、 8; else if(cmd_line01=':') strcpy(pre_dir,cmd_line0); strcat(pre_dir,""); else if(strcmp(cmd_line0,"clear")=0) clear(); else printf("The command is not supported!n"); void dir()/* 列出文件及文件夹 */ struct ffblk ff; char filepath255; strcpy(filepath,pre_dir); findfirs

14、t(strcat(filepath,"*.*"),&ff,FA_DIREC); if(ff.ff_attrib=16) printf("<DIR>t"); else printf(" t"); printf("%sn",ff.ff_name); while(findnext(&ff)=0) if(ff.ff_attrib=16) printf("<DIR>t"); else printf(" t"); printf("%sn&

15、quot;,ff.ff_name); void cd()/* 改变当前目录 */ int i=0; struct ffblk ff; char filepath255; strcpy(filepath,pre_dir); if(strcmp(cmd_line1,".")=0)/* 返回上一层目录 */ while(filepathi!='0') i+; if(filepathi-2!=':') i=i-2; while(filepathi!='' && i>=2) i-; filepathi+1='

16、;0' strcpy(pre_dir,filepath); else if(strcmp(cmd_line1,"")=0)/*返回根目录*/ while(filepathi!='') i+; filepathi+1='0' strcpy(pre_dir,filepath); else findfirst(strcat(filepath,"*.*"),&ff,FA_DIREC); while(strcmp(ff.ff_name,cmd_line1)!=0) if(findnext(&ff)!=0) b

17、reak; if(strcmp(ff.ff_name,cmd_line1)=0) strcat(pre_dir,cmd_line1); strcat(pre_dir,""); else printf("Can't find the file!n"); void clear() clrscr();void newdir() char filepath255; strcpy(filepath,pre_dir); if(mkdir(strcat(filepath,cmd_line1)=0) printf("Make dir '%s&#

18、39; successfully!n",cmd_line1); else printf("Make dir error!n"); void deldir() char filepath255; strcpy(filepath,pre_dir); if(rmdir(strcat(filepath,cmd_line1)=0) printf("Delete dir '%s' successfully!n",cmd_line1); else printf("Delete dir error!n"); void del

19、() char filepath255; strcpy(filepath,pre_dir); if(unlink(strcat(filepath,cmd_line1)=0) printf("Delete %s successfully!n",cmd_line1); else printf("Delete error!n"); void copy() char filepath255; char sourcepath255; char aimpath255; FILE *newfp; FILE *oldfp; char ch; strcpy(filepat

20、h,pre_dir); if(cmd_line11!=':') strcpy(sourcepath,filepath); strcat(sourcepath,cmd_line1); else strcpy(sourcepath,cmd_line1); if(cmd_line21!=':') strcpy(aimpath,filepath); strcat(aimpath,cmd_line2); else strcpy(aimpath,cmd_line2); if(oldfp=fopen(sourcepath,"r")=NULL) printf

21、("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 fil

22、epath255; char sourcepath255; char aimpath255; FILE *newfp; FILE *oldfp; char ch; strcpy(filepath,pre_dir); if(cmd_line11!=':') strcpy(sourcepath,filepath); strcat(sourcepath,cmd_line1); else strcpy(sourcepath,cmd_line1); if(cmd_line21!=':') strcpy(aimpath,filepath); strcat(aimpath,c

23、md_line2); else strcpy(aimpath,cmd_line2); 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(newf

24、p); 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_line1,"/add")=0) if(fp=fopen("inf.dll","r+")=NULL) printf(&

25、quot;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_line2)!=0) if(strcmp(inf.username,cmd_line2)=0) printf("Create user error, the user is exist!n"); else strcpy(inf.user

26、name,cmd_line2); strcpy(inf.userpass,cmd_line3); 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_line1,"/edit")=0) if(fp=fopen("inf.dll","r+&q

27、uot;)=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_line2)!=0) if(strcmp(inf.username,cmd_line2)!=0) printf("Edit user error, the user is not exist!n"

28、); else strcpy(inf.username,cmd_line2); strcpy(inf.userpass,cmd_line3); 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,typ

29、e /? for help!n"); void help() if(strcmp(cmd_line0,"/?")=0) printf("The list of commands.nn"); printf(" dirttList the files and dirs.n"); printf(" cdttChange the dir.n"); printf(" clearttClear the screen.n"); printf(" newdirtMake a dir.n&qu

30、ot;); printf(" deldirtDelete a dir.n"); printf(" delttDelete a file.n"); printf(" copyttCopy a file from a place to another.n"); printf(" cutttCut a file from a place to another.n"); printf(" accounttAdd edit or delete a account.nn"); printf("Fo

31、r more information add type /? after command.nn"); printf("Notice:All the command line is capitalization distinction!nn"); else if(strcmp(cmd_line0,"dir")=0) printf("List the files and dirs.nn"); printf("dir pathnn"); printf(" pathttThe dir you want

32、to list.n"); printf(" ttif path is NULL then list the current dir.nn"); else if(strcmp(cmd_line0,"cd")=0) printf("Change the dir.nn"); printf("cd < | . | path>nn"); printf(" ttReturn to the root dir.n"); printf(" .ttReturn to the pare

33、nt dir.n"); printf(" pathttThe dir you want change to.nn"); else if(strcmp(cmd_line0,"clear")=0) printf("Clear the screen.nn"); printf("clearnn"); else if(strcmp(cmd_line0,"newdir")=0) printf("Make a dir.nn"); printf("newdir pathnn"); printf(" pathttThe dir path which you want to create.nn"); else if(strcmp(cmd_line0,"deldir")=0) printf("Delete a dir.nn"); printf("deldir pathnn"); printf(" pathttThe dir path which you want to delete.nn"); else if(strcmp(cmd_line0,&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论