繁体   English   中英

基于堆的 char 嵌套数组

[英]Heap based char nested array

I want to pass a heap based array that contains sub arrays for arguments to a function that uses execvp , why a heap based array, because I splitting user input into a list of arguments

int split(char * str, const char * token , char *** arr)
{
    int c = 1;
    char * ptr = strtok(str , token);
    (*arr)[0] = malloc(1024);
    strcpy((*arr)[0], "-c");
    while (ptr != NULL) 
    {
        (*arr)[c] = malloc(1024);
        strcpy((*arr)[c], ptr);
        c++;
        *arr = realloc(*arr, sizeof(char*)*(c+1));
        ptr = strtok(NULL, token);
    }
    (*arr)[c] = NULL;
    return c;
}

int  run(char * args[])
{
    int link[2];
    pid_t pid;
    char foo[4096];
    if (pipe(link)==-1)
        return -1;
    if ((pid = fork()) == -1)
        return -1;
    if(pid == 0){
        dup2 (link[1], STDOUT_FILENO);
        close(link[0]);
        close(link[1]);
        execvp("/bin/bash", args);
    } else {
        close(link[1]);
        int nbytes = read(link[0], foo, sizeof(foo));
        printf("Output: (%.*s)\n", nbytes, foo);
        wait(NULL);
  }
  return 0;
}

int main()
{
    char * tmp = malloc(1024);
    /* list of pointers */
    char ** arr = malloc(16);
    /* buffer to hold user input */
    char input[1024];
    scanf("%[^\n]", input);
    //printf("%s\n",input);
    int x = split(input, " ", &arr);
    for (int i=0; i<x;i++)
    {
        printf("%s ",arr[i]);
    }

    run(arr);

    char * arr2[] = {getenv("SHELL"), "-c" ,"ls","-la", NULL};
    run(arr2);
}

split接受用户输入并创建一个指针列表,其中第一个元素是-c ,最后一个元素是NULL ,用户输入在中间,接下来它被传递给run function 尝试使用/bin/bash运行命令/bin/bash但抛出错误

ls -la
/usr/bin/ls: /usr/bin/ls: cannot execute binary file
-c ls -la Output: ()

好像我在哪里创建一个基于堆栈的数组 arrays 然后将它传递给run function 一切正常,我得到目录列表

我在split function 中使用getenv ,然后传递整个数组以这种方式run ,每件事都按正确的顺序

int split(char * str, const char * token , char *** arr)
{
    int c = 2;
    char * ptr = strtok(str , token);
    (*arr)[0] = malloc(1024);
    /* added this as the first element in the array */
    strcpy((*arr)[0], getenv("SHELL")); 
    (*arr)[1] = malloc(1024);
    /* added this as the second element in the array */
    strcpy((*arr)[1], "-c");            

    while (ptr != NULL) 
    {
        (*arr)[c] = malloc(1024);
        strcpy((*arr)[c], ptr);
        c++;
        *arr = realloc(*arr, sizeof(char*)*(c+1));
        ptr = strtok(NULL, token);
    }
    (*arr)[c] = NULL;
    return c;
}


int  run(char * args[])
{
    int link[2];
    pid_t pid;
    char foo[4096];
    if (pipe(link)==-1)
        return -1;
    if ((pid = fork()) == -1)
        return -1;
    if(pid == 0){
        dup2 (link[1], STDOUT_FILENO);
        close(link[0]);
        close(link[1]);
        /* /bin/bash to args[0] */
        execvp(args[0], args);
    } else {
        close(link[1]);
        int nbytes = read(link[0], foo, sizeof(foo));
        printf("Output: (%.*s)\n", nbytes, foo);
        wait(NULL);
  }
  return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM