荔园在线
荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀
[回到开始]
[上一篇][下一篇]
发信人: laurance.bbs@bbs.bnu.dhs.org (石头~~~长高,长胖), 信区: Linux
标 题: 一个非常简单的shell实现程序
发信站: 京师网事 (Wed Nov 20 03:25:30 2002)
转信站: SZU!news.tiaozhan.com!news.bjsing.net!news.feeling.smth.org!news.happyn
以下代码虽然简单,但是体现了SHELL设计的精髓:进程管理。
/*----------------------------------------------------------------------*\
** File: simpleshell.c **
** Description: A limited shell program, which can complete the func- **
** tion of ls, ps, df. **
** Creation Date: 09/28/98 **
** Modification Date: **
** Author: F.K. **
\*----------------------------------------------------------------------*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX 256
#define CMD_MAX 10
char *vallid_cmds = " ls ps df ";
int
main(void) {
char line_input[MAX], the_cmd[CMD_MAX];
char *new_args[CMD_MAX], *cp;
int i;
int status;
while (1) {
printf("cmd> ");
if (gets(line_input) != NULL) {
cp = line_input;
i = 0;
if ((new_args[i] = strtok(cp, " ")) != NULL) {
do {
++i;
cp = NULL;
new_args[i] = strtok(cp, " ");
} while ( i< CMD_MAX - 1 && new_args[i] != NULL);
new_args[i] = NULL;
switch (fork()) {
case 0:
execvp(new_args[0], new_args);
perror("exec failure");
exit(1);
case -1:
perror("fork failure");
break;
default:
/* Wait for child to finish */
;
}
wait ( &status);
} else
printf("huh?\n");
}
}
}
--
※ 来源:.京师网事 bbs.bnu.dhs.org.[FROM: tu153210.tsinghu]
[回到开始]
[上一篇][下一篇]
荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店