zoukankan      html  css  js  c++  java
  • shell-like program(shell程序的基本实施部分)

    直接上代码:

    #include "apue.h"
    #include <sys/wait.h>
    
    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] == '
    ') {
                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);
    }

    非常多时候不喜欢csdn这样的代码显示,再贴上vim的


    測试:



    这个小程序的功能限制就是不能给命令传入參数,比方我们不能指定文件夹名给list,我们仅仅能在当前工作文件夹中执行。

  • 相关阅读:
    JAVA类与对象(一)----基础概念理解
    JAVA数组
    JAVA算术运算符、关系运算符和位运算符
    JAVA数据类型
    JAVA配置环境变量
    C#中怎么生成36位的GUID
    jqAjax
    二进制1的个数
    成绩转换
    对决
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6994399.html
Copyright © 2011-2022 走看看