<% dim ModuleName,InfoID,ChannelShortName,CorrelativeArticle,InstallDir,ChannelDir,Keyword,PageTitle,ArticleIntro,Articlecontent Keyword=stripHTML("C,shell") PageTitle=stripHTML("用标准C实现shell功能") ArticleIntro=stripHTML("") Articlecontent=stripHTML("                #include <unistd.h>    #include <stdio.h>    #include <stdlib.h>…") ModuleName = stripHTML("programme") InfoID = stripHTML("180894") ChannelShortName=stripHTML("编程") InstallDir=stripHTML("http://www.77169.com/") ChannelDir=stripHTML("programme") %> 用标准C实现shell功能 - 华盟网 - http://www.77169.com
您现在的位置: 华盟网 >> 编程 >> C语言 >> 正文

用标准C实现shell功能

2015/2/5 作者:不详 来源: 华盟收集
导读 <% if len(ArticleIntro)<3 then Response.Write Articlecontent 'Response.Write "Articlecontent" else Response.Write ArticleIntro 'Response.Write "ArticleIntro" end if %>

 

 

 

 

 

 

 

 

 

 

 

 

 

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

    #define SHELL_NAME "sh1"
    #define PROMPT_ENVIRONMENT_VARIABLE "PROMPT"

    char *prompt;

    int main(int argc, char **argv)
    {
    char cmd[80];
    int statval;

    /* Determine prompt value. */
    if ((prompt = getenv(PROMPT_ENVIRONMENT_VARIABLE)) == NULL)
    prompt = SHELL_NAME ":";

    /* Process commands until exit, or death by signal. */
    while (1)
    {
    /* Prompt and read a command. */
    printf(prompt);
    gets(cmd);

    /* Process built-in commands. */
    if(strcasecmp(cmd, "exit") == 0)
    break;

    /* Process non-built-in commands. */
    if(fork() == 0) {
    execlp(cmd, cmd, NULL);
    fprintf(stderr, "%s: Exec %s failed: %s\n", argv[0],
    cmd, strerror(errno));
    exit(1);
    }

    wait(&statval);
    if(WIFEXITED(statval))
    {
    if(WEXITSTATUS(statval))
    {
    fprintf(stderr,
    "%s: child exited with status %d.\n",
    argv[0], WEXITSTATUS(statval));
    }
    } else {
    fprintf(stderr, "%s: child died uneXPectedly.\n",
    argv[0]);
    }
    }
    }