Myshell分段错误,获取输入可能出现问题?

时间:2016-02-12 03:32:15

标签: c++ shell

我一直在研究这个项目。目的是创建一个可以执行几乎所有shell命令的功能shell(cd除外)。除了几件事之外,它几乎可以完成我想要的任何事情。首先是当我放一个'&'时为了表示后台处理,它做到了,但是然后不打印另一个myshell>线。我仍然可以输入一些东西,但是myshell>永远不会出现,无论我在哪里放另一个cout<<“myshell>”;。

另一个问题是,如果我按下回车键,使myString为空,很多次,它会因为seg故障而崩溃程序。在我做'&'之后后台处理并按Enter键以获取myshell>回来,它打印一个myshell>然后在下一次进入时出现故障。如果我没有解释清楚,我很抱歉,但这真的让我发疯。如果您有任何建议,请告诉我。

#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <cstdio>
#include <sys/wait.h>
#include <stdio.h>

/*Function that parses the command the user inputs.
  It takes myArgv and myString as inputs.
  It returns the value of exitcond, which is used to see if the user wants to exit or not.
  Also, this is where myString is tokenized using strok()*/
int parseCommand(char *myArgv[10], char myString[255])
{
    int exitcond=0;
    if((strcmp(myArgv[0], "exit") == 0)||(strcmp(myArgv[0], "quit")==0))
    {
        exitcond = 1;
        return exitcond;
    }
    int i;
    char *token;
    token = strtok(myString," ");
        i=0;
        while (token != NULL)
        {
            myArgv[i] = token;
            token = strtok(NULL," ");
            i++;
        }

        /*
         * Set the last entry our new argv to a null char
         * (see man execvp to understand why).
         */
        myArgv[i] = '\0';
    return exitcond;
}
/*Function that gets the command from the user and sees if they want 
background processing or not (presence of '&').
  It takes inputs of choose and myString. choose is the variable for 
whether background processing is necessary or not, while myString is 
an empty character array.
  It outputs the value of the choose variable for lter use.*/
int getCommand(int choose, char myString[255])
{
    int i;
    choose=0;
    fgets(myString, 256, stdin);
    if (myString[0]=='\0')
    {
        choose=0;
        return choose;
    }
        for (i=0; myString[i]; i++)
        {
            if (myString[i]== '&')
            {
            choose=1;
            myString[i]=' ';
        }

        if (myString[i] == '\n')
        {
            myString[i] = '\0';
        }
    }
    return choose;
}
/*Main function where all the calling of other functions and processes 
is done. This is where the user enters and exits the shell also. All 
usage of fork, pid, waitpid and execvp is done here.*/
int main() 
{
    using namespace std;
    int exitCondition=0, i=0, status;
    char myString[255];
    char *token, *myArgv[10];
    pid_t pid, waiting;
    int bg=0;
    while (!exitCondition)
    {
        /* print a prompt and allow the user to enter a stream of characters */
        cout << "myshell> ";
        bg=0;
        int choose=0;
        bg=getCommand(choose,myString);
        exitCondition=parseCommand(myArgv,myString);
        if(exitCondition==1)
        {
            cout<<"Thank you for using my shell.\n";
        }

    else {
     /*   while (myString[0]=='\0')
        {
            cout<<"myshell> ";
            bg=getCommand(choose,myString);
        }*/
        /* The user has a command, so spawn it in a child process */

        pid = fork();

        if (pid == -1)
        {
            /* to understand why this is here, see man 2 fork */
            cout << "A problem arose, the shell failed to spawn a child process" << endl;
            return(1);
        }

        else if (pid == 0)
        {
            // Child process 
            execvp(myArgv[0],myArgv);
            cout << "Bad command or file name, please try again!\n" << endl;
            return 0;
        } else {
            /* This makes sure that the spawned process is run in the foreground, 
               because the user did not choose background */
            if(bg==0)
            {
                waitpid(pid,NULL,0);

            }
          }

    }
}
return 0;
}

1 个答案:

答案 0 :(得分:0)

好的,你有三个错误,其中一个导致了段错误。在其他人中,一个会在传递给execvp的数组中放入一个垃圾参数,另一个会为后台作业泄漏僵尸进程。

