文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>编写一个简单的 Shell

编写一个简单的 Shell

时间:2007-06-03  来源:jackie.niu

这是一个在solaris系统中运行通过的简单的程序,仅仅提供学习参考

 

 

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <errno.h>

#define LINE_LEN  256



static void sigint (int sig);

int main (void)
{
    char buf [LINE_LEN];
    pid_t pid;
    int status;

    if (sigset (SIGINT, sigint) == SIG_ERR)
        err_msg ("sigset");

    printf ("-> ");

    for (;;) {
        while (fgets (buf, LINE_LEN, stdin) != NULL) {
            if (*buf != '\0')
                buf [strlen (buf) - 1] = '\0';
            if (strlen (buf) == 0) {
                printf ("-> ");
                continue;
            }

            switch (pid = fork ()) {
                case -1:
                    err_msg ("Can't fork");
                    break;

                case 0:
                    /*
                     * Child process
                     */
                    execlp (buf, buf, NULL);
                    err_msg ("Can't exec %s", buf);
                    break;

                default:
                    /*
                     * Parent process
                     */
                    if (waitpid (pid, &status, 0) == -1)
                        err_ret ("waitpid");
                    break;
            }

            printf ("-> ");
        }

        if (errno != EINTR)
            break;

        errno = 0;
    }

    return (0);
}

static void sigint (int sig)
{
    printf ("Caught SIGINT\n");
    printf ("-> ");
}

 
相关阅读 更多 +
排行榜 更多 +
封仙之倚天记应用宝版本

封仙之倚天记应用宝版本

角色扮演 下载
阴阳师旧版本

阴阳师旧版本

棋牌卡牌 下载
仙境情缘变态高爆版

仙境情缘变态高爆版

角色扮演 下载