1

我正在创建自己的 shell,它执行以下重定向选项 ">、>>、<、|" 使用管道。但我不断收到这个奇怪的错误。当我尝试命令 ps -ax > processes.txt 我得到这个:

    Error: Must set personality to get -x option

或者

    Error: Garbage option

这是我到目前为止编写的代码。

for (i=0; i<nargs; i++)
{



    //> redirect output to output file
    if((strcmp(args[i], ">")==0))
    {   //printf("Forking.\n");
        pid = fork();
        //printf("Forked\n");
        if(pid == 0) 
            { /* child process */
                const char* file = args[i+1];
                printf("entered child process\n");
                int fd;

                if((fd=creat(file,O_WRONLY| O_TRUNC | O_CREAT ))==-1)
                {
                    fprintf(stderr,"Couldn't open %s\n", args[i+1]);
                    exit(-1);
                }//this works
                else
                    {fd=creat(file,O_WRONLY | O_TRUNC | O_CREAT);
                    dup2(fd,1);
                    close(fd);                  
                    args[i]= 0;                 
                    execvp(args[0], args);
                    perror("exec failed\n");
                    exit(-1);}
            }

        else if(pid > 0) 
            { /* parent process */
                if(!async) waitpid(pid, NULL, 0);
                else printf("this is an async call\n");
                //execvp(args[i+1], args);

              } 
        else { /* error occurred */
                perror("fork failed\n");
                exit(1);
              }

        printf("Child complete\n");

    }//>> redirect output to output file does not overwrite just writes to the end of the file
    else if((strcmp(args[i], ">>")==0))
    {
        printf("Forking.\n");
        pid = fork();
        printf("Forked\n");
        if(pid == 0) 
            { /* child process */
                const char* file = args[i+1];
                printf("entered child process\n");
                int fd;
                if((fd=open(file,O_WRONLY | O_CREAT | O_APPEND))==-1)
                {
                    fprintf(stderr,"Couldn't open %s\n", args[i+1]);
                    exit(-1);
                }


                    fd=open(file,O_WRONLY | O_CREAT | O_APPEND);
                    dup2(fd,1);
                    close(fd);
                    args[i]=NULL;
                //printf("%d", fd);
                execvp(args[0], args);
                /* return only when exec fails */
                perror("exec failed\n");
                exit(-1);
            }
        else if(pid > 0) 
            { /* parent process */
                if(!async) waitpid(pid, NULL, 0);
                else printf("this is an async call\n");
              } 
        else { /* error occurred */
                perror("fork failed\n");
                exit(1);
              }

        printf("Child complete\n");
    }//input comes from a file
    else if((strcmp(args[i], "<")==0))
    {
        printf("Forking.\n");
        pid = fork();
        printf("Forked\n");
        if(pid == 0) 
            { /* child process */
                const char* file = args[i+1];
                printf("entered child process\n");
                int fd;
                if((fd=open(file,O_RDONLY)==-1))
                {
                    fprintf(stderr,"File does not exist %s\n", args[i+1]);
                    exit(-1);
                }

                    fd=open(file,O_RDONLY);
                    dup2(fd,0);
                    close(fd);
                    args[i]=NULL;
                execvp(args[0], args);
                /* return only when exec fails */
                perror("exec failed\n");
                exit(-1);
            }
        else if(pid > 0) 
            { /* parent process */
                if(!async) waitpid(pid, NULL, 0);
                else printf("this is an async call\n");
              } 
        else { /* error occurred */
                perror("fork failed\n");
                exit(1);
              }

        printf("Child complete\n");
    }//| output is input for another command
    else if((strcmp(args[i], "|")==0))
    {//the mypipe code edited
        printf("Forking.\n");
        pid = fork();
        printf("Forked\n");
        if(pid == 0)            
        {       const char* file = args[i+1];
                printf("entered child process\n");
                int fd;
                if((fd=open(file,O_WRONLY | O_CREAT | O_TRUNC))==-1)
                {
                    fprintf(stderr,"Couldn't open %s\n", args[i+1]);
                    exit(-1);
                }//this works

                    fd=open(file,O_WRONLY | O_CREAT | O_TRUNC);
                    dup2(fd,1);
                    close(fd);
                    //close(fd);

                args[i]=NULL;
                execvp(args[0], args);
                /* return only when exec fails */
                perror("exec failed\n");
                exit(-1);
            }
        else if(pid > 0) 
            { /* parent process */
                if(!async) waitpid(pid, NULL, 0);
                else printf("this is an async call\n");
              } 
        else { /* error occurred */
                perror("fork failed\n");
                exit(1);
              }

        printf("Child complete\n");

    }//
4

1 回答 1

-1

你试过用gdb调试吗?我认为 gdb 会提供更好的图片或尝试使用 strace 运行您的二进制文件,它将为您提供系统级图片,您的程序究竟在哪里遇到问题。还有一件事,这是完整的程序吗,如果是,我也会在我的系统上尝试

于 2012-10-25T16:50:40.137 回答