我已经更正了代码并在其中注明错误的位置以及修复[请原谅无偿的样式清理]:

#include <cstdio>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define AVCOUNT     100
#define STRBUFLEN   2000

/*Function that parses the command the user inputs.
  It takes myArgv and myString as inputs.
  It returns the value of exitcond, which is used to see if the user wants to
  exit or not.
  Also, this is where myString is tokenized using strok()*/
int
parseCommand(char **myArgv, char *myString)
{
    char *token;
    char *bp;
    int exitcond = 0;
    int i;

    // NOTE/BUG: original check for exit/quit was here -- at this point
    // myArgv is undefined (hence the segfault)

    // NOTE/BUG: your original loop -- at the end i was one beyond where it
    // should have been so that when myArgv gets passed to execvp it would
    // have an undefined value at the end
#if 0
    token = strtok(myString, " ");
    i = 0;
    while (token != NULL) {
        myArgv[i] = token;
        token = strtok(NULL, " ");
        i++;
    }
#endif

    // NOTE/BUGFIX: here is the corrected loop
    i = 0;
    bp = myString;
    while (1) {
        token = strtok(bp, " ");
        bp = NULL;

        if (token == NULL)
            break;

        myArgv[i++] = token;
    }

    /*
     * Set the last entry our new argv to a null pointer
     * (see man execvp to understand why).
     */
    // NOTE/BUG: with your code, i was one too high here
    myArgv[i] = NULL;

    // NOTE/BUGFIX: moved exit/quit check to here now that myArgv is valid
    token = myArgv[0];
    if (token != NULL) {
        if ((strcmp(token, "exit") == 0) || (strcmp(token, "quit") == 0))
            exitcond = 1;
    }

    return exitcond;
}

/*Function that gets the command from the user and sees if they want
  background processing or not (presence of '&').
  It takes inputs of choose and myString. choose is the variable for
  whether background processing is necessary or not, while myString is
  an empty character array.
  It outputs the value of the choose variable for lter use.*/
int
getCommand(int choose, char *myString)
{
    int i;

    choose = 0;
    fgets(myString, STRBUFLEN, stdin);

    if (myString[0] == '\0') {
        choose = 0;
        return choose;
    }

    for (i = 0; myString[i]; i++) {
        if (myString[i] == '&') {
            choose = 1;
            myString[i] = ' ';
        }

        if (myString[i] == '\n') {
            myString[i] = '\0';
            break;
        }
    }

    return choose;
}

/*Main function where all the calling of other functions and processes
  is done. This is where the user enters and exits the shell also. All
  usage of fork, pid, waitpid and execvp is done here.*/
int
main()
{
    using namespace std;
    int exitCondition = 0;
    int status;
    char myString[STRBUFLEN];
    char *myArgv[AVCOUNT];
    pid_t pid;
    int bg = 0;

    while (!exitCondition) {
        // NOTE/BUGFIX: without this, any background process that completed
        // would become a zombie because it was never waited for [again]
        // reap any finished background jobs
        while (1) {
            pid = waitpid(0,&status,WNOHANG);
            if (pid < 0)
                break;
        }

        /* print a prompt and allow the user to enter a stream of characters */
        cout << "myshell> ";
        bg = 0;
        int choose = 0;

        bg = getCommand(choose, myString);

        exitCondition = parseCommand(myArgv, myString);
        if (exitCondition == 1) {
            cout << "Thank you for using my shell.\n";
            break;
        }

        /* while (myString[0]=='\0') { cout<<"myshell> "; bg=getCommand(choose,myString); } */
        /* The user has a command, so spawn it in a child process */

        pid = fork();

        if (pid == -1) {
            /* to understand why this is here, see man 2 fork */
            cout << "A problem arose, the shell failed to spawn a child process" << endl;
            return 1;
        }

        if (pid == 0) {
            // Child process
            execvp(myArgv[0], myArgv);
            cout << "Bad command or file name, please try again!\n" << endl;
            return 1;
        }

        /* This makes sure that the spawned process is run in the
        foreground, because the user did not choose background */
        if (bg == 0)
            waitpid(pid, &status, 0);
    }

    return 0;
}