热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

shelllikeprogram(shell程序的基本实施部分)

直接上代码:#includeapue.h#includeintmain(void){charbuf[MAXLINE];*formapue.h4096*pid_t

直接上代码:

#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&#39;t execute: %s", buf);
            exit(127);
        }

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

    exit(0);
}

很多时候不喜欢csdn这种代码显示,再贴上vim的

技术分享

测试:

技术分享


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

版权声明:本文为博主原创文章,未经博主允许不得转载。

shell-like program(shell程序的基本实施部分)


推荐阅读
author-avatar
迷人的哈喽柯柯_458
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有