设为首页 加入收藏

TOP

shell-like program(Shell程序的基本实施部分)
2015-08-31 21:23:58 来源: 作者: 【 】 浏览:32
Tags:shell-like program Shell 程序 基本 实施 部分

shell-like program(Shell程序的基本实施部分),直接上代码:


#include "apue.h"
#include


int main(void)
{
? ? char? ? buf[MAXLINE]; /* form apue.h? 4096 */
? ? pid_t? pid;
? ? int? ? status;


? ? printf("%% "); /* print promt (printf requires %% to print %) */
? ? while (fgets(buf, MAXLINE, stdin) != NULL) {
? ? ? ? if (buf[strlen(buf) - 1] == '\n') {
? ? ? ? ? ? buf[strlen(buf) - 1] = 0; /* replace newline while null */
? ? ? ? }


? ? ? ? if ((pid = fork()) < 0) {
? ? ? ? ? ? err_sys("fork error");
? ? ? ? } else if (pid == 0) {
? ? ? ? ? ? execlp(buf, buf, (char *)0);
? ? ? ? ? ? err_ret("couldn't execute: %s", buf);
? ? ? ? ? ? exit(127);
? ? ? ? }


? ? ? ? /* parent */
? ? ? ? if ((pid = waitpid(pid, &status, 0)) < 0) {
? ? ? ? ? ? err_sys("waitpid error");
? ? ? ? }
? ? ? ? printf("%% ");
? ? }


? ? exit(0);
}


再贴上vim的



测试:



这个小程序的功能限制就是不能给命令传入参数,比如我们不能指定目录名给list,我们只能在当前工作目录中运行。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇PHP 性能分析与实验:性能的宏观.. 下一篇Python的数据类型总结

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: