Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用execvp在C中执行外部命令(在类似shell的程序中)_C_Unix_Segmentation Fault_Execvp - Fatal编程技术网

尝试使用execvp在C中执行外部命令(在类似shell的程序中)

尝试使用execvp在C中执行外部命令(在类似shell的程序中),c,unix,segmentation-fault,execvp,C,Unix,Segmentation Fault,Execvp,因此,我必须实现对执行外部命令的支持(对于C编程语言中的linux)。 这就是我到目前为止所做的,我使用了readline库来实现历史函数,但这是不相关的。。。有人能告诉我我做错了什么吗?(我想这是我称之为“execvp”的方式) 这是我的代码: #include<fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.

因此,我必须实现对执行外部命令的支持(对于C编程语言中的linux)。 这就是我到目前为止所做的,我使用了readline库来实现历史函数,但这是不相关的。。。有人能告诉我我做错了什么吗?(我想这是我称之为“execvp”的方式) 这是我的代码:

#include<fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> 
#include <readline/readline.h>
#include <readline/history.h>
#define BUFFER_SIZE 256
#define READFILE_SIZE 4096

char ** parseCMD( char * );
void  EXEC(char *, char *);

int main(int argc, char ** argv)
{
    char  myPrompt[]= {'>', '_', '\0'};
    char *currLine = (char* ) malloc(sizeof(char) * BUFFER_SIZE),
                 *command = (char* ) malloc(sizeof(char) * BUFFER_SIZE),
                            *argument = (char* ) malloc(sizeof(char) * BUFFER_SIZE);

    currLine = readline(myPrompt);
    while((strcmp(currLine, "exit") != 0))
    {
        command = strtok(currLine, " ");
        argument = strtok( NULL, "");
        EXEC(command, argument);
        currLine = readline(myPrompt);
    }
return 0;
}

char ** parseCMD( char * buff )
{
    int i = 0, n = strlen(buff), j, count = 0;
    char ** CMDargs = (char **) malloc( sizeof( char ) * 100 * 100);
    if( buff == NULL )
        return NULL;
    for(i; i < n; i ++)
    {
        j = 0;
        char * aux = (char *) malloc( sizeof( char ) * 100);
        while( buff[i] != ' ' || buff[i] != '\t' || buff[i] != '\n')
        aux[j++] = buff[i++];
        aux[j] = '\0';
        CMDargs[count] = strdup( aux );
        count++;
        //printf("Argument %d is: %s", count - 1, CMDargs[count - 1]);
        free(aux);
    }
CMDargs[ count ] = NULL;
return CMDargs;
}

void  EXEC(char *command, char *argBuffer)
{
    pid_t  pid;
    int status, fd[2], n;
    char s[255];
    char ** Args;
    pipe( fd );
    Args = parseCMD( argBuffer );
    if ((pid = fork()) < 0)
    {
        printf("ERROR: forking child process failed\n");
        exit(1);
    }
    else if (pid == 0)
    {
        close(fd[0]);
        dup2(fd[1],1);
        if (execvp(command, Args) < 0)
        {
            printf("ERROR: execvp call failed\n");
            exit(1);
        }
        close(fd[1]);
    }
    else
    {
        close(fd[1]);
        while( ( n = read( fd[0], s, 255 ) ) > 0 )
        {
            s[n] = '\0';
            printf("%s",s);

        }
        while (wait(&status) != pid);
        close(fd[0]);
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义缓冲区大小256
#定义读取文件大小4096
字符**parseCMD(字符*);
void EXEC(char*,char*);
int main(int argc,字符**argv)
{
char myPrompt[]={'>',''','\0'};
char*currLine=(char*)malloc(sizeof(char)*缓冲区大小),
*命令=(char*)malloc(sizeof(char)*缓冲区大小),
*参数=(char*)malloc(sizeof(char)*缓冲区大小);
currLine=readline(myPrompt);
而((strcmp(currLine,“exit”)!=0))
{
命令=strtok(currLine,“”);
参数=strtok(NULL,“”);
EXEC(命令、参数);
currLine=readline(myPrompt);
}
返回0;
}
char**parseCMD(char*buff)
{
int i=0,n=strlen(buff),j,count=0;
char**CMDargs=(char**)malloc(sizeof(char)*100*100);
如果(buff==NULL)
返回NULL;
对于(i;i0)
{
s[n]='\0';
printf(“%s”,s);
}
while(等待和状态)!=pid);
关闭(fd[0]);
}
}

这里面有太多的错误,问问你自己,当我输入空命令时会发生什么(variable command为NULL,你以后会得到segfault),如果我输入不带参数的命令会发生什么(variable argument为NULL,你以后会得到segfault),如果我输入带参数的命令会发生什么?(而函数parseCMD中的cycle将永远不会终止,最终您将访问不应该访问的内容)。也许是学习使用调试器的时候了。试着一行一行地执行程序,观察正在发生的事情。

那么问题出在哪里?什么预期的行为没有发生?我打赌传递给execvp的参数(字符串)有问题。在打电话给execvp之前试着把它们打印出来我试过了。。。我做了不同的打印,参数看起来不错。。。我现在输入的任何命令都有seg错误,比如“ls-la”是的,我没有使用任何IDE。。。谢谢你的回答。。。这是一个start@Theo使用调试器不需要IDE。gdb命令行,带有简单的中断、步骤、下一步、显示或打印功能。